mirror of
https://github.com/Combodo/iTop.git
synced 2026-05-19 23:32:17 +02:00
N°5341 - refactoring to have a dedicated ormcase log service
This commit is contained in:
@@ -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);
|
$this->m_aIndex = $aIndex;
|
||||||
if (count($this->m_aIndex) === 0 && count($aIndex) !== 0) {
|
|
||||||
$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,23 +544,18 @@ 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);
|
// Rebuild failed
|
||||||
if (count($aIndex) === 0) {
|
$iSepLength = strlen($sSeparator);
|
||||||
// Rebuild failed
|
$iTextLength = strlen($sText);
|
||||||
$iSepLength = strlen($sSeparator);
|
$this->m_aIndex[] = array(
|
||||||
$iTextLength = strlen($sText);
|
'user_name' => $sOnBehalfOf,
|
||||||
$this->m_aIndex[] = array(
|
'user_id' => $iUserId,
|
||||||
'user_name' => $sOnBehalfOf,
|
'date' => time(),
|
||||||
'user_id' => $iUserId,
|
'text_length' => $iTextLength,
|
||||||
'date' => time(),
|
'separator_length' => $iSepLength,
|
||||||
'text_length' => $iTextLength,
|
'format' => 'html',
|
||||||
'separator_length' => $iSepLength,
|
);
|
||||||
'format' => 'html',
|
$this->RebuildIndex();
|
||||||
);
|
|
||||||
} else {
|
|
||||||
$this->m_aIndex = $aIndex;
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->m_bModified = true;
|
$this->m_bModified = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -629,23 +622,18 @@ 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);
|
// Rebuild failed
|
||||||
if (count($aIndex) === 0) {
|
$iSepLength = strlen($sSeparator);
|
||||||
// Rebuild failed
|
$iTextLength = strlen($sText);
|
||||||
$iSepLength = strlen($sSeparator);
|
$this->m_aIndex[] = array(
|
||||||
$iTextLength = strlen($sText);
|
'user_name' => $sOnBehalfOf,
|
||||||
$this->m_aIndex[] = array(
|
'user_id' => $iUserId,
|
||||||
'user_name' => $sOnBehalfOf,
|
'date' => time(),
|
||||||
'user_id' => $iUserId,
|
'text_length' => $iTextLength,
|
||||||
'date' => time(),
|
'separator_length' => $iSepLength,
|
||||||
'text_length' => $iTextLength,
|
'format' => 'html',
|
||||||
'separator_length' => $iSepLength,
|
);
|
||||||
'format' => 'html',
|
$this->RebuildIndex();
|
||||||
);
|
|
||||||
} else {
|
|
||||||
$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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
57
core/ormcaselogservice.inc.php
Normal file
57
core/ormcaselogservice.inc.php
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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',
|
||||||
|
|||||||
@@ -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',
|
||||||
|
|||||||
Reference in New Issue
Block a user