mirror of
https://github.com/Combodo/iTop.git
synced 2026-05-18 23:08:46 +02:00
N°931 integrate TagSet widget
This commit is contained in:
@@ -1627,10 +1627,10 @@ EOF
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $oPage
|
||||
* @param $sClass
|
||||
* @param $sAttCode
|
||||
* @param $oAttDef
|
||||
* @param \iTopWebPage $oPage
|
||||
* @param string $sClass
|
||||
* @param string $sAttCode
|
||||
* @param \AttributeDefinition $oAttDef
|
||||
* @param string $value
|
||||
* @param string $sDisplayValue
|
||||
* @param string $iId
|
||||
@@ -1640,6 +1640,9 @@ EOF
|
||||
* @param bool $bPreserveCurrentValue Preserve the current value even if not allowed
|
||||
*
|
||||
* @return string
|
||||
* @throws \ArchivedObjectException
|
||||
* @throws \CoreException
|
||||
* @throws \DictExceptionMissingString
|
||||
*/
|
||||
public static function GetFormElementForField(
|
||||
$oPage, $sClass, $sAttCode, $oAttDef, $value = '', $sDisplayValue = '', $iId = '', $sNameSuffix = '',
|
||||
@@ -2025,6 +2028,20 @@ EOF
|
||||
$oPage->add_ready_script("$('#{$iId}').bind('validate', function(evt, sFormId) { return ValidateCustomFields('$iId', sFormId) } );"); // Custom validation function
|
||||
break;
|
||||
|
||||
case 'TagSet':
|
||||
$oPage->add_linked_script(utils::GetAbsoluteUrlAppRoot().'/js/selectize.min.js');
|
||||
$oPage->add_linked_stylesheet(utils::GetAbsoluteUrlAppRoot().'css/selectize.default.css');
|
||||
$oPage->add_linked_script(utils::GetAbsoluteUrlAppRoot().'/js/jquery.itop-tagset-widget.js');
|
||||
|
||||
/** @var \ormTagSet $value */
|
||||
$sJson = static::GetTagSetJsonForWidget($value);
|
||||
$sInputId = "attr_{$sFormPrefix}{$sAttCode}";
|
||||
$sHTMLValue = "<input id='$sInputId' name='$sInputId' type='text' value='$sJson' style='display: none;'>";
|
||||
$sScript = "$('#$sInputId').tagset_widget();";
|
||||
$oPage->add_ready_script($sScript);
|
||||
|
||||
break;
|
||||
|
||||
case 'String':
|
||||
default:
|
||||
$aEventsList[] = 'validate';
|
||||
@@ -2132,6 +2149,43 @@ EOF
|
||||
return "<div id=\"field_{$iId}\" class=\"field_value_container\"><div class=\"attribute-edit\" data-attcode=\"$sAttCode\">{$sHTMLValue}</div></div>";
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \ormTagSet $oValue
|
||||
*
|
||||
* @return string JSON to be used in the itop.tagset_widget JQuery widget
|
||||
* @throws \CoreException
|
||||
* @throws \CoreUnexpectedValue
|
||||
* @throws \MySQLException
|
||||
*/
|
||||
private static function GetTagSetJsonForWidget($oValue)
|
||||
{
|
||||
$aJson = array();
|
||||
|
||||
// possible_values
|
||||
$aTagSetObjectData = TagSetFieldData::GetAllowedValues($oValue->GetClass(), $oValue->GetAttCode());
|
||||
$aTagSetKeyValData = array();
|
||||
foreach($aTagSetObjectData as $oTagSet)
|
||||
{
|
||||
$aTagSetKeyValData[] = [
|
||||
'code' => $oTagSet->Get('tag_code'),
|
||||
'label' => $oTagSet->Get('tag_label')
|
||||
];
|
||||
}
|
||||
$aJson['possible_values'] = $aTagSetKeyValData;
|
||||
|
||||
$aJson['partial_values'] = $oValue->GetModifiedTags();
|
||||
|
||||
// orig_values
|
||||
$aJson['orig_value'] = $oValue->GetValue();
|
||||
|
||||
// added
|
||||
$aJson['added'] = array();
|
||||
// removed
|
||||
$aJson['removed'] = array();
|
||||
|
||||
return json_encode($aJson);
|
||||
}
|
||||
|
||||
public function DisplayModifyForm(WebPage $oPage, $aExtraParams = array())
|
||||
{
|
||||
$sOwnershipToken = null;
|
||||
@@ -3258,6 +3312,10 @@ EOF
|
||||
case 'TagSet':
|
||||
/** @var ormTagSet $oTagSet */
|
||||
$oTagSet = $this->Get($sAttCode);
|
||||
if (is_null($oTagSet))
|
||||
{
|
||||
$oTagSet = new ormTagSet(get_class($this), $sAttCode);
|
||||
}
|
||||
$oTagSet->ApplyDelta($value);
|
||||
$this->Set($sAttCode, $oTagSet);
|
||||
break;
|
||||
@@ -3452,14 +3510,8 @@ EOF
|
||||
break;
|
||||
|
||||
case 'TagSet':
|
||||
$sPreserved = utils::ReadPostedParam("attr_{$sFormPrefix}{$sAttCode}", null, 'raw_data');
|
||||
$sAdded = utils::ReadPostedParam("attr_{$sFormPrefix}{$sAttCode}_tba", null, 'raw_data');
|
||||
$sRemoved = utils::ReadPostedParam("attr_{$sFormPrefix}{$sAttCode}_tbr", null, 'raw_data');
|
||||
$value = array(
|
||||
'preserved' => $sPreserved,
|
||||
'added' => $sAdded,
|
||||
'removed' => $sRemoved,
|
||||
);
|
||||
$sTagSetJson = utils::ReadPostedParam("attr_{$sFormPrefix}{$sAttCode}", null, 'raw_data');
|
||||
$value = json_decode($sTagSetJson, true);
|
||||
break;
|
||||
|
||||
default:
|
||||
@@ -3864,6 +3916,19 @@ EOF
|
||||
/**
|
||||
* Display a form for modifying several objects at once
|
||||
* The form will be submitted to the current page, with the specified additional values
|
||||
*
|
||||
* @param \iTopWebPage $oP
|
||||
* @param string $sClass
|
||||
* @param array $aSelectedObj
|
||||
* @param string $sCustomOperation
|
||||
* @param string $sCancelUrl
|
||||
* @param array $aExcludeAttributes
|
||||
* @param array $aContextData
|
||||
*
|
||||
* @throws \CoreException
|
||||
* @throws \CoreUnexpectedValue
|
||||
* @throws \MySQLException
|
||||
* @throws \OQLException
|
||||
*/
|
||||
public static function DisplayBulkModifyForm(
|
||||
$oP, $sClass, $aSelectedObj, $sCustomOperation, $sCancelUrl, $aExcludeAttributes = array(),
|
||||
@@ -4024,15 +4089,18 @@ EOF
|
||||
$oDummyObj->Set($sAttCode, $currValue);
|
||||
/** @var ormTagSet $oTagSet */
|
||||
$oTagSet = $oDummyObj->Get($sAttCode);
|
||||
foreach($aKeys as $sValues)
|
||||
foreach($aKeys as $iIndex => $sValues)
|
||||
{
|
||||
if ($iIndex == 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
$aTagCodes = array();
|
||||
if (!empty($sValues))
|
||||
{
|
||||
foreach(explode(' ', $sValues) as $sTagCode)
|
||||
{
|
||||
$oTagSet->AddTag($sTagCode);
|
||||
}
|
||||
$aTagCodes = explode(' ', $sValues);
|
||||
}
|
||||
$oTagSet->GenerateDiffFromTags($aTagCodes);
|
||||
}
|
||||
$oDummyObj->Set($sAttCode, $oTagSet);
|
||||
}
|
||||
|
||||
@@ -311,7 +311,10 @@ class WebPage implements Page
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a script (as an include, i.e. link) to the header of the page
|
||||
* Add a script (as an include, i.e. link) to the header of the page.<br>
|
||||
* Handles duplicates : calling twice with the same script will add the script only once
|
||||
*
|
||||
* @param string $s_linked_script
|
||||
*/
|
||||
public function add_linked_script($s_linked_script)
|
||||
{
|
||||
|
||||
@@ -5964,18 +5964,11 @@ class AttributeTagSet extends AttributeDBFieldVoid
|
||||
return ($val1 == $val2);
|
||||
}
|
||||
|
||||
public function GetAllowedValues($aArgs = array(), $sContains = '')
|
||||
{
|
||||
$sAttCode = $this->GetCode();
|
||||
$sClass = MetaModel::GetAttributeOrigin($this->GetHostClass(), $sAttCode);
|
||||
$aAllowedTags = TagSetFieldData::GetAllowedValues($sClass, $sAttCode);
|
||||
$aAllowedValues = array();
|
||||
foreach($aAllowedTags as $oAllowedTag)
|
||||
{
|
||||
$aAllowedValues[$oAllowedTag->Get('tag_code')] = $oAllowedTag->Get('tag_label');
|
||||
}
|
||||
return $aAllowedValues;
|
||||
}
|
||||
public function GetAllowedValues($aArgs = array(), $sContains = '')
|
||||
{
|
||||
// The check is done when adding / removing tags, no need to have also this check here
|
||||
return array();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $aCols
|
||||
|
||||
@@ -30,11 +30,6 @@ final class ormTagSet
|
||||
private $sAttCode; // attcode of the tag field
|
||||
private $aOriginalObjects = null;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
private $bHasDelta = false;
|
||||
|
||||
/**
|
||||
* Object from the original set, minus the removed objects
|
||||
*
|
||||
@@ -52,6 +47,11 @@ final class ormTagSet
|
||||
*/
|
||||
private $aRemoved = array();
|
||||
|
||||
/**
|
||||
* @var DBObject[] Modified items (mass edit)
|
||||
*/
|
||||
private $aModified = array();
|
||||
|
||||
/**
|
||||
* __toString magical function overload.
|
||||
*/
|
||||
@@ -88,6 +88,22 @@ final class ormTagSet
|
||||
$this->sClass = $sClass;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function GetClass()
|
||||
{
|
||||
return $this->sClass;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function GetAttCode()
|
||||
{
|
||||
return $this->sAttCode;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param array $aTagCodes
|
||||
@@ -106,14 +122,14 @@ final class ormTagSet
|
||||
foreach($aTagCodes as $sTagCode)
|
||||
{
|
||||
$oTag = $this->GetTagFromCode($sTagCode);
|
||||
$oTags[$oTag->GetKey()] = $oTag;
|
||||
$oTags[$sTagCode] = $oTag;
|
||||
}
|
||||
|
||||
$this->aPreserved = &$oTags;
|
||||
$this->aRemoved = array();
|
||||
$this->aAdded = array();
|
||||
$this->aModified = array();
|
||||
$this->aOriginalObjects = $oTags;
|
||||
$this->bHasDelta = false;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -122,25 +138,13 @@ final class ormTagSet
|
||||
public function GetValue()
|
||||
{
|
||||
$aValues = array();
|
||||
foreach($this->aPreserved as $oTag)
|
||||
foreach($this->aPreserved as $sTagCode => $oTag)
|
||||
{
|
||||
try
|
||||
{
|
||||
$aValues[] = $oTag->Get('tag_code');
|
||||
} catch (CoreException $e)
|
||||
{
|
||||
IssueLog::Error($e->getMessage());
|
||||
}
|
||||
$aValues[] = $sTagCode;
|
||||
}
|
||||
foreach($this->aAdded as $oTag)
|
||||
foreach($this->aAdded as $sTagCode => $oTag)
|
||||
{
|
||||
try
|
||||
{
|
||||
$aValues[] = $oTag->Get('tag_code');
|
||||
} catch (CoreException $e)
|
||||
{
|
||||
IssueLog::Error($e->getMessage());
|
||||
}
|
||||
$aValues[] = $sTagCode;
|
||||
}
|
||||
|
||||
sort($aValues);
|
||||
@@ -154,21 +158,21 @@ final class ormTagSet
|
||||
public function GetTags()
|
||||
{
|
||||
$aTags = array();
|
||||
foreach($this->aPreserved as $oTag)
|
||||
foreach($this->aPreserved as $sTagCode => $oTag)
|
||||
{
|
||||
try
|
||||
{
|
||||
$aTags[$oTag->Get('tag_code')] = $oTag->Get('tag_label');
|
||||
$aTags[$sTagCode] = $oTag->Get('tag_label');
|
||||
} catch (CoreException $e)
|
||||
{
|
||||
IssueLog::Error($e->getMessage());
|
||||
}
|
||||
}
|
||||
foreach($this->aAdded as $oTag)
|
||||
foreach($this->aAdded as $sTagCode => $oTag)
|
||||
{
|
||||
try
|
||||
{
|
||||
$aTags[$oTag->Get('tag_code')] = $oTag->Get('tag_label');
|
||||
$aTags[$sTagCode] = $oTag->Get('tag_label');
|
||||
} catch (CoreException $e)
|
||||
{
|
||||
IssueLog::Error($e->getMessage());
|
||||
@@ -185,15 +189,9 @@ final class ormTagSet
|
||||
public function GetAddedTags()
|
||||
{
|
||||
$aTags = array();
|
||||
foreach($this->aAdded as $oTag)
|
||||
foreach($this->aAdded as $sTagCode => $oTag)
|
||||
{
|
||||
try
|
||||
{
|
||||
$aTags[$oTag->Get('tag_code')] = $oTag->Get('tag_label');
|
||||
} catch (CoreException $e)
|
||||
{
|
||||
IssueLog::Error($e->getMessage());
|
||||
}
|
||||
$aTags[] = $sTagCode;
|
||||
}
|
||||
ksort($aTags);
|
||||
|
||||
@@ -206,15 +204,9 @@ final class ormTagSet
|
||||
public function GetRemovedTags()
|
||||
{
|
||||
$aTags = array();
|
||||
foreach($this->aRemoved as $oTag)
|
||||
foreach($this->aRemoved as $sTagCode => $oTag)
|
||||
{
|
||||
try
|
||||
{
|
||||
$aTags[$oTag->Get('tag_code')] = $oTag->Get('tag_label');
|
||||
} catch (CoreException $e)
|
||||
{
|
||||
IssueLog::Error($e->getMessage());
|
||||
}
|
||||
$aTags[] = $sTagCode;
|
||||
}
|
||||
ksort($aTags);
|
||||
|
||||
@@ -257,6 +249,17 @@ final class ormTagSet
|
||||
return $aDelta;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[] list of codes for partial entries
|
||||
*/
|
||||
public function GetModifiedTags()
|
||||
{
|
||||
$aModifiedTagCodes = array_keys($this->aModified);
|
||||
sort($aModifiedTagCodes);
|
||||
|
||||
return $aModifiedTagCodes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply a delta to the current TagSet
|
||||
*
|
||||
@@ -269,14 +272,14 @@ final class ormTagSet
|
||||
{
|
||||
if (isset($aDelta['removed']))
|
||||
{
|
||||
foreach($aDelta['removed'] as $sTagCode => $aTagLabel)
|
||||
foreach($aDelta['removed'] as $sTagCode)
|
||||
{
|
||||
$this->RemoveTag($sTagCode);
|
||||
}
|
||||
}
|
||||
if (isset($aDelta['added']))
|
||||
{
|
||||
foreach($aDelta['added'] as $sTagCode => $aTagLabel)
|
||||
foreach($aDelta['added'] as $sTagCode)
|
||||
{
|
||||
$this->AddTag($sTagCode);
|
||||
}
|
||||
@@ -320,13 +323,15 @@ final class ormTagSet
|
||||
if (($oTag = $this->RemoveTagFromList($this->aRemoved, $sTagCode)) !== false)
|
||||
{
|
||||
// put it back into preserved
|
||||
$this->aPreserved[] = $oTag;
|
||||
$this->aPreserved[$sTagCode] = $oTag;
|
||||
// no need to add it to aModified : was already done when calling RemoveTag method
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->aAdded[] = $this->GetTagFromCode($sTagCode);
|
||||
$oTag = $this->GetTagFromCode($sTagCode);
|
||||
$this->aAdded[$sTagCode] = $oTag;
|
||||
$this->aModified[$sTagCode] = $oTag;
|
||||
}
|
||||
$this->UpdateHasDeltaFlag();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -339,75 +344,71 @@ final class ormTagSet
|
||||
// nothing to do, already removed tag
|
||||
return;
|
||||
}
|
||||
// if added then remove it
|
||||
if (($oTag = $this->RemoveTagFromList($this->aAdded, $sTagCode)) === false)
|
||||
|
||||
$oTag = $this->RemoveTagFromList($this->aAdded, $sTagCode);
|
||||
if ($oTag !== false)
|
||||
{
|
||||
// if present then remove it
|
||||
if (($oTag = $this->RemoveTagFromList($this->aPreserved, $sTagCode)) !== false)
|
||||
{
|
||||
$this->aRemoved[] = $oTag;
|
||||
}
|
||||
$this->aModified[$sTagCode] = $oTag;
|
||||
|
||||
return; // if present in added, can't be in preserved !
|
||||
}
|
||||
|
||||
$oTag = $this->RemoveTagFromList($this->aPreserved, $sTagCode);
|
||||
if ($oTag !== false)
|
||||
{
|
||||
$this->aModified[$sTagCode] = $oTag;
|
||||
$this->aRemoved[$sTagCode] = $oTag;
|
||||
}
|
||||
$this->UpdateHasDeltaFlag();
|
||||
}
|
||||
|
||||
private function IsTagInList($aTagList, $sTagCode)
|
||||
{
|
||||
foreach($aTagList as $oTag)
|
||||
{
|
||||
/** @var \TagSetFieldData $oTag */
|
||||
try
|
||||
{
|
||||
$sCode = $oTag->Get('tag_code');
|
||||
if ($sCode === $sTagCode)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
} catch (CoreException $e)
|
||||
{
|
||||
IssueLog::Error($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
return isset($aTagList[$sTagCode]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \ormTagSet[] $aTagList
|
||||
* @param string $sTagCode
|
||||
*
|
||||
* @return bool|\ormTagSet false if not found, else the removed element
|
||||
*/
|
||||
private function RemoveTagFromList(&$aTagList, $sTagCode)
|
||||
{
|
||||
foreach($aTagList as $index => $oTag)
|
||||
if (!($this->IsTagInList($aTagList, $sTagCode)))
|
||||
{
|
||||
/** @var \TagSetFieldData $oTag */
|
||||
try
|
||||
{
|
||||
$sCode = $oTag->Get('tag_code');
|
||||
if ($sCode === $sTagCode)
|
||||
{
|
||||
unset($aTagList[$index]);
|
||||
return false;
|
||||
}
|
||||
|
||||
return $oTag;
|
||||
}
|
||||
} catch (CoreException $e)
|
||||
$oTag = $aTagList[$sTagCode];
|
||||
unset($aTagList[$sTagCode]);
|
||||
|
||||
return $oTag;
|
||||
}
|
||||
|
||||
/**
|
||||
* Populates the added and removed arrays for bulk edit
|
||||
*
|
||||
* @param string[] $aTagCodes
|
||||
*
|
||||
* @throws \CoreException
|
||||
* @throws \CoreUnexpectedValue
|
||||
*/
|
||||
public function GenerateDiffFromTags($aTagCodes)
|
||||
{
|
||||
foreach($this->GetValue() as $sCurrentTagCode)
|
||||
{
|
||||
if (!in_array($sCurrentTagCode, $aTagCodes))
|
||||
{
|
||||
IssueLog::Error($e->getMessage());
|
||||
$this->RemoveTag($sCurrentTagCode);
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private function UpdateHasDeltaFlag()
|
||||
{
|
||||
if ((count($this->aAdded) == 0) && (count($this->aRemoved) == 0))
|
||||
foreach($aTagCodes as $sNewTagCode)
|
||||
{
|
||||
$this->bHasDelta = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->bHasDelta = true;
|
||||
$this->AddTag($sNewTagCode);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $sTagCode
|
||||
*
|
||||
@@ -429,7 +430,10 @@ final class ormTagSet
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
* @return \TagSetFieldData[]
|
||||
* @throws \CoreException
|
||||
* @throws \CoreUnexpectedValue
|
||||
* @throws \MySQLException
|
||||
*/
|
||||
private function GetAllowedTags()
|
||||
{
|
||||
|
||||
@@ -192,7 +192,7 @@ abstract class TagSetFieldData extends cmdbAbstractObject
|
||||
* @param $sClass
|
||||
* @param $sAttCode
|
||||
*
|
||||
* @return mixed
|
||||
* @return \TagSetFieldData[]
|
||||
* @throws \CoreException
|
||||
* @throws \CoreUnexpectedValue
|
||||
* @throws \MySQLException
|
||||
|
||||
@@ -2889,3 +2889,29 @@ table.listResults .originColor {
|
||||
background-color: lightgrey;
|
||||
font-style: italic;
|
||||
}
|
||||
div.selectize-control {
|
||||
position: static;
|
||||
display: inline-block;
|
||||
vertical-align: middle;
|
||||
}
|
||||
div.selectize-input {
|
||||
width: auto;
|
||||
min-width: 8em;
|
||||
}
|
||||
div.selectize-input.has-items:after {
|
||||
content: "➕";
|
||||
display: inline-block;
|
||||
}
|
||||
div.selectize-control > div.selectize-input > div.item.partial-code {
|
||||
color: floralwhite;
|
||||
background-color: grey;
|
||||
background-image: linear-gradient(to bottom, white, grey);
|
||||
border-color: darkgray;
|
||||
font-style: italic;
|
||||
}
|
||||
div.selectize-control > div.selectize-input > div.item.partial-code.active {
|
||||
background-image: linear-gradient(to bottom, grey, darkgrey);
|
||||
}
|
||||
div.selectize-control > div.selectize-input > div.item.partial-code > a.remove {
|
||||
border-color: grey;
|
||||
}
|
||||
|
||||
@@ -3297,4 +3297,36 @@ table.listResults .originColor{
|
||||
color: grey;
|
||||
background-color: lightgrey;
|
||||
font-style: italic;
|
||||
}
|
||||
}
|
||||
|
||||
div.selectize-control {
|
||||
position: static;
|
||||
display: inline-block;
|
||||
vertical-align: middle;
|
||||
}
|
||||
|
||||
div.selectize-input {
|
||||
width: auto;
|
||||
min-width: 8em;
|
||||
}
|
||||
|
||||
div.selectize-input.has-items:after {
|
||||
content: "➕";
|
||||
display: inline-block;
|
||||
}
|
||||
|
||||
div.selectize-control > div.selectize-input > div.item.partial-code {
|
||||
color: floralwhite;
|
||||
background-color: grey;
|
||||
background-image: linear-gradient(to bottom, white, grey);
|
||||
border-color: darkgray;
|
||||
font-style: italic;
|
||||
}
|
||||
|
||||
div.selectize-control > div.selectize-input > div.item.partial-code.active {
|
||||
background-image: linear-gradient(to bottom, grey, darkgrey);
|
||||
}
|
||||
|
||||
div.selectize-control > div.selectize-input > div.item.partial-code > a.remove {
|
||||
border-color: grey;
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -54,7 +54,7 @@
|
||||
* }
|
||||
* </code>
|
||||
*
|
||||
* <p>Needs js/selectize.js already loaded !!<br>
|
||||
* <p>Needs js/selectize.js already loaded !! (https://github.com/selectize/selectize.js)<br>
|
||||
* In the future we could use a solution like this :
|
||||
* https://www.safaribooksonline.com/library/view/learning-javascript-design/9781449334840/ch13s09.html
|
||||
*/
|
||||
@@ -95,7 +95,7 @@ $.widget('itop.tagset_widget',
|
||||
_initWidgetData: function (originalFieldValue) {
|
||||
var dataArray = JSON.parse(originalFieldValue);
|
||||
this.possibleValues = dataArray[this.POSSIBLE_VAL_KEY];
|
||||
this.partialValues = dataArray[this.PARTIAL_VAL_KEY];
|
||||
this.partialValues = ($.isArray(dataArray[this.PARTIAL_VAL_KEY])) ? dataArray[this.PARTIAL_VAL_KEY] : [];
|
||||
this.originalValue = dataArray[this.ORIG_VAL_KEY];
|
||||
this.tagSetCodesStatus = {};
|
||||
},
|
||||
|
||||
1034
js/selectize.min.js
vendored
Normal file
1034
js/selectize.min.js
vendored
Normal file
File diff suppressed because it is too large
Load Diff
@@ -38,157 +38,240 @@ use ormTagSet;
|
||||
* @runTestsInSeparateProcesses
|
||||
* @preserveGlobalState disabled
|
||||
* @backupGlobals disabled
|
||||
*/class ormTagSetTest extends ItopDataTestCase
|
||||
*/
|
||||
class ormTagSetTest extends ItopDataTestCase
|
||||
{
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
protected function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
}
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
protected function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
public function testGetTagDataClass()
|
||||
{
|
||||
$oTagSet = new ormTagSet('Ticket', 'tagfield');
|
||||
static::assertEquals($oTagSet->GetTagDataClass(), 'TagSetFieldDataFor_Ticket_tagfield');
|
||||
}
|
||||
$this->CreateTagData(TAG_CLASS, TAG_ATTCODE, 'tag1', 'First');
|
||||
$this->CreateTagData(TAG_CLASS, TAG_ATTCODE, 'tag2', 'Second');
|
||||
$this->CreateTagData(TAG_CLASS, TAG_ATTCODE, 'tag3', 'Third');
|
||||
$this->CreateTagData(TAG_CLASS, TAG_ATTCODE, 'tag4', 'Fourth');
|
||||
}
|
||||
|
||||
public function testGetValue()
|
||||
{
|
||||
$this->CreateTagData('Ticket', 'tagfield', 'tag1', 'First');
|
||||
$this->CreateTagData('Ticket', 'tagfield', 'tag2', 'Second');
|
||||
public function testGetTagDataClass()
|
||||
{
|
||||
$oTagSet = new ormTagSet(TAG_CLASS, TAG_ATTCODE);
|
||||
static::assertEquals($oTagSet->GetTagDataClass(), 'TagSetFieldDataFor_Ticket_tagfield');
|
||||
}
|
||||
|
||||
$oTagSet = new ormTagSet('Ticket', 'tagfield');
|
||||
static::assertEquals($oTagSet->GetValue(), array());
|
||||
public function testGetValue()
|
||||
{
|
||||
$oTagSet = new ormTagSet(TAG_CLASS, TAG_ATTCODE);
|
||||
static::assertEquals($oTagSet->GetValue(), array());
|
||||
|
||||
$oTagSet->AddTag('tag1');
|
||||
static::assertEquals($oTagSet->GetValue(), array('tag1'));
|
||||
$oTagSet->AddTag('tag1');
|
||||
static::assertEquals($oTagSet->GetValue(), array('tag1'));
|
||||
|
||||
$oTagSet->AddTag('tag2');
|
||||
static::assertEquals($oTagSet->GetValue(), array('tag1', 'tag2'));
|
||||
}
|
||||
$oTagSet->AddTag('tag2');
|
||||
static::assertEquals($oTagSet->GetValue(), array('tag1', 'tag2'));
|
||||
}
|
||||
|
||||
public function testAddTag()
|
||||
{
|
||||
$this->CreateTagData('Ticket', 'tagfield', 'tag1', 'First');
|
||||
$this->CreateTagData('Ticket', 'tagfield', 'tag2', 'Second');
|
||||
public function testAddTag()
|
||||
{
|
||||
$oTagSet = new ormTagSet(TAG_CLASS, TAG_ATTCODE);
|
||||
|
||||
$oTagSet = new ormTagSet('Ticket', 'tagfield');
|
||||
$oTagSet->AddTag('tag1');
|
||||
static::assertEquals($oTagSet->GetValue(), array('tag1'));
|
||||
|
||||
$oTagSet->AddTag('tag1');
|
||||
static::assertEquals($oTagSet->GetValue(), array('tag1'));
|
||||
$oTagSet->SetValue(array('tag1', 'tag2'));
|
||||
static::assertEquals($oTagSet->GetValue(), array('tag1', 'tag2'));
|
||||
|
||||
$oTagSet->SetValue(array('tag1', 'tag2'));
|
||||
static::assertEquals($oTagSet->GetValue(), array('tag1', 'tag2'));
|
||||
$oTagSet->RemoveTag('tag1');
|
||||
static::assertEquals($oTagSet->GetValue(), array('tag2'));
|
||||
|
||||
$oTagSet->RemoveTag('tag1');
|
||||
static::assertEquals($oTagSet->GetValue(), array('tag2'));
|
||||
$oTagSet->AddTag('tag1');
|
||||
static::assertEquals($oTagSet->GetValue(), array('tag1', 'tag2'));
|
||||
}
|
||||
|
||||
$oTagSet->AddTag('tag1');
|
||||
static::assertEquals($oTagSet->GetValue(), array('tag1', 'tag2'));
|
||||
}
|
||||
public function testEquals()
|
||||
{
|
||||
$oTagSet1 = new ormTagSet(TAG_CLASS, TAG_ATTCODE);
|
||||
$oTagSet1->AddTag('tag1');
|
||||
static::assertTrue($oTagSet1->Equals($oTagSet1));
|
||||
|
||||
public function testEquals()
|
||||
{
|
||||
$this->CreateTagData('Ticket', 'tagfield', 'tag1', 'First');
|
||||
$this->CreateTagData('Ticket', 'tagfield', 'tag2', 'Second');
|
||||
$oTagSet2 = new ormTagSet(TAG_CLASS, TAG_ATTCODE);
|
||||
$oTagSet2->SetValue(array('tag1'));
|
||||
|
||||
$oTagSet1 = new ormTagSet('Ticket', 'tagfield');
|
||||
$oTagSet1->AddTag('tag1');
|
||||
static::assertTrue($oTagSet1->Equals($oTagSet1));
|
||||
static::assertTrue($oTagSet1->Equals($oTagSet2));
|
||||
|
||||
$oTagSet2 = new ormTagSet('Ticket', 'tagfield');
|
||||
$oTagSet2->SetValue(array('tag1'));
|
||||
$oTagSet1->AddTag('tag2');
|
||||
static::assertFalse($oTagSet1->Equals($oTagSet2));
|
||||
}
|
||||
|
||||
static::assertTrue($oTagSet1->Equals($oTagSet2));
|
||||
public function testSetValue()
|
||||
{
|
||||
$oTagSet = new ormTagSet(TAG_CLASS, TAG_ATTCODE);
|
||||
|
||||
$oTagSet1->AddTag('tag2');
|
||||
static::assertFalse($oTagSet1->Equals($oTagSet2));
|
||||
}
|
||||
$oTagSet->SetValue(array('tag1'));
|
||||
static::assertEquals($oTagSet->GetValue(), array('tag1'));
|
||||
|
||||
public function testSetValue()
|
||||
{
|
||||
$this->CreateTagData('Ticket', 'tagfield', 'tag1', 'First');
|
||||
$this->CreateTagData('Ticket', 'tagfield', 'tag2', 'Second');
|
||||
$oTagSet->SetValue(array('tag1', 'tag2'));
|
||||
static::assertEquals($oTagSet->GetValue(), array('tag1', 'tag2'));
|
||||
|
||||
$oTagSet = new ormTagSet('Ticket', 'tagfield');
|
||||
}
|
||||
|
||||
$oTagSet->SetValue(array('tag1'));
|
||||
static::assertEquals($oTagSet->GetValue(), array('tag1'));
|
||||
public function testRemoveTag()
|
||||
{
|
||||
$oTagSet = new ormTagSet(TAG_CLASS, TAG_ATTCODE);
|
||||
$oTagSet->RemoveTag('tag_unknown');
|
||||
static::assertEquals($oTagSet->GetValue(), array());
|
||||
|
||||
$oTagSet->SetValue(array('tag1', 'tag2'));
|
||||
static::assertEquals($oTagSet->GetValue(), array('tag1', 'tag2'));
|
||||
$oTagSet->SetValue(array('tag1'));
|
||||
$oTagSet->RemoveTag('tag_unknown');
|
||||
static::assertEquals($oTagSet->GetValue(), array('tag1'));
|
||||
|
||||
}
|
||||
$oTagSet->SetValue(array('tag1', 'tag2'));
|
||||
$oTagSet->RemoveTag('tag1');
|
||||
static::assertEquals($oTagSet->GetValue(), array('tag2'));
|
||||
|
||||
public function testRemoveTag()
|
||||
{
|
||||
$this->CreateTagData('Ticket', 'tagfield', 'tag1', 'First');
|
||||
$this->CreateTagData('Ticket', 'tagfield', 'tag2', 'Second');
|
||||
$oTagSet->AddTag('tag1');
|
||||
static::assertEquals($oTagSet->GetValue(), array('tag1', 'tag2'));
|
||||
|
||||
$oTagSet = new ormTagSet('Ticket', 'tagfield');
|
||||
$oTagSet->RemoveTag('tag_unknown');
|
||||
static::assertEquals($oTagSet->GetValue(), array());
|
||||
$oTagSet->RemoveTag('tag1');
|
||||
static::assertEquals($oTagSet->GetValue(), array('tag2'));
|
||||
|
||||
$oTagSet->SetValue(array('tag1'));
|
||||
$oTagSet->RemoveTag('tag_unknown');
|
||||
static::assertEquals($oTagSet->GetValue(), array('tag1'));
|
||||
$oTagSet->RemoveTag('tag1');
|
||||
static::assertEquals($oTagSet->GetValue(), array('tag2'));
|
||||
|
||||
$oTagSet->SetValue(array('tag1', 'tag2'));
|
||||
$oTagSet->RemoveTag('tag1');
|
||||
static::assertEquals($oTagSet->GetValue(), array('tag2'));
|
||||
$oTagSet->RemoveTag('tag2');
|
||||
static::assertEquals($oTagSet->GetValue(), array());
|
||||
}
|
||||
|
||||
$oTagSet->AddTag('tag1');
|
||||
static::assertEquals($oTagSet->GetValue(), array('tag1', 'tag2'));
|
||||
public function testGetDelta()
|
||||
{
|
||||
$oTagSet1 = new ormTagSet(TAG_CLASS, TAG_ATTCODE);
|
||||
$oTagSet1->SetValue(array('tag1', 'tag2'));
|
||||
|
||||
$oTagSet->RemoveTag('tag1');
|
||||
static::assertEquals($oTagSet->GetValue(), array('tag2'));
|
||||
|
||||
$oTagSet->RemoveTag('tag1');
|
||||
static::assertEquals($oTagSet->GetValue(), array('tag2'));
|
||||
|
||||
$oTagSet->RemoveTag('tag2');
|
||||
static::assertEquals($oTagSet->GetValue(), array());
|
||||
}
|
||||
|
||||
public function testGetDelta()
|
||||
{
|
||||
$this->CreateTagData('Ticket', 'tagfield', 'tag1', 'First');
|
||||
$this->CreateTagData('Ticket', 'tagfield', 'tag2', 'Second');
|
||||
$this->CreateTagData('Ticket', 'tagfield', 'tag3', 'Third');
|
||||
$this->CreateTagData('Ticket', 'tagfield', 'tag4', 'Fourth');
|
||||
|
||||
$oTagSet1 = new ormTagSet('Ticket', 'tagfield');
|
||||
$oTagSet1->SetValue(array('tag1', 'tag2'));
|
||||
|
||||
$oTagSet2 = new ormTagSet('Ticket', 'tagfield');
|
||||
$oTagSet2->SetValue(array('tag1', 'tag3', 'tag4'));
|
||||
$oTagSet2 = new ormTagSet(TAG_CLASS, TAG_ATTCODE);
|
||||
$oTagSet2->SetValue(array('tag1', 'tag3', 'tag4'));
|
||||
|
||||
$aDelta = $oTagSet1->GetDelta($oTagSet2);
|
||||
static::assertCount(2, $aDelta);
|
||||
static::assertCount(2, $aDelta['added']);
|
||||
static::assertCount(1, $aDelta['removed']);
|
||||
}
|
||||
static::assertCount(2, $aDelta);
|
||||
static::assertCount(2, $aDelta['added']);
|
||||
static::assertCount(1, $aDelta['removed']);
|
||||
}
|
||||
|
||||
public function testApplyDelta()
|
||||
{
|
||||
$this->CreateTagData('Ticket', 'tagfield', 'tag1', 'First');
|
||||
$this->CreateTagData('Ticket', 'tagfield', 'tag2', 'Second');
|
||||
$this->CreateTagData('Ticket', 'tagfield', 'tag3', 'Third');
|
||||
$this->CreateTagData('Ticket', 'tagfield', 'tag4', 'Fourth');
|
||||
public function testApplyDelta()
|
||||
{
|
||||
$oTagSet1 = new ormTagSet(TAG_CLASS, TAG_ATTCODE);
|
||||
$oTagSet1->SetValue(array('tag1', 'tag2'));
|
||||
|
||||
$oTagSet1 = new ormTagSet('Ticket', 'tagfield');
|
||||
$oTagSet1->SetValue(array('tag1', 'tag2'));
|
||||
$oTagSet2 = new ormTagSet(TAG_CLASS, TAG_ATTCODE);
|
||||
$oTagSet2->SetValue(array('tag1', 'tag3', 'tag4'));
|
||||
|
||||
$oTagSet2 = new ormTagSet('Ticket', 'tagfield');
|
||||
$oTagSet2->SetValue(array('tag1', 'tag3', 'tag4'));
|
||||
$aDelta = $oTagSet1->GetDelta($oTagSet2);
|
||||
|
||||
$aDelta = $oTagSet1->GetDelta($oTagSet2);
|
||||
$oTagSet1->ApplyDelta($aDelta);
|
||||
|
||||
$oTagSet1->ApplyDelta($aDelta);
|
||||
static::assertTrue($oTagSet1->Equals($oTagSet2));
|
||||
}
|
||||
|
||||
static::assertTrue($oTagSet1->Equals($oTagSet2));
|
||||
}
|
||||
/**
|
||||
* @param $aInitialTags
|
||||
* @param $aDiffTags
|
||||
*
|
||||
* @throws \CoreException
|
||||
* @throws \CoreUnexpectedValue
|
||||
*
|
||||
* @dataProvider GetModifiedProvider
|
||||
*/
|
||||
public function testGetModified($aInitialTags, $aDiffAndExpectedTags)
|
||||
{
|
||||
$oTagSet1 = new ormTagSet(TAG_CLASS, TAG_ATTCODE);
|
||||
$oTagSet1->SetValue($aInitialTags);
|
||||
|
||||
foreach($aDiffAndExpectedTags as $aTestItem)
|
||||
{
|
||||
$oTagSet1->GenerateDiffFromTags($aTestItem['diff']);
|
||||
static::assertEquals($aTestItem['modified'], $oTagSet1->GetModifiedTags());
|
||||
}
|
||||
}
|
||||
|
||||
public function GetModifiedProvider()
|
||||
{
|
||||
return array(
|
||||
array(
|
||||
array('tag2'),
|
||||
array(
|
||||
array('diff' => array('tag1', 'tag2'), 'modified' => array('tag1')),
|
||||
array('diff' => array('tag2'), 'modified' => array('tag1')),
|
||||
array('diff' => array(), 'modified' => array('tag1', 'tag2')),
|
||||
)
|
||||
),
|
||||
array(
|
||||
array('tag1', 'tag2'),
|
||||
array(
|
||||
array('diff' => array('tag1', 'tag3'), 'modified' => array('tag2', 'tag3')),
|
||||
array('diff' => array('tag1', 'tag2'), 'modified' => array('tag2', 'tag3')),
|
||||
array('diff' => array('tag1', 'tag2', 'tag3', 'tag4'), 'modified' => array('tag2', 'tag3', 'tag4')),
|
||||
)
|
||||
),
|
||||
array(
|
||||
array(),
|
||||
array(
|
||||
array('diff' => array('tag2'), 'modified' => array('tag2')),
|
||||
array('diff' => array('tag1', 'tag2'), 'modified' => array('tag1', 'tag2')),
|
||||
array('diff' => array('tag2'), 'modified' => array('tag1', 'tag2')),
|
||||
)
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $aInitialTags
|
||||
* @param $aDelta
|
||||
* @param $aExpectedTags
|
||||
*
|
||||
* @throws \CoreException
|
||||
* @throws \CoreUnexpectedValue
|
||||
* @throws \Exception
|
||||
* @dataProvider BulkModifyProvider
|
||||
*/
|
||||
public function testBulkModify($aInitialTags, $aDelta, $aExpectedTags)
|
||||
{
|
||||
$oTagSet1 = new ormTagSet(TAG_CLASS, TAG_ATTCODE);
|
||||
$oTagSet1->SetValue($aInitialTags);
|
||||
|
||||
$oTagSet1->ApplyDelta($aDelta);
|
||||
|
||||
static::assertEquals($aExpectedTags, $oTagSet1->GetValue());
|
||||
}
|
||||
|
||||
public function BulkModifyProvider()
|
||||
{
|
||||
return array(
|
||||
'Add one tag' => array(
|
||||
array('tag1', 'tag2'),
|
||||
array('added' => array('tag3')),
|
||||
array('tag1', 'tag2', 'tag3')
|
||||
),
|
||||
'Remove one tag' => array(
|
||||
array('tag1', 'tag2'),
|
||||
array('removed' => array('tag2')),
|
||||
array('tag1')
|
||||
),
|
||||
'Remove unexisting tag' => array(
|
||||
array('tag1', 'tag2'),
|
||||
array('removed' => array('tag3')),
|
||||
array('tag1', 'tag2')
|
||||
),
|
||||
'Add one and remove one tag' => array(
|
||||
array('tag1', 'tag2'),
|
||||
array('added' => array('tag3'), 'removed' => array('tag2')),
|
||||
array('tag1', 'tag3')
|
||||
),
|
||||
'Remove first tag' => array(
|
||||
array('tag1', 'tag2'),
|
||||
array('removed' => array('tag1')),
|
||||
array('tag2')
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user