* @package Combodo\iTop\Application\UI\Layout\ActivityPanel\ActivityEntry * @internal * @since 3.0.0 */ class EditsEntry extends ActivityEntry { // Overloaded constants public const BLOCK_CODE = 'ibo-edits-entry'; public const HTML_TEMPLATE_REL_PATH = 'layouts/activity-panel/activity-entry/edits-entry'; public const DEFAULT_TYPE = 'edits'; public const DEFAULT_DECORATION_CLASSES = 'fas fa-fw fa-pen'; /** @var string $sObjectClass */ protected $sObjectClass; /** @var array $aAttributes Array of edited attributes with their code, label and description */ protected $aAttributes; /** * EditsEntry constructor. * * @param \DateTime $oDateTime * @param \User $sAuthorLogin * @param string $sObjectClass Class of the object concerned by the edits * @param string|null $sId * * @throws \OQLException */ public function __construct(DateTime $oDateTime, string $sAuthorLogin, string $sObjectClass, ?string $sId = null) { parent::__construct($oDateTime, $sAuthorLogin, null, $sId); $this->sObjectClass = $sObjectClass; $this->SetAttributes([]); } /** * Return the class of the object concerned by the edits * * @return string */ public function GetObjectClass() { return $this->sObjectClass; } /** * Set all attributes at once, replacing all existing. * * @param array $aAttributes * * @return $this */ public function SetAttributes(array $aAttributes) { $this->aAttributes = $aAttributes; return $this; } /** * Return an array of edited attributes with their code, label and description * * @return array */ public function GetAttributes() { return $this->aAttributes; } /** * Add the attribute identified by $sAttCode to the edited attribute. * Note that if an attribute with the same $sAttCode already exists, it will be replaced. * * @param string $sAttCode * @param string $sEditDescriptionAsHtml The description of the edit already in HTML, it MUSt have been sanitized first (Already in * HTML because most of the time it comes from CMDBChangeOp::GetDescription()) * * @throws \Exception */ public function AddAttribute(string $sAttCode, string $sEditDescriptionAsHtml) { $this->aAttributes[$sAttCode] = [ 'code' => $sAttCode, 'label' => MetaModel::GetLabel($this->sObjectClass, $sAttCode), 'description' => $sEditDescriptionAsHtml, ]; } /** * Remove the attribute of code $sAttCode from the edited attributes. * Note that if there is no attribute with this code, it will proceed silently. * * @param string $sAttCode * * @return array */ public function RemoveAttribute(string $sAttCode) { if (array_key_exists($sAttCode, $this->aAttributes)) { unset($this->aAttributes[$sAttCode]); } return $this->aAttributes; } /** * Merge $oEntry into the current one ($this). * Note that edits on any existing attribute codes will be replaced. * * @param \Combodo\iTop\Application\UI\Layout\ActivityPanel\ActivityEntry\EditsEntry $oEntry * * @return $this * @throws \Exception */ public function Merge(EditsEntry $oEntry) { if($oEntry->GetObjectClass() !== $this->GetObjectClass()) { throw new Exception("Cannot merge an entry from {$oEntry->GetObjectClass()} into {$this->GetObjectClass()}, they must be for the same class"); } // Merging attributes foreach($oEntry->GetAttributes() as $sAttCode => $aAttData) { $this->aAttributes[$sAttCode] = $aAttData; } return $this; } /** * Return the short description of the edits entry in HTML * * @return string */ public function GetShortDescriptionAsHtml() { // We need the array to be indexed by numbers instead of being associative $aAttributesData = array_values($this->GetAttributes()); $iAttributesCount = count($aAttributesData); switch($iAttributesCount) { case 0: $sDescriptionAsHtml = ''; break; case 1: $sDescriptionAsHtml = $aAttributesData[0]['description']; break; default: $sFirstAttLabelAsHtml = ''.$aAttributesData[0]['label'].''; $sSecondAttLabelAsHtml = ''.$aAttributesData[1]['label'].''; switch($iAttributesCount) { case 2: $sDescriptionAsHtml = Dict::Format('Change:TwoAttributesChanged', $sFirstAttLabelAsHtml, $sSecondAttLabelAsHtml); break; case 3: $sDescriptionAsHtml = Dict::Format('Change:ThreeAttributesChanged', $sFirstAttLabelAsHtml, $sSecondAttLabelAsHtml); break; default: $sDescriptionAsHtml = Dict::Format('Change:FourOrMoreAttributesChanged', $sFirstAttLabelAsHtml, $sSecondAttLabelAsHtml, count($aAttributesData) - 2); break; } } return $sDescriptionAsHtml; } }