From a1d01e252b4d589cf3e861beb331834e627f216f Mon Sep 17 00:00:00 2001 From: bruno DA SILVA Date: Mon, 20 May 2019 17:00:05 +0200 Subject: [PATCH] =?UTF-8?q?N=C2=B02191=20-=20Stopwatch=20sub-items=20no=20?= =?UTF-8?q?more=20available=20as=20search=20criteria?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/attributedef.class.inc.php | 115 +++++++++++++++++- js/search/search_form_criteria_enum.js | 10 ++ .../search/searchform.class.inc.php | 6 +- 3 files changed, 124 insertions(+), 7 deletions(-) diff --git a/core/attributedef.class.inc.php b/core/attributedef.class.inc.php index f409adfe2..cfa0007bc 100644 --- a/core/attributedef.class.inc.php +++ b/core/attributedef.class.inc.php @@ -7836,6 +7836,87 @@ class AttributeStopWatch extends AttributeDefinition throw new CoreException("Unknown item code '$sItemCode' for attribute ".$this->GetHostClass().'::'.$this->GetCode()); } + + public function GetSubItemSearchType($sItemCode) + { + switch ($sItemCode) + { + case 'timespent': + return static::SEARCH_WIDGET_TYPE_NUMERIC; //seconds + case 'started': + case 'laststart': + case 'stopped': + return static::SEARCH_WIDGET_TYPE_DATE_TIME; //timestamp + } + + foreach($this->ListThresholds() as $iThreshold => $aFoo) + { + $sThPrefix = $iThreshold.'_'; + if (substr($sItemCode, 0, strlen($sThPrefix)) == $sThPrefix) + { + // The current threshold is concerned + $sThresholdCode = substr($sItemCode, strlen($sThPrefix)); + switch ($sThresholdCode) + { + case 'deadline': + return static::SEARCH_WIDGET_TYPE_DATE_TIME; //timestamp + case 'passed': + case 'triggered': + return static::SEARCH_WIDGET_TYPE_ENUM; //booleans, used in conjuction with GetSubItemAllowedValues and IsSubItemNullAllowed + case 'overrun': + return static::SEARCH_WIDGET_TYPE_NUMERIC; //seconds + } + } + } + + return static::SEARCH_WIDGET_TYPE_RAW; + } + + public function GetSubItemAllowedValues($sItemCode, $aArgs = array(), $sContains = '') + { + foreach($this->ListThresholds() as $iThreshold => $aFoo) + { + $sThPrefix = $iThreshold.'_'; + if (substr($sItemCode, 0, strlen($sThPrefix)) == $sThPrefix) + { + // The current threshold is concerned + $sThresholdCode = substr($sItemCode, strlen($sThPrefix)); + switch ($sThresholdCode) + { + case 'passed': + case 'triggered': + return array( + 0 => $this->GetBooleanLabel(0), + 1 => $this->GetBooleanLabel(1), + ); + } + } + } + + return null; + } + + public function IsSubItemNullAllowed($sItemCode, $bDefaultValue) + { + foreach($this->ListThresholds() as $iThreshold => $aFoo) + { + $sThPrefix = $iThreshold.'_'; + if (substr($sItemCode, 0, strlen($sThPrefix)) == $sThPrefix) + { + // The current threshold is concerned + $sThresholdCode = substr($sItemCode, strlen($sThPrefix)); + switch ($sThresholdCode) + { + case 'passed': + case 'triggered': + return false; + } + } + } + + return $bDefaultValue; + } + protected function GetBooleanLabel($bValue) { $sDictKey = $bValue ? 'yes' : 'no'; @@ -8185,9 +8266,39 @@ class AttributeStopWatch extends AttributeDefinition */ class AttributeSubItem extends AttributeDefinition { - const SEARCH_WIDGET_TYPE = self::SEARCH_WIDGET_TYPE_RAW; + /** + * Return the search widget type corresponding to this attribute + * the computation is made by AttributeStopWatch::GetSubItemSearchType + * + * @return string + */ + public function GetSearchType() + { + /** @var AttributeStopWatch $oParent */ + $oParent = $this->GetTargetAttDef(); - static public function ListExpectedParams() + return $oParent->GetSubItemSearchType($this->Get('item_code')); + } + + public function GetAllowedValues($aArgs = array(), $sContains = '') + { + /** @var AttributeStopWatch $oParent */ + $oParent = $this->GetTargetAttDef(); + + return $oParent->GetSubItemAllowedValues($this->Get('item_code'), $aArgs, $sContains); + } + + public function IsNullAllowed() + { + /** @var AttributeStopWatch $oParent */ + $oParent = $this->GetTargetAttDef(); + + $bDefaultValue = parent::IsNullAllowed(); + + return $oParent->IsSubItemNullAllowed($this->Get('item_code'), $bDefaultValue); + } + + static public function ListExpectedParams() { return array_merge(parent::ListExpectedParams(), array('target_attcode', 'item_code')); } diff --git a/js/search/search_form_criteria_enum.js b/js/search/search_form_criteria_enum.js index abec2c50e..86fc48633 100644 --- a/js/search/search_form_criteria_enum.js +++ b/js/search/search_form_criteria_enum.js @@ -42,6 +42,16 @@ $(function() { var me = this; + // when the allowed valued indexes has sequentially & numerical keys, they are passed has an array which break some further code, let's enforce them as an object. + if (Array.isArray(this.options.field.allowed_values.values)) + { + var arrayValues = this.options.field.allowed_values.values; + this.options.field.allowed_values.values = {}; + for (var i = 0; i < arrayValues.length; i++) { + this.options.field.allowed_values.values[i] = arrayValues[i]; + } + } + this._super(); this.element.addClass('search_form_criteria_enum'); }, diff --git a/sources/application/search/searchform.class.inc.php b/sources/application/search/searchform.class.inc.php index 0ae821fa6..2afaaa415 100644 --- a/sources/application/search/searchform.class.inc.php +++ b/sources/application/search/searchform.class.inc.php @@ -387,7 +387,7 @@ class SearchForm foreach($aAttributeDefs as $sAttCode => $oAttDef) { - if ($this->IsSubAttribute($oAttDef)) continue; + if ($oAttDef instanceof AttributeFriendlyName) continue; //it was already forced into $aZList in the code above $bHasIndex = isset($aIndexes[$sAttCode]); $aOthers = $this->AppendField($sClass, $sAlias, $sAttCode, $oAttDef, $aOthers, $bHasIndex); @@ -414,10 +414,6 @@ class SearchForm return $aDBIndexes; } - protected function IsSubAttribute($oAttDef) - { - return (($oAttDef instanceof AttributeFriendlyName) || ($oAttDef instanceof AttributeSubItem)); - } /** * @param \AttributeDefinition $oAttrDef