From 0563b05b0336a346bca0dc94debdcf3a78b5b05b Mon Sep 17 00:00:00 2001 From: Denis Flaven Date: Thu, 15 Dec 2011 10:41:01 +0000 Subject: [PATCH] Bug fix: apply the AllowedValues constraints(as default values) when selecting elements via the "magnifier" button or creating an new element via the "plus" button... also make sure that allowed values is enforced SVN:trunk[1720] --- application/cmdbabstract.class.inc.php | 4 +- application/ui.extkeywidget.class.inc.php | 6 +- core/dbobjectsearch.class.php | 9 ++ core/dbobjectset.class.php | 41 +++++--- core/expression.class.inc.php | 114 +++++++++++++++++++++- 5 files changed, 154 insertions(+), 20 deletions(-) diff --git a/application/cmdbabstract.class.inc.php b/application/cmdbabstract.class.inc.php index 5eee4f917..a4fbeb3f9 100644 --- a/application/cmdbabstract.class.inc.php +++ b/application/cmdbabstract.class.inc.php @@ -883,7 +883,9 @@ EOF $sDisplayList = json_encode($aList); $sCssCount = isset($aExtraParams['cssCount']) ? ", cssCount: '{$aExtraParams['cssCount']}'" : ''; $iPageSize = MetaModel::GetConfig()->GetMinDisplayLimit(); - $oPage->add_ready_script("$('#{$iListId} table.listResults').tablesorter( { $sHeaders widgets: ['myZebra', 'truncatedList']} ).tablesorterPager({container: $('#pager{$iListId}'), totalRows:$iCount, size: $iPageSize, filter: '$sFilter', extra_params: '$sExtraParams', select_mode: '$sSelectMode', displayKey: $sDisplayKey, displayList: $sDisplayList $sCssCount});\n"); + $oSet->ApplyParameters(); + $sOQL = addslashes($oSet->GetFilter()->serialize()); + $oPage->add_ready_script("$('#{$iListId} table.listResults').tablesorter( { $sHeaders widgets: ['myZebra', 'truncatedList']} ).tablesorterPager({container: $('#pager{$iListId}'), totalRows:$iCount, size: $iPageSize, filter: '$sOQL', extra_params: '$sExtraParams', select_mode: '$sSelectMode', displayKey: $sDisplayKey, displayList: $sDisplayList $sCssCount});\n"); } else { diff --git a/application/ui.extkeywidget.class.inc.php b/application/ui.extkeywidget.class.inc.php index 624903882..0a4682876 100644 --- a/application/ui.extkeywidget.class.inc.php +++ b/application/ui.extkeywidget.class.inc.php @@ -319,7 +319,7 @@ EOF try { $oFilter = DBObjectSearch::FromOQL($sFilter); - $oBlock = new DisplayBlock($oFilter, 'list', false); + $oBlock = new DisplayBlock($oFilter, 'list', false, array('query_params' => array('this' => $oObj))); $oBlock->Display($oP, $this->iId.'_results', array('this' => $oObj, 'cssCount'=> '#count_'.$this->iId, 'menu' => false, 'selection_mode' => true, 'selection_type' => 'single')); // Don't display the 'Actions' menu on the results } catch(MissingQueryArgument $e) @@ -328,8 +328,8 @@ EOF // TODO check if we can improve this behavior... $sOQL = 'SELECT '.$sRemoteClass; $oFilter = DBObjectSearch::FromOQL($sOQL); - $oBlock = new DisplayBlock($oFilter, 'list', false); - $oBlock->Display($oP, $this->iId.'_results', array('cssCount'=> '#count_'.$this->iId, 'menu' => false, 'selection_mode' => true, 'selection_type' => 'single')); // Don't display the 'Actions' menu on the results + //$oBlock = new DisplayBlock($oFilter, 'list', false); + //$oBlock->Display($oP, $this->iId.'_results', array('cssCount'=> '#count_'.$this->iId, 'menu' => false, 'selection_mode' => true, 'selection_type' => 'single')); // Don't display the 'Actions' menu on the results } } diff --git a/core/dbobjectsearch.class.php b/core/dbobjectsearch.class.php index 35a19c00d..28a8872ab 100644 --- a/core/dbobjectsearch.class.php +++ b/core/dbobjectsearch.class.php @@ -765,6 +765,15 @@ class DBObjectSearch return $this->m_oSearchCondition->Render($this->m_aParams, false); } + /** + * Turn the parameters (:xxx) into scalar values in order to easily + * serialize a search + */ + public function ApplyParameters($aArgs) + { + return $this->m_oSearchCondition->ApplyParameters(array_merge($this->m_aParams, $aArgs)); + } + public function serialize($bDevelopParams = false, $aContextParams = null) { $sOql = $this->ToOql($bDevelopParams, $aContextParams); diff --git a/core/dbobjectset.class.php b/core/dbobjectset.class.php index 43b3c1e6d..8953cfa56 100644 --- a/core/dbobjectset.class.php +++ b/core/dbobjectset.class.php @@ -694,22 +694,9 @@ class DBObjectSet */ public function ListConstantFields() { - $aScalarArgs = array(); - foreach($this->m_aArgs as $sArgName => $value) - { - if (MetaModel::IsValidObject($value)) - { - $aScalarArgs = array_merge($aScalarArgs, $value->ToArgs($sArgName)); - } - else - { - $aScalarArgs[$sArgName] = (string) $value; - } - } - $aScalarArgs['current_contact_id'] = UserRights::GetContactId(); - + $aScalarArgs = $this->ExpandArgs(); $aConst = $this->m_oFilter->ListConstantFields(); - + foreach($aConst as $sClassAlias => $aVals) { foreach($aVals as $sCode => $oExpr) @@ -726,6 +713,30 @@ class DBObjectSet } return $aConst; } + + protected function ExpandArgs($aArgs) + { + $aScalarArgs = array(); + foreach($this->m_aArgs as $sArgName => $value) + { + if (MetaModel::IsValidObject($value)) + { + $aScalarArgs = array_merge($aScalarArgs, $value->ToArgs($sArgName)); + } + else + { + $aScalarArgs[$sArgName] = (string) $value; + } + } + $aScalarArgs['current_contact_id'] = UserRights::GetContactId(); + return $aScalarArgs; + } + + public function ApplyParameters() + { + $aScalarArgs = $this->ExpandArgs(); + $this->m_oFilter->ApplyParameters($aScalarArgs); + } } /** diff --git a/core/expression.class.inc.php b/core/expression.class.inc.php index ee07e2565..ad2590ca9 100644 --- a/core/expression.class.inc.php +++ b/core/expression.class.inc.php @@ -36,6 +36,8 @@ abstract class Expression // recursive rendering (aArgs used as input by default, or used as output if bRetrofitParams set to True abstract public function Render(&$aArgs = null, $bRetrofitParams = false); + abstract public function ApplyParameters($aArgs); + // recursively builds an array of class => fieldname abstract public function ListRequiredFields(); @@ -111,6 +113,10 @@ class SQLExpression extends Expression return $this->m_sSQL; } + public function ApplyParameters($aArgs) + { + } + public function GetUnresolvedFields($sAlias, &$aUnresolved) { } @@ -200,7 +206,27 @@ class BinaryExpression extends Expression $sRight = $this->GetRightExpr()->Render($aArgs, $bRetrofitParams); return "($sLeft $sOperator $sRight)"; } - + + public function ApplyParameters($aArgs) + { + if ($this->m_oLeftExpr instanceof VariableExpression) + { + $this->m_oLeftExpr = $this->m_oLeftExpr->GetAsScalar($aArgs); + } + else //if ($this->m_oLeftExpr instanceof Expression) + { + $this->m_oLeftExpr->ApplyParameters($aArgs); + } + if ($this->m_oRightExpr instanceof VariableExpression) + { + $this->m_oRightExpr = $this->m_oRightExpr->GetAsScalar($aArgs); + } + else //if ($this->m_oRightExpr instanceof Expression) + { + $this->m_oRightExpr->ApplyParameters($aArgs); + } + } + public function GetUnresolvedFields($sAlias, &$aUnresolved) { $this->GetLeftExpr()->GetUnresolvedFields($sAlias, $aUnresolved); @@ -247,6 +273,14 @@ class BinaryExpression extends Expression { $aResult[$this->m_oRightExpr->GetParent()][$this->m_oRightExpr->GetName()] = $this->m_oLeftExpr; } + else + { + $aResult = array_merge($this->m_oRightExpr->ListConstantFields(), $this->m_oLeftExpr->ListConstantFields()) ; + } + } + else + { + $aResult = array_merge($this->m_oRightExpr->ListConstantFields(), $this->m_oLeftExpr->ListConstantFields()) ; } return $aResult; } @@ -285,6 +319,10 @@ class UnaryExpression extends Expression return CMDBSource::Quote($this->m_value); } + public function ApplyParameters($aArgs) + { + } + public function GetUnresolvedFields($sAlias, &$aUnresolved) { } @@ -490,6 +528,20 @@ class VariableExpression extends UnaryExpression $this->m_sName = $sNewName; } } + + public function GetAsScalar($aArgs) + { + $value = ''; + if (array_key_exists($this->m_sName, $aArgs)) + { + $value = $aArgs[$this->m_sName]; + } + else + { + throw new MissingQueryArgument('Missing query argument', array('expecting'=>$this->m_sName, 'available'=>array_keys($aArgs))); + } + return new ScalarExpression($value); + } } // Temporary, until we implement functions and expression casting! @@ -535,6 +587,22 @@ class ListExpression extends Expression return '('.implode(', ', $aRes).')'; } + public function ApplyParameters($aArgs) + { + $aRes = array(); + foreach ($this->m_aExpressions as $idx => $oExpr) + { + if ($oExpr instanceof VariableExpression) + { + $this->m_aExpressions[$idx] = $oExpr->GetAsScalar(); + } + else + { + $oExpr->ApplyParameters($aArgs); + } + } + } + public function GetUnresolvedFields($sAlias, &$aUnresolved) { foreach ($this->m_aExpressions as $oExpr) @@ -622,6 +690,22 @@ class FunctionExpression extends Expression return $this->m_sVerb.'('.implode(', ', $aRes).')'; } + public function ApplyParameters($aArgs) + { + $aRes = array(); + foreach ($this->m_aArgs as $idx => $oExpr) + { + if ($oExpr instanceof VariableExpression) + { + $this->m_aArgs[$idx] = $oExpr->GetAsScalar($aArgs); + } + else + { + $oExpr->ApplyParameters($aArgs); + } + } + } + public function GetUnresolvedFields($sAlias, &$aUnresolved) { foreach ($this->m_aArgs as $oExpr) @@ -702,6 +786,18 @@ class IntervalExpression extends Expression return 'INTERVAL '.$this->m_oValue->Render($aArgs, $bRetrofitParams).' '.$this->m_sUnit; } + public function ApplyParameters($aArgs) + { + if ($this->m_oValue instanceof VariableExpression) + { + $this->m_oValue = $this->m_oValue->GetAsScalar($aArgs); + } + else + { + $this->m_oValue->ApplyParameters($aArgs); + } + } + public function GetUnresolvedFields($sAlias, &$aUnresolved) { $this->m_oValue->GetUnresolvedFields($sAlias, $aUnresolved); @@ -761,6 +857,22 @@ class CharConcatExpression extends Expression return "CAST(CONCAT(".implode(', ', $aRes).") AS CHAR)"; } + public function ApplyParameters($aArgs) + { + $aRes = array(); + foreach ($this->m_aExpressions as $idx => $oExpr) + { + if ($oExpr instanceof VariableExpression) + { + $this->m_aExpressions[$idx] = $oExpr->GetAsScalar(); + } + else + { + $this->m_aExpressions->ApplyParameters($aArgs); + } + } + } + public function GetUnresolvedFields($sAlias, &$aUnresolved) { foreach ($this->m_aExpressions as $oExpr)