main.attachments.php 27 KB

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