ormcaselog.class.inc.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  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. define('CASELOG_VISIBLE_ITEMS', 2);
  19. define('CASELOG_SEPARATOR', "\n".'========== %1$s : %2$s (%3$d) ============'."\n\n");
  20. /**
  21. * Class to store a "case log" in a structured way, keeping track of its successive entries
  22. *
  23. * @copyright Copyright (C) 2010-2012 Combodo SARL
  24. * @license http://opensource.org/licenses/AGPL-3.0
  25. */
  26. class ormCaseLog {
  27. protected $m_sLog;
  28. protected $m_aIndex;
  29. protected $m_bModified;
  30. /**
  31. * Initializes the log with the first (initial) entry
  32. * @param $sLog string The text of the whole case log
  33. * @param $aIndex hash The case log index
  34. */
  35. public function __construct($sLog = '', $aIndex = array())
  36. {
  37. $this->m_sLog = $sLog;
  38. $this->m_aIndex = $aIndex;
  39. $this->m_bModified = false;
  40. }
  41. public function GetText()
  42. {
  43. return $this->m_sLog;
  44. }
  45. public static function FromJSON($oJson)
  46. {
  47. if (!isset($oJson->items))
  48. {
  49. throw new Exception("Missing 'items' elements");
  50. }
  51. $oCaseLog = new ormCaseLog();
  52. foreach($oJson->items as $oItem)
  53. {
  54. $oCaseLog->AddLogEntryFromJSON($oItem);
  55. }
  56. return $oCaseLog;
  57. }
  58. /**
  59. * Return a value that will be further JSON encoded
  60. */
  61. public function GetForJSON()
  62. {
  63. $aEntries = array();
  64. $iPos = 0;
  65. for($index=count($this->m_aIndex)-1 ; $index >= 0 ; $index--)
  66. {
  67. $iPos += $this->m_aIndex[$index]['separator_length'];
  68. $sTextEntry = substr($this->m_sLog, $iPos, $this->m_aIndex[$index]['text_length']);
  69. $iPos += $this->m_aIndex[$index]['text_length'];
  70. // Workaround: PHP < 5.3 cannot unserialize correctly DateTime objects,
  71. // therefore we have changed the format. To preserve the compatibility with existing
  72. // installations of iTop, both format are allowed:
  73. // the 'date' item is either a DateTime object, or a unix timestamp
  74. if (is_int($this->m_aIndex[$index]['date']))
  75. {
  76. // Unix timestamp
  77. $sDate = date(Dict::S('UI:CaseLog:DateFormat'),$this->m_aIndex[$index]['date']);
  78. }
  79. elseif (is_object($this->m_aIndex[$index]['date']))
  80. {
  81. if (version_compare(phpversion(), '5.3.0', '>='))
  82. {
  83. // DateTime
  84. $sDate = $this->m_aIndex[$index]['date']->format(Dict::S('UI:CaseLog:DateFormat'));
  85. }
  86. else
  87. {
  88. // No Warning... but the date is unknown
  89. $sDate = '';
  90. }
  91. }
  92. $aEntries[] = array(
  93. 'date' => $sDate,
  94. 'user_login' => $this->m_aIndex[$index]['user_name'],
  95. 'user_id' => $this->m_aIndex[$index]['user_id'],
  96. 'message' => $sTextEntry
  97. );
  98. }
  99. // Process the case of an eventual remainder (quick migration of AttributeText fields)
  100. if ($iPos < (strlen($this->m_sLog) - 1))
  101. {
  102. $sTextEntry = substr($this->m_sLog, $iPos);
  103. $aEntries[] = array(
  104. 'date' => '',
  105. 'user_login' => '',
  106. 'message' => $sTextEntry
  107. );
  108. }
  109. // Order by ascending date
  110. $aRet = array('entries' => array_reverse($aEntries));
  111. return $aRet;
  112. }
  113. public function GetIndex()
  114. {
  115. return $this->m_aIndex;
  116. }
  117. public function __toString()
  118. {
  119. return $this->m_sLog;
  120. }
  121. public function ClearModifiedFlag()
  122. {
  123. $this->m_bModified = false;
  124. }
  125. /**
  126. * Produces an HTML representation, aimed at being used within an email
  127. */
  128. public function GetAsEmailHtml()
  129. {
  130. $sStyleCaseLogHeader = '';
  131. $sStyleCaseLogEntry = '';
  132. $sHtml = '<table style="width:100%;table-layout:fixed"><tr><td>'; // Use table-layout:fixed to force the with to be independent from the actual content
  133. $iPos = 0;
  134. $aIndex = $this->m_aIndex;
  135. for($index=count($aIndex)-1 ; $index >= 0 ; $index--)
  136. {
  137. $iPos += $aIndex[$index]['separator_length'];
  138. $sTextEntry = substr($this->m_sLog, $iPos, $aIndex[$index]['text_length']);
  139. $sTextEntry = str_replace(array("\r\n", "\n", "\r"), "<br/>", htmlentities($sTextEntry, ENT_QUOTES, 'UTF-8'));
  140. $iPos += $aIndex[$index]['text_length'];
  141. $sEntry = '<div class="caselog_header" style="'.$sStyleCaseLogHeader.'">';
  142. // Workaround: PHP < 5.3 cannot unserialize correctly DateTime objects,
  143. // therefore we have changed the format. To preserve the compatibility with existing
  144. // installations of iTop, both format are allowed:
  145. // the 'date' item is either a DateTime object, or a unix timestamp
  146. if (is_int($aIndex[$index]['date']))
  147. {
  148. // Unix timestamp
  149. $sDate = date(Dict::S('UI:CaseLog:DateFormat'),$aIndex[$index]['date']);
  150. }
  151. elseif (is_object($aIndex[$index]['date']))
  152. {
  153. if (version_compare(phpversion(), '5.3.0', '>='))
  154. {
  155. // DateTime
  156. $sDate = $aIndex[$index]['date']->format(Dict::S('UI:CaseLog:DateFormat'));
  157. }
  158. else
  159. {
  160. // No Warning... but the date is unknown
  161. $sDate = '';
  162. }
  163. }
  164. $sEntry .= sprintf(Dict::S('UI:CaseLog:Header_Date_UserName'), '<span class="caselog_header_date">'.$sDate.'</span>', '<span class="caselog_header_user">'.$aIndex[$index]['user_name'].'</span>');
  165. $sEntry .= '</div>';
  166. $sEntry .= '<div class="caselog_entry" style="'.$sStyleCaseLogEntry.'">';
  167. $sEntry .= $sTextEntry;
  168. $sEntry .= '</div>';
  169. $sHtml = $sHtml.$sEntry;
  170. }
  171. // Process the case of an eventual remainder (quick migration of AttributeText fields)
  172. if ($iPos < (strlen($this->m_sLog) - 1))
  173. {
  174. $sTextEntry = substr($this->m_sLog, $iPos);
  175. $sTextEntry = str_replace(array("\r\n", "\n", "\r"), "<br/>", htmlentities($sTextEntry, ENT_QUOTES, 'UTF-8'));
  176. if (count($this->m_aIndex) == 0)
  177. {
  178. $sHtml .= '<div class="caselog_entry" style="'.$sStyleCaseLogEntry.'"">';
  179. $sHtml .= $sTextEntry;
  180. $sHtml .= '</div>';
  181. }
  182. else
  183. {
  184. $sHtml .= '<div class="caselog_header" style="'.$sStyleCaseLogHeader.'">';
  185. $sHtml .= Dict::S('UI:CaseLog:InitialValue');
  186. $sHtml .= '</div>';
  187. $sHtml .= '<div class="caselog_entry" style="'.$sStyleCaseLogEntry.'">';
  188. $sHtml .= $sTextEntry;
  189. $sHtml .= '</div>';
  190. }
  191. }
  192. $sHtml .= '</td></tr></table>';
  193. return $sHtml;
  194. }
  195. /**
  196. * Produces an HTML representation, aimed at being used within the iTop framework
  197. */
  198. public function GetAsHTML(WebPage $oP = null, $bEditMode = false, $aTransfoHandler = null)
  199. {
  200. $sHtml = '<table style="width:100%;table-layout:fixed"><tr><td>'; // Use table-layout:fixed to force the with to be independent from the actual content
  201. $iPos = 0;
  202. $aIndex = $this->m_aIndex;
  203. if (($bEditMode) && (count($aIndex) > 0) && $this->m_bModified)
  204. {
  205. // Don't display the first element, that is still considered as editable
  206. $iPos = $aIndex[0]['separator_length'] + $aIndex[0]['text_length'];
  207. array_shift($aIndex);
  208. }
  209. for($index=count($aIndex)-1 ; $index >= 0 ; $index--)
  210. {
  211. if ($index < count($aIndex) - CASELOG_VISIBLE_ITEMS)
  212. {
  213. $sOpen = '';
  214. $sDisplay = 'style="display:none;"';
  215. }
  216. else
  217. {
  218. $sOpen = ' open';
  219. $sDisplay = '';
  220. }
  221. $iPos += $aIndex[$index]['separator_length'];
  222. $sTextEntry = substr($this->m_sLog, $iPos, $aIndex[$index]['text_length']);
  223. $sTextEntry = str_replace(array("\r\n", "\n", "\r"), "<br/>", htmlentities($sTextEntry, ENT_QUOTES, 'UTF-8'));
  224. if (!is_null($aTransfoHandler))
  225. {
  226. $sTextEntry = call_user_func($aTransfoHandler, $sTextEntry);
  227. }
  228. $iPos += $aIndex[$index]['text_length'];
  229. $sEntry = '<div class="caselog_header'.$sOpen.'">';
  230. // Workaround: PHP < 5.3 cannot unserialize correctly DateTime objects,
  231. // therefore we have changed the format. To preserve the compatibility with existing
  232. // installations of iTop, both format are allowed:
  233. // the 'date' item is either a DateTime object, or a unix timestamp
  234. if (is_int($aIndex[$index]['date']))
  235. {
  236. // Unix timestamp
  237. $sDate = date(Dict::S('UI:CaseLog:DateFormat'),$aIndex[$index]['date']);
  238. }
  239. elseif (is_object($aIndex[$index]['date']))
  240. {
  241. if (version_compare(phpversion(), '5.3.0', '>='))
  242. {
  243. // DateTime
  244. $sDate = $aIndex[$index]['date']->format(Dict::S('UI:CaseLog:DateFormat'));
  245. }
  246. else
  247. {
  248. // No Warning... but the date is unknown
  249. $sDate = '';
  250. }
  251. }
  252. $sEntry .= sprintf(Dict::S('UI:CaseLog:Header_Date_UserName'), $sDate, $aIndex[$index]['user_name']);
  253. $sEntry .= '</div>';
  254. $sEntry .= '<div class="caselog_entry"'.$sDisplay.'>';
  255. $sEntry .= $sTextEntry;
  256. $sEntry .= '</div>';
  257. $sHtml = $sHtml.$sEntry;
  258. }
  259. // Process the case of an eventual remainder (quick migration of AttributeText fields)
  260. if ($iPos < (strlen($this->m_sLog) - 1))
  261. {
  262. $sTextEntry = substr($this->m_sLog, $iPos);
  263. $sTextEntry = str_replace(array("\r\n", "\n", "\r"), "<br/>", htmlentities($sTextEntry, ENT_QUOTES, 'UTF-8'));
  264. if (!is_null($aTransfoHandler))
  265. {
  266. $sTextEntry = call_user_func($aTransfoHandler, $sTextEntry);
  267. }
  268. if (count($this->m_aIndex) == 0)
  269. {
  270. $sHtml .= '<div class="caselog_entry open">';
  271. $sHtml .= $sTextEntry;
  272. $sHtml .= '</div>';
  273. }
  274. else
  275. {
  276. if (count($this->m_aIndex) - CASELOG_VISIBLE_ITEMS > 0)
  277. {
  278. $sOpen = '';
  279. $sDisplay = 'style="display:none;"';
  280. }
  281. else
  282. {
  283. $sOpen = ' open';
  284. $sDisplay = '';
  285. }
  286. $sHtml .= '<div class="caselog_header'.$sOpen.'">';
  287. $sHtml .= Dict::S('UI:CaseLog:InitialValue');
  288. $sHtml .= '</div>';
  289. $sHtml .= '<div class="caselog_entry"'.$sDisplay.'>';
  290. $sHtml .= $sTextEntry;
  291. $sHtml .= '</div>';
  292. }
  293. }
  294. $sHtml .= '</td></tr></table>';
  295. return $sHtml;
  296. }
  297. /**
  298. * Add a new entry to the log or merge the given text into the currently modified entry
  299. * and updates the internal index
  300. * @param $sText string The text of the new entry
  301. */
  302. public function AddLogEntry($sText, $sOnBehalfOf = '')
  303. {
  304. $bMergeEntries = false;
  305. $sDate = date(Dict::S('UI:CaseLog:DateFormat'));
  306. if ($sOnBehalfOf == '')
  307. {
  308. $sOnBehalfOf = UserRights::GetUserFriendlyName();
  309. $iUserId = UserRights::GetUserId();
  310. }
  311. else
  312. {
  313. $iUserId = null;
  314. }
  315. if ($this->m_bModified)
  316. {
  317. $aLatestEntry = end($this->m_aIndex);
  318. if ($aLatestEntry['user_name'] != $sOnBehalfOf)
  319. {
  320. $bMergeEntries = false;
  321. }
  322. else
  323. {
  324. $bMergeEntries = true;
  325. }
  326. }
  327. if ($bMergeEntries)
  328. {
  329. $aLatestEntry = end($this->m_aIndex);
  330. $this->m_sLog = substr($this->m_sLog, $aLatestEntry['separator_length']);
  331. $sSeparator = sprintf(CASELOG_SEPARATOR, $sDate, $sOnBehalfOf, $iUserId);
  332. $iSepLength = strlen($sSeparator);
  333. $iTextlength = strlen($sText."\n");
  334. $this->m_sLog = $sSeparator.$sText.$this->m_sLog; // Latest entry printed first
  335. $this->m_aIndex[] = array(
  336. 'user_name' => $sOnBehalfOf,
  337. 'user_id' => $iUserId,
  338. 'date' => time(),
  339. 'text_length' => $aLatestEntry['text_length'] + $iTextlength,
  340. 'separator_length' => $iSepLength,
  341. );
  342. }
  343. else
  344. {
  345. $sSeparator = sprintf(CASELOG_SEPARATOR, $sDate, $sOnBehalfOf, $iUserId);
  346. $iSepLength = strlen($sSeparator);
  347. $iTextlength = strlen($sText);
  348. $this->m_sLog = $sSeparator.$sText.$this->m_sLog; // Latest entry printed first
  349. $this->m_aIndex[] = array(
  350. 'user_name' => $sOnBehalfOf,
  351. 'user_id' => $iUserId,
  352. 'date' => time(),
  353. 'text_length' => $iTextlength,
  354. 'separator_length' => $iSepLength,
  355. );
  356. }
  357. $this->m_bModified = true;
  358. }
  359. public function AddLogEntryFromJSON($oJson)
  360. {
  361. $sText = isset($oJson->message) ? $oJson->message : '';
  362. if (isset($oJson->user_id))
  363. {
  364. if (!UserRights::IsAdministrator())
  365. {
  366. throw new Exception("Only administrators can set the user id", RestResult::UNAUTHORIZED);
  367. }
  368. try
  369. {
  370. $oUser = RestUtils::FindObjectFromKey('User', $oJson->user_id);
  371. }
  372. catch(Exception $e)
  373. {
  374. throw new Exception('user_id: '.$e->getMessage(), $e->getCode());
  375. }
  376. $iUserId = $oUser->GetKey();
  377. $sOnBehalfOf = $oUser->GetFriendlyName();
  378. }
  379. else
  380. {
  381. $iUserId = UserRights::GetUserId();
  382. $sOnBehalfOf = UserRights::GetUserFriendlyName();
  383. }
  384. if (isset($oJson->date))
  385. {
  386. $oDate = new DateTime($oJson->date);
  387. $iDate = (int) $oDate->format('U');
  388. }
  389. else
  390. {
  391. $iDate = time();
  392. }
  393. $sDate = date(Dict::S('UI:CaseLog:DateFormat'), $iDate);
  394. $sSeparator = sprintf(CASELOG_SEPARATOR, $sDate, $sOnBehalfOf, $iUserId);
  395. $iSepLength = strlen($sSeparator);
  396. $iTextlength = strlen($sText);
  397. $this->m_sLog = $sSeparator.$sText.$this->m_sLog; // Latest entry printed first
  398. $this->m_aIndex[] = array(
  399. 'user_name' => $sOnBehalfOf,
  400. 'user_id' => $iUserId,
  401. 'date' => $iDate,
  402. 'text_length' => $iTextlength,
  403. 'separator_length' => $iSepLength,
  404. );
  405. $this->m_bModified = true;
  406. }
  407. public function GetModifiedEntry()
  408. {
  409. $sModifiedEntry = '';
  410. if ($this->m_bModified)
  411. {
  412. $sModifiedEntry = $this->GetLatestEntry();
  413. }
  414. return $sModifiedEntry;
  415. }
  416. /**
  417. * Get the latest entry from the log
  418. * @return string
  419. */
  420. public function GetLatestEntry()
  421. {
  422. $aLastEntry = end($this->m_aIndex);
  423. $sRes = substr($this->m_sLog, $aLastEntry['separator_length'], $aLastEntry['text_length']);
  424. return $sRes;
  425. }
  426. /**
  427. * Get the index of the latest entry from the log
  428. * @return integer
  429. */
  430. public function GetLatestEntryIndex()
  431. {
  432. $aKeys = array_keys($this->m_aIndex);
  433. $iLast = end($aKeys); // Strict standards: the parameter passed to 'end' must be a variable since it is passed by reference
  434. return $iLast;
  435. }
  436. }
  437. ?>