diff --git a/core/attributedef.class.inc.php b/core/attributedef.class.inc.php index 7b35a4fb0..4867f46fe 100644 --- a/core/attributedef.class.inc.php +++ b/core/attributedef.class.inc.php @@ -6948,7 +6948,7 @@ class AttributeTagSet extends AttributeDBFieldVoid } if ($sValue instanceof ormTagSet) { - $aValues = $sValue->GetTags(); + $aValues = $sValue->GetLabels(); return implode(' ', $aValues); } @@ -7069,18 +7069,71 @@ class AttributeTagSet extends AttributeDBFieldVoid return parent::GetAsHTML($value, $oHostObject, $bLocalize); } + // Do not display friendly names in the history of change + public function DescribeChangeAsHTML($sOldValue, $sNewValue, $sLabel = null) + { + $sResult = Dict::Format('Change:AttName_Changed', $this->GetLabel()).", "; + + /** @var \ormTagSet $oOldValue */ + $oOldValue = $this->MakeRealValue($sOldValue, null); + + /** @var \ormTagSet $oNewValue */ + $oNewValue = $this->MakeRealValue($sNewValue, null); + + $aDelta = $oOldValue->GetDeltaTags($oNewValue); + $sAdded = null; + if (isset($aDelta['added']) && !empty($aDelta['added'])) + { + $sAdded = $this->GenerateViewHtmlForValues($aDelta['added']); + $sResult .= Dict::Format('Change:LinkSet:Added', $sAdded); + } + + if (isset($aDelta['removed']) && !empty($aDelta['removed'])) + { + if (!empty($sAdded)) + { + $sResult .= ', '; + } + $sRemoved = $this->GenerateViewHtmlForValues($aDelta['removed']); + $sResult .= Dict::Format('Change:LinkSet:Removed', $sRemoved); + } + + return $sResult; + } + /** + * HTML representation of a list of tags (read-only) + * accept a list of strings or a list of TagSetFieldData + * * @param array $aValues * @param string $sCssClass * * @return string + * @throws \CoreException */ private function GenerateViewHtmlForValues($aValues, $sCssClass = 'attribute-tagset') { $sHtml = ''; - foreach($aValues as $sTagSetLabel) + foreach($aValues as $oTag) { - $sHtml .= ''.$sTagSetLabel.''; + if ($oTag instanceof TagSetFieldData) + { + $sClass = MetaModel::GetAttributeOrigin($this->GetHostClass(), $this->GetCode()); + $sAttCode = $this->GetCode(); + $sTagCode = $oTag->Get('tag_code'); + $oFilter = DBSearch::FromOQL("SELECT $sClass WHERE $sAttCode MATCHES '$sTagCode'"); + $oAppContext = new ApplicationContext(); + $sContext = $oAppContext->GetForLink(); + $sUIPage = cmdbAbstractObject::ComputeStandardUIPage($oFilter->GetClass()); + $sFilter = urlencode($oFilter->serialize()); + $sUrl = utils::GetAbsoluteUrlAppRoot()."pages/$sUIPage?operation=search&filter=".$sFilter."&{$sContext}"; + + $sHtml .= ''.$oTag->Get('tag_label').''; + } + else + { + $sHtml .= ''.$oTag.''; + } } $sHtml .= ''; @@ -7102,7 +7155,7 @@ class AttributeTagSet extends AttributeDBFieldVoid $sRes = "\n"; if ($bLocalize) { - $aValues = $value->GetTags(); + $aValues = $value->GetLabels(); } else { @@ -7142,7 +7195,7 @@ class AttributeTagSet extends AttributeDBFieldVoid { if ($bLocalize) { - $aValues = $value->GetTags(); + $aValues = $value->GetLabels(); } else { @@ -7186,7 +7239,7 @@ class AttributeTagSet extends AttributeDBFieldVoid { if ($bLocalize) { - $aValues = $value->GetTags(); + $aValues = $value->GetLabels(); } else { @@ -7215,7 +7268,6 @@ class AttributeTagSet extends AttributeDBFieldVoid * @param \ormTagSet $value * * @return array - * @throws \CoreException */ public function GetForJSON($value) { diff --git a/core/ormtagset.class.inc.php b/core/ormtagset.class.inc.php index 8246a6c95..4849ea1bc 100644 --- a/core/ormtagset.class.inc.php +++ b/core/ormtagset.class.inc.php @@ -180,7 +180,7 @@ final class ormTagSet /** * @return array of tag labels indexed by code */ - public function GetTags() + public function GetLabels() { $aTags = array(); foreach($this->aPreserved as $sTagCode => $oTag) @@ -208,10 +208,29 @@ final class ormTagSet return $aTags; } + /** + * @return array of tags indexed by code + */ + public function GetTags() + { + $aTags = array(); + foreach($this->aPreserved as $sTagCode => $oTag) + { + $aTags[$sTagCode] = $oTag; + } + foreach($this->aAdded as $sTagCode => $oTag) + { + $aTags[$sTagCode] = $oTag; + } + ksort($aTags); + + return $aTags; + } + /** * @return array of tag labels indexed by code for only the added tags */ - public function GetAddedTags() + private function GetAddedCodes() { $aTags = array(); foreach($this->aAdded as $sTagCode => $oTag) @@ -226,7 +245,7 @@ final class ormTagSet /** * @return array of tag labels indexed by code for only the removed tags */ - public function GetRemovedTags() + private function GetRemovedCodes() { $aTags = array(); foreach($this->aRemoved as $sTagCode => $oTag) @@ -238,6 +257,72 @@ final class ormTagSet return $aTags; } + /** + * @return array of tag labels indexed by code for only the added tags + */ + private function GetAddedTags() + { + $aTags = array(); + foreach($this->aAdded as $sTagCode => $oTag) + { + $aTags[$sTagCode] = $oTag; + } + ksort($aTags); + + return $aTags; + } + + /** + * @return array of tag labels indexed by code for only the removed tags + */ + private function GetRemovedTags() + { + $aTags = array(); + foreach($this->aRemoved as $sTagCode => $oTag) + { + $aTags[$sTagCode] = $oTag; + } + ksort($aTags); + + return $aTags; + } + + /** Get the delta with another TagSet + * + * $aDelta['added] = array of tag codes for only the added tags + * $aDelta['removed'] = array of tag codes for only the removed tags + * + * @param \ormTagSet $oOtherTagSet + * + * @return array + * + * @throws \CoreException + * @throws \CoreUnexpectedValue + * @throws \Exception + */ + public function GetDelta(ormTagSet $oOtherTagSet) + { + $oTag = new ormTagSet($this->sClass, $this->sAttCode); + // Set the initial value + $aOrigTagCodes = $this->GetValue(); + $oTag->SetValue($aOrigTagCodes); + // now remove everything + foreach($aOrigTagCodes as $sTagCode) + { + $oTag->RemoveTag($sTagCode); + } + // now add the tags of the other TagSet + foreach($oOtherTagSet->GetValue() as $sTagCode) + { + $oTag->AddTag($sTagCode); + } + $aDelta = array(); + $aDelta['added'] = $oTag->GetAddedCodes(); + $aDelta['removed'] = $oTag->GetRemovedCodes(); + + return $aDelta; + } + /** Get the delta with another TagSet * * $aDelta['added] = array of tag labels indexed by code for only the added tags @@ -251,7 +336,7 @@ final class ormTagSet * @throws \CoreUnexpectedValue * @throws \Exception */ - public function GetDelta(ormTagSet $oOtherTagSet) + public function GetDeltaTags(ormTagSet $oOtherTagSet) { $oTag = new ormTagSet($this->sClass, $this->sAttCode); // Set the initial value @@ -287,6 +372,8 @@ final class ormTagSet /** * Apply a delta to the current TagSet + * $aDelta['added] = array of tag code for only the added tags + * $aDelta['removed'] = array of tag code for only the removed tags * * @param $aDelta *