Refactor GetJSONForWidget helper (moved from cmdbAbstract to AttributeTagSet)

This commit is contained in:
Molkobain
2018-09-21 17:24:34 +02:00
parent 1af1e92b60
commit 00515ac14a
2 changed files with 42 additions and 49 deletions

View File

@@ -2034,7 +2034,7 @@ EOF
$oPage->add_dict_entry('Core:AttributeTagSet:placeholder'); $oPage->add_dict_entry('Core:AttributeTagSet:placeholder');
/** @var \ormTagSet $value */ /** @var \ormTagSet $value */
$sJson = static::GetTagSetJsonForWidget($value, $sClass, $sAttCode); $sJson = $oAttDef->GetJsonForWidget($value);
$sInputId = "attr_{$sFormPrefix}{$sAttCode}"; $sInputId = "attr_{$sFormPrefix}{$sAttCode}";
$sHTMLValue = "<div class=\"field_input_zone field_input_tagset\"><input id='$sInputId' name='$sInputId' type='hidden' value='$sJson'></div>{$sValidationSpan}{$sReloadSpan}"; $sHTMLValue = "<div class=\"field_input_zone field_input_tagset\"><input id='$sInputId' name='$sInputId' type='hidden' value='$sJson'></div>{$sValidationSpan}{$sReloadSpan}";
$sScript = "$('#$sInputId').tagset_widget();"; $sScript = "$('#$sInputId').tagset_widget();";
@@ -2149,54 +2149,6 @@ EOF
return "<div id=\"field_{$iId}\" class=\"field_value_container\"><div class=\"attribute-edit\" data-attcode=\"$sAttCode\">{$sHTMLValue}</div></div>"; return "<div id=\"field_{$iId}\" class=\"field_value_container\"><div class=\"attribute-edit\" data-attcode=\"$sAttCode\">{$sHTMLValue}</div></div>";
} }
/**
* @param \ormTagSet $oValue
*
* @param string $sClass
* @param string $sAttCode
*
* @return string JSON to be used in the itop.tagset_widget JQuery widget
* @throws \CoreException
* @throws \CoreUnexpectedValue
* @throws \MySQLException
*/
private static function GetTagSetJsonForWidget($oValue, $sClass, $sAttCode)
{
$aJson = array();
// possible_values
$aTagSetObjectData = TagSetFieldData::GetAllowedValues($sClass, $sAttCode);
$aTagSetKeyValData = array();
foreach($aTagSetObjectData as $oTagSet)
{
$aTagSetKeyValData[] = [
'code' => $oTagSet->Get('code'),
'label' => $oTagSet->Get('label')
];
}
$aJson['possible_values'] = $aTagSetKeyValData;
if (is_null($oValue))
{
$aJson['partial_values'] = array();
$aJson['orig_value'] = array();
}
else
{
$aJson['partial_values'] = $oValue->GetModifiedTags();
$aJson['orig_value'] = array_merge($oValue->GetValue(), $oValue->GetModifiedTags());
}
$aJson['added'] = array();
$aJson['removed'] = array();
/** @var \AttributeTagSet $oAttDef */
$oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
$iMaxTags = $oAttDef->GetTagMaxNb();
$aJson['max_tags_allowed'] = $iMaxTags;
return json_encode($aJson);
}
public function DisplayModifyForm(WebPage $oPage, $aExtraParams = array()) public function DisplayModifyForm(WebPage $oPage, $aExtraParams = array())
{ {
$sOwnershipToken = null; $sOwnershipToken = null;

View File

@@ -6715,6 +6715,47 @@ class AttributeTagSet extends AttributeDBFieldVoid
return array_merge(parent::ListExpectedParams(), array('is_null_allowed', 'tag_max_nb', 'tag_code_max_len')); return array_merge(parent::ListExpectedParams(), array('is_null_allowed', 'tag_max_nb', 'tag_code_max_len'));
} }
/**
* @param \ormTagSet $oValue
*
* @return string JSON to be used in the itop.tagset_widget JQuery widget
* @throws \CoreException
*/
public function GetJsonForWidget($oValue)
{
$aJson = array();
// possible_values
$aTagSetObjectData = $this->GetAllowedValues();
$aTagSetKeyValData = array();
foreach($aTagSetObjectData as $sTagCode => $sTagLabel)
{
$aTagSetKeyValData[] = [
'code' => $sTagCode,
'label' => $sTagLabel,
];
}
$aJson['possible_values'] = $aTagSetKeyValData;
if (is_null($oValue))
{
$aJson['partial_values'] = array();
$aJson['orig_value'] = array();
}
else
{
$aJson['partial_values'] = $oValue->GetModifiedTags();
$aJson['orig_value'] = array_merge($oValue->GetValue(), $oValue->GetModifiedTags());
}
$aJson['added'] = array();
$aJson['removed'] = array();
$iMaxTags = $this->GetTagMaxNb();
$aJson['max_tags_allowed'] = $iMaxTags;
return json_encode($aJson);
}
public function GetDefaultValue(DBObject $oHostObject = null) public function GetDefaultValue(DBObject $oHostObject = null)
{ {
return null; return null;