main.attachments.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811
  1. <?php
  2. // Copyright (C) 2010-2016 Combodo SARL
  3. //
  4. // This file is part of iTop.
  5. //
  6. // iTop is free software; you can redistribute it and/or modify
  7. // it under the terms of the GNU Affero General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // iTop is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Affero General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Affero General Public License
  17. // along with iTop. If not, see <http://www.gnu.org/licenses/>
  18. define('ATTACHMENT_DOWNLOAD_URL', 'pages/ajax.document.php?operation=download_document&class=Attachment&field=contents&id=');
  19. class AttachmentPlugIn implements iApplicationUIExtension, iApplicationObjectExtension
  20. {
  21. const ENUM_GUI_ALL = 'all';
  22. const ENUM_GUI_BACKOFFICE = 'backoffice';
  23. const ENUM_GUI_PORTALS = 'portals';
  24. protected static $m_bIsModified = false;
  25. public function OnDisplayProperties($oObject, WebPage $oPage, $bEditMode = false)
  26. {
  27. if ($this->GetAttachmentsPosition() == 'properties')
  28. {
  29. $this->DisplayAttachments($oObject, $oPage, $bEditMode);
  30. }
  31. }
  32. public function OnDisplayRelations($oObject, WebPage $oPage, $bEditMode = false)
  33. {
  34. if ($this->GetAttachmentsPosition() == 'relations')
  35. {
  36. $this->DisplayAttachments($oObject, $oPage, $bEditMode);
  37. }
  38. }
  39. public function OnFormSubmit($oObject, $sFormPrefix = '')
  40. {
  41. if ($this->IsTargetObject($oObject))
  42. {
  43. // For new objects attachments are processed in OnDBInsert
  44. if (!$oObject->IsNew())
  45. {
  46. self::UpdateAttachments($oObject);
  47. }
  48. }
  49. }
  50. protected function GetMaxUpload()
  51. {
  52. $iMaxUpload = ini_get('upload_max_filesize');
  53. if (!$iMaxUpload)
  54. {
  55. $sRet = Dict::S('Attachments:UploadNotAllowedOnThisSystem');
  56. }
  57. else
  58. {
  59. $iMaxUpload = utils::ConvertToBytes($iMaxUpload);
  60. if ($iMaxUpload > 1024*1024*1024)
  61. {
  62. $sRet = Dict::Format('Attachment:Max_Go', sprintf('%0.2f', $iMaxUpload/(1024*1024*1024)));
  63. }
  64. else if ($iMaxUpload > 1024*1024)
  65. {
  66. $sRet = Dict::Format('Attachment:Max_Mo', sprintf('%0.2f', $iMaxUpload/(1024*1024)));
  67. }
  68. else
  69. {
  70. $sRet = Dict::Format('Attachment:Max_Ko', sprintf('%0.2f', $iMaxUpload/(1024)));
  71. }
  72. }
  73. return $sRet;
  74. }
  75. public function OnFormCancel($sTempId)
  76. {
  77. // Delete all "pending" attachments for this form
  78. $sOQL = 'SELECT Attachment WHERE temp_id = :temp_id';
  79. $oSearch = DBObjectSearch::FromOQL($sOQL);
  80. $oSet = new DBObjectSet($oSearch, array(), array('temp_id' => $sTempId));
  81. while($oAttachment = $oSet->Fetch())
  82. {
  83. $oAttachment->DBDelete();
  84. // Pending attachment, don't mention it in the history
  85. }
  86. }
  87. public function EnumUsedAttributes($oObject)
  88. {
  89. return array();
  90. }
  91. public function GetIcon($oObject)
  92. {
  93. return '';
  94. }
  95. public function GetHilightClass($oObject)
  96. {
  97. // Possible return values are:
  98. // HILIGHT_CLASS_CRITICAL, HILIGHT_CLASS_WARNING, HILIGHT_CLASS_OK, HILIGHT_CLASS_NONE
  99. return HILIGHT_CLASS_NONE;
  100. }
  101. public function EnumAllowedActions(DBObjectSet $oSet)
  102. {
  103. // No action
  104. return array();
  105. }
  106. public function OnIsModified($oObject)
  107. {
  108. return self::$m_bIsModified;
  109. }
  110. public function OnCheckToWrite($oObject)
  111. {
  112. return array();
  113. }
  114. public function OnCheckToDelete($oObject)
  115. {
  116. return array();
  117. }
  118. public function OnDBUpdate($oObject, $oChange = null)
  119. {
  120. if ($this->IsTargetObject($oObject))
  121. {
  122. // Get all current attachments
  123. $oSearch = DBObjectSearch::FromOQL("SELECT Attachment WHERE item_class = :class AND item_id = :item_id");
  124. $oSet = new DBObjectSet($oSearch, array(), array('class' => get_class($oObject), 'item_id' => $oObject->GetKey()));
  125. while ($oAttachment = $oSet->Fetch())
  126. {
  127. $oAttachment->SetItem($oObject, true /*updateonchange*/);
  128. }
  129. }
  130. }
  131. public function OnDBInsert($oObject, $oChange = null)
  132. {
  133. if ($this->IsTargetObject($oObject))
  134. {
  135. self::UpdateAttachments($oObject, $oChange);
  136. }
  137. }
  138. public function OnDBDelete($oObject, $oChange = null)
  139. {
  140. if ($this->IsTargetObject($oObject))
  141. {
  142. $oSearch = DBObjectSearch::FromOQL("SELECT Attachment WHERE item_class = :class AND item_id = :item_id");
  143. $oSet = new DBObjectSet($oSearch, array(), array('class' => get_class($oObject), 'item_id' => $oObject->GetKey()));
  144. while ($oAttachment = $oSet->Fetch())
  145. {
  146. $oAttachment->DBDelete();
  147. }
  148. }
  149. }
  150. ///////////////////////////////////////////////////////////////////////////////////////////////////////
  151. //
  152. // Plug-ins specific functions
  153. //
  154. ///////////////////////////////////////////////////////////////////////////////////////////////////////
  155. protected function IsTargetObject($oObject)
  156. {
  157. $aAllowedClasses = MetaModel::GetModuleSetting('itop-attachments', 'allowed_classes', array('Ticket'));
  158. foreach($aAllowedClasses as $sAllowedClass)
  159. {
  160. if ($oObject instanceof $sAllowedClass)
  161. {
  162. return true;
  163. }
  164. }
  165. return false;
  166. }
  167. protected function GetAttachmentsPosition()
  168. {
  169. return MetaModel::GetModuleSetting('itop-attachments', 'position', 'relations');
  170. }
  171. var $m_bDeleteEnabled = true;
  172. public function EnableDelete($bEnabled)
  173. {
  174. $this->m_bDeleteEnabled = $bEnabled;
  175. }
  176. public function DisplayAttachments($oObject, WebPage $oPage, $bEditMode = false)
  177. {
  178. // Exit here if the class is not allowed
  179. if (!$this->IsTargetObject($oObject)) return;
  180. $oSearch = DBObjectSearch::FromOQL("SELECT Attachment WHERE item_class = :class AND item_id = :item_id");
  181. $oSet = new DBObjectSet($oSearch, array(), array('class' => get_class($oObject), 'item_id' => $oObject->GetKey()));
  182. if ($this->GetAttachmentsPosition() == 'relations')
  183. {
  184. $sTitle = ($oSet->Count() > 0)? Dict::Format('Attachments:TabTitle_Count', $oSet->Count()) : Dict::S('Attachments:EmptyTabTitle');
  185. $oPage->SetCurrentTab($sTitle);
  186. }
  187. $sMaxWidth = MetaModel::GetModuleSetting('itop-attachment', 'inline_image_max_width', '450px');
  188. $oPage->add_style(
  189. <<<EOF
  190. .attachment {
  191. display: inline-block;
  192. text-align:center;
  193. float:left;
  194. padding:5px;
  195. }
  196. .attachment:hover {
  197. background-color: #e0e0e0;
  198. }
  199. .attachment img {
  200. border: 0;
  201. }
  202. .attachment a {
  203. text-decoration: none;
  204. color: #1C94C4;
  205. }
  206. .btn_hidden {
  207. display: none;
  208. }
  209. .drag_in {
  210. -webkit-box-shadow:inset 0 0 10px 2px #1C94C4;
  211. box-shadow:inset 0 0 10px 2px #1C94C4;
  212. }
  213. #history .attachment-history-added {
  214. padding: 0;
  215. float: none;
  216. }
  217. .inline-image {
  218. cursor: zoom-in;
  219. }
  220. EOF
  221. );
  222. $oPage->add('<fieldset>');
  223. $oPage->add('<legend>'.Dict::S('Attachments:FieldsetTitle').'</legend>');
  224. if ($bEditMode && !static::IsReadonlyState($oObject, $oObject->GetState(), static::ENUM_GUI_BACKOFFICE) )
  225. {
  226. $sIsDeleteEnabled = $this->m_bDeleteEnabled ? 'true' : 'false';
  227. $iTransactionId = $oPage->GetTransactionId();
  228. $sClass = get_class($oObject);
  229. $iObjectId = $oObject->Getkey();
  230. $sTempId = session_id().'_'.$iTransactionId;
  231. $sDeleteBtn = Dict::S('Attachments:DeleteBtn');
  232. $oPage->add_script(
  233. <<<EOF
  234. function RemoveAttachment(att_id)
  235. {
  236. var bDelete = true;
  237. if ($('#display_attachment_'+att_id).hasClass('image-in-use'))
  238. {
  239. bDelete = window.confirm('This image is used in a description. Delete it anyway?');
  240. }
  241. if (bDelete)
  242. {
  243. $('#attachment_'+att_id).attr('name', 'removed_attachments[]');
  244. $('#display_attachment_'+att_id).hide();
  245. $('#attachment_plugin').trigger('remove_attachment', [att_id]);
  246. }
  247. return false; // Do not submit the form !
  248. }
  249. EOF
  250. );
  251. $oPage->add('<span id="attachments">');
  252. while ($oAttachment = $oSet->Fetch())
  253. {
  254. $iAttId = $oAttachment->GetKey();
  255. $oDoc = $oAttachment->Get('contents');
  256. $sFileName = htmlentities($oDoc->GetFileName(), ENT_QUOTES, 'UTF-8');
  257. $sIcon = utils::GetAbsoluteUrlAppRoot().AttachmentPlugIn::GetFileIcon($sFileName);
  258. $sPreview = $oDoc->IsPreviewAvailable() ? 'true' : 'false';
  259. $sDownloadLink = utils::GetAbsoluteUrlAppRoot().ATTACHMENT_DOWNLOAD_URL.$iAttId;
  260. $oPage->add('<div class="attachment" id="display_attachment_' . $iAttId . '"><a data-preview="' . $sPreview . '" href="' . $sDownloadLink . '"><img src="' . $sIcon . '"><br/>' . $sFileName . '<input id="attachment_' . $iAttId . '" type="hidden" name="attachments[]" value="' . $iAttId . '"/></a><br/>&nbsp;<input id="btn_remove_' . $iAttId . '" type="button" class="btn_hidden" value="' . Dict::S('Attachments:DeleteBtn') . '" onClick="RemoveAttachment(' . $iAttId . ');"/>&nbsp;</div>');
  261. }
  262. // Suggested attachments are listed here but treated as temporary
  263. $aDefault = utils::ReadParam('default', array(), false, 'raw_data');
  264. if (array_key_exists('suggested_attachments', $aDefault))
  265. {
  266. $sSuggestedAttachements = $aDefault['suggested_attachments'];
  267. if (is_array($sSuggestedAttachements))
  268. {
  269. $sSuggestedAttachements = implode(',', $sSuggestedAttachements);
  270. }
  271. $oSearch = DBObjectSearch::FromOQL("SELECT Attachment WHERE id IN($sSuggestedAttachements)");
  272. $oSet = new DBObjectSet($oSearch, array());
  273. if ($oSet->Count() > 0)
  274. {
  275. while ($oAttachment = $oSet->Fetch())
  276. {
  277. // Mark the attachments as temporary attachments for the current object/form
  278. $oAttachment->Set('temp_id', $sTempId);
  279. $oAttachment->DBUpdate();
  280. // Display them
  281. $iAttId = $oAttachment->GetKey();
  282. $oDoc = $oAttachment->Get('contents');
  283. $sFileName = htmlentities($oDoc->GetFileName(), ENT_QUOTES, 'UTF-8');
  284. $sIcon = utils::GetAbsoluteUrlAppRoot().AttachmentPlugIn::GetFileIcon($sFileName);
  285. $sDownloadLink = utils::GetAbsoluteUrlAppRoot().ATTACHMENT_DOWNLOAD_URL.$iAttId;
  286. $sPreview = $oDoc->IsPreviewAvailable() ? 'true' : 'false';
  287. $oPage->add('<div class="attachment" id="display_attachment_'.$iAttId.'"><a data-preview="'.$sPreview.'" href="'.$sDownloadLink.'"><img src="'.$sIcon.'"><br/>'.$sFileName.'<input id="attachment_'.$iAttId.'" type="hidden" name="attachments[]" value="'.$iAttId.'"/></a><br/>&nbsp;<input id="btn_remove_'.$iAttId.'" type="button" class="btn_hidden" value="Delete" onClick="RemoveAttachment('.$iAttId.');"/>&nbsp;</div>');
  288. $oPage->add_ready_script("$('#attachment_plugin').trigger('add_attachment', [$iAttId, '".addslashes($sFileName)."', false /* not an line image */]);");
  289. }
  290. }
  291. }
  292. $oPage->add('</span>');
  293. $oPage->add('<div style="clear:both"></div>');
  294. $sMaxUpload = $this->GetMaxUpload();
  295. $oPage->p(Dict::S('Attachments:AddAttachment').'<input type="file" name="file" id="file"><span style="display:none;" id="attachment_loading">&nbsp;<img src="../images/indicator.gif"></span> '.$sMaxUpload);
  296. $oPage->add_linked_script(utils::GetAbsoluteUrlAppRoot().'js/jquery.iframe-transport.js');
  297. $oPage->add_linked_script(utils::GetAbsoluteUrlAppRoot().'js/jquery.fileupload.js');
  298. $sDownloadLink = utils::GetAbsoluteUrlAppRoot().ATTACHMENT_DOWNLOAD_URL;
  299. $oPage->add_ready_script(
  300. <<< EOF
  301. $('#file').fileupload({
  302. url: GetAbsoluteUrlModulesRoot()+'itop-attachments/ajax.attachment.php',
  303. formData: { operation: 'add', temp_id: '$sTempId', obj_class: '$sClass' },
  304. dataType: 'json',
  305. pasteZone: null, // Don't accept files via Chrome's copy/paste
  306. done: function (e, data) {
  307. if(typeof(data.result.error) != 'undefined')
  308. {
  309. if(data.result.error != '')
  310. {
  311. alert(data.result.error);
  312. }
  313. else
  314. {
  315. var sDownloadLink = '$sDownloadLink'+data.result.att_id;
  316. $('#attachments').append('<div class="attachment" id="display_attachment_'+data.result.att_id+'"><a data-preview="'+data.result.preview+'" href="'+sDownloadLink+'"><img src="'+data.result.icon+'"><br/>'+data.result.msg+'<input id="attachment_'+data.result.att_id+'" type="hidden" name="attachments[]" value="'+data.result.att_id+'"/></a><br/><input type="button" class="btn_hidden" value="{$sDeleteBtn}" onClick="RemoveAttachment('+data.result.att_id+');"/></div>');
  317. if($sIsDeleteEnabled)
  318. {
  319. $('#display_attachment_'+data.result.att_id).hover( function() { $(this).children(':button').toggleClass('btn_hidden'); } );
  320. }
  321. $('#attachment_plugin').trigger('add_attachment', [data.result.att_id, data.result.msg, false /* inline image */]);
  322. }
  323. }
  324. },
  325. start: function() {
  326. $('#attachment_loading').show();
  327. },
  328. stop: function() {
  329. $('#attachment_loading').hide();
  330. }
  331. });
  332. $(document).bind('dragover', function (e) {
  333. var bFiles = false;
  334. if (e.dataTransfer && e.dataTransfer.types)
  335. {
  336. for (var i = 0; i < e.dataTransfer.types.length; i++)
  337. {
  338. if (e.dataTransfer.types[i] == "application/x-moz-nativeimage")
  339. {
  340. bFiles = false; // mozilla contains "Files" in the types list when dragging images inside the page, but it also contains "application/x-moz-nativeimage" before
  341. break;
  342. }
  343. if (e.dataTransfer.types[i] == "Files")
  344. {
  345. bFiles = true;
  346. break;
  347. }
  348. }
  349. }
  350. if (!bFiles) return; // Not dragging files
  351. var dropZone = $('#file').closest('fieldset');
  352. if (!dropZone.is(':visible'))
  353. {
  354. // Hidden, but inside an inactive tab? Higlight the tab
  355. var sTabId = dropZone.closest('.ui-tabs-panel').attr('aria-labelledby');
  356. dropZone = $('#'+sTabId).closest('li');
  357. }
  358. timeout = window.dropZoneTimeout;
  359. if (!timeout) {
  360. dropZone.addClass('drag_in');
  361. } else {
  362. clearTimeout(timeout);
  363. }
  364. window.dropZoneTimeout = setTimeout(function () {
  365. window.dropZoneTimeout = null;
  366. dropZone.removeClass('drag_in');
  367. }, 300);
  368. });
  369. // check if the attachments are used by inline images
  370. window.setTimeout( function() {
  371. $('.attachment a').each(function() {
  372. var sUrl = $(this).attr('href');
  373. if($('img[src="'+sUrl+'"]').length > 0)
  374. {
  375. $(this).addClass('image-in-use').find('img').wrap('<div class="image-in-use-wrapper" style="position:relative;display:inline-block;"></div>');
  376. }
  377. });
  378. $('.htmlEditor').each(function() {
  379. var oEditor = $(this).ckeditorGet();
  380. var sHtml = oEditor.getData();
  381. var jElement = $('<div/>').html(sHtml).contents();
  382. jElement.find('img').each(function() {
  383. var sSrc = $(this).attr('src');
  384. $('.attachment a[href="'+sSrc+'"]').parent().addClass('image-in-use').find('img').wrap('<div class="image-in-use-wrapper" style="position:relative;display:inline-block;"></div>');
  385. });
  386. });
  387. $('.image-in-use-wrapper').append('<div style="position:absolute;top:0;left:0;"><img src="../images/transp-lock.png"></div>');
  388. }, 200 );
  389. EOF
  390. );
  391. $oPage->p('<span style="display:none;" id="attachment_loading">Loading, please wait...</span>');
  392. $oPage->p('<input type="hidden" id="attachment_plugin" name="attachment_plugin"/>');
  393. if ($this->m_bDeleteEnabled)
  394. {
  395. $oPage->add_ready_script('$(".attachment").hover( function() {$(this).children(":button").toggleClass("btn_hidden"); } );');
  396. }
  397. }
  398. else
  399. {
  400. $oPage->add('<span id="attachments">');
  401. if ($oSet->Count() == 0)
  402. {
  403. $oPage->add(Dict::S('Attachments:NoAttachment'));
  404. }
  405. else
  406. {
  407. while ($oAttachment = $oSet->Fetch())
  408. {
  409. $iAttId = $oAttachment->GetKey();
  410. $oDoc = $oAttachment->Get('contents');
  411. $sFileName = htmlentities($oDoc->GetFileName(), ENT_QUOTES, 'UTF-8');
  412. $sIcon = utils::GetAbsoluteUrlAppRoot().AttachmentPlugIn::GetFileIcon($sFileName);
  413. $sPreview = $oDoc->IsPreviewAvailable() ? 'true' : 'false';
  414. $sDownloadLink = utils::GetAbsoluteUrlAppRoot().ATTACHMENT_DOWNLOAD_URL.$iAttId;
  415. $oPage->add('<div class="attachment" id="attachment_'.$iAttId.'"><a data-preview="'.$sPreview.'" href="'.$sDownloadLink.'"><img src="'.$sIcon.'"><br/>'.$sFileName.'</a><input type="hidden" name="attachments[]" value="'.$iAttId.'"/><br/>&nbsp;&nbsp;</div>');
  416. }
  417. }
  418. $oPage->add('</span>');
  419. }
  420. $oPage->add('</fieldset>');
  421. $sPreviewNotAvailable = addslashes(Dict::S('Attachments:PreviewNotAvailable'));
  422. $iMaxWidth = MetaModel::GetModuleSetting('itop-attachments', 'preview_max_width', 290);
  423. $oPage->add_ready_script(
  424. <<<EOF
  425. $(document).tooltip({
  426. items: '.attachment a',
  427. position: { my: 'left top', at: 'right top', using: function( position, feedback ) { $( this ).css( position ); }},
  428. content: function() { if ($(this).attr('data-preview') == 'true') { return('<img style=\"max-width:{$iMaxWidth}px\" src=\"'+$(this).attr('href')+'\"></img>');} else { return '$sPreviewNotAvailable'; }}
  429. });
  430. EOF
  431. );
  432. }
  433. protected static function UpdateAttachments($oObject, $oChange = null)
  434. {
  435. self::$m_bIsModified = false;
  436. if (utils::ReadParam('attachment_plugin', 'not-in-form') == 'not-in-form')
  437. {
  438. // Workaround to an issue in iTop < 2.0
  439. // Leave silently if there is no trace of the attachment form
  440. return;
  441. }
  442. $iTransactionId = utils::ReadParam('transaction_id', null);
  443. if (!is_null($iTransactionId))
  444. {
  445. $aActions = array();
  446. $aAttachmentIds = utils::ReadParam('attachments', array());
  447. $aRemovedAttachmentIds = utils::ReadParam('removed_attachments', array());
  448. // Get all current attachments
  449. $oSearch = DBObjectSearch::FromOQL("SELECT Attachment WHERE item_class = :class AND item_id = :item_id");
  450. $oSet = new DBObjectSet($oSearch, array(), array('class' => get_class($oObject), 'item_id' => $oObject->GetKey()));
  451. while ($oAttachment = $oSet->Fetch())
  452. {
  453. // Remove attachments that are no longer attached to the current object
  454. if (in_array($oAttachment->GetKey(), $aRemovedAttachmentIds))
  455. {
  456. $oAttachment->DBDelete();
  457. $aActions[] = self::GetActionChangeOp($oAttachment, false /* false => deletion */);
  458. }
  459. }
  460. // Attach new (temporary) attachements
  461. $sTempId = session_id().'_'.$iTransactionId;
  462. // The object is being created from a form, check if there are pending attachments
  463. // for this object, but deleting the "new" ones that were already removed from the form
  464. $sOQL = 'SELECT Attachment WHERE temp_id = :temp_id';
  465. $oSearch = DBObjectSearch::FromOQL($sOQL);
  466. foreach($aAttachmentIds as $iAttachmentId)
  467. {
  468. $oSet = new DBObjectSet($oSearch, array(), array('temp_id' => $sTempId));
  469. while($oAttachment = $oSet->Fetch())
  470. {
  471. if (in_array($oAttachment->GetKey(),$aRemovedAttachmentIds))
  472. {
  473. $oAttachment->DBDelete();
  474. // temporary attachment removed, don't even mention it in the history
  475. }
  476. else
  477. {
  478. $oAttachment->SetItem($oObject);
  479. $oAttachment->Set('temp_id', '');
  480. $oAttachment->DBUpdate();
  481. // temporary attachment confirmed, list it in the history
  482. $aActions[] = self::GetActionChangeOp($oAttachment, true /* true => creation */);
  483. }
  484. }
  485. }
  486. if (count($aActions) > 0)
  487. {
  488. foreach($aActions as $oChangeOp)
  489. {
  490. self::RecordHistory($oChange, $oObject, $oChangeOp);
  491. }
  492. self::$m_bIsModified = true;
  493. }
  494. }
  495. }
  496. /////////////////////////////////////////////////////////////////////////////////////////
  497. public static function GetFileIcon($sFileName)
  498. {
  499. $aPathParts = pathinfo($sFileName);
  500. if (!array_key_exists('extension', $aPathParts))
  501. {
  502. // No extension: use the default icon
  503. $sIcon = 'document.png';
  504. }
  505. else
  506. {
  507. switch($aPathParts['extension'])
  508. {
  509. case 'doc':
  510. case 'docx':
  511. $sIcon = 'doc.png';
  512. break;
  513. case 'xls':
  514. case 'xlsx':
  515. $sIcon = 'xls.png';
  516. break;
  517. case 'ppt':
  518. case 'pptx':
  519. $sIcon = 'ppt.png';
  520. break;
  521. case 'pdf':
  522. $sIcon = 'pdf.png';
  523. break;
  524. case 'txt':
  525. case 'text':
  526. $sIcon = 'txt.png';
  527. break;
  528. case 'rtf':
  529. $sIcon = 'rtf.png';
  530. break;
  531. case 'odt':
  532. $sIcon = 'odt.png';
  533. break;
  534. case 'ods':
  535. $sIcon = 'ods.png';
  536. break;
  537. case 'odp':
  538. $sIcon = 'odp.png';
  539. break;
  540. case 'html':
  541. case 'htm':
  542. $sIcon = 'html.png';
  543. break;
  544. case 'png':
  545. case 'gif':
  546. case 'jpg':
  547. case 'jpeg':
  548. case 'tiff':
  549. case 'tif':
  550. case 'bmp':
  551. $sIcon = 'image.png';
  552. break;
  553. case 'zip':
  554. case 'gz':
  555. case 'tgz':
  556. case 'rar':
  557. $sIcon = 'zip.png';
  558. break;
  559. default:
  560. $sIcon = 'document.png';
  561. break;
  562. }
  563. }
  564. return 'env-'.utils::GetCurrentEnvironment()."/itop-attachments/icons/$sIcon";
  565. }
  566. /////////////////////////////////////////////////////////////////////////
  567. private static function RecordHistory($oChange, $oTargetObject, $oMyChangeOp)
  568. {
  569. if (!is_null($oChange))
  570. {
  571. $oMyChangeOp->Set("change", $oChange->GetKey());
  572. }
  573. $oMyChangeOp->Set("objclass", get_class($oTargetObject));
  574. $oMyChangeOp->Set("objkey", $oTargetObject->GetKey());
  575. $iId = $oMyChangeOp->DBInsertNoReload();
  576. }
  577. /////////////////////////////////////////////////////////////////////////
  578. private static function GetActionChangeOp($oAttachment, $bCreate = true)
  579. {
  580. $oBlob = $oAttachment->Get('contents');
  581. $sFileName = $oBlob->GetFileName();
  582. if ($bCreate)
  583. {
  584. $oChangeOp = new CMDBChangeOpAttachmentAdded();
  585. $oChangeOp->Set('attachment_id', $oAttachment->GetKey());
  586. $oChangeOp->Set('filename', $sFileName);
  587. }
  588. else
  589. {
  590. $oChangeOp = new CMDBChangeOpAttachmentRemoved();
  591. $oChangeOp->Set('filename', $sFileName);
  592. }
  593. return $oChangeOp;
  594. }
  595. /////////////////////////////////////////////////////////////////////////
  596. /**
  597. * Returns if Attachments should be readonly for $oObject in the $sState state for the $sGUI GUI
  598. *
  599. * @param DBObject $oObject
  600. * @param string $sState
  601. * @param string $sGUI
  602. * @return bool
  603. */
  604. public static function IsReadonlyState(DBObject $oObject, $sState, $sGUI = self::ENUM_GUI_ALL)
  605. {
  606. $aParamDefaultValue = array(
  607. static::ENUM_GUI_ALL => array(
  608. 'Ticket' => array('closed')
  609. )
  610. );
  611. $bReadonly = false;
  612. $sClass = get_class($oObject);
  613. $aReadonlyStatus = MetaModel::GetModuleSetting('itop-attachments', 'readonly_states', $aParamDefaultValue);
  614. if(!empty($aReadonlyStatus))
  615. {
  616. // Merging GUIs entries
  617. $aEntries = array();
  618. // - All
  619. if( array_key_exists(static::ENUM_GUI_ALL, $aReadonlyStatus) )
  620. {
  621. $aEntries = array_merge_recursive($aEntries, $aReadonlyStatus[static::ENUM_GUI_ALL]);
  622. }
  623. // - Backoffice & Portals
  624. foreach( array(static::ENUM_GUI_BACKOFFICE, static::ENUM_GUI_PORTALS) as $sEnumGUI)
  625. {
  626. if( in_array($sGUI, array(static::ENUM_GUI_ALL, $sEnumGUI)) )
  627. {
  628. if( array_key_exists($sEnumGUI, $aReadonlyStatus) )
  629. {
  630. $aEntries = array_merge_recursive($aEntries, $aReadonlyStatus[$sEnumGUI]);
  631. }
  632. }
  633. }
  634. $aParentClasses = array_reverse( MetaModel::EnumParentClasses($sClass, ENUM_PARENT_CLASSES_ALL) );
  635. foreach($aParentClasses as $sParentClass)
  636. {
  637. if( array_key_exists($sParentClass, $aEntries) )
  638. {
  639. // If we found an ancestor of the object's class, we stop looking event if the current state is not specified
  640. if( in_array($oObject->GetState(), $aEntries[$sParentClass]) )
  641. {
  642. $bReadonly = true;
  643. }
  644. break;
  645. }
  646. }
  647. }
  648. return $bReadonly;
  649. }
  650. }
  651. /**
  652. * Record the modification of a caselog (text)
  653. * since the caselog itself stores the history
  654. * of its entries, there is no need to duplicate
  655. * the text here
  656. *
  657. * @package iTopORM
  658. */
  659. class CMDBChangeOpAttachmentAdded extends CMDBChangeOp
  660. {
  661. public static function Init()
  662. {
  663. $aParams = array
  664. (
  665. "category" => "core/cmdb",
  666. "key_type" => "",
  667. "name_attcode" => "change",
  668. "state_attcode" => "",
  669. "reconc_keys" => array(),
  670. "db_table" => "priv_changeop_attachment_added",
  671. "db_key_field" => "id",
  672. "db_finalclass_field" => "",
  673. );
  674. MetaModel::Init_Params($aParams);
  675. MetaModel::Init_InheritAttributes();
  676. MetaModel::Init_AddAttribute(new AttributeExternalKey("attachment_id", array("targetclass"=>"Attachment", "allowed_values"=>null, "sql"=>"attachment_id", "is_null_allowed"=>true, "on_target_delete"=>DEL_SILENT, "depends_on"=>array())));
  677. MetaModel::Init_AddAttribute(new AttributeString("filename", array("allowed_values"=>null, "sql"=>"filename", "default_value"=>"", "is_null_allowed"=>false, "depends_on"=>array())));
  678. // Display lists
  679. MetaModel::Init_SetZListItems('details', array('attachment_id')); // Attributes to be displayed for the complete details
  680. MetaModel::Init_SetZListItems('list', array('attachment_id')); // Attributes to be displayed for a list
  681. }
  682. /**
  683. * Describe (as a text string) the modifications corresponding to this change
  684. */
  685. public function GetDescription()
  686. {
  687. // Temporary, until we change the options of GetDescription() -needs a more global revision
  688. $bIsHtml = true;
  689. $sResult = '';
  690. $sTargetObjectClass = 'Attachment';
  691. $iTargetObjectKey = $this->Get('attachment_id');
  692. $sFilename = htmlentities($this->Get('filename'), ENT_QUOTES, 'UTF-8');
  693. $oTargetSearch = new DBObjectSearch($sTargetObjectClass);
  694. $oTargetSearch->AddCondition('id', $iTargetObjectKey, '=');
  695. $oMonoObjectSet = new DBObjectSet($oTargetSearch);
  696. if ($oMonoObjectSet->Count() > 0)
  697. {
  698. $oAttachment = $oMonoObjectSet->Fetch();
  699. $oDoc = $oAttachment->Get('contents');
  700. $sPreview = $oDoc->IsPreviewAvailable() ? 'data-preview="true"' : '';
  701. $sResult = Dict::Format('Attachments:History_File_Added', '<span class="attachment-history-added attachment"><a '.$sPreview.' target="_blank" href="'.$oDoc->GetDownloadURL($sTargetObjectClass, $iTargetObjectKey, 'contents').'">'.$sFilename.'</a></span>');
  702. }
  703. else
  704. {
  705. $sResult = Dict::Format('Attachments:History_File_Added', '<span class="attachment-history-deleted">'.$sFilename.'</span>');
  706. }
  707. return $sResult;
  708. }
  709. }
  710. class CMDBChangeOpAttachmentRemoved extends CMDBChangeOp
  711. {
  712. public static function Init()
  713. {
  714. $aParams = array
  715. (
  716. "category" => "core/cmdb",
  717. "key_type" => "",
  718. "name_attcode" => "change",
  719. "state_attcode" => "",
  720. "reconc_keys" => array(),
  721. "db_table" => "priv_changeop_attachment_removed",
  722. "db_key_field" => "id",
  723. "db_finalclass_field" => "",
  724. );
  725. MetaModel::Init_Params($aParams);
  726. MetaModel::Init_InheritAttributes();
  727. MetaModel::Init_AddAttribute(new AttributeString("filename", array("allowed_values"=>null, "sql"=>"filename", "default_value"=>"", "is_null_allowed"=>false, "depends_on"=>array())));
  728. // Display lists
  729. MetaModel::Init_SetZListItems('details', array('filename')); // Attributes to be displayed for the complete details
  730. MetaModel::Init_SetZListItems('list', array('filename')); // Attributes to be displayed for a list
  731. }
  732. /**
  733. * Describe (as a text string) the modifications corresponding to this change
  734. */
  735. public function GetDescription()
  736. {
  737. // Temporary, until we change the options of GetDescription() -needs a more global revision
  738. $bIsHtml = true;
  739. $sResult = Dict::Format('Attachments:History_File_Removed', '<span class="attachment-history-deleted">'.htmlentities($this->Get('filename'), ENT_QUOTES, 'UTF-8').'</span>');
  740. return $sResult;
  741. }
  742. }