N°931: Add link to read-only HTML representation of tags

This commit is contained in:
Eric
2018-09-18 17:34:03 +02:00
parent cdcfe03d67
commit e495b36dfb
2 changed files with 150 additions and 11 deletions

View File

@@ -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 = '<span class="'.$sCssClass.'">';
foreach($aValues as $sTagSetLabel)
foreach($aValues as $oTag)
{
$sHtml .= '<span>'.$sTagSetLabel.'</span>';
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 .= '<a href="'.$sUrl.'"><span>'.$oTag->Get('tag_label').'</span></a>';
}
else
{
$sHtml .= '<span>'.$oTag.'</span>';
}
}
$sHtml .= '</span>';
@@ -7102,7 +7155,7 @@ class AttributeTagSet extends AttributeDBFieldVoid
$sRes = "<Set>\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)
{

View File

@@ -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
*