ormcaselog.class.inc.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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_DATE_FORMAT', 'Y-m-d H:i:s');
  17. define('CASELOG_HEADER_FORMAT', 'On %1$s, %2$s wrote:');
  18. define('CASELOG_VISIBLE_ITEMS', 2);
  19. define('CASELOG_SEPARATOR', "\n".'========== %1$s : %2$s (%3$d) ============'."\n\n");
  20. //require_once(APPROOT.'/core/userrights.class.inc.php');
  21. //require_once(APPROOT.'/application/webpage.class.inc.php');
  22. /**
  23. * Class to store a "case log" in a structured way, keeping track of its successive entries
  24. */
  25. class ormCaseLog {
  26. protected $m_sLog;
  27. protected $m_aIndex;
  28. /**
  29. * Initializes the log with the first (initial) entry
  30. * @param $sLog string The text of the whole case log
  31. * @param $aIndex hash The case log index
  32. */
  33. public function __construct($sLog = '', $aIndex = array())
  34. {
  35. $this->m_sLog = $sLog;
  36. $this->m_aIndex = $aIndex;
  37. }
  38. public function GetText()
  39. {
  40. return $this->m_sLog;
  41. }
  42. public function GetIndex()
  43. {
  44. return $this->m_aIndex;
  45. }
  46. public function GetAsHTML(WebPage $oP = null, $bEditMode = false)
  47. {
  48. $sHtml = '';
  49. $iPos = strlen($this->m_sLog);
  50. for($index=0; $index < count($this->m_aIndex); $index++)
  51. {
  52. if ($index < count($this->m_aIndex) - CASELOG_VISIBLE_ITEMS)
  53. {
  54. $sOpen = '';
  55. $sDisplay = 'style="display:none;"';
  56. }
  57. else
  58. {
  59. $sOpen = ' open';
  60. $sDisplay = '';
  61. }
  62. $iStart = $iPos - $this->m_aIndex[$index]['text_length'];
  63. $sTextEntry = substr($this->m_sLog, $iStart, $this->m_aIndex[$index]['text_length']);
  64. $iPos = $iStart - $this->m_aIndex[$index]['separator_length'];
  65. $sEntry = '<div class="caselog_header'.$sOpen.'">';
  66. $sEntry .= sprintf(CASELOG_HEADER_FORMAT, $this->m_aIndex[$index]['date']->format(CASELOG_DATE_FORMAT), $this->m_aIndex[$index]['user_name']);
  67. $sEntry .= '</div>';
  68. $sEntry .= '<div class="caselog_entry"'.$sDisplay.'>';
  69. $sEntry .= str_replace("\n", "<br/>", htmlentities($sTextEntry, ENT_QUOTES, 'UTF-8'));
  70. $sEntry .= '</div>';
  71. $sHtml = $sEntry . $sHtml;
  72. }
  73. $sHtml = '<div class="caselog">'.$sHtml.'</div>';
  74. return $sHtml;
  75. }
  76. /**
  77. * Add a new entry to the log and updates the internal index
  78. * @param $sText string The text of the new entry
  79. */
  80. public function AddLogEntry($sText)
  81. {
  82. $sDate = date(CASELOG_DATE_FORMAT);
  83. $sSeparator = sprintf(CASELOG_SEPARATOR, $sDate, UserRights::GetUserFriendlyName(), UserRights::GetUserId());
  84. $iSepLength = strlen($sSeparator);
  85. $iTextlength = strlen($sText);
  86. $this->m_sLog = $sSeparator.$sText.$this->m_sLog; // Latest entry printed first
  87. $this->m_aIndex[] = array(
  88. 'user_name' => UserRights::GetUserFriendlyName(),
  89. 'user_id' => UserRights::GetUserId(),
  90. 'date' => new DateTime(),
  91. 'text_length' => $iTextlength,
  92. 'separator_length' => $iSepLength,
  93. );
  94. }
  95. }
  96. ?>