N°5341 - refactoring to have a dedicated ormcase log service

This commit is contained in:
odain
2023-04-06 17:24:32 +02:00
parent 8d711fb37b
commit 1b0dedaf31
4 changed files with 116 additions and 96 deletions

View File

@@ -19,6 +19,7 @@
define('CASELOG_VISIBLE_ITEMS', 2); define('CASELOG_VISIBLE_ITEMS', 2);
define('CASELOG_SEPARATOR', "\n".'========== %1$s : %2$s (%3$d) ============'."\n\n"); define('CASELOG_SEPARATOR', "\n".'========== %1$s : %2$s (%3$d) ============'."\n\n");
require_once('ormcaselogservice.inc.php');
/** /**
* Class to store a "case log" in a structured way, keeping track of its successive entries * Class to store a "case log" in a structured way, keeping track of its successive entries
@@ -27,26 +28,23 @@ define('CASELOG_SEPARATOR', "\n".'========== %1$s : %2$s (%3$d) ============'."\
* @license http://opensource.org/licenses/AGPL-3.0 * @license http://opensource.org/licenses/AGPL-3.0
*/ */
class ormCaseLog { class ormCaseLog {
const CASE_LOG_SEPARATOR_REGEX_FIND = "\r?\n?========== \w+-\d+-\d+ \d+:\d+:\d+ : .*\s\(\d+\) ============\r?\n\r?\n";
const CASE_LOG_SEPARATOR_REGEX_EXTRACT = "\r?\n?========== (?<date>\w+-\d+-\d+ \d+:\d+:\d+) : (?<user_name>.*)\s\((?<user_id>\d+)\) ============\r?\n\r?\n";
protected $m_sLog; protected $m_sLog;
protected $m_aIndex; protected $m_aIndex;
protected $m_bModified; protected $m_bModified;
protected \ormCaseLogService $oOrmCaseLogService;
/** /**
* Initializes the log with the first (initial) entry * Initializes the log with the first (initial) entry
* @param $sLog string The text of the whole case log * @param $sLog string The text of the whole case log
* @param $aIndex array The case log index * @param $aIndex array The case log index
*/ */
public function __construct($sLog = '', $aIndex = array()) public function __construct($sLog = '', $aIndex = [], \ormCaseLogService $oOrmCaseLogService=null)
{ {
$this->m_sLog = $sLog; $this->m_sLog = $sLog;
$this->m_aIndex = $this->RebuildIndex($sLog);
if (count($this->m_aIndex) === 0 && count($aIndex) !== 0) {
$this->m_aIndex = $aIndex; $this->m_aIndex = $aIndex;
}
$this->m_bModified = false; $this->m_bModified = false;
$this->oOrmCaseLogService = (is_null($oOrmCaseLogService)) ? new \ormCaseLogService() : $oOrmCaseLogService;
$this->RebuildIndex();
} }
public function GetText($bConvertToPlainText = false) public function GetText($bConvertToPlainText = false)
@@ -546,8 +544,6 @@ class ormCaseLog {
$sSeparator = sprintf(CASELOG_SEPARATOR, $sDate, $sOnBehalfOf, $iUserId); $sSeparator = sprintf(CASELOG_SEPARATOR, $sDate, $sOnBehalfOf, $iUserId);
$this->m_sLog = $sSeparator.$sText.$this->m_sLog; // Latest entry printed first $this->m_sLog = $sSeparator.$sText.$this->m_sLog; // Latest entry printed first
$aIndex = $this->RebuildIndex($this->m_sLog);
if (count($aIndex) === 0) {
// Rebuild failed // Rebuild failed
$iSepLength = strlen($sSeparator); $iSepLength = strlen($sSeparator);
$iTextLength = strlen($sText); $iTextLength = strlen($sText);
@@ -559,10 +555,7 @@ class ormCaseLog {
'separator_length' => $iSepLength, 'separator_length' => $iSepLength,
'format' => 'html', 'format' => 'html',
); );
} else { $this->RebuildIndex();
$this->m_aIndex = $aIndex;
}
$this->m_bModified = true; $this->m_bModified = true;
} }
@@ -629,8 +622,6 @@ class ormCaseLog {
$sSeparator = sprintf(CASELOG_SEPARATOR, $sDate, $sOnBehalfOf, $iUserId); $sSeparator = sprintf(CASELOG_SEPARATOR, $sDate, $sOnBehalfOf, $iUserId);
$this->m_sLog = $sSeparator.$sText.$this->m_sLog; // Latest entry printed first $this->m_sLog = $sSeparator.$sText.$this->m_sLog; // Latest entry printed first
$aIndex = $this->RebuildIndex($this->m_sLog);
if (count($aIndex) === 0) {
// Rebuild failed // Rebuild failed
$iSepLength = strlen($sSeparator); $iSepLength = strlen($sSeparator);
$iTextLength = strlen($sText); $iTextLength = strlen($sText);
@@ -642,10 +633,7 @@ class ormCaseLog {
'separator_length' => $iSepLength, 'separator_length' => $iSepLength,
'format' => 'html', 'format' => 'html',
); );
} else { $this->RebuildIndex();
$this->m_aIndex = $aIndex;
}
$this->m_bModified = true; $this->m_bModified = true;
} }
@@ -728,39 +716,12 @@ class ormCaseLog {
return $sText; return $sText;
} }
public function RebuildIndex($sLog): array public function RebuildIndex(): void
{ {
$aTexts = preg_split('/'.self::CASE_LOG_SEPARATOR_REGEX_FIND.'/', $sLog, 0, PREG_SPLIT_NO_EMPTY); $aIndex = $this->oOrmCaseLogService->RebuildIndex($this->m_sLog, $this->m_aIndex);
preg_match_all('/'.self::CASE_LOG_SEPARATOR_REGEX_FIND.'/', $sLog, $aMatches); if (count($aIndex) !== 0) {
//index rebuild required
if (count($aTexts) != count($aMatches[0])) { $this->m_aIndex = $aIndex;
return []; }
}
$aIndex = [];
$iPrevDate = 0;
for ($index = count($aTexts) - 1; $index >= 0; $index--) {
$sSeparator = $aMatches[0][$index];
preg_match('/'.self::CASE_LOG_SEPARATOR_REGEX_EXTRACT.'/', $sSeparator, $aSeparatorParts);
try {
$iDate = (int)AttributeDateTime::GetAsUnixSeconds($aSeparatorParts['date']);
$iPrevDate = $iDate + 1;
}
catch (Exception $e) {
$iDate = $iPrevDate;
}
$aIndex[] = array(
'user_name' => $aSeparatorParts['user_name'],
'user_id' => $aSeparatorParts['user_id'],
'date' => $iDate,
'text_length' => strlen($aTexts[$index]),
'separator_length' => strlen($sSeparator),
'format' => 'html',
);
}
return $aIndex;
} }
} }

View File

@@ -0,0 +1,57 @@
<?php
/**
* @copyright Copyright (C) 2010-2022 Combodo SARL
* @license http://opensource.org/licenses/AGPL-3.0
*/
class ormCaseLogService
{
const CASE_LOG_SEPARATOR_REGEX_FIND = "\n?========== \w+-\d+-\d+ \d+:\d+:\d+ : .*\s\(\d+\) ============\n\n";
const CASE_LOG_SEPARATOR_REGEX_EXTRACT = "\n?========== (?<date>\w+-\d+-\d+ \d+:\d+:\d+) : (?<user_name>.*)\s\((?<user_id>\d+)\) ============\n\n";
public function __construct()
{
}
/**
* @param $sLog
*
* @return array
*/
public function RebuildIndex(string $sLog, array $aIndex)
{
$aTexts = preg_split('/'.self::CASE_LOG_SEPARATOR_REGEX_FIND.'/', $sLog, 0, PREG_SPLIT_NO_EMPTY);
preg_match_all('/'.self::CASE_LOG_SEPARATOR_REGEX_FIND.'/', $sLog, $aMatches);
if (count($aTexts) != count($aMatches[0])) {
return [];
}
$aRebuiltIndex = [];
$iPrevDate = 0;
for ($index = count($aTexts) - 1; $index >= 0; $index--) {
$sSeparator = $aMatches[0][$index];
preg_match('/'.self::CASE_LOG_SEPARATOR_REGEX_EXTRACT.'/', $sSeparator, $aSeparatorParts);
try {
$iDate = (int)AttributeDateTime::GetAsUnixSeconds($aSeparatorParts['date']);
$iPrevDate = $iDate + 1;
}
catch (Exception $e) {
$iDate = $iPrevDate;
}
$user_id = $aSeparatorParts['user_id'];
$aRebuiltIndex[] = array(
'user_name' => $aSeparatorParts['user_name'],
'user_id' => $user_id ==='0' ? null : $user_id,
'date' => $iDate,
'text_length' => strlen($aTexts[$index]),
'separator_length' => strlen($sSeparator),
'format' => 'html',
);
}
return $aRebuiltIndex;
}
}

View File

@@ -2740,6 +2740,7 @@ return array(
'iWorkingTimeComputer' => $baseDir . '/core/computing.inc.php', 'iWorkingTimeComputer' => $baseDir . '/core/computing.inc.php',
'lnkTriggerAction' => $baseDir . '/core/trigger.class.inc.php', 'lnkTriggerAction' => $baseDir . '/core/trigger.class.inc.php',
'ormCaseLog' => $baseDir . '/core/ormcaselog.class.inc.php', 'ormCaseLog' => $baseDir . '/core/ormcaselog.class.inc.php',
'ormCaseLogService' => $baseDir . '/core/ormcaselogservice.class.inc.php',
'ormCustomFieldsValue' => $baseDir . '/core/ormcustomfieldsvalue.class.inc.php', 'ormCustomFieldsValue' => $baseDir . '/core/ormcustomfieldsvalue.class.inc.php',
'ormDocument' => $baseDir . '/core/ormdocument.class.inc.php', 'ormDocument' => $baseDir . '/core/ormdocument.class.inc.php',
'ormLinkSet' => $baseDir . '/core/ormlinkset.class.inc.php', 'ormLinkSet' => $baseDir . '/core/ormlinkset.class.inc.php',

View File

@@ -3108,6 +3108,7 @@ class ComposerStaticInit0018331147de7601e7552f7da8e3bb8b
'iWorkingTimeComputer' => __DIR__ . '/../..' . '/core/computing.inc.php', 'iWorkingTimeComputer' => __DIR__ . '/../..' . '/core/computing.inc.php',
'lnkTriggerAction' => __DIR__ . '/../..' . '/core/trigger.class.inc.php', 'lnkTriggerAction' => __DIR__ . '/../..' . '/core/trigger.class.inc.php',
'ormCaseLog' => __DIR__ . '/../..' . '/core/ormcaselog.class.inc.php', 'ormCaseLog' => __DIR__ . '/../..' . '/core/ormcaselog.class.inc.php',
'ormCaseLogService' => __DIR__ . '/../..' . '/core/ormcaselogservice.class.inc.php',
'ormCustomFieldsValue' => __DIR__ . '/../..' . '/core/ormcustomfieldsvalue.class.inc.php', 'ormCustomFieldsValue' => __DIR__ . '/../..' . '/core/ormcustomfieldsvalue.class.inc.php',
'ormDocument' => __DIR__ . '/../..' . '/core/ormdocument.class.inc.php', 'ormDocument' => __DIR__ . '/../..' . '/core/ormdocument.class.inc.php',
'ormLinkSet' => __DIR__ . '/../..' . '/core/ormlinkset.class.inc.php', 'ormLinkSet' => __DIR__ . '/../..' . '/core/ormlinkset.class.inc.php',