ormcaselog.class.inc.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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. public function GetAsHTML(WebPage $oP = null, $bEditMode = false, $aTransfoHandler = null)
  126. {
  127. $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
  128. $iPos = 0;
  129. $aIndex = $this->m_aIndex;
  130. if (($bEditMode) && (count($aIndex) > 0) && $this->m_bModified)
  131. {
  132. // Don't display the first element, that is still considered as editable
  133. $iPos = $aIndex[0]['separator_length'] + $aIndex[0]['text_length'];
  134. array_shift($aIndex);
  135. }
  136. for($index=count($aIndex)-1 ; $index >= 0 ; $index--)
  137. {
  138. if ($index < count($aIndex) - CASELOG_VISIBLE_ITEMS)
  139. {
  140. $sOpen = '';
  141. $sDisplay = 'style="display:none;"';
  142. }
  143. else
  144. {
  145. $sOpen = ' open';
  146. $sDisplay = '';
  147. }
  148. $iPos += $aIndex[$index]['separator_length'];
  149. $sTextEntry = substr($this->m_sLog, $iPos, $aIndex[$index]['text_length']);
  150. $sTextEntry = str_replace(array("\r\n", "\n", "\r"), "<br/>", htmlentities($sTextEntry, ENT_QUOTES, 'UTF-8'));
  151. if (!is_null($aTransfoHandler))
  152. {
  153. $sTextEntry = call_user_func($aTransfoHandler, $sTextEntry);
  154. }
  155. $iPos += $aIndex[$index]['text_length'];
  156. $sEntry = '<div class="caselog_header'.$sOpen.'">';
  157. // Workaround: PHP < 5.3 cannot unserialize correctly DateTime objects,
  158. // therefore we have changed the format. To preserve the compatibility with existing
  159. // installations of iTop, both format are allowed:
  160. // the 'date' item is either a DateTime object, or a unix timestamp
  161. if (is_int($aIndex[$index]['date']))
  162. {
  163. // Unix timestamp
  164. $sDate = date(Dict::S('UI:CaseLog:DateFormat'),$aIndex[$index]['date']);
  165. }
  166. elseif (is_object($aIndex[$index]['date']))
  167. {
  168. if (version_compare(phpversion(), '5.3.0', '>='))
  169. {
  170. // DateTime
  171. $sDate = $aIndex[$index]['date']->format(Dict::S('UI:CaseLog:DateFormat'));
  172. }
  173. else
  174. {
  175. // No Warning... but the date is unknown
  176. $sDate = '';
  177. }
  178. }
  179. $sEntry .= sprintf(Dict::S('UI:CaseLog:Header_Date_UserName'), $sDate, $aIndex[$index]['user_name']);
  180. $sEntry .= '</div>';
  181. $sEntry .= '<div class="caselog_entry"'.$sDisplay.'>';
  182. $sEntry .= $sTextEntry;
  183. $sEntry .= '</div>';
  184. $sHtml = $sHtml.$sEntry;
  185. }
  186. // Process the case of an eventual remainder (quick migration of AttributeText fields)
  187. if ($iPos < (strlen($this->m_sLog) - 1))
  188. {
  189. $sTextEntry = substr($this->m_sLog, $iPos);
  190. $sTextEntry = str_replace(array("\r\n", "\n", "\r"), "<br/>", htmlentities($sTextEntry, ENT_QUOTES, 'UTF-8'));
  191. if (!is_null($aTransfoHandler))
  192. {
  193. $sTextEntry = call_user_func($aTransfoHandler, $sTextEntry);
  194. }
  195. if (count($this->m_aIndex) == 0)
  196. {
  197. $sHtml .= '<div class="caselog_entry open">';
  198. $sHtml .= $sTextEntry;
  199. $sHtml .= '</div>';
  200. }
  201. else
  202. {
  203. if (count($this->m_aIndex) - CASELOG_VISIBLE_ITEMS > 0)
  204. {
  205. $sOpen = '';
  206. $sDisplay = 'style="display:none;"';
  207. }
  208. else
  209. {
  210. $sOpen = ' open';
  211. $sDisplay = '';
  212. }
  213. $sHtml .= '<div class="caselog_header'.$sOpen.'">';
  214. $sHtml .= Dict::S('UI:CaseLog:InitialValue');
  215. $sHtml .= '</div>';
  216. $sHtml .= '<div class="caselog_entry"'.$sDisplay.'>';
  217. $sHtml .= $sTextEntry;
  218. $sHtml .= '</div>';
  219. }
  220. }
  221. $sHtml .= '</td></tr></table>';
  222. return $sHtml;
  223. }
  224. /**
  225. * Add a new entry to the log or merge the given text into the currently modified entry
  226. * and updates the internal index
  227. * @param $sText string The text of the new entry
  228. */
  229. public function AddLogEntry($sText, $sOnBehalfOf = '')
  230. {
  231. $bMergeEntries = false;
  232. $sDate = date(Dict::S('UI:CaseLog:DateFormat'));
  233. if ($sOnBehalfOf == '')
  234. {
  235. $sOnBehalfOf = UserRights::GetUserFriendlyName();
  236. $iUserId = UserRights::GetUserId();
  237. }
  238. else
  239. {
  240. $iUserId = null;
  241. }
  242. if ($this->m_bModified)
  243. {
  244. $aLatestEntry = end($this->m_aIndex);
  245. if ($aLatestEntry['user_name'] != $sOnBehalfOf)
  246. {
  247. $bMergeEntries = false;
  248. }
  249. else
  250. {
  251. $bMergeEntries = true;
  252. }
  253. }
  254. if ($bMergeEntries)
  255. {
  256. $aLatestEntry = end($this->m_aIndex);
  257. $this->m_sLog = substr($this->m_sLog, $aLatestEntry['separator_length']);
  258. $sSeparator = sprintf(CASELOG_SEPARATOR, $sDate, $sOnBehalfOf, $iUserId);
  259. $iSepLength = strlen($sSeparator);
  260. $iTextlength = strlen($sText."\n");
  261. $this->m_sLog = $sSeparator.$sText.$this->m_sLog; // Latest entry printed first
  262. $this->m_aIndex[] = array(
  263. 'user_name' => $sOnBehalfOf,
  264. 'user_id' => $iUserId,
  265. 'date' => time(),
  266. 'text_length' => $aLatestEntry['text_length'] + $iTextlength,
  267. 'separator_length' => $iSepLength,
  268. );
  269. }
  270. else
  271. {
  272. $sSeparator = sprintf(CASELOG_SEPARATOR, $sDate, $sOnBehalfOf, $iUserId);
  273. $iSepLength = strlen($sSeparator);
  274. $iTextlength = strlen($sText);
  275. $this->m_sLog = $sSeparator.$sText.$this->m_sLog; // Latest entry printed first
  276. $this->m_aIndex[] = array(
  277. 'user_name' => $sOnBehalfOf,
  278. 'user_id' => $iUserId,
  279. 'date' => time(),
  280. 'text_length' => $iTextlength,
  281. 'separator_length' => $iSepLength,
  282. );
  283. }
  284. $this->m_bModified = true;
  285. }
  286. public function AddLogEntryFromJSON($oJson)
  287. {
  288. $sText = isset($oJson->message) ? $oJson->message : '';
  289. if (isset($oJson->user_id))
  290. {
  291. if (!UserRights::IsAdministrator())
  292. {
  293. throw new Exception("Only administrators can set the user id", RestResult::UNAUTHORIZED);
  294. }
  295. try
  296. {
  297. $oUser = RestUtils::FindObjectFromKey('User', $oJson->user_id);
  298. }
  299. catch(Exception $e)
  300. {
  301. throw new Exception('user_id: '.$e->getMessage(), $e->getCode());
  302. }
  303. $iUserId = $oUser->GetKey();
  304. $sOnBehalfOf = $oUser->GetFriendlyName();
  305. }
  306. else
  307. {
  308. $iUserId = UserRights::GetUserId();
  309. $sOnBehalfOf = UserRights::GetUserFriendlyName();
  310. }
  311. if (isset($oJson->date))
  312. {
  313. $oDate = new DateTime($oJson->date);
  314. $iDate = (int) $oDate->format('U');
  315. }
  316. else
  317. {
  318. $iDate = time();
  319. }
  320. $sDate = date(Dict::S('UI:CaseLog:DateFormat'), $iDate);
  321. $sSeparator = sprintf(CASELOG_SEPARATOR, $sDate, $sOnBehalfOf, $iUserId);
  322. $iSepLength = strlen($sSeparator);
  323. $iTextlength = strlen($sText);
  324. $this->m_sLog = $sSeparator.$sText.$this->m_sLog; // Latest entry printed first
  325. $this->m_aIndex[] = array(
  326. 'user_name' => $sOnBehalfOf,
  327. 'user_id' => $iUserId,
  328. 'date' => $iDate,
  329. 'text_length' => $iTextlength,
  330. 'separator_length' => $iSepLength,
  331. );
  332. $this->m_bModified = true;
  333. }
  334. public function GetModifiedEntry()
  335. {
  336. $sModifiedEntry = '';
  337. if ($this->m_bModified)
  338. {
  339. $sModifiedEntry = $this->GetLatestEntry();
  340. }
  341. return $sModifiedEntry;
  342. }
  343. /**
  344. * Get the latest entry from the log
  345. * @return string
  346. */
  347. public function GetLatestEntry()
  348. {
  349. $aLastEntry = end($this->m_aIndex);
  350. $sRes = substr($this->m_sLog, $aLastEntry['separator_length'], $aLastEntry['text_length']);
  351. return $sRes;
  352. }
  353. /**
  354. * Get the index of the latest entry from the log
  355. * @return integer
  356. */
  357. public function GetLatestEntryIndex()
  358. {
  359. $aKeys = array_keys($this->m_aIndex);
  360. $iLast = end($aKeys); // Strict standards: the parameter passed to 'end' must be a variable since it is passed by reference
  361. return $iLast;
  362. }
  363. }
  364. ?>