ormcaselog.class.inc.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. // Copyright (C) 2011 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. define('CASELOG_VISIBLE_ITEMS', 2);
  17. define('CASELOG_SEPARATOR', "\n".'========== %1$s : %2$s (%3$d) ============'."\n\n");
  18. //require_once(APPROOT.'/core/userrights.class.inc.php');
  19. //require_once(APPROOT.'/application/webpage.class.inc.php');
  20. /**
  21. * Class to store a "case log" in a structured way, keeping track of its successive entries
  22. */
  23. class ormCaseLog {
  24. protected $m_sLog;
  25. protected $m_aIndex;
  26. /**
  27. * Initializes the log with the first (initial) entry
  28. * @param $sLog string The text of the whole case log
  29. * @param $aIndex hash The case log index
  30. */
  31. public function __construct($sLog = '', $aIndex = array())
  32. {
  33. $this->m_sLog = $sLog;
  34. $this->m_aIndex = $aIndex;
  35. }
  36. public function GetText()
  37. {
  38. return $this->m_sLog;
  39. }
  40. public function GetIndex()
  41. {
  42. return $this->m_aIndex;
  43. }
  44. public function __toString()
  45. {
  46. return $this->m_sLog;
  47. }
  48. public function GetAsHTML(WebPage $oP = null, $bEditMode = false, $aTransfoHandler = null)
  49. {
  50. $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
  51. $iPos = 0;
  52. for($index=count($this->m_aIndex)-1 ; $index >= 0 ; $index--)
  53. {
  54. if ($index < count($this->m_aIndex) - CASELOG_VISIBLE_ITEMS)
  55. {
  56. $sOpen = '';
  57. $sDisplay = 'style="display:none;"';
  58. }
  59. else
  60. {
  61. $sOpen = ' open';
  62. $sDisplay = '';
  63. }
  64. $iPos += $this->m_aIndex[$index]['separator_length'];
  65. $sTextEntry = substr($this->m_sLog, $iPos, $this->m_aIndex[$index]['text_length']);
  66. $sTextEntry = str_replace(array("\r\n", "\n", "\r"), "<br/>", htmlentities($sTextEntry, ENT_QUOTES, 'UTF-8'));
  67. if (!is_null($aTransfoHandler))
  68. {
  69. $sTextEntry = call_user_func($aTransfoHandler, $sTextEntry);
  70. }
  71. $iPos += $this->m_aIndex[$index]['text_length'];
  72. $sEntry = '<div class="caselog_header'.$sOpen.'">';
  73. // Workaround: PHP < 5.3 cannot unserialize correctly DateTime objects,
  74. // therefore we have changed the format. To preserve the compatibility with existing
  75. // installations of iTop, both format are allowed:
  76. // the 'date' item is either a DateTime object, or a unix timestamp
  77. if (is_int($this->m_aIndex[$index]['date']))
  78. {
  79. // Unix timestamp
  80. $sDate = date(Dict::S('UI:CaseLog:DateFormat'), $this->m_aIndex[$index]['date']);
  81. }
  82. elseif (is_object($this->m_aIndex[$index]['date']))
  83. {
  84. if (version_compare(phpversion(), '5.3.0', '>='))
  85. {
  86. // DateTime
  87. $sDate = $this->m_aIndex[$index]['date']->format(Dict::S('UI:CaseLog:DateFormat'));
  88. }
  89. else
  90. {
  91. // No Warning... but the date is unknown
  92. $sDate = '';
  93. }
  94. }
  95. $sEntry .= sprintf(Dict::S('UI:CaseLog:Header_Date_UserName'), $sDate, $this->m_aIndex[$index]['user_name']);
  96. $sEntry .= '</div>';
  97. $sEntry .= '<div class="caselog_entry"'.$sDisplay.'>';
  98. $sEntry .= $sTextEntry;
  99. $sEntry .= '</div>';
  100. $sHtml = $sHtml.$sEntry;
  101. }
  102. // Process the case of an eventual remainder (quick migration of AttributeText fields)
  103. if ($iPos < (strlen($this->m_sLog) - 1))
  104. {
  105. $sTextEntry = substr($this->m_sLog, $iPos);
  106. $sTextEntry = str_replace(array("\r\n", "\n", "\r"), "<br/>", htmlentities($sTextEntry, ENT_QUOTES, 'UTF-8'));
  107. if (!is_null($aTransfoHandler))
  108. {
  109. $sTextEntry = call_user_func($aTransfoHandler, $sTextEntry);
  110. }
  111. if (count($this->m_aIndex) == 0)
  112. {
  113. $sHtml .= '<div class="caselog_entry open">';
  114. $sHtml .= $sTextEntry;
  115. $sHtml .= '</div>';
  116. }
  117. else
  118. {
  119. if (count($this->m_aIndex) - CASELOG_VISIBLE_ITEMS > 0)
  120. {
  121. $sOpen = '';
  122. $sDisplay = 'style="display:none;"';
  123. }
  124. else
  125. {
  126. $sOpen = ' open';
  127. $sDisplay = '';
  128. }
  129. $sHtml .= '<div class="caselog_header'.$sOpen.'">';
  130. $sHtml .= Dict::S('UI:CaseLog:InitialValue');
  131. $sHtml .= '</div>';
  132. $sHtml .= '<div class="caselog_entry"'.$sDisplay.'>';
  133. $sHtml .= $sTextEntry;
  134. $sHtml .= '</div>';
  135. }
  136. }
  137. $sHtml .= '</td></tr></table>';
  138. return $sHtml;
  139. }
  140. /**
  141. * Add a new entry to the log and updates the internal index
  142. * @param $sText string The text of the new entry
  143. */
  144. public function AddLogEntry($sText, $sOnBehalfOf = '')
  145. {
  146. $sDate = date(Dict::S('UI:CaseLog:DateFormat'));
  147. if ($sOnBehalfOf == '')
  148. {
  149. $sOnBehalfOf = UserRights::GetUserFriendlyName();
  150. $iUserId = UserRights::GetUserId();
  151. }
  152. else
  153. {
  154. $iUserId = null;
  155. }
  156. $sSeparator = sprintf(CASELOG_SEPARATOR, $sDate, $sOnBehalfOf, $iUserId);
  157. $iSepLength = strlen($sSeparator);
  158. $iTextlength = strlen($sText);
  159. $this->m_sLog = $sSeparator.$sText.$this->m_sLog; // Latest entry printed first
  160. $this->m_aIndex[] = array(
  161. 'user_name' => $sOnBehalfOf,
  162. 'user_id' => $iUserId,
  163. 'date' => time(),
  164. 'text_length' => $iTextlength,
  165. 'separator_length' => $iSepLength,
  166. );
  167. }
  168. /**
  169. * Get the latest entry from the log
  170. * @return string
  171. */
  172. public function GetLatestEntry()
  173. {
  174. $aLastEntry = end($this->m_aIndex);
  175. $sRes = substr($this->m_sLog, $aLastEntry['separator_length'], $aLastEntry['text_length']);
  176. return $sRes;
  177. }
  178. /**
  179. * Get the index of the latest entry from the log
  180. * @return integer
  181. */
  182. public function GetLatestEntryIndex()
  183. {
  184. $aKeys = array_keys($this->m_aIndex);
  185. $iLast = end($aKeys); // Strict standards: the parameter passed to 'end' must be a variable since it is passed by reference
  186. return $iLast;
  187. }
  188. }
  189. ?>