model.attachments.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  1. <?php
  2. // Copyright (C) 2010 Combodo SARL
  3. //
  4. // This program is free software; you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation; version 3 of the License.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. /**
  17. * Module attachments
  18. *
  19. * A quick and easy way to upload and attach files to *any* (see Configuration below) object in the CMBD in one click
  20. *
  21. * Configuration: the list of classes for which the "Attachments" tab is visible is defined via the module's 'allowed_classes'
  22. * configuration parameter. By default the tab is active for all kind of Tickets.
  23. *
  24. * @author Erwan Taloc <erwan.taloc@combodo.com>
  25. * @author Romain Quetiez <romain.quetiez@combodo.com>
  26. * @author Denis Flaven <denis.flaven@combodo.com>
  27. * @license http://www.opensource.org/licenses/gpl-3.0.html LGPL
  28. */
  29. class Attachment extends DBObject
  30. {
  31. public static function Init()
  32. {
  33. $aParams = array
  34. (
  35. "category" => "addon,bizmodel",
  36. "key_type" => "autoincrement",
  37. "name_attcode" => array('item_class', 'temp_id'),
  38. "state_attcode" => "",
  39. "reconc_keys" => array(),
  40. "db_table" => "attachment",
  41. "db_key_field" => "id",
  42. "db_finalclass_field" => "",
  43. );
  44. MetaModel::Init_Params($aParams);
  45. MetaModel::Init_InheritAttributes();
  46. MetaModel::Init_AddAttribute(new AttributeDateTime("expire", array("allowed_values"=>null, "sql"=>"expire", "default_value"=>"", "is_null_allowed"=>false, "depends_on"=>array())));
  47. MetaModel::Init_AddAttribute(new AttributeString("temp_id", array("allowed_values"=>null, "sql"=>"temp_id", "default_value"=>"", "is_null_allowed"=>true, "depends_on"=>array())));
  48. MetaModel::Init_AddAttribute(new AttributeString("item_class", array("allowed_values"=>null, "sql"=>"item_class", "default_value"=>"", "is_null_allowed"=>false, "depends_on"=>array())));
  49. MetaModel::Init_AddAttribute(new AttributeString("item_id", array("allowed_values"=>null, "sql"=>"item_id", "default_value"=>"", "is_null_allowed"=>true, "depends_on"=>array())));
  50. MetaModel::Init_AddAttribute(new AttributeInteger("item_org_id", array("allowed_values"=>null, "sql"=>"item_org_id", "default_value"=>0, "is_null_allowed"=>true, "depends_on"=>array())));
  51. MetaModel::Init_AddAttribute(new AttributeBlob("contents", array("depends_on"=>array())));
  52. MetaModel::Init_SetZListItems('details', array('temp_id', 'item_class', 'item_id', 'item_org_id'));
  53. MetaModel::Init_SetZListItems('advanced_search', array('temp_id', 'item_class', 'item_id'));
  54. MetaModel::Init_SetZListItems('standard_search', array('temp_id', 'item_class', 'item_id'));
  55. MetaModel::Init_SetZListItems('list', array('temp_id', 'item_class', 'item_id'));
  56. }
  57. /**
  58. * Maps the given context parameter name to the appropriate filter/search code for this class
  59. * @param string $sContextParam Name of the context parameter, e.g. 'org_id'
  60. * @return string Filter code, e.g. 'customer_id'
  61. */
  62. public static function MapContextParam($sContextParam)
  63. {
  64. if ($sContextParam == 'org_id')
  65. {
  66. return 'item_org_id';
  67. }
  68. else
  69. {
  70. return null;
  71. }
  72. }
  73. /**
  74. * Set/Update all of the '_item' fields
  75. * @param object $oItem Container item
  76. * @return void
  77. */
  78. public function SetItem($oItem, $bUpdateOnChange = false)
  79. {
  80. $sClass = get_class($oItem);
  81. $iItemId = $oItem->GetKey();
  82. $this->Set('item_class', $sClass);
  83. $this->Set('item_id', $iItemId);
  84. $aCallSpec = array($sClass, 'MapContextParam');
  85. if (is_callable($aCallSpec))
  86. {
  87. $sAttCode = call_user_func($aCallSpec, 'org_id'); // Returns null when there is no mapping for this parameter
  88. if (MetaModel::IsValidAttCode($sClass, $sAttCode))
  89. {
  90. $iOrgId = $oItem->Get($sAttCode);
  91. if ($iOrgId > 0)
  92. {
  93. if ($iOrgId != $this->Get('item_org_id'))
  94. {
  95. $this->Set('item_org_id', $iOrgId);
  96. if ($bUpdateOnChange)
  97. {
  98. $this->DBUpdate();
  99. }
  100. }
  101. }
  102. }
  103. }
  104. }
  105. /**
  106. * Give a default value for item_org_id (if relevant...)
  107. * @return void
  108. */
  109. public function SetDefaultOrgId()
  110. {
  111. // First check that the organization CAN be fetched from the target class
  112. //
  113. $sClass = $this->Get('item_class');
  114. $aCallSpec = array($sClass, 'MapContextParam');
  115. if (is_callable($aCallSpec))
  116. {
  117. $sAttCode = call_user_func($aCallSpec, 'org_id'); // Returns null when there is no mapping for this parameter
  118. if (MetaModel::IsValidAttCode($sClass, $sAttCode))
  119. {
  120. // Second: check that the organization CAN be fetched from the current user
  121. //
  122. if (MetaModel::IsValidClass('Person'))
  123. {
  124. $aCallSpec = array($sClass, 'MapContextParam');
  125. if (is_callable($aCallSpec))
  126. {
  127. $sAttCode = call_user_func($aCallSpec, 'org_id'); // Returns null when there is no mapping for this parameter
  128. if (MetaModel::IsValidAttCode($sClass, $sAttCode))
  129. {
  130. // OK - try it
  131. //
  132. $oCurrentPerson = MetaModel::GetObject('Person', UserRights::GetContactId(), false);
  133. if ($oCurrentPerson)
  134. {
  135. $this->Set('item_org_id', $oCurrentPerson->Get($sAttCode));
  136. }
  137. }
  138. }
  139. }
  140. }
  141. }
  142. }
  143. // Todo - implement a cleanup function (see a way to do that generic !)
  144. }
  145. class AttachmentPlugIn implements iApplicationUIExtension, iApplicationObjectExtension
  146. {
  147. public function OnDisplayProperties($oObject, WebPage $oPage, $bEditMode = false)
  148. {
  149. if ($this->GetAttachmentsPosition() == 'properties')
  150. {
  151. $this->DisplayAttachments($oObject, $oPage, $bEditMode);
  152. }
  153. }
  154. public function OnDisplayRelations($oObject, WebPage $oPage, $bEditMode = false)
  155. {
  156. if ($this->GetAttachmentsPosition() == 'relations')
  157. {
  158. $this->DisplayAttachments($oObject, $oPage, $bEditMode);
  159. }
  160. }
  161. public function OnFormSubmit($oObject, $sFormPrefix = '')
  162. {
  163. if ($this->IsTargetObject($oObject))
  164. {
  165. // For new objects attachments are processed in OnDBInsert
  166. if (!$oObject->IsNew())
  167. {
  168. self::UpdateAttachments($oObject);
  169. }
  170. }
  171. }
  172. protected function GetMaxUpload()
  173. {
  174. $iMaxUpload = ini_get('upload_max_filesize');
  175. if (!$iMaxUpload)
  176. {
  177. $sRet = Dict::S('Attachments:UploadNotAllowedOnThisSystem');
  178. }
  179. else
  180. {
  181. $iMaxUpload = utils::ConvertToBytes($iMaxUpload);
  182. if ($iMaxUpload > 1024*1024*1024)
  183. {
  184. $sRet = Dict::Format('Attachment:Max_Go', sprintf('%0.2f', $iMaxUpload/(1024*1024*1024)));
  185. }
  186. else if ($iMaxUpload > 1024*1024)
  187. {
  188. $sRet = Dict::Format('Attachment:Max_Mo', sprintf('%0.2f', $iMaxUpload/(1024*1024)));
  189. }
  190. else
  191. {
  192. $sRet = Dict::Format('Attachment:Max_Ko', sprintf('%0.2f', $iMaxUpload/(1024)));
  193. }
  194. }
  195. return $sRet;
  196. }
  197. public function OnFormCancel($sTempId)
  198. {
  199. // Delete all "pending" attachments for this form
  200. $sOQL = 'SELECT Attachment WHERE temp_id = :temp_id';
  201. $oSearch = DBObjectSearch::FromOQL($sOQL);
  202. $oSet = new DBObjectSet($oSearch, array(), array('temp_id' => $sTempId));
  203. while($oAttachment = $oSet->Fetch())
  204. {
  205. $oAttachment->DBDelete();
  206. // Pending attachment, don't mention it in the history
  207. }
  208. }
  209. public function EnumUsedAttributes($oObject)
  210. {
  211. return array();
  212. }
  213. public function GetIcon($oObject)
  214. {
  215. return '';
  216. }
  217. public function GetHilightClass($oObject)
  218. {
  219. // Possible return values are:
  220. // HILIGHT_CLASS_CRITICAL, HILIGHT_CLASS_WARNING, HILIGHT_CLASS_OK, HILIGHT_CLASS_NONE
  221. return HILIGHT_CLASS_NONE;
  222. }
  223. public function EnumAllowedActions(DBObjectSet $oSet)
  224. {
  225. // No action
  226. return array();
  227. }
  228. public function OnIsModified($oObject)
  229. {
  230. if ($this->IsTargetObject($oObject))
  231. {
  232. $aAttachmentIds = utils::ReadParam('attachments', array());
  233. $aRemovedAttachmentIds = utils::ReadParam('removed_attachments', array());
  234. if ( (count($aAttachmentIds) > 0) || (count($aRemovedAttachmentIds) > 0) )
  235. {
  236. return true;
  237. }
  238. }
  239. return false;
  240. }
  241. public function OnCheckToWrite($oObject)
  242. {
  243. return array();
  244. }
  245. public function OnCheckToDelete($oObject)
  246. {
  247. return array();
  248. }
  249. public function OnDBUpdate($oObject, $oChange = null)
  250. {
  251. if ($this->IsTargetObject($oObject))
  252. {
  253. // Get all current attachments
  254. $oSearch = DBObjectSearch::FromOQL("SELECT Attachment WHERE item_class = :class AND item_id = :item_id");
  255. $oSet = new DBObjectSet($oSearch, array(), array('class' => get_class($oObject), 'item_id' => $oObject->GetKey()));
  256. while ($oAttachment = $oSet->Fetch())
  257. {
  258. $oAttachment->SetItem($oObject, true /*updateonchange*/);
  259. }
  260. }
  261. }
  262. public function OnDBInsert($oObject, $oChange = null)
  263. {
  264. if ($this->IsTargetObject($oObject))
  265. {
  266. self::UpdateAttachments($oObject, $oChange);
  267. }
  268. }
  269. public function OnDBDelete($oObject, $oChange = null)
  270. {
  271. if ($this->IsTargetObject($oObject))
  272. {
  273. $oSearch = DBObjectSearch::FromOQL("SELECT Attachment WHERE item_class = :class AND item_id = :item_id");
  274. $oSet = new DBObjectSet($oSearch, array(), array('class' => get_class($oObject), 'item_id' => $oObject->GetKey()));
  275. while ($oAttachment = $oSet->Fetch())
  276. {
  277. $oAttachment->DBDelete();
  278. }
  279. }
  280. }
  281. ///////////////////////////////////////////////////////////////////////////////////////////////////////
  282. //
  283. // Plug-ins specific functions
  284. //
  285. ///////////////////////////////////////////////////////////////////////////////////////////////////////
  286. protected function IsTargetObject($oObject)
  287. {
  288. $aAllowedClasses = MetaModel::GetModuleSetting('itop-attachments', 'allowed_classes', array('Ticket'));
  289. foreach($aAllowedClasses as $sAllowedClass)
  290. {
  291. if ($oObject instanceof $sAllowedClass)
  292. {
  293. return true;
  294. }
  295. }
  296. return false;
  297. }
  298. protected function GetAttachmentsPosition()
  299. {
  300. return MetaModel::GetModuleSetting('itop-attachments', 'position', 'relations');
  301. }
  302. var $m_bDeleteEnabled = true;
  303. public function EnableDelete($bEnabled)
  304. {
  305. $this->m_bDeleteEnabled = $bEnabled;
  306. }
  307. public function DisplayAttachments($oObject, WebPage $oPage, $bEditMode = false)
  308. {
  309. // Exit here if the class is not allowed
  310. if (!$this->IsTargetObject($oObject)) return;
  311. $oSearch = DBObjectSearch::FromOQL("SELECT Attachment WHERE item_class = :class AND item_id = :item_id");
  312. $oSet = new DBObjectSet($oSearch, array(), array('class' => get_class($oObject), 'item_id' => $oObject->GetKey()));
  313. if ($this->GetAttachmentsPosition() == 'relations')
  314. {
  315. $sTitle = ($oSet->Count() > 0)? Dict::Format('Attachments:TabTitle_Count', $oSet->Count()) : Dict::S('Attachments:EmptyTabTitle');
  316. $oPage->SetCurrentTab($sTitle);
  317. }
  318. $oPage->add_style(
  319. <<<EOF
  320. .attachment {
  321. display: inline-block;
  322. text-align:center;
  323. float:left;
  324. padding:5px;
  325. }
  326. .attachment:hover {
  327. background-color: #e0e0e0;
  328. }
  329. .attachment img {
  330. border: 0;
  331. }
  332. .attachment a {
  333. text-decoration: none;
  334. color: #1C94C4;
  335. }
  336. .btn_hidden {
  337. display: none;
  338. }
  339. EOF
  340. );
  341. $oPage->add('<fieldset>');
  342. $oPage->add('<legend>'.Dict::S('Attachments:FieldsetTitle').'</legend>');
  343. if ($bEditMode)
  344. {
  345. $sIsDeleteEnabled = $this->m_bDeleteEnabled ? 'true' : 'false';
  346. $iTransactionId = $oPage->GetTransactionId();
  347. $oPage->add_linked_script(utils::GetAbsoluteUrlAppRoot().'modules/itop-attachments/ajaxfileupload.js');
  348. $sClass = get_class($oObject);
  349. $sTempId = session_id().'_'.$iTransactionId;
  350. $sDeleteBtn = Dict::S('Attachments:DeleteBtn');
  351. $oPage->add_script(
  352. <<<EOF
  353. function RemoveNewAttachment(att_id)
  354. {
  355. $('#attachment_'+att_id).attr('name', 'removed_attachments[]');
  356. $('#display_attachment_'+att_id).hide();
  357. return false; // Do not submit the form !
  358. }
  359. function ajaxFileUpload()
  360. {
  361. //starting setting some animation when the ajax starts and completes
  362. $("#loading").ajaxStart(function(){
  363. $(this).show();
  364. }).ajaxComplete(function(){
  365. $(this).hide();
  366. });
  367. /*
  368. prepareing ajax file upload
  369. url: the url of script file handling the uploaded files
  370. fileElementId: the file type of input element id and it will be the index of $_FILES Array()
  371. dataType: it support json, xml
  372. secureuri:use secure protocol
  373. success: call back function when the ajax complete
  374. error: callback function when the ajax failed
  375. */
  376. $.ajaxFileUpload
  377. (
  378. {
  379. url: GetAbsoluteUrlAppRoot()+'modules/itop-attachments/ajax.attachment.php?obj_class={$sClass}&temp_id={$sTempId}&operation=add',
  380. secureuri:false,
  381. fileElementId:'file',
  382. dataType: 'json',
  383. success: function (data, status)
  384. {
  385. if(typeof(data.error) != 'undefined')
  386. {
  387. if(data.error != '')
  388. {
  389. alert(data.error);
  390. }
  391. else
  392. {
  393. var sDownloadLink = GetAbsoluteUrlAppRoot()+'pages/ajax.render.php/?operation=download_document&class=Attachment&id='+data.att_id+'&field=contents';
  394. $('#attachments').append('<div class="attachment" id="display_attachment_'+data.att_id+'"><a 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>');
  395. if($sIsDeleteEnabled)
  396. {
  397. $('#display_attachment_'+data.att_id).hover( function() { $(this).children(':button').toggleClass('btn_hidden'); } );
  398. }
  399. //alert(data.msg);
  400. }
  401. }
  402. },
  403. error: function (data, status, e)
  404. {
  405. alert(e);
  406. }
  407. }
  408. )
  409. return false;
  410. }
  411. EOF
  412. );
  413. $oPage->add('<span id="attachments">');
  414. while ($oAttachment = $oSet->Fetch())
  415. {
  416. $iAttId = $oAttachment->GetKey();
  417. $oDoc = $oAttachment->Get('contents');
  418. $sFileName = $oDoc->GetFileName();
  419. $sIcon = utils::GetAbsoluteUrlAppRoot().AttachmentPlugIn::GetFileIcon($sFileName);
  420. $sDownloadLink = utils::GetAbsoluteUrlAppRoot().'pages/ajax.render.php/?operation=download_document&class=Attachment&id='.$iAttId.'&field=contents';
  421. $oPage->add('<div class="attachment" id="attachment_'.$iAttId.'"><a 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>');
  422. }
  423. $oPage->add('</span>');
  424. $oPage->add('<div style="clear:both"></div>');
  425. $sMaxUpload = $this->GetMaxUpload();
  426. $oPage->p(Dict::S('Attachments:AddAttachment').'<input type="file" name="file" id="file" onChange="ajaxFileUpload();"><span style="display:none;" id="loading">&nbsp;<img src="../images/indicator.gif"></span> '.$sMaxUpload);
  427. //$oPage->p('<input type="button" onClick="ajaxFileUpload();" value=" Upload !">');
  428. $oPage->p('<span style="display:none;" id="loading">Loading, please wait...</span>');
  429. $oPage->add('</fieldset>');
  430. if ($this->m_bDeleteEnabled)
  431. {
  432. $oPage->add_ready_script('$(".attachment").hover( function() {$(this).children(":button").toggleClass("btn_hidden"); } );');
  433. }
  434. }
  435. else
  436. {
  437. $oPage->add('<span id="attachments">');
  438. while ($oAttachment = $oSet->Fetch())
  439. {
  440. $iAttId = $oAttachment->GetKey();
  441. $oDoc = $oAttachment->Get('contents');
  442. $sFileName = $oDoc->GetFileName();
  443. $sIcon = utils::GetAbsoluteUrlAppRoot().AttachmentPlugIn::GetFileIcon($sFileName);
  444. $sDownloadLink = utils::GetAbsoluteUrlAppRoot().'pages/ajax.render.php/?operation=download_document&class=Attachment&id='.$iAttId.'&field=contents';
  445. $oPage->add('<div class="attachment" id="attachment_'.$iAttId.'"><a href="'.$sDownloadLink.'"><img src="'.$sIcon.'"><br/>'.$sFileName.'</a><input type="hidden" name="attachments[]" value="'.$iAttId.'"/><br/>&nbsp;&nbsp;</div>');
  446. }
  447. }
  448. }
  449. protected static function UpdateAttachments($oObject, $oChange = null)
  450. {
  451. $iTransactionId = utils::ReadParam('transaction_id', null);
  452. if (!is_null($iTransactionId))
  453. {
  454. $aActions = array();
  455. $aAttachmentIds = utils::ReadParam('attachments', array());
  456. // Get all current attachments
  457. $oSearch = DBObjectSearch::FromOQL("SELECT Attachment WHERE item_class = :class AND item_id = :item_id");
  458. $oSet = new DBObjectSet($oSearch, array(), array('class' => get_class($oObject), 'item_id' => $oObject->GetKey()));
  459. while ($oAttachment = $oSet->Fetch())
  460. {
  461. // Remove attachments that are no longer attached to the current object
  462. if (!in_array($oAttachment->GetKey(), $aAttachmentIds))
  463. {
  464. $oAttachment->DBDelete();
  465. $aActions[] = self::GetActionDescription($oAttachment, false /* false => deletion */);
  466. }
  467. }
  468. // Attach new (temporary) attachements
  469. $sTempId = session_id().'_'.$iTransactionId;
  470. // The object is being created from a form, check if there are pending attachments
  471. // for this object, but deleting the "new" ones that were already removed from the form
  472. $aRemovedAttachmentIds = utils::ReadParam('removed_attachments', array());
  473. $sOQL = 'SELECT Attachment WHERE temp_id = :temp_id';
  474. $oSearch = DBObjectSearch::FromOQL($sOQL);
  475. foreach($aAttachmentIds as $iAttachmentId)
  476. {
  477. $oSet = new DBObjectSet($oSearch, array(), array('temp_id' => $sTempId));
  478. while($oAttachment = $oSet->Fetch())
  479. {
  480. if (in_array($oAttachment->GetKey(),$aRemovedAttachmentIds))
  481. {
  482. $oAttachment->DBDelete();
  483. // temporary attachment removed, don't even mention it in the history
  484. }
  485. else
  486. {
  487. $oAttachment->SetItem($oObject);
  488. $oAttachment->Set('temp_id', '');
  489. $oAttachment->DBUpdate();
  490. // temporary attachment confirmed, list it in the history
  491. $aActions[] = self::GetActionDescription($oAttachment, true /* true => creation */);
  492. }
  493. }
  494. }
  495. if (count($aActions) > 0)
  496. {
  497. if ($oChange == null)
  498. {
  499. // Let's create a change if non is supplied
  500. $oChange = MetaModel::NewObject("CMDBChange");
  501. $oChange->Set("date", time());
  502. $sUserString = CMDBChange::GetCurrentUserName();
  503. $oChange->Set("userinfo", $sUserString);
  504. $iChangeId = $oChange->DBInsert();
  505. }
  506. foreach($aActions as $sActionDescription)
  507. {
  508. self::RecordHistory($oChange, $oObject, $sActionDescription);
  509. }
  510. }
  511. }
  512. }
  513. /////////////////////////////////////////////////////////////////////////////////////////
  514. public static function GetFileIcon($sFileName)
  515. {
  516. $aPathParts = pathinfo($sFileName);
  517. switch($aPathParts['extension'])
  518. {
  519. case 'doc':
  520. case 'docx':
  521. $sIcon = 'doc.png';
  522. break;
  523. case 'xls':
  524. case 'xlsx':
  525. $sIcon = 'xls.png';
  526. break;
  527. case 'ppt':
  528. case 'pptx':
  529. $sIcon = 'ppt.png';
  530. break;
  531. case 'pdf':
  532. $sIcon = 'pdf.png';
  533. break;
  534. case 'txt':
  535. case 'text':
  536. $sIcon = 'txt.png';
  537. break;
  538. case 'rtf':
  539. $sIcon = 'rtf.png';
  540. break;
  541. case 'odt':
  542. $sIcon = 'odt.png';
  543. break;
  544. case 'ods':
  545. $sIcon = 'ods.png';
  546. break;
  547. case 'odp':
  548. $sIcon = 'odp.png';
  549. break;
  550. case 'html':
  551. case 'htm':
  552. $sIcon = 'html.png';
  553. break;
  554. case 'png':
  555. case 'gif':
  556. case 'jpg':
  557. case 'jpeg':
  558. case 'tiff':
  559. case 'tif':
  560. case 'bmp':
  561. $sIcon = 'image.png';
  562. break;
  563. case 'zip':
  564. case 'gz':
  565. case 'tgz':
  566. case 'rar':
  567. $sIcon = 'zip.png';
  568. break;
  569. default:
  570. $sIcon = 'document.png';
  571. break;
  572. }
  573. return "modules/itop-attachments/icons/$sIcon";
  574. }
  575. /////////////////////////////////////////////////////////////////////////
  576. private static function RecordHistory(CMDBChange $oChange, $oTargetObject, $sDescription)
  577. {
  578. $oMyChangeOp = MetaModel::NewObject("CMDBChangeOpPlugin");
  579. $oMyChangeOp->Set("change", $oChange->GetKey());
  580. $oMyChangeOp->Set("objclass", get_class($oTargetObject));
  581. $oMyChangeOp->Set("objkey", $oTargetObject->GetKey());
  582. $oMyChangeOp->Set("description", $sDescription);
  583. $iId = $oMyChangeOp->DBInsertNoReload();
  584. }
  585. /////////////////////////////////////////////////////////////////////////
  586. private static function GetActionDescription($oAttachment, $bCreate = true)
  587. {
  588. $oBlob = $oAttachment->Get('contents');
  589. $sFileName = $oBlob->GetFileName();
  590. if ($bCreate)
  591. {
  592. $sDescription = Dict::Format('Attachments:History_File_Added', $sFileName);
  593. }
  594. else
  595. {
  596. $sDescription = Dict::Format('Attachments:History_File_Removed', $sFileName);
  597. }
  598. return $sDescription;
  599. }
  600. }
  601. ?>