Explorar o código

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

git-svn-id: http://svn.code.sf.net/p/itop/code/trunk@1720 a333f486-631f-4898-b8df-5754b55c2be0
dflaven %!s(int64=13) %!d(string=hai) anos
pai
achega
238aef84dc

+ 3 - 1
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
 		{

+ 3 - 3
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
 		}
 	}
 	

+ 9 - 0
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);

+ 26 - 15
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);
+	}
 }
 
 /**

+ 113 - 1
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)