main.attachments.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  1. <?php
  2. // Copyright (C) 2010-2012 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. $oPage->add_style(
  184. <<<EOF
  185. .attachment {
  186. display: inline-block;
  187. text-align:center;
  188. float:left;
  189. padding:5px;
  190. }
  191. .attachment:hover {
  192. background-color: #e0e0e0;
  193. }
  194. .attachment img {
  195. border: 0;
  196. }
  197. .attachment a {
  198. text-decoration: none;
  199. color: #1C94C4;
  200. }
  201. .btn_hidden {
  202. display: none;
  203. }
  204. .drag_in {
  205. -webkit-box-shadow:inset 0 0 10px 2px #1C94C4;
  206. box-shadow:inset 0 0 10px 2px #1C94C4;
  207. }
  208. EOF
  209. );
  210. $oPage->add('<fieldset>');
  211. $oPage->add('<legend>'.Dict::S('Attachments:FieldsetTitle').'</legend>');
  212. if ($bEditMode)
  213. {
  214. $sIsDeleteEnabled = $this->m_bDeleteEnabled ? 'true' : 'false';
  215. $iTransactionId = $oPage->GetTransactionId();
  216. $sClass = get_class($oObject);
  217. $sTempId = session_id().'_'.$iTransactionId;
  218. $sDeleteBtn = Dict::S('Attachments:DeleteBtn');
  219. $oPage->add_script(
  220. <<<EOF
  221. function RemoveNewAttachment(att_id)
  222. {
  223. $('#attachment_'+att_id).attr('name', 'removed_attachments[]');
  224. $('#display_attachment_'+att_id).hide();
  225. $('#attachment_plugin').trigger('remove_attachment', [att_id]);
  226. return false; // Do not submit the form !
  227. }
  228. function ajaxFileUpload()
  229. {
  230. //starting setting some animation when the ajax starts and completes
  231. $("#attachment_loading").ajaxStart(function(){
  232. $(this).show();
  233. }).ajaxComplete(function(){
  234. $(this).hide();
  235. });
  236. /*
  237. prepareing ajax file upload
  238. url: the url of script file handling the uploaded files
  239. fileElementId: the file type of input element id and it will be the index of \$_FILES Array()
  240. dataType: it support json, xml
  241. secureuri:use secure protocol
  242. success: call back function when the ajax complete
  243. error: callback function when the ajax failed
  244. */
  245. $.ajaxFileUpload
  246. (
  247. {
  248. url: GetAbsoluteUrlModulesRoot()+'itop-attachments/ajax.attachment.php?obj_class={$sClass}&temp_id={$sTempId}&operation=add',
  249. secureuri:false,
  250. fileElementId:'file',
  251. dataType: 'json',
  252. success: function (data, status)
  253. {
  254. if(typeof(data.error) != 'undefined')
  255. {
  256. if(data.error != '')
  257. {
  258. alert(data.error);
  259. }
  260. else
  261. {
  262. var sDownloadLink = GetAbsoluteUrlAppRoot()+'pages/ajax.render.php?operation=download_document&class=Attachment&id='+data.att_id+'&field=contents';
  263. $('#attachments').append('<div class="attachment" id="display_attachment_'+data.att_id+'"><a data-preview="'+data.preview+'" href="'+sDownloadLink+'"><img src="'+data.icon+'"><br/>'+data.msg+'<input id="attachment_'+data.att_id+'" type="hidden" name="attachments[]" value="'+data.att_id+'"/></a><br/><input type="button" class="btn_hidden" value="{$sDeleteBtn}" onClick="RemoveNewAttachment('+data.att_id+');"/></div>');
  264. if($sIsDeleteEnabled)
  265. {
  266. $('#display_attachment_'+data.att_id).hover( function() { $(this).children(':button').toggleClass('btn_hidden'); } );
  267. }
  268. $('#attachment_plugin').trigger('add_attachment', [data.att_id, data.msg]);
  269. //alert(data.msg);
  270. }
  271. }
  272. },
  273. error: function (data, status, e)
  274. {
  275. alert(e);
  276. }
  277. }
  278. )
  279. return false;
  280. }
  281. EOF
  282. );
  283. $oPage->add('<span id="attachments">');
  284. while ($oAttachment = $oSet->Fetch())
  285. {
  286. $iAttId = $oAttachment->GetKey();
  287. $oDoc = $oAttachment->Get('contents');
  288. $sFileName = $oDoc->GetFileName();
  289. $sIcon = utils::GetAbsoluteUrlAppRoot().AttachmentPlugIn::GetFileIcon($sFileName);
  290. $sPreview = $oDoc->IsPreviewAvailable() ? 'true' : 'false';
  291. $sDownloadLink = utils::GetAbsoluteUrlAppRoot().'pages/ajax.render.php?operation=download_document&class=Attachment&id='.$iAttId.'&field=contents';
  292. $oPage->add('<div class="attachment" id="attachment_'.$iAttId.'"><a data-preview="'.$sPreview.'" href="'.$sDownloadLink.'"><img src="'.$sIcon.'"><br/>'.$sFileName.'<input type="hidden" name="attachments[]" value="'.$iAttId.'"/></a><br/>&nbsp;<input id="btn_remove_'.$iAttId.'" type="button" class="btn_hidden" value="Delete" onClick="$(\'#attachment_'.$iAttId.'\').remove();"/>&nbsp;</div>');
  293. }
  294. // Suggested attachments are listed here but treated as temporary
  295. $aDefault = utils::ReadParam('default', array(), false, 'raw_data');
  296. if (array_key_exists('suggested_attachments', $aDefault))
  297. {
  298. $sSuggestedAttachements = $aDefault['suggested_attachments'];
  299. if (is_array($sSuggestedAttachements))
  300. {
  301. $sSuggestedAttachements = implode(',', $sSuggestedAttachements);
  302. }
  303. $oSearch = DBObjectSearch::FromOQL("SELECT Attachment WHERE id IN($sSuggestedAttachements)");
  304. $oSet = new DBObjectSet($oSearch, array());
  305. if ($oSet->Count() > 0)
  306. {
  307. while ($oAttachment = $oSet->Fetch())
  308. {
  309. // Mark the attachments as temporary attachments for the current object/form
  310. $oAttachment->Set('temp_id', $sTempId);
  311. $oAttachment->DBUpdate();
  312. // Display them
  313. $iAttId = $oAttachment->GetKey();
  314. $oDoc = $oAttachment->Get('contents');
  315. $sFileName = $oDoc->GetFileName();
  316. $sIcon = utils::GetAbsoluteUrlAppRoot().AttachmentPlugIn::GetFileIcon($sFileName);
  317. $sDownloadLink = utils::GetAbsoluteUrlAppRoot().'pages/ajax.render.php?operation=download_document&class=Attachment&id='.$iAttId.'&field=contents';
  318. $sPreview = $oDoc->IsPreviewAvailable() ? 'true' : 'false';
  319. $oPage->add('<div class="attachment" id="display_attachment_'.$iAttId.'"><a data-preview="'.$sPreview.'" href="'.$sDownloadLink.'"><img src="'.$sIcon.'"><br/>'.$sFileName.'<input type="hidden" name="attachments[]" value="'.$iAttId.'"/></a><br/>&nbsp;<input id="btn_remove_'.$iAttId.'" type="button" class="btn_hidden" value="Delete" onClick="RemoveNewAttachment('.$iAttId.');"/>&nbsp;</div>');
  320. $oPage->add_ready_script("$('#attachment_plugin').trigger('add_attachment', [$iAttId, '".addslashes($sFileName)."']);");
  321. }
  322. }
  323. }
  324. $oPage->add('</span>');
  325. $oPage->add('<div style="clear:both"></div>');
  326. $sMaxUpload = $this->GetMaxUpload();
  327. // $oPage->p(Dict::S('Attachments:AddAttachment').'<input type="file" name="file" id="file" onChange="ajaxFileUpload();"><span style="display:none;" id="attachment_loading">&nbsp;<img src="../images/indicator.gif"></span> '.$sMaxUpload);
  328. $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);
  329. $oPage->add_linked_script('../js/jquery.iframe-transport.js');
  330. $oPage->add_linked_script('../js/jquery.fileupload.js');
  331. $oPage->add_ready_script(
  332. <<< EOF
  333. $('#file').fileupload({
  334. url: GetAbsoluteUrlModulesRoot()+'itop-attachments/ajax.attachment.php',
  335. formData: { operation: 'add', temp_id: '$sTempId', obj_class: '$sClass' },
  336. dataType: 'json',
  337. done: function (e, data) {
  338. if(typeof(data.result.error) != 'undefined')
  339. {
  340. if(data.result.error != '')
  341. {
  342. alert(data.result.error);
  343. }
  344. else
  345. {
  346. var sDownloadLink = GetAbsoluteUrlAppRoot()+'pages/ajax.render.php?operation=download_document&class=Attachment&id='+data.result.att_id+'&field=contents';
  347. $('#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.att_id+'" type="hidden" name="attachments[]" value="'+data.result.att_id+'"/></a><br/><input type="button" class="btn_hidden" value="{$sDeleteBtn}" onClick="RemoveNewAttachment('+data.result.att_id+');"/></div>');
  348. if($sIsDeleteEnabled)
  349. {
  350. $('#display_attachment_'+data.result.att_id).hover( function() { $(this).children(':button').toggleClass('btn_hidden'); } );
  351. }
  352. $('#attachment_plugin').trigger('add_attachment', [data.result.att_id, data.msg]);
  353. }
  354. }
  355. },
  356. start: function() {
  357. $('#attachment_loading').show();
  358. },
  359. stop: function() {
  360. $('#attachment_loading').hide();
  361. }
  362. });
  363. $(document).bind('dragover', function (e) {
  364. var bFiles = false;
  365. console.log(e);
  366. if (e.dataTransfer.types)
  367. {
  368. for (var i = 0; i < e.dataTransfer.types.length; i++)
  369. {
  370. if (e.dataTransfer.types[i] == "text/plain")
  371. {
  372. bFiles = false; // mozilla contains "Files" in the types list when dragging images inside the page, but it also contains "text/plain" before
  373. break;
  374. }
  375. if (e.dataTransfer.types[i] == "Files")
  376. {
  377. bFiles = true;
  378. break;
  379. }
  380. }
  381. }
  382. if (!bFiles) return; // Not dragging files
  383. var dropZone = $('#file').closest('fieldset');
  384. if (!dropZone.is(':visible'))
  385. {
  386. // Hidden, but inside an inactive tab? Higlight the tab
  387. var sTabId = dropZone.closest('.ui-tabs-panel').attr('aria-labelledby');
  388. dropZone = $('#'+sTabId).closest('li');
  389. }
  390. timeout = window.dropZoneTimeout;
  391. if (!timeout) {
  392. dropZone.addClass('drag_in');
  393. } else {
  394. clearTimeout(timeout);
  395. }
  396. window.dropZoneTimeout = setTimeout(function () {
  397. window.dropZoneTimeout = null;
  398. dropZone.removeClass('drag_in');
  399. }, 300);
  400. });
  401. EOF
  402. );
  403. $oPage->p('<span style="display:none;" id="attachment_loading">Loading, please wait...</span>');
  404. $oPage->p('<input type="hidden" id="attachment_plugin" name="attachment_plugin"/>');
  405. $oPage->add('</fieldset>');
  406. if ($this->m_bDeleteEnabled)
  407. {
  408. $oPage->add_ready_script('$(".attachment").hover( function() {$(this).children(":button").toggleClass("btn_hidden"); } );');
  409. }
  410. }
  411. else
  412. {
  413. $oPage->add('<span id="attachments">');
  414. if ($oSet->Count() == 0)
  415. {
  416. $oPage->add(Dict::S('Attachments:NoAttachment'));
  417. }
  418. else
  419. {
  420. while ($oAttachment = $oSet->Fetch())
  421. {
  422. $iAttId = $oAttachment->GetKey();
  423. $oDoc = $oAttachment->Get('contents');
  424. $sFileName = $oDoc->GetFileName();
  425. $sIcon = utils::GetAbsoluteUrlAppRoot().AttachmentPlugIn::GetFileIcon($sFileName);
  426. $sPreview = $oDoc->IsPreviewAvailable() ? 'true' : 'false';
  427. $sDownloadLink = utils::GetAbsoluteUrlAppRoot().'pages/ajax.render.php?operation=download_document&class=Attachment&id='.$iAttId.'&field=contents';
  428. $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>');
  429. }
  430. }
  431. }
  432. $sPreviewNotAvailable = addslashes(Dict::S('Attachments:PreviewNotAvailable'));
  433. $oPage->add_ready_script("$(document).tooltip({ items: '.attachment a', position: { my: 'left top', at: 'right top', using: function( position, feedback ) { $( this ).css( position ); }}, content: function() { if ($(this).attr('data-preview') == 'true') { return('<img style=\"max-width:290px\" src=\"'+$(this).attr('href')+'\"></img>');} else { return '$sPreviewNotAvailable'; }}});");
  434. }
  435. protected static function UpdateAttachments($oObject, $oChange = null)
  436. {
  437. self::$m_bIsModified = false;
  438. if (utils::ReadParam('attachment_plugin', 'not-in-form') == 'not-in-form')
  439. {
  440. // Workaround to an issue in iTop < 2.0
  441. // Leave silently if there is no trace of the attachment form
  442. return;
  443. }
  444. $iTransactionId = utils::ReadParam('transaction_id', null);
  445. if (!is_null($iTransactionId))
  446. {
  447. $aActions = array();
  448. $aAttachmentIds = utils::ReadParam('attachments', array());
  449. // Get all current attachments
  450. $oSearch = DBObjectSearch::FromOQL("SELECT Attachment WHERE item_class = :class AND item_id = :item_id");
  451. $oSet = new DBObjectSet($oSearch, array(), array('class' => get_class($oObject), 'item_id' => $oObject->GetKey()));
  452. while ($oAttachment = $oSet->Fetch())
  453. {
  454. // Remove attachments that are no longer attached to the current object
  455. if (!in_array($oAttachment->GetKey(), $aAttachmentIds))
  456. {
  457. $oAttachment->DBDelete();
  458. $aActions[] = self::GetActionDescription($oAttachment, false /* false => deletion */);
  459. }
  460. }
  461. // Attach new (temporary) attachements
  462. $sTempId = session_id().'_'.$iTransactionId;
  463. // The object is being created from a form, check if there are pending attachments
  464. // for this object, but deleting the "new" ones that were already removed from the form
  465. $aRemovedAttachmentIds = utils::ReadParam('removed_attachments', array());
  466. $sOQL = 'SELECT Attachment WHERE temp_id = :temp_id';
  467. $oSearch = DBObjectSearch::FromOQL($sOQL);
  468. foreach($aAttachmentIds as $iAttachmentId)
  469. {
  470. $oSet = new DBObjectSet($oSearch, array(), array('temp_id' => $sTempId));
  471. while($oAttachment = $oSet->Fetch())
  472. {
  473. if (in_array($oAttachment->GetKey(),$aRemovedAttachmentIds))
  474. {
  475. $oAttachment->DBDelete();
  476. // temporary attachment removed, don't even mention it in the history
  477. }
  478. else
  479. {
  480. $oAttachment->SetItem($oObject);
  481. $oAttachment->Set('temp_id', '');
  482. $oAttachment->DBUpdate();
  483. // temporary attachment confirmed, list it in the history
  484. $aActions[] = self::GetActionDescription($oAttachment, true /* true => creation */);
  485. }
  486. }
  487. }
  488. if (count($aActions) > 0)
  489. {
  490. if ($oChange == null)
  491. {
  492. // Let's create a change if non is supplied
  493. $oChange = MetaModel::NewObject("CMDBChange");
  494. $oChange->Set("date", time());
  495. $sUserString = CMDBChange::GetCurrentUserName();
  496. $oChange->Set("userinfo", $sUserString);
  497. $iChangeId = $oChange->DBInsert();
  498. }
  499. foreach($aActions as $sActionDescription)
  500. {
  501. self::RecordHistory($oChange, $oObject, $sActionDescription);
  502. }
  503. self::$m_bIsModified = true;
  504. }
  505. }
  506. }
  507. /////////////////////////////////////////////////////////////////////////////////////////
  508. public static function GetFileIcon($sFileName)
  509. {
  510. $aPathParts = pathinfo($sFileName);
  511. switch($aPathParts['extension'])
  512. {
  513. case 'doc':
  514. case 'docx':
  515. $sIcon = 'doc.png';
  516. break;
  517. case 'xls':
  518. case 'xlsx':
  519. $sIcon = 'xls.png';
  520. break;
  521. case 'ppt':
  522. case 'pptx':
  523. $sIcon = 'ppt.png';
  524. break;
  525. case 'pdf':
  526. $sIcon = 'pdf.png';
  527. break;
  528. case 'txt':
  529. case 'text':
  530. $sIcon = 'txt.png';
  531. break;
  532. case 'rtf':
  533. $sIcon = 'rtf.png';
  534. break;
  535. case 'odt':
  536. $sIcon = 'odt.png';
  537. break;
  538. case 'ods':
  539. $sIcon = 'ods.png';
  540. break;
  541. case 'odp':
  542. $sIcon = 'odp.png';
  543. break;
  544. case 'html':
  545. case 'htm':
  546. $sIcon = 'html.png';
  547. break;
  548. case 'png':
  549. case 'gif':
  550. case 'jpg':
  551. case 'jpeg':
  552. case 'tiff':
  553. case 'tif':
  554. case 'bmp':
  555. $sIcon = 'image.png';
  556. break;
  557. case 'zip':
  558. case 'gz':
  559. case 'tgz':
  560. case 'rar':
  561. $sIcon = 'zip.png';
  562. break;
  563. default:
  564. $sIcon = 'document.png';
  565. break;
  566. }
  567. return 'env-'.utils::GetCurrentEnvironment()."/itop-attachments/icons/$sIcon";
  568. }
  569. /////////////////////////////////////////////////////////////////////////
  570. private static function RecordHistory(CMDBChange $oChange, $oTargetObject, $sDescription)
  571. {
  572. $oMyChangeOp = MetaModel::NewObject("CMDBChangeOpPlugin");
  573. $oMyChangeOp->Set("change", $oChange->GetKey());
  574. $oMyChangeOp->Set("objclass", get_class($oTargetObject));
  575. $oMyChangeOp->Set("objkey", $oTargetObject->GetKey());
  576. $oMyChangeOp->Set("description", $sDescription);
  577. $iId = $oMyChangeOp->DBInsertNoReload();
  578. }
  579. /////////////////////////////////////////////////////////////////////////
  580. private static function GetActionDescription($oAttachment, $bCreate = true)
  581. {
  582. $oBlob = $oAttachment->Get('contents');
  583. $sFileName = $oBlob->GetFileName();
  584. if ($bCreate)
  585. {
  586. $sDescription = Dict::Format('Attachments:History_File_Added', $sFileName);
  587. }
  588. else
  589. {
  590. $sDescription = Dict::Format('Attachments:History_File_Removed', $sFileName);
  591. }
  592. return $sDescription;
  593. }
  594. }
  595. ?>