diff --git a/datamodels/2.x/itop-attachments/main.attachments.php b/datamodels/2.x/itop-attachments/main.attachments.php index fe11fea7f..6e645654e 100755 --- a/datamodels/2.x/itop-attachments/main.attachments.php +++ b/datamodels/2.x/itop-attachments/main.attachments.php @@ -204,9 +204,16 @@ class AttachmentPlugIn implements iApplicationUIExtension, iApplicationObjectExt $oSearch = DBObjectSearch::FromOQL("SELECT Attachment WHERE item_class = :class AND item_id = :item_id"); $oSet = new DBObjectSet($oSearch, array(), array('class' => get_class($oObject), 'item_id' => $oObject->GetKey())); + + $iTransactionId = $oPage->GetTransactionId(); + $sTempId = session_id().'_'.$iTransactionId; + $oSearchTemp = DBObjectSearch::FromOQL("SELECT Attachment WHERE temp_id = :temp_id"); + $oSetTemp = new DBObjectSet($oSearchTemp, array(), array('temp_id' => $sTempId)); + if ($this->GetAttachmentsPosition() == 'relations') { - $sTitle = ($oSet->Count() > 0)? Dict::Format('Attachments:TabTitle_Count', $oSet->Count()) : Dict::S('Attachments:EmptyTabTitle'); + $iCount = $oSet->Count() + $oSetTemp->Count(); + $sTitle = ($iCount > 0)? Dict::Format('Attachments:TabTitle_Count', $iCount) : Dict::S('Attachments:EmptyTabTitle'); $oPage->SetCurrentTab($sTitle); } $oPage->add_style( @@ -249,9 +256,7 @@ EOF if ($bEditMode && !static::IsReadonlyState($oObject, $oObject->GetState(), static::ENUM_GUI_BACKOFFICE) ) { $sIsDeleteEnabled = $this->m_bDeleteEnabled ? 'true' : 'false'; - $iTransactionId = $oPage->GetTransactionId(); $sClass = get_class($oObject); - $sTempId = session_id().'_'.$iTransactionId; $sDeleteBtn = Dict::S('Attachments:DeleteBtn'); $oPage->add_script( <<add(''); while ($oAttachment = $oSet->Fetch()) { - $iAttId = $oAttachment->GetKey(); - $oDoc = $oAttachment->Get('contents'); - $sFileName = htmlentities($oDoc->GetFileName(), ENT_QUOTES, 'UTF-8'); - $sIcon = utils::GetAbsoluteUrlAppRoot().AttachmentPlugIn::GetFileIcon($sFileName); - $sPreview = $oDoc->IsPreviewAvailable() ? 'true' : 'false'; - $sDownloadLink = utils::GetAbsoluteUrlAppRoot().ATTACHMENT_DOWNLOAD_URL.$iAttId; - $oPage->add(''); + $this->DisplayOneAttachment($oPage, $oAttachment); } - + + // Display Temporary attachments + while ($oAttachment = $oSetTemp->Fetch()) + { + $this->DisplayOneAttachment($oPage, $oAttachment, true); + } + + // Suggested attachments are listed here but treated as temporary $aDefault = utils::ReadParam('default', array(), false, 'raw_data'); if (array_key_exists('suggested_attachments', $aDefault)) @@ -303,18 +309,11 @@ EOF $oAttachment->Set('temp_id', $sTempId); $oAttachment->DBUpdate(); // Display them - $iAttId = $oAttachment->GetKey(); - $oDoc = $oAttachment->Get('contents'); - $sFileName = htmlentities($oDoc->GetFileName(), ENT_QUOTES, 'UTF-8'); - $sIcon = utils::GetAbsoluteUrlAppRoot().AttachmentPlugIn::GetFileIcon($sFileName); - $sDownloadLink = utils::GetAbsoluteUrlAppRoot().ATTACHMENT_DOWNLOAD_URL.$iAttId; - $sPreview = $oDoc->IsPreviewAvailable() ? 'true' : 'false'; - $oPage->add(''); - $oPage->add_ready_script("$('#attachment_plugin').trigger('add_attachment', [$iAttId, '".addslashes($sFileName)."', false /* not an line image */]);"); + $this->DisplayOneAttachment($oPage, $oAttachment, true); } } } - + $oPage->add(''); $oPage->add('
'); $sMaxUpload = $this->GetMaxUpload(); @@ -474,8 +473,8 @@ EOF // Leave silently if there is no trace of the attachment form return; } - $iTransactionId = utils::ReadParam('transaction_id', null); - if (!is_null($iTransactionId)) + $sTransactionId = utils::ReadParam('transaction_id', null); + if (!is_null($sTransactionId)) { $aActions = array(); $aAttachmentIds = utils::ReadParam('attachments', array()); @@ -494,8 +493,8 @@ EOF } } - // Attach new (temporary) attachements - $sTempId = session_id().'_'.$iTransactionId; + // Attach new (temporary) attachments + $sTempId = session_id().'_'.$sTransactionId; // The object is being created from a form, check if there are pending attachments // for this object, but deleting the "new" ones that were already removed from the form $sOQL = 'SELECT Attachment WHERE temp_id = :temp_id'; @@ -530,6 +529,21 @@ EOF } } } + + public static function CopyAttachments($oObject, $sTransactionId) + { + $oSearch = DBObjectSearch::FromOQL("SELECT Attachment WHERE item_class = :class AND item_id = :item_id"); + $oSet = new DBObjectSet($oSearch, array(), array('class' => get_class($oObject), 'item_id' => $oObject->GetKey())); + // Attach new (temporary) attachments + $sTempId = session_id().'_'.$sTransactionId; + while ($oAttachment = $oSet->Fetch()) + { + $oTempAttachment = clone $oAttachment; + $oTempAttachment->Set('item_id', null); + $oTempAttachment->Set('temp_id', $sTempId); + $oTempAttachment->DBInsert(); + } + } ///////////////////////////////////////////////////////////////////////////////////////// public static function GetFileIcon($sFileName) @@ -707,6 +721,28 @@ EOF return $bReadonly; } + + /** + * @param \WebPage $oPage + * @param $oAttachment + * @param bool $bIsTemporary + * + * @throws \Exception + */ + protected function DisplayOneAttachment(WebPage $oPage, $oAttachment, $bIsTemporary = false) + { + $iAttId = $oAttachment->GetKey(); + $oDoc = $oAttachment->Get('contents'); + $sFileName = htmlentities($oDoc->GetFileName(), ENT_QUOTES, 'UTF-8'); + $sIcon = utils::GetAbsoluteUrlAppRoot().AttachmentPlugIn::GetFileIcon($sFileName); + $sDownloadLink = utils::GetAbsoluteUrlAppRoot().ATTACHMENT_DOWNLOAD_URL.$iAttId; + $sPreview = $oDoc->IsPreviewAvailable() ? 'true' : 'false'; + $oPage->add('

'.$sFileName.'

  
'); + if ($bIsTemporary) + { + $oPage->add_ready_script("$('#attachment_plugin').trigger('add_attachment', [$iAttId, '".addslashes($sFileName)."', false /* not an line image */]);"); + } + } } /**