ormcaselog.class.inc.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. <?php
  2. // Copyright (C) 2010-2015 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-2015 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. $bPrintableVersion = (utils::ReadParam('printable', '0') == '1');
  201. $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
  202. $iPos = 0;
  203. $aIndex = $this->m_aIndex;
  204. if (($bEditMode) && (count($aIndex) > 0) && $this->m_bModified)
  205. {
  206. // Don't display the first element, that is still considered as editable
  207. $iPos = $aIndex[0]['separator_length'] + $aIndex[0]['text_length'];
  208. array_shift($aIndex);
  209. }
  210. for($index=count($aIndex)-1 ; $index >= 0 ; $index--)
  211. {
  212. if (!$bPrintableVersion && ($index < count($aIndex) - CASELOG_VISIBLE_ITEMS))
  213. {
  214. $sOpen = '';
  215. $sDisplay = 'style="display:none;"';
  216. }
  217. else
  218. {
  219. $sOpen = ' open';
  220. $sDisplay = '';
  221. }
  222. $iPos += $aIndex[$index]['separator_length'];
  223. $sTextEntry = substr($this->m_sLog, $iPos, $aIndex[$index]['text_length']);
  224. $sTextEntry = str_replace(array("\r\n", "\n", "\r"), "<br/>", htmlentities($sTextEntry, ENT_QUOTES, 'UTF-8'));
  225. if (!is_null($aTransfoHandler))
  226. {
  227. $sTextEntry = call_user_func($aTransfoHandler, $sTextEntry);
  228. }
  229. $iPos += $aIndex[$index]['text_length'];
  230. $sEntry = '<div class="caselog_header'.$sOpen.'">';
  231. // Workaround: PHP < 5.3 cannot unserialize correctly DateTime objects,
  232. // therefore we have changed the format. To preserve the compatibility with existing
  233. // installations of iTop, both format are allowed:
  234. // the 'date' item is either a DateTime object, or a unix timestamp
  235. if (is_int($aIndex[$index]['date']))
  236. {
  237. // Unix timestamp
  238. $sDate = date(Dict::S('UI:CaseLog:DateFormat'),$aIndex[$index]['date']);
  239. }
  240. elseif (is_object($aIndex[$index]['date']))
  241. {
  242. if (version_compare(phpversion(), '5.3.0', '>='))
  243. {
  244. // DateTime
  245. $sDate = $aIndex[$index]['date']->format(Dict::S('UI:CaseLog:DateFormat'));
  246. }
  247. else
  248. {
  249. // No Warning... but the date is unknown
  250. $sDate = '';
  251. }
  252. }
  253. $sEntry .= sprintf(Dict::S('UI:CaseLog:Header_Date_UserName'), $sDate, $aIndex[$index]['user_name']);
  254. $sEntry .= '</div>';
  255. $sEntry .= '<div class="caselog_entry"'.$sDisplay.'>';
  256. $sEntry .= $sTextEntry;
  257. $sEntry .= '</div>';
  258. $sHtml = $sHtml.$sEntry;
  259. }
  260. // Process the case of an eventual remainder (quick migration of AttributeText fields)
  261. if ($iPos < (strlen($this->m_sLog) - 1))
  262. {
  263. $sTextEntry = substr($this->m_sLog, $iPos);
  264. $sTextEntry = str_replace(array("\r\n", "\n", "\r"), "<br/>", htmlentities($sTextEntry, ENT_QUOTES, 'UTF-8'));
  265. if (!is_null($aTransfoHandler))
  266. {
  267. $sTextEntry = call_user_func($aTransfoHandler, $sTextEntry);
  268. }
  269. if (count($this->m_aIndex) == 0)
  270. {
  271. $sHtml .= '<div class="caselog_entry open">';
  272. $sHtml .= $sTextEntry;
  273. $sHtml .= '</div>';
  274. }
  275. else
  276. {
  277. if (!$bPrintableVersion && (count($this->m_aIndex) - CASELOG_VISIBLE_ITEMS > 0))
  278. {
  279. $sOpen = '';
  280. $sDisplay = 'style="display:none;"';
  281. }
  282. else
  283. {
  284. $sOpen = ' open';
  285. $sDisplay = '';
  286. }
  287. $sHtml .= '<div class="caselog_header'.$sOpen.'">';
  288. $sHtml .= Dict::S('UI:CaseLog:InitialValue');
  289. $sHtml .= '</div>';
  290. $sHtml .= '<div class="caselog_entry"'.$sDisplay.'>';
  291. $sHtml .= $sTextEntry;
  292. $sHtml .= '</div>';
  293. }
  294. }
  295. $sHtml .= '</td></tr></table>';
  296. return $sHtml;
  297. }
  298. /**
  299. * Add a new entry to the log or merge the given text into the currently modified entry
  300. * and updates the internal index
  301. * @param $sText string The text of the new entry
  302. */
  303. public function AddLogEntry($sText, $sOnBehalfOf = '')
  304. {
  305. $bMergeEntries = false;
  306. $sDate = date(Dict::S('UI:CaseLog:DateFormat'));
  307. if ($sOnBehalfOf == '')
  308. {
  309. $sOnBehalfOf = UserRights::GetUserFriendlyName();
  310. $iUserId = UserRights::GetUserId();
  311. }
  312. else
  313. {
  314. $iUserId = null;
  315. }
  316. if ($this->m_bModified)
  317. {
  318. $aLatestEntry = end($this->m_aIndex);
  319. if ($aLatestEntry['user_name'] != $sOnBehalfOf)
  320. {
  321. $bMergeEntries = false;
  322. }
  323. else
  324. {
  325. $bMergeEntries = true;
  326. }
  327. }
  328. if ($bMergeEntries)
  329. {
  330. $aLatestEntry = end($this->m_aIndex);
  331. $this->m_sLog = substr($this->m_sLog, $aLatestEntry['separator_length']);
  332. $sSeparator = sprintf(CASELOG_SEPARATOR, $sDate, $sOnBehalfOf, $iUserId);
  333. $iSepLength = strlen($sSeparator);
  334. $iTextlength = strlen($sText."\n");
  335. $this->m_sLog = $sSeparator.$sText.$this->m_sLog; // Latest entry printed first
  336. $this->m_aIndex[] = array(
  337. 'user_name' => $sOnBehalfOf,
  338. 'user_id' => $iUserId,
  339. 'date' => time(),
  340. 'text_length' => $aLatestEntry['text_length'] + $iTextlength,
  341. 'separator_length' => $iSepLength,
  342. );
  343. }
  344. else
  345. {
  346. $sSeparator = sprintf(CASELOG_SEPARATOR, $sDate, $sOnBehalfOf, $iUserId);
  347. $iSepLength = strlen($sSeparator);
  348. $iTextlength = strlen($sText);
  349. $this->m_sLog = $sSeparator.$sText.$this->m_sLog; // Latest entry printed first
  350. $this->m_aIndex[] = array(
  351. 'user_name' => $sOnBehalfOf,
  352. 'user_id' => $iUserId,
  353. 'date' => time(),
  354. 'text_length' => $iTextlength,
  355. 'separator_length' => $iSepLength,
  356. );
  357. }
  358. $this->m_bModified = true;
  359. }
  360. public function AddLogEntryFromJSON($oJson, $bCheckUserId = true)
  361. {
  362. $sText = isset($oJson->message) ? $oJson->message : '';
  363. if (isset($oJson->user_id))
  364. {
  365. if (!UserRights::IsAdministrator())
  366. {
  367. throw new Exception("Only administrators can set the user id", RestResult::UNAUTHORIZED);
  368. }
  369. if ($bCheckUserId && ($oJson->user_id != 0))
  370. {
  371. try
  372. {
  373. $oUser = RestUtils::FindObjectFromKey('User', $oJson->user_id);
  374. }
  375. catch(Exception $e)
  376. {
  377. throw new Exception('user_id: '.$e->getMessage(), $e->getCode());
  378. }
  379. $iUserId = $oUser->GetKey();
  380. $sOnBehalfOf = $oUser->GetFriendlyName();
  381. }
  382. else
  383. {
  384. $iUserId = $oJson->user_id;
  385. $sOnBehalfOf = $oJson->user_login;
  386. }
  387. }
  388. else
  389. {
  390. $iUserId = UserRights::GetUserId();
  391. $sOnBehalfOf = UserRights::GetUserFriendlyName();
  392. }
  393. if (isset($oJson->date))
  394. {
  395. $oDate = new DateTime($oJson->date);
  396. $iDate = (int) $oDate->format('U');
  397. }
  398. else
  399. {
  400. $iDate = time();
  401. }
  402. $sDate = date(Dict::S('UI:CaseLog:DateFormat'), $iDate);
  403. $sSeparator = sprintf(CASELOG_SEPARATOR, $sDate, $sOnBehalfOf, $iUserId);
  404. $iSepLength = strlen($sSeparator);
  405. $iTextlength = strlen($sText);
  406. $this->m_sLog = $sSeparator.$sText.$this->m_sLog; // Latest entry printed first
  407. $this->m_aIndex[] = array(
  408. 'user_name' => $sOnBehalfOf,
  409. 'user_id' => $iUserId,
  410. 'date' => $iDate,
  411. 'text_length' => $iTextlength,
  412. 'separator_length' => $iSepLength,
  413. );
  414. $this->m_bModified = true;
  415. }
  416. public function GetModifiedEntry()
  417. {
  418. $sModifiedEntry = '';
  419. if ($this->m_bModified)
  420. {
  421. $sModifiedEntry = $this->GetLatestEntry();
  422. }
  423. return $sModifiedEntry;
  424. }
  425. /**
  426. * Get the latest entry from the log
  427. * @return string
  428. */
  429. public function GetLatestEntry()
  430. {
  431. $aLastEntry = end($this->m_aIndex);
  432. $sRes = substr($this->m_sLog, $aLastEntry['separator_length'], $aLastEntry['text_length']);
  433. return $sRes;
  434. }
  435. /**
  436. * Get the index of the latest entry from the log
  437. * @return integer
  438. */
  439. public function GetLatestEntryIndex()
  440. {
  441. $aKeys = array_keys($this->m_aIndex);
  442. $iLast = end($aKeys); // Strict standards: the parameter passed to 'end' must be a variable since it is passed by reference
  443. return $iLast;
  444. }
  445. }
  446. ?>