diff --git a/core/dbobjectsearch.class.php b/core/dbobjectsearch.class.php index f79dec488e..def2722a2d 100644 --- a/core/dbobjectsearch.class.php +++ b/core/dbobjectsearch.class.php @@ -1704,7 +1704,7 @@ class DBObjectSearch extends DBSearch * @return array|mixed|\SQLObjectQuery|null * @throws \CoreException */ - public function GetSQLQueryStructure($aAttToLoad, $bGetCount, $aGroupByExpr = null, $aSelectedClasses = null, $aSelectExpr = null) + public function GetSQLQueryStructure($aAttToLoad, $bGetCount, $aGroupByExpr = null, $aSelectedClasses = null, $aSelectExpr = null, $bSelectOnlyIds = false) { // Hide objects that are not visible to the current user // @@ -1775,6 +1775,8 @@ class DBObjectSearch extends DBSearch $aContextData['aSelectExpr'] = $aSelectExpr; $sRawId .= $bGetCount; $aContextData['bGetCount'] = $bGetCount; + $sRawId .= 'ids:'.($bSelectOnlyIds ? '1' : '0'); + $aContextData['bSelectOnlyIds'] = $bSelectOnlyIds; if (is_array($aSelectedClasses)) { $sRawId .= implode(',', $aSelectedClasses); // Unions may alter the list of selected columns } @@ -1828,7 +1830,7 @@ class DBObjectSearch extends DBSearch if (!isset($oSQLQuery)) { $oKPI = new ExecutionKPI(); $oSQLObjectQueryBuilder = new SQLObjectQueryBuilder($oSearch); - $oSQLQuery = $oSQLObjectQueryBuilder->BuildSQLQueryStruct($aAttToLoad, $bGetCount, $aModifierProperties, $aGroupByExpr, $aSelectedClasses, $aSelectExpr); + $oSQLQuery = $oSQLObjectQueryBuilder->BuildSQLQueryStruct($aAttToLoad, $bGetCount, $aModifierProperties, $aGroupByExpr, $aSelectedClasses, $aSelectExpr, $bSelectOnlyIds); $oKPI->ComputeStats('BuildSQLQueryStruct', $sOqlQuery); if (self::$m_bQueryCacheEnabled) { diff --git a/core/dbsearch.class.php b/core/dbsearch.class.php index 08ca8d658f..fc14930a34 100644 --- a/core/dbsearch.class.php +++ b/core/dbsearch.class.php @@ -995,7 +995,7 @@ abstract class DBSearch * @internal * */ - public function MakeSelectQuery($aOrderBy = [], $aArgs = [], $aAttToLoad = null, $aExtendedDataSpec = null, $iLimitCount = 0, $iLimitStart = 0, $bGetCount = false, $bBeautifulSQL = true) + public function MakeSelectQuery($aOrderBy = [], $aArgs = [], $aAttToLoad = null, $aExtendedDataSpec = null, $iLimitCount = 0, $iLimitStart = 0, $bGetCount = false, $bBeautifulSQL = true, $bSelectOnlyIds = false) { // Check the order by specification, and prefix with the class alias // and make sure that the ordering columns are going to be selected @@ -1036,7 +1036,7 @@ abstract class DBSearch } } - $oSQLQuery = $this->GetSQLQuery($aOrderBy, $aArgs, $aAttToLoad, $aExtendedDataSpec, $iLimitCount, $iLimitStart, $bGetCount); + $oSQLQuery = $this->GetSQLQuery($aOrderBy, $aArgs, $aAttToLoad, $aExtendedDataSpec, $iLimitCount, $iLimitStart, $bGetCount, null, null, $bSelectOnlyIds); if ($this->m_bNoContextParameters) { // Only internal parameters @@ -1120,7 +1120,7 @@ abstract class DBSearch * @internal * */ - protected function GetSQLQuery($aOrderBy, $aArgs, $aAttToLoad, $aExtendedDataSpec, $iLimitCount, $iLimitStart, $bGetCount, $aGroupByExpr = null, $aSelectExpr = null) + protected function GetSQLQuery($aOrderBy, $aArgs, $aAttToLoad, $aExtendedDataSpec, $iLimitCount, $iLimitStart, $bGetCount, $aGroupByExpr = null, $aSelectExpr = null, $bSelectOnlyIds = false) { $oSearch = $this->ApplyDataFilters(); @@ -1142,7 +1142,7 @@ abstract class DBSearch } } - $oSQLQuery = $oSearch->GetSQLQueryStructure($aAttToLoad, $bGetCount, $aGroupByExpr, null, $aSelectExpr); + $oSQLQuery = $oSearch->GetSQLQueryStructure($aAttToLoad, $bGetCount, $aGroupByExpr, null, $aSelectExpr, $bSelectOnlyIds); $oSQLQuery->SetSourceOQL($oSearch->ToOQL()); // Join to an additional table, if required... @@ -1177,7 +1177,8 @@ abstract class DBSearch $bGetCount, $aGroupByExpr = null, $aSelectedClasses = null, - $aSelectExpr = null + $aSelectExpr = null, + $bSelectOnlyIds = false ); /** diff --git a/core/dbunionsearch.class.php b/core/dbunionsearch.class.php index 100fb20a0e..75702fe73f 100644 --- a/core/dbunionsearch.class.php +++ b/core/dbunionsearch.class.php @@ -527,10 +527,10 @@ class DBUnionSearch extends DBSearch throw new Exception('MakeUpdateQuery is not implemented for the unions!'); } - public function GetSQLQueryStructure($aAttToLoad, $bGetCount, $aGroupByExpr = null, $aSelectedClasses = null, $aSelectExpr = null) + public function GetSQLQueryStructure($aAttToLoad, $bGetCount, $aGroupByExpr = null, $aSelectedClasses = null, $aSelectExpr = null, $bSelectOnlyIds = false) { if (count($this->aSearches) == 1) { - return $this->aSearches[0]->GetSQLQueryStructure($aAttToLoad, $bGetCount, $aGroupByExpr, $aSelectedClasses, $aSelectExpr); + return $this->aSearches[0]->GetSQLQueryStructure($aAttToLoad, $bGetCount, $aGroupByExpr, $aSelectedClasses, $aSelectExpr, $bSelectOnlyIds); } $aSQLQueries = []; @@ -610,7 +610,7 @@ class DBUnionSearch extends DBSearch } } } - $oSubQuery = $oSearch->GetSQLQueryStructure($aQueryAttToLoad, false, $aQueryGroupByExpr, $aSearchSelectedClasses, $aQuerySelectExpr); + $oSubQuery = $oSearch->GetSQLQueryStructure($aQueryAttToLoad, false, $aQueryGroupByExpr, $aSearchSelectedClasses, $aQuerySelectExpr, $bSelectOnlyIds); if (count($aSearchAliases) > 1) { // Necessary to make sure that selected columns will match throughout all the queries // (default order of selected fields depending on the order of JOINS) diff --git a/core/oql/expression.class.inc.php b/core/oql/expression.class.inc.php index aa53587fa6..858f7730c1 100644 --- a/core/oql/expression.class.inc.php +++ b/core/oql/expression.class.inc.php @@ -1,14 +1,16 @@ $oExpression) - { + $aRet = []; + foreach ($aExpressions as $sName => $oExpression) { /** @var Expression $oExpression */ $aRet[$sName] = $oExpression->RenderExpression(false, $aArgs); } return $aRet; } - public final static function ConvertArrayFromOQL($aExpressions) + final public static function ConvertArrayFromOQL($aExpressions) { - $aRet = array(); - foreach ($aExpressions as $sName => $sConditionExpr) - { + $aRet = []; + foreach ($aExpressions as $sName => $sConditionExpr) { /** @var Expression $oExpression */ $aRet[$sName] = Expression::FromOQL($sConditionExpr); } return $aRet; } - /** * recursive rendering * @@ -131,22 +131,19 @@ abstract class Expression { */ public function GetParameters($sParentFilter = null) { - $aParameters = array(); + $aParameters = []; $unused = $this->RenderExpression(false, $aParameters, true); - if (!is_null($sParentFilter)) $sParentFilter .= '->'; + if (!is_null($sParentFilter)) { + $sParentFilter .= '->'; + } - $aRet = array(); - foreach($aParameters as $sParameter => $unused) - { - if (is_null($sParentFilter)) - { + $aRet = []; + foreach ($aParameters as $sParameter => $unused) { + if (is_null($sParentFilter)) { $aRet[] = $sParameter; - } - else - { - if (substr($sParameter, 0, strlen($sParentFilter)) == $sParentFilter) - { + } else { + if (substr($sParameter, 0, strlen($sParentFilter)) == $sParentFilter) { $aRet[] = substr($sParameter, strlen($sParentFilter)); } } @@ -182,12 +179,12 @@ abstract class Expression { * @return array parameters for the search form * @throws \MissingQueryArgument */ - public function Display($oSearch, &$aArgs = null, $oAttDef = null, &$aCtx = array()) + public function Display($oSearch, &$aArgs = null, $oAttDef = null, &$aCtx = []) { return $this->RenderExpression(false, $aArgs); } - public function GetAttDef($aClasses = array()) + public function GetAttDef($aClasses = []) { return null; } @@ -222,10 +219,13 @@ abstract class Expression { // recursively builds an array of parameters to give to current request abstract public function ListParameters(); - public function RequiresField($sClass, $sFieldName) { + public function RequiresField($sClass, $sFieldName) + { // #@# todo - optimize : this is called quite often when building a single query ! $aRequired = $this->ListRequiredFields(); - if (!in_array($sClass.'.'.$sFieldName, $aRequired)) return false; + if (!in_array($sClass.'.'.$sFieldName, $aRequired)) { + return false; + } return true; } @@ -244,7 +244,7 @@ abstract class Expression { * @return Expression * @throws OQLException */ - static public function unserialize($sValue) + public static function unserialize($sValue) { return self::FromOQL(base64_decode($sValue)); } @@ -255,11 +255,10 @@ abstract class Expression { * @return Expression * @throws \OQLException */ - static public function FromOQL($sConditionExpr) + public static function FromOQL($sConditionExpr) { - static $aCache = array(); - if (array_key_exists($sConditionExpr, $aCache)) - { + static $aCache = []; + if (array_key_exists($sConditionExpr, $aCache)) { return unserialize($aCache[$sConditionExpr]); } $oOql = new OqlInterpreter($sConditionExpr); @@ -269,7 +268,7 @@ abstract class Expression { return $oExpression; } - static public function FromSQL($sSQL) + public static function FromSQL($sSQL) { return new SQLExpression($sSQL); } @@ -282,8 +281,12 @@ abstract class Expression { */ public function LogAnd(Expression $oExpr) { - if ($this->IsTrue()) return clone $oExpr; - if ($oExpr->IsTrue()) return clone $this; + if ($this->IsTrue()) { + return clone $oExpr; + } + if ($oExpr->IsTrue()) { + return clone $this; + } return new BinaryExpression($this, 'AND', $oExpr); } @@ -326,12 +329,12 @@ abstract class Expression { */ public function GetCriterion($oSearch, &$aArgs = null, $bRetrofitParams = false, $oAttDef = null) { - return array( + return [ 'widget' => AttributeDefinition::SEARCH_WIDGET_TYPE_RAW, 'oql' => $this->RenderExpression(false, $aArgs, $bRetrofitParams), 'label' => $this->Display($oSearch, $aArgs, $oAttDef), 'source' => get_class($this), - ); + ]; } /** @@ -343,15 +346,12 @@ abstract class Expression { * * @return array of expressions */ - public static function Split($oExpr, $sOperator = 'AND', &$aAndExpr = array()) + public static function Split($oExpr, $sOperator = 'AND', &$aAndExpr = []) { - if (($oExpr instanceof BinaryExpression) && ($oExpr->GetOperator() == $sOperator)) - { + if (($oExpr instanceof BinaryExpression) && ($oExpr->GetOperator() == $sOperator)) { static::Split($oExpr->GetLeftExpr(), $sOperator, $aAndExpr); static::Split($oExpr->GetRightExpr(), $sOperator, $aAndExpr); - } - else - { + } else { $aAndExpr[] = $oExpr; } @@ -415,7 +415,7 @@ class SQLExpression extends Expression public function ListRequiredFields() { - return array(); + return []; } public function CollectUsedParents(&$aTable) @@ -424,12 +424,12 @@ class SQLExpression extends Expression public function ListConstantFields() { - return array(); + return []; } public function ListParameters() { - return array(); + return []; } public function RenameParam($sOldName, $sNewName) @@ -443,8 +443,6 @@ class SQLExpression extends Expression } } - - class BinaryExpression extends Expression { protected $m_oLeftExpr; // filter code or an SQL expression (later?) @@ -476,39 +474,37 @@ class BinaryExpression extends Expression */ protected function ValidateConstructorParams($oLeftExpr, $sOperator, $oRightExpr) { - if (!is_object($oLeftExpr)) - { - throw new CoreException('Expecting an Expression object on the left hand', array('found_type' => gettype($oLeftExpr))); + if (!is_object($oLeftExpr)) { + throw new CoreException('Expecting an Expression object on the left hand', ['found_type' => gettype($oLeftExpr)]); } - if (!is_object($oRightExpr)) - { - throw new CoreException('Expecting an Expression object on the right hand', array('found_type' => gettype($oRightExpr))); + if (!is_object($oRightExpr)) { + throw new CoreException('Expecting an Expression object on the right hand', ['found_type' => gettype($oRightExpr)]); } - if (!$oLeftExpr instanceof Expression) - { - throw new CoreException('Expecting an Expression object on the left hand', array('found_class' => get_class($oLeftExpr))); + if (!$oLeftExpr instanceof Expression) { + throw new CoreException('Expecting an Expression object on the left hand', ['found_class' => get_class($oLeftExpr)]); } - if (!$oRightExpr instanceof Expression) - { - throw new CoreException('Expecting an Expression object on the right hand', array('found_class' => get_class($oRightExpr))); + if (!$oRightExpr instanceof Expression) { + throw new CoreException('Expecting an Expression object on the right hand', ['found_class' => get_class($oRightExpr)]); } - if ((($sOperator == "IN") || ($sOperator == "NOT IN")) && !($oRightExpr instanceof ListExpression || $oRightExpr instanceof NestedQueryExpression)) - { - throw new CoreException("Expecting a List Expression object on the right hand for operator $sOperator", - array('found_class' => get_class($oRightExpr))); + if ((($sOperator == "IN") || ($sOperator == "NOT IN")) && !($oRightExpr instanceof ListExpression || $oRightExpr instanceof NestedQueryExpression)) { + throw new CoreException( + "Expecting a List Expression object on the right hand for operator $sOperator", + ['found_class' => get_class($oRightExpr)] + ); } } public function IsTrue() { // return true if we are certain that it will be true - if ($this->m_sOperator == 'AND') - { - if ($this->m_oLeftExpr->IsTrue() && $this->m_oRightExpr->IsTrue()) return true; - } - elseif ($this->m_sOperator == 'OR') - { - if ($this->m_oLeftExpr->IsTrue() || $this->m_oRightExpr->IsTrue()) return true; + if ($this->m_sOperator == 'AND') { + if ($this->m_oLeftExpr->IsTrue() && $this->m_oRightExpr->IsTrue()) { + return true; + } + } elseif ($this->m_sOperator == 'OR') { + if ($this->m_oLeftExpr->IsTrue() || $this->m_oRightExpr->IsTrue()) { + return true; + } } return false; } @@ -549,8 +545,7 @@ class BinaryExpression extends Expression $sOperator = $this->GetOperator(); $sType = null; - switch($sOperator) - { + switch ($sOperator) { case '+': case '-': case '*': @@ -587,12 +582,11 @@ class BinaryExpression extends Expression default: throw new Exception("Operator '$sOperator' not yet supported"); } - switch ($sType){ + switch ($sType) { case 'logical': $bLeft = static::CastToBool($mLeft); $bRight = static::CastToBool($mRight); - switch ($sOperator) - { + switch ($sOperator) { case 'OR': $result = (int)($bLeft || $bRight); break; @@ -607,12 +601,15 @@ class BinaryExpression extends Expression case 'maths': $iLeft = (int) $mLeft; $iRight = (int) $mRight; - switch ($sOperator) - { - case '+' : $result = $iLeft + $iRight; break; - case '-' : $result = $iLeft - $iRight; break; - case '*' : $result = $iLeft * $iRight; break; - case '/' : $result = $iLeft / $iRight; break; + switch ($sOperator) { + case '+' : $result = $iLeft + $iRight; + break; + case '-' : $result = $iLeft - $iRight; + break; + case '*' : $result = $iLeft * $iRight; + break; + case '/' : $result = $iLeft / $iRight; + break; default: throw new Exception("Logic: unknown operator '$sOperator'"); } @@ -620,11 +617,13 @@ class BinaryExpression extends Expression case 'comp': $left = $mLeft; $right = $mRight; - switch ($sOperator) - { - case '=' : $result = ($left == $right); break; - case '!=' : $result = ($left != $right); break; - case '<>' : $result = ($left != $right); break; + switch ($sOperator) { + case '=' : $result = ($left == $right); + break; + case '!=' : $result = ($left != $right); + break; + case '<>' : $result = ($left != $right); + break; default: throw new Exception("Logic: unknown operator '$sOperator'"); } @@ -632,22 +631,28 @@ class BinaryExpression extends Expression case 'numcomp': $iLeft = static::ComparableValue($mLeft); $iRight = static::ComparableValue($mRight); - switch ($sOperator) - { - case '=' : $result = ($iLeft == $iRight); break; - case '>' : $result = ($iLeft > $iRight); break; - case '<' : $result = ($iLeft < $iRight); break; - case '>=' : $result = ($iLeft >= $iRight); break; - case '<=' : $result = ($iLeft <= $iRight); break; - case '!=' : $result = ($iLeft != $iRight); break; - case '<>' : $result = ($iLeft != $iRight); break; + switch ($sOperator) { + case '=' : $result = ($iLeft == $iRight); + break; + case '>' : $result = ($iLeft > $iRight); + break; + case '<' : $result = ($iLeft < $iRight); + break; + case '>=' : $result = ($iLeft >= $iRight); + break; + case '<=' : $result = ($iLeft <= $iRight); + break; + case '!=' : $result = ($iLeft != $iRight); + break; + case '<>' : $result = ($iLeft != $iRight); + break; default: throw new Exception("Logic: unknown operator '$sOperator'"); } break; case 'like': $sEscaped = preg_quote($mRight, '/'); - $sEscaped = str_replace(array('%', '_', '\\\\.*', '\\\\.'), array('.*', '.', '%', '_'), $sEscaped); + $sEscaped = str_replace(['%', '_', '\\\\.*', '\\\\.'], ['.*', '.', '%', '_'], $sEscaped); $pregRes = preg_match("/$sEscaped/i", $mLeft); if ($pregRes === false) { throw new Exception("Error in regular expression '$sEscaped'"); @@ -656,7 +661,7 @@ class BinaryExpression extends Expression break; case 'notlike': $sEscaped = preg_quote($mRight, '/'); - $sEscaped = str_replace(array('%', '_', '\\\\.*', '\\\\.'), array('.*', '.', '%', '_'), $sEscaped); + $sEscaped = str_replace(['%', '_', '\\\\.*', '\\\\.'], ['.*', '.', '%', '_'], $sEscaped); $pregRes = preg_match("/$sEscaped/i", $mLeft); if ($pregRes === false) { throw new Exception("Error in regular expression '$sEscaped'"); @@ -673,34 +678,26 @@ class BinaryExpression extends Expression return $result; } - static protected function CastToBool($mValue) + protected static function CastToBool($mValue) { - if (is_string($mValue)) - { - if (is_numeric($mValue)) - { + if (is_string($mValue)) { + if (is_numeric($mValue)) { return abs($mValue) > 0; } return false; } return (bool)$mValue; } - static protected function ComparableValue($mixed) + protected static function ComparableValue($mixed) { - if (is_string($mixed)) - { + if (is_string($mixed)) { $oDate = new \DateTime($mixed); - if (($oDate->format('Y-m-d') == $mixed) || ($oDate->format('Y-m-d H:i:s') == $mixed)) - { + if (($oDate->format('Y-m-d') == $mixed) || ($oDate->format('Y-m-d H:i:s') == $mixed)) { $iRet = $oDate->format('U'); - } - else - { + } else { $iRet = (int) $mixed; } - } - else - { + } else { $iRet = $mixed; } @@ -714,23 +711,22 @@ class BinaryExpression extends Expression */ public function ToJSON(&$aArgs = null, $bRetrofitParams = false) { - if (($this->GetOperator() == 'AND') || ($this->GetOperator() == 'OR')) - { - return array( + if (($this->GetOperator() == 'AND') || ($this->GetOperator() == 'OR')) { + return [ 'type' => static::OPERATOR_BOOLEAN, 'operator' => $this->GetOperator(), 'left' => $this->GetLeftExpr()->ToJSON($aArgs, $bRetrofitParams), 'right' => $this->GetRightExpr()->ToJSON($aArgs, $bRetrofitParams), 'oql' => $this->RenderExpression(false, $aArgs, $bRetrofitParams), - ); + ]; } - return array( + return [ 'type' => static::OPERATOR_BINARY, 'operator' => $this->GetOperator(), 'left' => $this->GetLeftExpr()->ToJSON($aArgs, $bRetrofitParams), 'right' => $this->GetRightExpr()->ToJSON($aArgs, $bRetrofitParams), 'oql' => $this->RenderExpression(false, $aArgs, $bRetrofitParams), - ); + ]; } public function Browse(Closure $callback) @@ -748,20 +744,14 @@ class BinaryExpression extends Expression */ public function ApplyParameters($aArgs) { - if ($this->m_oLeftExpr instanceof VariableExpression) - { + if ($this->m_oLeftExpr instanceof VariableExpression) { $this->m_oLeftExpr = $this->m_oLeftExpr->GetAsScalar($aArgs); - } - else //if ($this->m_oLeftExpr instanceof Expression) - { + } else { //if ($this->m_oLeftExpr instanceof Expression) $this->m_oLeftExpr->ApplyParameters($aArgs); } - if ($this->m_oRightExpr instanceof VariableExpression) - { + if ($this->m_oRightExpr instanceof VariableExpression) { $this->m_oRightExpr = $this->m_oRightExpr->GetAsScalar($aArgs); - } - else //if ($this->m_oRightExpr instanceof Expression) - { + } else { //if ($this->m_oRightExpr instanceof Expression) $this->m_oRightExpr->ApplyParameters($aArgs); } } @@ -786,7 +776,7 @@ class BinaryExpression extends Expression $oRight = $this->GetRightExpr()->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved); return new BinaryExpression($oLeft, $this->GetOperator(), $oRight); } - + public function ListRequiredFields() { $aLeft = $this->GetLeftExpr()->ListRequiredFields(); @@ -800,10 +790,12 @@ class BinaryExpression extends Expression $this->GetRightExpr()->CollectUsedParents($aTable); } - public function GetAttDef($aClasses = array()) + public function GetAttDef($aClasses = []) { $oAttDef = $this->GetLeftExpr()->GetAttDef($aClasses); - if (!is_null($oAttDef)) return $oAttDef; + if (!is_null($oAttDef)) { + return $oAttDef; + } return $this->GetRightExpr()->GetAttDef($aClasses); } @@ -814,28 +806,18 @@ class BinaryExpression extends Expression */ public function ListConstantFields() { - $aResult = array(); - if ($this->m_sOperator == '=') - { - if (($this->m_oLeftExpr instanceof FieldExpression) && ($this->m_oRightExpr instanceof ScalarExpression)) - { + $aResult = []; + if ($this->m_sOperator == '=') { + if (($this->m_oLeftExpr instanceof FieldExpression) && ($this->m_oRightExpr instanceof ScalarExpression)) { $aResult[$this->m_oLeftExpr->GetParent()][$this->m_oLeftExpr->GetName()] = $this->m_oRightExpr; - } - else if (($this->m_oRightExpr instanceof FieldExpression) && ($this->m_oLeftExpr instanceof ScalarExpression)) - { + } elseif (($this->m_oRightExpr instanceof FieldExpression) && ($this->m_oLeftExpr instanceof ScalarExpression)) { + $aResult[$this->m_oRightExpr->GetParent()][$this->m_oRightExpr->GetName()] = $this->m_oLeftExpr; + } elseif (($this->m_oLeftExpr instanceof FieldExpression) && ($this->m_oRightExpr instanceof VariableExpression)) { + $aResult[$this->m_oLeftExpr->GetParent()][$this->m_oLeftExpr->GetName()] = $this->m_oRightExpr; + } elseif (($this->m_oRightExpr instanceof FieldExpression) && ($this->m_oLeftExpr instanceof VariableExpression)) { $aResult[$this->m_oRightExpr->GetParent()][$this->m_oRightExpr->GetName()] = $this->m_oLeftExpr; } - else if (($this->m_oLeftExpr instanceof FieldExpression) && ($this->m_oRightExpr instanceof VariableExpression)) - { - $aResult[$this->m_oLeftExpr->GetParent()][$this->m_oLeftExpr->GetName()] = $this->m_oRightExpr; - } - else if (($this->m_oRightExpr instanceof FieldExpression) && ($this->m_oLeftExpr instanceof VariableExpression)) - { - $aResult[$this->m_oRightExpr->GetParent()][$this->m_oRightExpr->GetName()] = $this->m_oLeftExpr; - } - } - else if ($this->m_sOperator == 'AND') - { + } elseif ($this->m_sOperator == 'AND') { // Strictly, this should be done only for the AND operator $aResult = array_merge_recursive($this->m_oRightExpr->ListConstantFields(), $this->m_oLeftExpr->ListConstantFields()); } @@ -874,39 +856,31 @@ class BinaryExpression extends Expression * @throws \DictExceptionMissingString * @throws \MissingQueryArgument */ - public function Display($oSearch, &$aArgs = null, $oAttDef = null, &$aCtx = array()) + public function Display($oSearch, &$aArgs = null, $oAttDef = null, &$aCtx = []) { $bReverseOperator = false; - if (method_exists($oSearch, 'GetJoinedClasses')) - { + if (method_exists($oSearch, 'GetJoinedClasses')) { $aClasses = $oSearch->GetJoinedClasses(); - } - else - { - $aClasses = array($oSearch->GetClass()); + } else { + $aClasses = [$oSearch->GetClass()]; } $oLeftExpr = $this->GetLeftExpr(); - if ($oLeftExpr instanceof FieldExpression) - { + if ($oLeftExpr instanceof FieldExpression) { $oAttDef = $oLeftExpr->GetAttDef($aClasses); } $oRightExpr = $this->GetRightExpr(); - if ($oRightExpr instanceof FieldExpression) - { + if ($oRightExpr instanceof FieldExpression) { $oAttDef = $oRightExpr->GetAttDef($aClasses); $bReverseOperator = true; } - - if ($bReverseOperator) - { + if ($bReverseOperator) { $sRight = $oRightExpr->Display($oSearch, $aArgs, $oAttDef, $aCtx); $sLeft = $oLeftExpr->Display($oSearch, $aArgs, $oAttDef, $aCtx); // switch left and right expressions so reverse the operator // Note that the operation is the same so < becomes > and not >= - switch ($this->GetOperator()) - { + switch ($this->GetOperator()) { case '>': $sOperator = '<'; break; @@ -939,8 +913,7 @@ class BinaryExpression extends Expression private function OperatorToNaturalLanguage($sOperator, $oAttDef) { - if ($oAttDef instanceof AttributeDateTime) - { + if ($oAttDef instanceof AttributeDateTime) { return Dict::S('Expression:Operator:Date:'.$sOperator, " $sOperator "); } @@ -964,30 +937,23 @@ class BinaryExpression extends Expression $oLeftExpr = $this->GetLeftExpr(); $oRightExpr = $this->GetRightExpr(); - if (method_exists($oSearch, 'GetJoinedClasses')) - { + if (method_exists($oSearch, 'GetJoinedClasses')) { $aClasses = $oSearch->GetJoinedClasses(); - } - else - { - $aClasses = array($oSearch->GetClass()); + } else { + $aClasses = [$oSearch->GetClass()]; } $oAttDef = $oLeftExpr->GetAttDef($aClasses); - if (is_null($oAttDef)) - { + if (is_null($oAttDef)) { $oAttDef = $oRightExpr->GetAttDef($aClasses); $bReverseOperator = true; } - if (is_null($oAttDef)) - { + if (is_null($oAttDef)) { return parent::GetCriterion($oSearch, $aArgs, $bRetrofitParams, $oAttDef); } - - if ($bReverseOperator) - { + if ($bReverseOperator) { $aCriteriaRight = $oRightExpr->GetCriterion($oSearch, $aArgs, $bRetrofitParams, $oAttDef); // $oAttDef can be different now $oAttDef = $oRightExpr->GetAttDef($aClasses); @@ -995,8 +961,7 @@ class BinaryExpression extends Expression // switch left and right expressions so reverse the operator // Note that the operation is the same so < becomes > and not >= - switch ($this->GetOperator()) - { + switch ($this->GetOperator()) { case '>': $sOperator = '<'; break; @@ -1014,9 +979,7 @@ class BinaryExpression extends Expression break; } $aCriteria = self::MergeCriteria($aCriteriaRight, $aCriteriaLeft, $sOperator); - } - else - { + } else { $aCriteriaLeft = $oLeftExpr->GetCriterion($oSearch, $aArgs, $bRetrofitParams, $oAttDef); // $oAttDef can be different now $oAttDef = $oLeftExpr->GetAttDef($aClasses); @@ -1027,8 +990,7 @@ class BinaryExpression extends Expression $aCriteria['oql'] = $this->RenderExpression(false, $aArgs, $bRetrofitParams); $aCriteria['label'] = $this->Display($oSearch, $aArgs, $oAttDef); - if (isset($aCriteriaLeft['ref']) && isset($aCriteriaRight['ref']) && ($aCriteriaLeft['ref'] != $aCriteriaRight['ref'])) - { + if (isset($aCriteriaLeft['ref']) && isset($aCriteriaRight['ref']) && ($aCriteriaLeft['ref'] != $aCriteriaRight['ref'])) { // Only one Field is supported in the expressions $aCriteria['widget'] = AttributeDefinition::SEARCH_WIDGET_TYPE_RAW; } @@ -1038,25 +1000,20 @@ class BinaryExpression extends Expression protected static function MergeCriteria($aCriteriaLeft, $aCriteriaRight, $sOperator) { - $aCriteriaOverride = array(); + $aCriteriaOverride = []; $aCriteriaOverride['operator'] = $sOperator; - if ($sOperator == 'OR') - { - if (isset($aCriteriaLeft['ref']) && isset($aCriteriaRight['ref']) && ($aCriteriaLeft['ref'] == $aCriteriaRight['ref'])) - { - if (isset($aCriteriaLeft['widget']) && isset($aCriteriaRight['widget']) && ($aCriteriaLeft['widget'] == AttributeDefinition::SEARCH_WIDGET_TYPE_HIERARCHICAL_KEY) && ($aCriteriaRight['widget'] == AttributeDefinition::SEARCH_WIDGET_TYPE_HIERARCHICAL_KEY)) - { + if ($sOperator == 'OR') { + if (isset($aCriteriaLeft['ref']) && isset($aCriteriaRight['ref']) && ($aCriteriaLeft['ref'] == $aCriteriaRight['ref'])) { + if (isset($aCriteriaLeft['widget']) && isset($aCriteriaRight['widget']) && ($aCriteriaLeft['widget'] == AttributeDefinition::SEARCH_WIDGET_TYPE_HIERARCHICAL_KEY) && ($aCriteriaRight['widget'] == AttributeDefinition::SEARCH_WIDGET_TYPE_HIERARCHICAL_KEY)) { $aCriteriaOverride['operator'] = 'IN'; $aCriteriaOverride['is_hierarchical'] = true; - if (isset($aCriteriaLeft['values']) && isset($aCriteriaRight['values'])) - { + if (isset($aCriteriaLeft['values']) && isset($aCriteriaRight['values'])) { $aCriteriaOverride['values'] = array_merge($aCriteriaLeft['values'], $aCriteriaRight['values']); } } } - if (isset($aCriteriaLeft['widget']) && isset($aCriteriaRight['widget']) && ($aCriteriaLeft['widget'] == AttributeDefinition::SEARCH_WIDGET_TYPE_TAG_SET) && ($aCriteriaRight['widget'] == AttributeDefinition::SEARCH_WIDGET_TYPE_TAG_SET)) - { + if (isset($aCriteriaLeft['widget']) && isset($aCriteriaRight['widget']) && ($aCriteriaLeft['widget'] == AttributeDefinition::SEARCH_WIDGET_TYPE_TAG_SET) && ($aCriteriaRight['widget'] == AttributeDefinition::SEARCH_WIDGET_TYPE_TAG_SET)) { $aCriteriaOverride['operator'] = 'MATCHES'; } } @@ -1065,7 +1022,6 @@ class BinaryExpression extends Expression } } - /** * @since 2.6.0 N°931 tag fields */ @@ -1097,12 +1053,9 @@ class MatchExpression extends BinaryExpression $sLeft = $this->GetLeftExpr()->RenderExpression($bForSQL, $aArgs, $bRetrofitParams); $sRight = $this->GetRightExpr()->RenderExpression($bForSQL, $aArgs, $bRetrofitParams); - if ($bForSQL) - { + if ($bForSQL) { $sRet = "MATCH ($sLeft) AGAINST ($sRight IN BOOLEAN MODE)"; - } - else - { + } else { $sRet = "$sLeft MATCHES $sRight"; } @@ -1130,7 +1083,6 @@ class MatchExpression extends BinaryExpression } } - class UnaryExpression extends Expression { protected $m_value; @@ -1174,11 +1126,11 @@ class UnaryExpression extends Expression */ public function ToJSON(&$aArgs = null, $bRetrofitParams = false) { - return array( + return [ 'type' => static::OPERATOR_UNARY, 'value' => $this->m_value, 'oql' => $this->RenderExpression(false, $aArgs, $bRetrofitParams), - ); + ]; } public function Browse(Closure $callback) @@ -1201,7 +1153,7 @@ class UnaryExpression extends Expression public function ListRequiredFields() { - return array(); + return []; } public function CollectUsedParents(&$aTable) @@ -1210,12 +1162,12 @@ class UnaryExpression extends Expression public function ListConstantFields() { - return array(); + return []; } public function ListParameters() { - return array(); + return []; } public function RenameParam($sOldName, $sNewName) @@ -1241,9 +1193,8 @@ class ScalarExpression extends UnaryExpression */ public function __construct($value) { - if (!is_scalar($value) && !is_null($value) && (!$value instanceof OqlHexValue)) - { - throw new CoreException('Attempt to create a scalar expression from a non scalar', array('var_type'=>gettype($value))); + if (!is_scalar($value) && !is_null($value) && (!$value instanceof OqlHexValue)) { + throw new CoreException('Attempt to create a scalar expression from a non scalar', ['var_type' => gettype($value)]); } parent::__construct($value); } @@ -1258,41 +1209,33 @@ class ScalarExpression extends UnaryExpression * @return array|string * @throws \Exception */ - public function Display($oSearch, &$aArgs = null, $oAttDef = null, &$aCtx = array()) + public function Display($oSearch, &$aArgs = null, $oAttDef = null, &$aCtx = []) { - if (!is_null($oAttDef)) - { - if ($oAttDef->IsExternalKey()) - { - try - { + if (!is_null($oAttDef)) { + if ($oAttDef->IsExternalKey()) { + try { /** @var AttributeExternalKey $oAttDef */ $sTarget = $oAttDef->GetTargetClass(); $oObj = MetaModel::GetObject($sTarget, $this->m_value, false); - if (empty($oObj)) - { + if (empty($oObj)) { return Dict::S('Enum:Undefined'); } return $oObj->Get("friendlyname"); - } catch (CoreException $e) - { + } catch (CoreException $e) { } } - if (!($oAttDef instanceof AttributeDateTime)) - { + if (!($oAttDef instanceof AttributeDateTime)) { return $oAttDef->GetAsPlainText($this->m_value); } } - if (strpos($this->m_value, '%') === 0) - { + if (strpos($this->m_value, '%') === 0) { return ''; } - if (isset($aCtx['date_display'])) - { + if (isset($aCtx['date_display'])) { return $aCtx['date_display']->MakeValueLabel($oSearch, $this->m_value, $this->m_value); } @@ -1302,12 +1245,9 @@ class ScalarExpression extends UnaryExpression // recursive rendering public function RenderExpression($bForSQL = false, &$aArgs = null, $bRetrofitParams = false) { - if (is_null($this->m_value)) - { + if (is_null($this->m_value)) { $sRet = 'NULL'; - } - else - { + } else { $sRet = CMDBSource::Quote($this->m_value); } return $sRet; @@ -1329,11 +1269,11 @@ class ScalarExpression extends UnaryExpression */ public function ToJSON(&$aArgs = null, $bRetrofitParams = false) { - return array( + return [ 'type' => static::OPERATOR_SCALAR, 'value' => $this->m_value, 'oql' => $this->RenderExpression(false, $aArgs, $bRetrofitParams), - ); + ]; } /** @@ -1357,9 +1297,8 @@ class ScalarExpression extends UnaryExpression */ public function GetCriterion($oSearch, &$aArgs = null, $bRetrofitParams = false, $oAttDef = null) { - $aCriterion = array(); - switch ((string)($this->m_value)) - { + $aCriterion = []; + switch ((string)($this->m_value)) { case '%Y-%m-%d': $aCriterion['unit'] = 'DAY'; break; @@ -1373,139 +1312,105 @@ class ScalarExpression extends UnaryExpression $aCriterion['unit'] = 'HOUR'; break; default: - $aValue = array(); - if (!is_null($oAttDef)) - { - switch (true) - { + $aValue = []; + if (!is_null($oAttDef)) { + switch (true) { case ($oAttDef instanceof AttributeExternalField): - try - { + try { $oFinalAttDef = $oAttDef->GetFinalAttDef(); - if($oFinalAttDef instanceof AttributeExternalKey) - { - if ($this->GetValue() !== 0) - { + if ($oFinalAttDef instanceof AttributeExternalKey) { + if ($this->GetValue() !== 0) { /** @var AttributeExternalKey $oFinalAttDef */ $sTarget = $oFinalAttDef->GetTargetClass(); $oObj = MetaModel::GetObject($sTarget, $this->GetValue()); $aValue['label'] = $oObj->Get("friendlyname"); $aValue['value'] = $this->GetValue(); - } - else - { + } else { $aValue['label'] = Dict::S('Enum:Undefined'); $aValue['value'] = $this->GetValue(); } - } - else - { + } else { $aValue['label'] = $this->GetValue(); $aValue['value'] = $this->GetValue(); } - $aCriterion['values'] = array($aValue); - } - catch (Exception $e) - { + $aCriterion['values'] = [$aValue]; + } catch (Exception $e) { IssueLog::Error($e->getMessage()); } break; case ($oAttDef instanceof AttributeTagSet): - try - { - if (!empty($this->GetValue())) - { - $aValues = array(); + try { + if (!empty($this->GetValue())) { + $aValues = []; $oValue = $this->GetValue(); - if (is_string($oValue)) - { + if (is_string($oValue)) { $oValue = $oAttDef->GetExistingTagsFromString($oValue, true); } /** @var \ormTagSet $oValue */ $aTags = $oValue->GetTags(); - foreach($aTags as $oTag) - { + foreach ($aTags as $oTag) { $aValue['label'] = $oTag->Get('label'); $aValue['value'] = $oTag->Get('code'); $aValues[] = $aValue; } $aCriterion['values'] = $aValues; - } - else - { + } else { $aCriterion['has_undefined'] = true; } - } catch (Exception $e) - { + } catch (Exception $e) { IssueLog::Error($e->getMessage()); } break; case ($oAttDef instanceof AttributeEnumSet): - try - { - if (!empty($this->GetValue())) - { - $aValues = array(); + try { + if (!empty($this->GetValue())) { + $aValues = []; $sValue = $this->GetValue(); - if (is_string($sValue)) - { + if (is_string($sValue)) { $aTags = $oAttDef->FromStringToArray($sValue, ' '); + } else { + $aTags = []; } - else - { - $aTags = array(); - } - foreach($aTags as $sLabel => $sValue) - { + foreach ($aTags as $sLabel => $sValue) { $aValue['label'] = $sLabel; $aValue['value'] = $sValue; $aValues[] = $aValue; } $aCriterion['values'] = $aValues; - } - else - { + } else { $aCriterion['has_undefined'] = true; } - } catch (Exception $e) - { + } catch (Exception $e) { IssueLog::Error($e->getMessage()); } break; case $oAttDef->IsExternalKey(): - try - { - if ($this->GetValue() != 0) - { + try { + if ($this->GetValue() != 0) { /** @var AttributeExternalKey $oAttDef */ $sTarget = $oAttDef->GetTargetClass(); $oObj = MetaModel::GetObject($sTarget, $this->GetValue(), true, true); $aValue['label'] = $oObj->Get("friendlyname"); $aValue['value'] = $this->GetValue(); - $aCriterion['values'] = array($aValue); - } - else - { + $aCriterion['values'] = [$aValue]; + } else { $aValue['label'] = Dict::S('Enum:Undefined'); $aValue['value'] = $this->GetValue(); - $aCriterion['values'] = array($aValue); + $aCriterion['values'] = [$aValue]; } - } catch (Exception $e) - { + } catch (Exception $e) { // This object cannot be seen... ignore } break; default: - try - { + try { $aValue['label'] = $oAttDef->GetAsPlainText($this->GetValue()); $aValue['value'] = $this->GetValue(); - $aCriterion['values'] = array($aValue); - } catch (Exception $e) - { + $aCriterion['values'] = [$aValue]; + } catch (Exception $e) { $aValue['label'] = $this->GetValue(); $aValue['value'] = $this->GetValue(); - $aCriterion['values'] = array($aValue); + $aCriterion['values'] = [$aValue]; } break; } @@ -1564,8 +1469,14 @@ class FieldExpression extends UnaryExpression return false; } - public function GetParent() {return $this->m_sParent;} - public function GetName() {return $this->m_sName;} + public function GetParent() + { + return $this->m_sParent; + } + public function GetName() + { + return $this->m_sName; + } public function SetParent($sParent) { @@ -1573,14 +1484,11 @@ class FieldExpression extends UnaryExpression $this->m_value = $sParent.'.'.$this->m_sName; } - private function GetClassName($aClasses = array()) + private function GetClassName($aClasses = []) { - if (isset($aClasses[$this->m_sParent])) - { + if (isset($aClasses[$this->m_sParent])) { return $aClasses[$this->m_sParent]; - } - else - { + } else { return $this->m_sParent; } } @@ -1597,24 +1505,19 @@ class FieldExpression extends UnaryExpression * @throws \DictExceptionMissingString * @throws \Exception */ - public function Display($oSearch, &$aArgs = null, $oAttDef = null, &$aCtx = array()) + public function Display($oSearch, &$aArgs = null, $oAttDef = null, &$aCtx = []) { - if (empty($this->m_sParent)) - { + if (empty($this->m_sParent)) { return "`{$this->m_sName}`"; } - if (method_exists($oSearch, 'GetJoinedClasses')) - { + if (method_exists($oSearch, 'GetJoinedClasses')) { $aClasses = $oSearch->GetJoinedClasses(); - } - else - { - $aClasses = array($oSearch->GetClass()); + } else { + $aClasses = [$oSearch->GetClass()]; } $sClass = $this->GetClassName($aClasses); $sAttName = MetaModel::GetLabel($sClass, $this->m_sName); - if ($sClass != $oSearch->GetClass()) - { + if ($sClass != $oSearch->GetClass()) { $sAttName = MetaModel::GetName($sClass).':'.$sAttName; } @@ -1624,8 +1527,7 @@ class FieldExpression extends UnaryExpression // recursive rendering public function RenderExpression($bForSQL = false, &$aArgs = null, $bRetrofitParams = false) { - if (empty($this->m_sParent)) - { + if (empty($this->m_sParent)) { return "`{$this->m_sName}`"; } return "`{$this->m_sParent}`.`{$this->m_sName}`"; @@ -1639,8 +1541,7 @@ class FieldExpression extends UnaryExpression public function Evaluate(array $aArgs) { $sKey = empty($this->m_sParent) ? $this->m_sName : "{$this->m_sParent}.{$this->m_sName}"; - if (!array_key_exists($sKey, $aArgs)) - { + if (!array_key_exists($sKey, $aArgs)) { throw new Exception("Missing field '$sKey' from context"); } return $aArgs[$sKey]; @@ -1652,13 +1553,13 @@ class FieldExpression extends UnaryExpression */ public function ToJSON(&$aArgs = null, $bRetrofitParams = false) { - return array( + return [ 'type' => static::OPERATOR_FIELD, 'value' => $this->m_value, 'alias' => $this->m_sParent, 'field' => $this->m_sName, 'oql' => $this->RenderExpression(false, $aArgs, $bRetrofitParams), - ); + ]; } /** @@ -1668,27 +1569,22 @@ class FieldExpression extends UnaryExpression * @throws \CoreException * @throws \Exception */ - public function GetAttDef($aClasses = array()) + public function GetAttDef($aClasses = []) { - if (!empty($this->m_sParent)) - { + if (!empty($this->m_sParent)) { $sClass = $this->GetClassName($aClasses); $aAttDefs = MetaModel::ListAttributeDefs($sClass); - if (isset($aAttDefs[$this->m_sName])) - { + if (isset($aAttDefs[$this->m_sName])) { return $aAttDefs[$this->m_sName]; - } - else - { - if ($this->m_sName == 'id') - { - $aParams = array( + } else { + if ($this->m_sName == 'id') { + $aParams = [ 'default_value' => 0, 'is_null_allowed' => false, 'allowed_values' => null, 'depends_on' => null, 'sql' => 'id', - ); + ]; return new AttributeInteger($this->m_sName, $aParams); } @@ -1698,11 +1594,10 @@ class FieldExpression extends UnaryExpression return null; } - public function ListRequiredFields() { $sField = empty($this->m_sParent) ? $this->m_sName : "{$this->m_sParent}.{$this->m_sName}"; - return array($sField); + return [$sField]; } public function CollectUsedParents(&$aTable) @@ -1712,13 +1607,10 @@ class FieldExpression extends UnaryExpression public function GetUnresolvedFields($sAlias, &$aUnresolved) { - if ($this->m_sParent == $sAlias) - { + if ($this->m_sParent == $sAlias) { // Add a reference to the field $aUnresolved[$this->m_sName] = $this; - } - elseif ($sAlias == '') - { + } elseif ($sAlias == '') { // An empty alias means "any alias" // In such a case, the results are indexed differently $aUnresolved[$this->m_sParent][$this->m_sName] = $this; @@ -1735,33 +1627,29 @@ class FieldExpression extends UnaryExpression */ public function Translate($aTranslationData, $bMatchAll = true, $bMarkFieldsAsResolved = true) { - if (!array_key_exists($this->m_sParent, $aTranslationData)) - { - if ($bMatchAll) throw new CoreException('Unknown parent id in translation table', array('parent_id' => $this->m_sParent, 'translation_table' => array_keys($aTranslationData))); + if (!array_key_exists($this->m_sParent, $aTranslationData)) { + if ($bMatchAll) { + throw new CoreException('Unknown parent id in translation table', ['parent_id' => $this->m_sParent, 'translation_table' => array_keys($aTranslationData)]); + } return clone $this; } - if (!array_key_exists($this->m_sName, $aTranslationData[$this->m_sParent])) - { - if (!array_key_exists('*', $aTranslationData[$this->m_sParent])) - { + if (!array_key_exists($this->m_sName, $aTranslationData[$this->m_sParent])) { + if (!array_key_exists('*', $aTranslationData[$this->m_sParent])) { // #@# debug - if ($bMatchAll) MyHelpers::var_dump_html($aTranslationData, true); - if ($bMatchAll) throw new CoreException('Unknown name in translation table', array('name' => $this->m_sName, 'parent_id' => $this->m_sParent, 'translation_table' => array_keys($aTranslationData[$this->m_sParent]))); + if ($bMatchAll) { + throw new CoreException('Unknown name in translation table', ['name' => $this->m_sName, 'parent_id' => $this->m_sParent, 'translation_table' => array_keys($aTranslationData[$this->m_sParent])]); + } return clone $this; } $sNewParent = $aTranslationData[$this->m_sParent]['*']; $sNewName = $this->m_sName; - if ($bMarkFieldsAsResolved) - { + if ($bMarkFieldsAsResolved) { $oRet = new FieldExpressionResolved($sNewName, $sNewParent); - } - else - { + } else { $oRet = new FieldExpression($sNewName, $sNewParent); } - } - else - { + } else { $oRet = $aTranslationData[$this->m_sParent][$this->m_sName]; } return $oRet; @@ -1792,35 +1680,25 @@ class FieldExpression extends UnaryExpression $sRes = null; // Exceptions... - if ($oAttDef->IsExternalKey()) - { + if ($oAttDef->IsExternalKey()) { /** @var AttributeExternalKey $oAttDef */ $sObjClass = $oAttDef->GetTargetClass(); $iObjKey = (int)$sValue; - if ($iObjKey > 0) - { + if ($iObjKey > 0) { $oObject = MetaModel::GetObjectWithArchive($sObjClass, $iObjKey, true, true); $sRes = $oObject->GetHyperlink(); - } - else - { + } else { // Undefined $sRes = DBObject::MakeHyperLink($sObjClass, 0); } - } - elseif ($oAttDef->IsExternalField()) - { - if (is_null($sValue)) - { + } elseif ($oAttDef->IsExternalField()) { + if (is_null($sValue)) { $sRes = Dict::S('UI:UndefinedObject'); } - } - elseif ($oAttDef instanceof AttributeEnum) - { + } elseif ($oAttDef instanceof AttributeEnum) { $sRes = $oAttDef->GetAsPlainText($sValue); } - if(is_null($sRes)) - { + if (is_null($sRes)) { $sRes = $oAttDef->GetAsHtml($sValue); } return $sRes; @@ -1828,24 +1706,19 @@ class FieldExpression extends UnaryExpression public function RenameAlias($sOldName, $sNewName) { - if ($this->m_sParent == $sOldName) - { + if ($this->m_sParent == $sOldName) { $this->m_sParent = $sNewName; } } private function GetJoinedFilters($oSearch, $iOperatorCodeTarget) { - $aFilters = array(); + $aFilters = []; $aPointingToByKey = $oSearch->GetCriteria_PointingTo(); - foreach ($aPointingToByKey as $sExtKey => $aPointingTo) - { - foreach($aPointingTo as $iOperatorCode => $aFilter) - { - if ($iOperatorCode == $iOperatorCodeTarget) - { - foreach($aFilter as $oExtFilter) - { + foreach ($aPointingToByKey as $sExtKey => $aPointingTo) { + foreach ($aPointingTo as $iOperatorCode => $aFilter) { + if ($iOperatorCode == $iOperatorCodeTarget) { + foreach ($aFilter as $oExtFilter) { $aFilters[$sExtKey] = $oExtFilter; } } @@ -1865,26 +1738,20 @@ class FieldExpression extends UnaryExpression */ public function GetCriterion($oSearch, &$aArgs = null, $bRetrofitParams = false, $oAttDef = null) { - $aCriteria = array(); + $aCriteria = []; $aCriteria['is_hierarchical'] = false; // Replace BELOW joins by the corresponding external key for the search // Try to detect hierarchical links - if ($this->m_sName == 'id') - { - if (method_exists($oSearch, 'GetCriteria_PointingTo')) - { + if ($this->m_sName == 'id') { + if (method_exists($oSearch, 'GetCriteria_PointingTo')) { $aFilters = $this->GetJoinedFilters($oSearch, TREE_OPERATOR_EQUALS); - if (!empty($aFilters)) - { - foreach($aFilters as $sExtKey => $oFilter) - { + if (!empty($aFilters)) { + foreach ($aFilters as $sExtKey => $oFilter) { $aSubFilters = $this->GetJoinedFilters($oFilter, TREE_OPERATOR_BELOW); - foreach($aSubFilters as $oSubFilter) - { + foreach ($aSubFilters as $oSubFilter) { /** @var \DBObjectSearch $oSubFilter */ $sClassAlias = $oSubFilter->GetClassAlias(); - if ($sClassAlias == $this->m_sParent) - { + if ($sClassAlias == $this->m_sParent) { // Hierarchical link detected // replace current field with the corresponding external key $this->m_sName = $sExtKey; @@ -1897,33 +1764,22 @@ class FieldExpression extends UnaryExpression } } - if (method_exists($oSearch, 'GetJoinedClasses')) - { + if (method_exists($oSearch, 'GetJoinedClasses')) { $oAttDef = $this->GetAttDef($oSearch->GetJoinedClasses()); - } - else - { + } else { $oAttDef = $this->GetAttDef($oSearch->GetSelectedClasses()); } - if (!is_null($oAttDef)) - { + if (!is_null($oAttDef)) { $sSearchType = $oAttDef->GetSearchType(); - try - { - if ($sSearchType == AttributeDefinition::SEARCH_WIDGET_TYPE_EXTERNAL_KEY) - { - if (MetaModel::IsHierarchicalClass($oAttDef->GetTargetClass())) - { + try { + if ($sSearchType == AttributeDefinition::SEARCH_WIDGET_TYPE_EXTERNAL_KEY) { + if (MetaModel::IsHierarchicalClass($oAttDef->GetTargetClass())) { $sSearchType = AttributeDefinition::SEARCH_WIDGET_TYPE_HIERARCHICAL_KEY; } } + } catch (CoreException $e) { } - catch (CoreException $e) - { - } - } - else - { + } else { $sSearchType = AttributeDefinition::SEARCH_WIDGET_TYPE; } @@ -1942,20 +1798,15 @@ class FieldExpressionResolved extends FieldExpression public function __construct($mExpression, $sParent = '') { - $this->m_aAdditionalExpressions = array(); - if (is_array($mExpression)) - { - foreach ($mExpression as $sSuffix => $sExpression) - { - if ($sSuffix == '') - { + $this->m_aAdditionalExpressions = []; + if (is_array($mExpression)) { + foreach ($mExpression as $sSuffix => $sExpression) { + if ($sSuffix == '') { $sName = $sExpression; } $this->m_aAdditionalExpressions[$sSuffix] = new FieldExpressionResolved($sExpression, $sParent); } - } - else - { + } else { $sName = $mExpression; } @@ -2013,56 +1864,41 @@ class VariableExpression extends UnaryExpression * @throws \MissingQueryArgument * @throws \Exception */ - public function Display($oSearch, &$aArgs = null, $oAttDef = null, &$aCtx = array()) + public function Display($oSearch, &$aArgs = null, $oAttDef = null, &$aCtx = []) { $sValue = $this->m_value; - if (!is_null($aArgs) && (array_key_exists($this->m_sName, $aArgs))) - { + if (!is_null($aArgs) && (array_key_exists($this->m_sName, $aArgs))) { $sValue = $aArgs[$this->m_sName]; - } - elseif (($iPos = strpos($this->m_sName, '->')) !== false) - { + } elseif (($iPos = strpos($this->m_sName, '->')) !== false) { $sParamName = substr($this->m_sName, 0, $iPos); $oObj = null; $sAttCode = 'id'; - if (array_key_exists($sParamName.'->object()', $aArgs)) - { + if (array_key_exists($sParamName.'->object()', $aArgs)) { $sAttCode = substr($this->m_sName, $iPos + 2); $oObj = $aArgs[$sParamName.'->object()']; - } - elseif (array_key_exists($sParamName, $aArgs)) - { + } elseif (array_key_exists($sParamName, $aArgs)) { $sAttCode = substr($this->m_sName, $iPos + 2); $oObj = $aArgs[$sParamName]; } - if (!is_null($oObj)) - { - if ($sAttCode == 'id') - { + if (!is_null($oObj)) { + if ($sAttCode == 'id') { $sValue = $oObj->Get("friendlyname"); - } - else - { + } else { $sValue = $oObj->Get($sAttCode); } return $sValue; } } - if (!is_null($oAttDef)) - { - if ($oAttDef->IsExternalKey()) - { - try - { + if (!is_null($oAttDef)) { + if ($oAttDef->IsExternalKey()) { + try { /** @var AttributeExternalKey $oAttDef */ $sTarget = $oAttDef->GetTargetClass(); $oObj = MetaModel::GetObject($sTarget, $sValue); return $oObj->Get("friendlyname"); - } - catch (CoreException $e) - { + } catch (CoreException $e) { } } @@ -2082,42 +1918,31 @@ class VariableExpression extends UnaryExpression */ public function RenderExpression($bForSQL = false, &$aArgs = null, $bRetrofitParams = false) { - if (is_null($aArgs)) - { + if (is_null($aArgs)) { return ':'.$this->m_sName; - } - elseif (array_key_exists($this->m_sName, $aArgs)) - { + } elseif (array_key_exists($this->m_sName, $aArgs)) { $res = CMDBSource::Quote($aArgs[$this->m_sName]); - if (is_array($res)) - { + if (is_array($res)) { $res = implode(', ', $res); } return $res; - } - elseif (($iPos = strpos($this->m_sName, '->')) !== false) - { + } elseif (($iPos = strpos($this->m_sName, '->')) !== false) { $sParamName = substr($this->m_sName, 0, $iPos); - if (array_key_exists($sParamName.'->object()', $aArgs)) - { + if (array_key_exists($sParamName.'->object()', $aArgs)) { $sAttCode = substr($this->m_sName, $iPos + 2); $oObj = $aArgs[$sParamName.'->object()']; - if ($sAttCode == 'id') - { + if ($sAttCode == 'id') { return CMDBSource::Quote($oObj->GetKey()); } return CMDBSource::Quote($oObj->Get($sAttCode)); } } - if ($bRetrofitParams) - { + if ($bRetrofitParams) { $aArgs[$this->m_sName] = null; return ':'.$this->m_sName; - } - else - { - throw new MissingQueryArgument('Missing query argument', array('expecting'=>$this->m_sName, 'available'=>array_keys($aArgs))); + } else { + throw new MissingQueryArgument('Missing query argument', ['expecting' => $this->m_sName, 'available' => array_keys($aArgs)]); } } @@ -2137,17 +1962,16 @@ class VariableExpression extends UnaryExpression */ public function ToJSON(&$aArgs = null, $bRetrofitParams = false) { - return array( + return [ 'type' => static::OPERATOR_VARIABLE, 'value' => $this->m_value, 'oql' => $this->RenderExpression(false, $aArgs, $bRetrofitParams), - ); + ]; } public function RenameParam($sOldName, $sNewName) { - if ($this->m_sName == $sOldName) - { + if ($this->m_sName == $sOldName) { $this->m_sName = $sNewName; } } @@ -2163,53 +1987,39 @@ class VariableExpression extends UnaryExpression public function GetAsScalar($aArgs) { $oRet = null; - if (array_key_exists($this->m_sName, $aArgs)) - { - if(is_array($aArgs[$this->m_sName])) - { - $aExpressions = array(); - foreach($aArgs[$this->m_sName] as $sValue) - { + if (array_key_exists($this->m_sName, $aArgs)) { + if (is_array($aArgs[$this->m_sName])) { + $aExpressions = []; + foreach ($aArgs[$this->m_sName] as $sValue) { $aExpressions[] = new ScalarExpression($sValue); } $oRet = new ListExpression($aExpressions); - } - else - { + } else { $oRet = new ScalarExpression($aArgs[$this->m_sName]); } - } - elseif (($iPos = strpos($this->m_sName, '->')) !== false) - { + } elseif (($iPos = strpos($this->m_sName, '->')) !== false) { $sParamName = substr($this->m_sName, 0, $iPos); - if (array_key_exists($sParamName.'->object()', $aArgs)) - { + if (array_key_exists($sParamName.'->object()', $aArgs)) { $sAttCode = substr($this->m_sName, $iPos + 2); $oObj = $aArgs[$sParamName.'->object()']; - if ($sAttCode == 'id') - { + if ($sAttCode == 'id') { $oRet = new ScalarExpression($oObj->GetKey()); - } - elseif (MetaModel::IsValidAttCode(get_class($oObj), $sAttCode)) - { + } elseif (MetaModel::IsValidAttCode(get_class($oObj), $sAttCode)) { $oRet = new ScalarExpression($oObj->Get($sAttCode)); - } - else - { + } else { throw new CoreException("Query argument {$this->m_sName} not matching any attribute of class ".get_class($oObj)); } } } - if (is_null($oRet)) - { - throw new MissingQueryArgument('Missing query argument', array('expecting'=>$this->m_sName, 'available'=>array_keys($aArgs))); + if (is_null($oRet)) { + throw new MissingQueryArgument('Missing query argument', ['expecting' => $this->m_sName, 'available' => array_keys($aArgs)]); } return $oRet; } public function ListParameters() { - return array($this); + return [$this]; } } @@ -2233,9 +2043,8 @@ class ListExpression extends Expression */ public static function FromScalars($aScalars) { - $aExpressions = array(); - foreach($aScalars as $value) - { + $aExpressions = []; + foreach ($aScalars as $value) { $aExpressions[] = new ScalarExpression($value); } return new ListExpression($aExpressions); @@ -2263,9 +2072,8 @@ class ListExpression extends Expression */ public function RenderExpression($bForSQL = false, &$aArgs = null, $bRetrofitParams = false) { - $aRes = array(); - foreach ($this->m_aExpressions as $oExpr) - { + $aRes = []; + foreach ($this->m_aExpressions as $oExpr) { $aRes[] = $oExpr->RenderExpression($bForSQL, $aArgs, $bRetrofitParams); } return '('.implode(', ', $aRes).')'; @@ -2292,23 +2100,21 @@ class ListExpression extends Expression */ public function ToJSON(&$aArgs = null, $bRetrofitParams = false) { - $aFields = array(); - foreach ($this->m_aExpressions as $oExpr) - { + $aFields = []; + foreach ($this->m_aExpressions as $oExpr) { $aFields[] = $oExpr->ToJSON($aArgs, $bRetrofitParams); } - return array( + return [ 'type' => static::OPERATOR_LIST, 'fields' => $aFields, 'oql' => $this->RenderExpression(false, $aArgs, $bRetrofitParams), - ); + ]; } public function Browse(Closure $callback) { $callback($this); - foreach ($this->m_aExpressions as $oExpr) - { + foreach ($this->m_aExpressions as $oExpr) { $oExpr->Browse($callback); } } @@ -2321,22 +2127,15 @@ class ListExpression extends Expression */ public function ApplyParameters($aArgs) { - foreach ($this->m_aExpressions as $idx => $oExpr) - { - if ($oExpr instanceof VariableExpression) - { + foreach ($this->m_aExpressions as $idx => $oExpr) { + if ($oExpr instanceof VariableExpression) { $oVarExpr = $oExpr->GetAsScalar($aArgs); - if ($oVarExpr instanceof ListExpression) - { + if ($oVarExpr instanceof ListExpression) { $this->m_aExpressions = $oVarExpr->GetItems(); - } - else - { + } else { $this->m_aExpressions[$idx] = $oVarExpr; } - } - else - { + } else { $oExpr->ApplyParameters($aArgs); } } @@ -2344,17 +2143,15 @@ class ListExpression extends Expression public function GetUnresolvedFields($sAlias, &$aUnresolved) { - foreach ($this->m_aExpressions as $oExpr) - { + foreach ($this->m_aExpressions as $oExpr) { $oExpr->GetUnresolvedFields($sAlias, $aUnresolved); } } public function Translate($aTranslationData, $bMatchAll = true, $bMarkFieldsAsResolved = true) { - $aRes = array(); - foreach ($this->m_aExpressions as $oExpr) - { + $aRes = []; + foreach ($this->m_aExpressions as $oExpr) { $aRes[] = $oExpr->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved); } return new ListExpression($aRes); @@ -2362,9 +2159,8 @@ class ListExpression extends Expression public function ListRequiredFields() { - $aRes = array(); - foreach ($this->m_aExpressions as $oExpr) - { + $aRes = []; + foreach ($this->m_aExpressions as $oExpr) { $aRes = array_merge($aRes, $oExpr->ListRequiredFields()); } return $aRes; @@ -2372,17 +2168,15 @@ class ListExpression extends Expression public function CollectUsedParents(&$aTable) { - foreach ($this->m_aExpressions as $oExpr) - { + foreach ($this->m_aExpressions as $oExpr) { $oExpr->CollectUsedParents($aTable); } } public function ListConstantFields() { - $aRes = array(); - foreach ($this->m_aExpressions as $oExpr) - { + $aRes = []; + foreach ($this->m_aExpressions as $oExpr) { $aRes = array_merge($aRes, $oExpr->ListConstantFields()); } return $aRes; @@ -2390,9 +2184,8 @@ class ListExpression extends Expression public function ListParameters() { - $aRes = array(); - foreach ($this->m_aExpressions as $oExpr) - { + $aRes = []; + foreach ($this->m_aExpressions as $oExpr) { $aRes = array_merge($aRes, $oExpr->ListParameters()); } return $aRes; @@ -2400,26 +2193,25 @@ class ListExpression extends Expression public function RenameParam($sOldName, $sNewName) { - foreach ($this->m_aExpressions as $key => $oExpr) - { + foreach ($this->m_aExpressions as $key => $oExpr) { $this->m_aExpressions[$key] = $oExpr->RenameParam($sOldName, $sNewName); } } public function RenameAlias($sOldName, $sNewName) { - foreach ($this->m_aExpressions as $key => $oExpr) - { + foreach ($this->m_aExpressions as $key => $oExpr) { $oExpr->RenameAlias($sOldName, $sNewName); } } - public function GetAttDef($aClasses = array()) + public function GetAttDef($aClasses = []) { - foreach($this->m_aExpressions as $oExpression) - { + foreach ($this->m_aExpressions as $oExpression) { $oAttDef = $oExpression->GetAttDef($aClasses); - if (!is_null($oAttDef)) return $oAttDef; + if (!is_null($oAttDef)) { + return $oAttDef; + } } return null; @@ -2427,18 +2219,16 @@ class ListExpression extends Expression public function GetCriterion($oSearch, &$aArgs = null, $bRetrofitParams = false, $oAttDef = null) { - $aValues = array(); + $aValues = []; - foreach($this->m_aExpressions as $oExpression) - { + foreach ($this->m_aExpressions as $oExpression) { $aCrit = $oExpression->GetCriterion($oSearch, $aArgs, $bRetrofitParams, $oAttDef); - if (array_key_exists('values', $aCrit)) - { + if (array_key_exists('values', $aCrit)) { $aValues = array_merge($aValues, $aCrit['values']); } } - return array('values' => $aValues); + return ['values' => $aValues]; } } @@ -2486,17 +2276,13 @@ class NestedQueryExpression extends Expression */ public function RenderExpression($bForSQL = false, &$aArgs = null, $bRetrofitParams = false) { - if ($bForSQL) - { - $aAttToLoad = array(); - foreach ($this->m_oNestedQuery->GetSelectedClasses() as $sClassAlias => $sClass) - { - $aAttToLoad[$sClassAlias] = array(); + if ($bForSQL) { + $aAttToLoad = []; + foreach ($this->m_oNestedQuery->GetSelectedClasses() as $sClassAlias => $sClass) { + $aAttToLoad[$sClassAlias] = []; } - return '('.$this->m_oNestedQuery->MakeSelectQuery(array(), $aArgs, $aAttToLoad).')'; - } - else - { + return '('.$this->m_oNestedQuery->MakeSelectQuery([], $aArgs, $aAttToLoad, null, 0, 0, false, true, true).')'; + } else { return '('.$this->m_oNestedQuery->ToOQL(false, null, false).')'; } } @@ -2517,49 +2303,59 @@ class NestedQueryExpression extends Expression } /**/ - public function ApplyParameters($aArgs) { + public function ApplyParameters($aArgs) + { $this->m_oNestedQuery->ApplyParameters($aArgs); } /**/ - public function GetUnresolvedFields($sAlias, &$aUnresolved) { + public function GetUnresolvedFields($sAlias, &$aUnresolved) + { } /**/ - public function Translate($aTranslationData, $bMatchAll = true, $bMarkFieldsAsResolved = true) { + public function Translate($aTranslationData, $bMatchAll = true, $bMarkFieldsAsResolved = true) + { // Check and prepare the select information $this->m_oNestedQuery->TranslateConditions($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved); return clone $this; } - public function ListRequiredFields() { - return array(); + public function ListRequiredFields() + { + return []; } - public function CollectUsedParents(&$aTable) { + public function CollectUsedParents(&$aTable) + { } - public function ListConstantFields() { + public function ListConstantFields() + { return $this->m_oNestedQuery->ListConstantFields(); } - public function ListParameters() { + public function ListParameters() + { return $this->m_oNestedQuery->GetExpectedArguments(); } - public function RenameParam($sOldName, $sNewName) { + public function RenameParam($sOldName, $sNewName) + { $this->m_oNestedQuery->RenameParam($sOldName, $sNewName); } - public function RenameAlias($sOldName, $sNewName) { + public function RenameAlias($sOldName, $sNewName) + { $this->m_oNestedQuery->RenameAlias($sOldName, $sNewName); } /** * @inheritDoc */ - public function ToJSON(&$aArgs = null, $bRetrofitParams = false) { + public function ToJSON(&$aArgs = null, $bRetrofitParams = false) + { return $this->m_oNestedQuery->ToJSON(); } @@ -2570,7 +2366,8 @@ class NestedQueryExpression extends Expression * * @uses \DBSearch::AllowAllData() */ - public function AllowAllData($bAllowAllData = true) { + public function AllowAllData($bAllowAllData = true) + { $this->m_oNestedQuery->AllowAllData($bAllowAllData); } } @@ -2605,9 +2402,8 @@ class FunctionExpression extends Expression // recursive rendering public function RenderExpression($bForSQL = false, &$aArgs = null, $bRetrofitParams = false) { - $aRes = array(); - foreach ($this->m_aArgs as $iPos => $oExpr) - { + $aRes = []; + foreach ($this->m_aArgs as $iPos => $oExpr) { $aRes[] = $oExpr->RenderExpression($bForSQL, $aArgs, $bRetrofitParams); } return $this->m_sVerb.'('.implode(', ', $aRes).')'; @@ -2620,51 +2416,49 @@ class FunctionExpression extends Expression */ public function Evaluate(array $aArgs) { - switch($this->m_sVerb) - { + switch ($this->m_sVerb) { case 'CONCAT': $sRet = ''; - foreach ($this->m_aArgs as $iPos => $oExpr) - { + foreach ($this->m_aArgs as $iPos => $oExpr) { $item = $oExpr->Evaluate($aArgs); - if (is_null($item)) return null; + if (is_null($item)) { + return null; + } $sRet .= $item; } return $sRet; case 'CONCAT_WS': - if (count($this->m_aArgs) < 3) - { + if (count($this->m_aArgs) < 3) { throw new \Exception("Function {$this->m_sVerb} requires at least 3 arguments"); } $sSeparator = $this->m_aArgs[0]->Evaluate($aArgs); - foreach ($this->m_aArgs as $iPos => $oExpr) - { - if ($iPos == 0) continue; + foreach ($this->m_aArgs as $iPos => $oExpr) { + if ($iPos == 0) { + continue; + } $item = $oExpr->Evaluate($aArgs); - if (is_null($item)) return null; + if (is_null($item)) { + return null; + } $aStrings[] = $item; } $sRet = implode($sSeparator, $aStrings); return $sRet; case 'SUBSTR': - if (count($this->m_aArgs) < 2) - { - throw new \Exception("Function {$this->m_sVerb} requires at least 2 arguments"); + if (count($this->m_aArgs) < 2) { + throw new \Exception("Function {$this->m_sVerb} requires at least 2 arguments"); } $sString = $this->m_aArgs[0]->Evaluate($aArgs); $iRawPos = $this->m_aArgs[1]->Evaluate($aArgs); $iPos = $iRawPos > 0 ? $iRawPos - 1// 0-based in PHP (1-based in SQL) : $iRawPos; // Negative - if (count($this->m_aArgs) == 2) - { + if (count($this->m_aArgs) == 2) { // Up to the end of the string $sRet = substr($sString, $iPos); - } - else - { + } else { // Length specified $iLen = $this->m_aArgs[2]->Evaluate($aArgs); $sRet = substr($sString, $iPos, $iLen); @@ -2672,111 +2466,95 @@ class FunctionExpression extends Expression return $sRet; case 'TRIM': - if (count($this->m_aArgs) != 1) - { + if (count($this->m_aArgs) != 1) { throw new \Exception("Function {$this->m_sVerb} requires 1 argument"); } $sRet = trim($this->m_aArgs[0]->Evaluate($aArgs)); return $sRet; case 'INET_ATON': - if (count($this->m_aArgs) != 1) - { + if (count($this->m_aArgs) != 1) { throw new \Exception("Function {$this->m_sVerb} requires 1 argument"); } $sRet = ip2long($this->m_aArgs[0]->Evaluate($aArgs)); return $sRet; case 'INET_NTOA': - if (count($this->m_aArgs) != 1) - { + if (count($this->m_aArgs) != 1) { throw new \Exception("Function {$this->m_sVerb} requires 1 argument"); } $sRet = long2ip($this->m_aArgs[0]->Evaluate($aArgs)); return $sRet; case 'ISNULL': - if (count($this->m_aArgs) != 1) - { + if (count($this->m_aArgs) != 1) { throw new \Exception("Function {$this->m_sVerb} requires 1 argument"); } $sRet = is_null($this->m_aArgs[0]->Evaluate($aArgs)); return $sRet; case 'COALESCE': - if (count($this->m_aArgs) < 1) - { + if (count($this->m_aArgs) < 1) { throw new \Exception("Function {$this->m_sVerb} requires at least 1 argument"); } $ret = null; - foreach($this->m_aArgs as $iPos => $oExpr) - { + foreach ($this->m_aArgs as $iPos => $oExpr) { $ret = $oExpr->Evaluate($aArgs); - if (!is_null($ret)) break; + if (!is_null($ret)) { + break; + } } return $ret; case 'IF': - if (count($this->m_aArgs) != 3) - { + if (count($this->m_aArgs) != 3) { throw new \Exception("Function {$this->m_sVerb} requires 3 arguments"); } $bCond = $this->m_aArgs[0]->Evaluate($aArgs); - if ($bCond) - { + if ($bCond) { $ret = $this->m_aArgs[1]->Evaluate($aArgs); - } - else - { + } else { $ret = $this->m_aArgs[2]->Evaluate($aArgs); } return $ret; case 'ELT': - if (count($this->m_aArgs) < 2) - { + if (count($this->m_aArgs) < 2) { throw new \Exception("Function {$this->m_sVerb} requires at least 2 arguments"); } // First argument is the 1-based position $iPosition = (int) $this->m_aArgs[0]->Evaluate($aArgs); - if (($iPosition == 0) || ($iPosition >= count($this->m_aArgs))) - { + if (($iPosition == 0) || ($iPosition >= count($this->m_aArgs))) { // Out of range $ret = null; - } - else - { + } else { $ret = $this->m_aArgs[$iPosition]->Evaluate($aArgs); } return $ret; case 'DATE': - if (count($this->m_aArgs) != 1) - { + if (count($this->m_aArgs) != 1) { throw new \Exception("Function {$this->m_sVerb} requires 1 argument"); } $sRet = date('Y-m-d', strtotime($this->m_aArgs[0]->Evaluate($aArgs))); return $sRet; case 'YEAR': - if (count($this->m_aArgs) != 1) - { + if (count($this->m_aArgs) != 1) { throw new \Exception("Function {$this->m_sVerb} requires 1 argument"); } $iRet = (int) date('Y', strtotime($this->m_aArgs[0]->Evaluate($aArgs))); return $iRet; case 'MONTH': - if (count($this->m_aArgs) != 1) - { + if (count($this->m_aArgs) != 1) { throw new \Exception("Function {$this->m_sVerb} requires 1 argument"); } $iRet = (int) date('m', strtotime($this->m_aArgs[0]->Evaluate($aArgs))); return $iRet; case 'DAY': - if (count($this->m_aArgs) != 1) - { + if (count($this->m_aArgs) != 1) { throw new \Exception("Function {$this->m_sVerb} requires 1 argument"); } $iRet = (int) date('d', strtotime($this->m_aArgs[0]->Evaluate($aArgs))); @@ -2833,7 +2611,7 @@ class FunctionExpression extends Expression '%m', '%H', '%i', - '%s' + '%s', ]; //@formatter:on /** @@ -2866,7 +2644,7 @@ class FunctionExpression extends Expression 'm', 'H', 'i', - 's' + 's', ]; //@formatter:on $sFormatForPhpDateFormat = str_replace($aFormatsForMysqlDateFormat, $aFormatsForPhpDateFormat, $sFormatForMysqlDateFormat); @@ -2876,8 +2654,7 @@ class FunctionExpression extends Expression return $sRet; case 'TO_DAYS': - if (count($this->m_aArgs) != 1) - { + if (count($this->m_aArgs) != 1) { throw new \Exception("Function {$this->m_sVerb} requires 1 argument"); } @@ -2903,8 +2680,7 @@ class FunctionExpression extends Expression return $iRet; case 'FROM_DAYS': - if (count($this->m_aArgs) != 1) - { + if (count($this->m_aArgs) != 1) { throw new \Exception("Function {$this->m_sVerb} requires 1 argument"); } $iSince1582 = $this->m_aArgs[0]->Evaluate($aArgs) - 577814; @@ -2921,8 +2697,7 @@ class FunctionExpression extends Expression return $sRet; case 'DATE_ADD': - if (count($this->m_aArgs) != 2) - { + if (count($this->m_aArgs) != 2) { throw new \Exception("Function {$this->m_sVerb} requires 2 arguments"); } $sStartDate = $this->m_aArgs[0]->Evaluate($aArgs); @@ -2932,8 +2707,7 @@ class FunctionExpression extends Expression return $sRet; case 'DATE_SUB': - if (count($this->m_aArgs) != 2) - { + if (count($this->m_aArgs) != 2) { throw new \Exception("Function {$this->m_sVerb} requires 2 arguments"); } $sStartDate = $this->m_aArgs[0]->Evaluate($aArgs); @@ -2953,39 +2727,33 @@ class FunctionExpression extends Expression */ public function ToJSON(&$aArgs = null, $bRetrofitParams = false) { - $aFields = array(); - foreach ($this->m_aArgs as $oExpr) - { + $aFields = []; + foreach ($this->m_aArgs as $oExpr) { $aFields[] = $oExpr->ToJSON($aArgs, $bRetrofitParams); } - return array( + return [ 'type' => static::OPERATOR_FUNCTION, 'operator' => $this->m_sVerb, 'fields' => $aFields, 'oql' => $this->RenderExpression(false, $aArgs, $bRetrofitParams), - ); + ]; } public function Browse(Closure $callback) { $callback($this); - foreach ($this->m_aArgs as $iPos => $oExpr) - { + foreach ($this->m_aArgs as $iPos => $oExpr) { $oExpr->Browse($callback); } } public function ApplyParameters($aArgs) { - foreach ($this->m_aArgs as $idx => $oExpr) - { - if ($oExpr instanceof VariableExpression) - { + foreach ($this->m_aArgs as $idx => $oExpr) { + if ($oExpr instanceof VariableExpression) { $this->m_aArgs[$idx] = $oExpr->GetAsScalar($aArgs); - } - else - { + } else { $oExpr->ApplyParameters($aArgs); } } @@ -2993,17 +2761,15 @@ class FunctionExpression extends Expression public function GetUnresolvedFields($sAlias, &$aUnresolved) { - foreach ($this->m_aArgs as $oExpr) - { + foreach ($this->m_aArgs as $oExpr) { $oExpr->GetUnresolvedFields($sAlias, $aUnresolved); } } public function Translate($aTranslationData, $bMatchAll = true, $bMarkFieldsAsResolved = true) { - $aRes = array(); - foreach ($this->m_aArgs as $oExpr) - { + $aRes = []; + foreach ($this->m_aArgs as $oExpr) { $aRes[] = $oExpr->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved); } return new FunctionExpression($this->m_sVerb, $aRes); @@ -3011,9 +2777,8 @@ class FunctionExpression extends Expression public function ListRequiredFields() { - $aRes = array(); - foreach ($this->m_aArgs as $oExpr) - { + $aRes = []; + foreach ($this->m_aArgs as $oExpr) { $aRes = array_merge($aRes, $oExpr->ListRequiredFields()); } return $aRes; @@ -3021,17 +2786,15 @@ class FunctionExpression extends Expression public function CollectUsedParents(&$aTable) { - foreach ($this->m_aArgs as $oExpr) - { + foreach ($this->m_aArgs as $oExpr) { $oExpr->CollectUsedParents($aTable); } } public function ListConstantFields() { - $aRes = array(); - foreach ($this->m_aArgs as $oExpr) - { + $aRes = []; + foreach ($this->m_aArgs as $oExpr) { $aRes = array_merge($aRes, $oExpr->ListConstantFields()); } return $aRes; @@ -3040,9 +2803,8 @@ class FunctionExpression extends Expression public function ListParameters() { - $aRes = array(); - foreach ($this->m_aArgs as $oExpr) - { + $aRes = []; + foreach ($this->m_aArgs as $oExpr) { $aRes = array_merge($aRes, $oExpr->ListParameters()); } return $aRes; @@ -3050,26 +2812,25 @@ class FunctionExpression extends Expression public function RenameParam($sOldName, $sNewName) { - foreach ($this->m_aArgs as $key => $oExpr) - { + foreach ($this->m_aArgs as $key => $oExpr) { $this->m_aArgs[$key] = $oExpr->RenameParam($sOldName, $sNewName); } } public function RenameAlias($sOldName, $sNewName) { - foreach ($this->m_aArgs as $key => $oExpr) - { + foreach ($this->m_aArgs as $key => $oExpr) { $oExpr->RenameAlias($sOldName, $sNewName); } } - public function GetAttDef($aClasses = array()) + public function GetAttDef($aClasses = []) { - foreach($this->m_aArgs as $oExpression) - { + foreach ($this->m_aArgs as $oExpression) { $oAttDef = $oExpression->GetAttDef($aClasses); - if (!is_null($oAttDef)) return $oAttDef; + if (!is_null($oAttDef)) { + return $oAttDef; + } } return null; @@ -3089,7 +2850,7 @@ class FunctionExpression extends Expression static $aWeekDayToString = null; if (is_null($aWeekDayToString)) { // Init the correspondance table - $aWeekDayToString = array( + $aWeekDayToString = [ 0 => Dict::S('DayOfWeek-Sunday'), 1 => Dict::S('DayOfWeek-Monday'), 2 => Dict::S('DayOfWeek-Tuesday'), @@ -3097,13 +2858,12 @@ class FunctionExpression extends Expression 4 => Dict::S('DayOfWeek-Thursday'), 5 => Dict::S('DayOfWeek-Friday'), 6 => Dict::S('DayOfWeek-Saturday'), - ); + ]; } static $aMonthToString = null; - if (is_null($aMonthToString)) - { + if (is_null($aMonthToString)) { // Init the correspondance table - $aMonthToString = array( + $aMonthToString = [ 1 => Dict::S('Month-01'), 2 => Dict::S('Month-02'), 3 => Dict::S('Month-03'), @@ -3116,7 +2876,7 @@ class FunctionExpression extends Expression 10 => Dict::S('Month-10'), 11 => Dict::S('Month-11'), 12 => Dict::S('Month-12'), - ); + ]; } $sRes = $sDefault; @@ -3143,12 +2903,11 @@ class FunctionExpression extends Expression return $sRes; } - public function Display($oSearch, &$aArgs = null, $oAttDef = null, &$aCtx = array()) + public function Display($oSearch, &$aArgs = null, $oAttDef = null, &$aCtx = []) { $sOperation = ''; $sVerb = ''; - switch ($this->m_sVerb) - { + switch ($this->m_sVerb) { case 'ISNULL': case 'NOW': $sVerb = $this->VerbToNaturalLanguage(); @@ -3166,18 +2925,15 @@ class FunctionExpression extends Expression return $this->RenderExpression(false, $aArgs); } - foreach($this->m_aArgs as $oExpression) - { - if ($oExpression instanceof IntervalExpression) - { + foreach ($this->m_aArgs as $oExpression) { + if ($oExpression instanceof IntervalExpression) { $sOperation .= $sVerb; $sVerb = ''; } $sOperation .= $oExpression->Display($oSearch, $aArgs, $oAttDef, $aCtx); } - if (!empty($sVerb)) - { + if (!empty($sVerb)) { $sOperation .= $sVerb; } return '('.$sOperation.')'; @@ -3190,13 +2946,11 @@ class FunctionExpression extends Expression public function GetCriterion($oSearch, &$aArgs = null, $bRetrofitParams = false, $oAttDef = null) { - $aCriteria = array(); - switch ($this->m_sVerb) - { + $aCriteria = []; + switch ($this->m_sVerb) { case 'ISNULL': $aCriteria['operator'] = $this->m_sVerb; - foreach($this->m_aArgs as $oExpression) - { + foreach ($this->m_aArgs as $oExpression) { $aCriteria = array_merge($oExpression->GetCriterion($oSearch, $aArgs, $bRetrofitParams, $oAttDef), $aCriteria); } $aCriteria['has_undefined'] = true; @@ -3204,7 +2958,7 @@ class FunctionExpression extends Expression break; case 'NOW': - $aCriteria = array('widget' => 'date_time'); + $aCriteria = ['widget' => 'date_time']; $aCriteria['is_relative'] = true; $aCriteria['verb'] = $this->m_sVerb; break; @@ -3212,9 +2966,8 @@ class FunctionExpression extends Expression case 'DATE_ADD': case 'DATE_SUB': case 'DATE_FORMAT': - $aCriteria = array('widget' => 'date_time'); - foreach($this->m_aArgs as $oExpression) - { + $aCriteria = ['widget' => 'date_time']; + foreach ($this->m_aArgs as $oExpression) { $aCriteria = array_merge($oExpression->GetCriterion($oSearch, $aArgs, $bRetrofitParams, $oAttDef), $aCriteria); } $aCriteria['verb'] = $this->m_sVerb; @@ -3281,12 +3034,12 @@ class IntervalExpression extends Expression */ public function ToJSON(&$aArgs = null, $bRetrofitParams = false) { - return array( + return [ 'type' => static::OPERATOR_INTERVAL, 'unit' => $this->m_sUnit, 'expression' => $this->m_oValue->ToJSON($aArgs, $bRetrofitParams), 'oql' => $this->RenderExpression(false, $aArgs, $bRetrofitParams), - ); + ]; } public function Browse(Closure $callback) @@ -3297,12 +3050,9 @@ class IntervalExpression extends Expression public function ApplyParameters($aArgs) { - if ($this->m_oValue instanceof VariableExpression) - { + if ($this->m_oValue instanceof VariableExpression) { $this->m_oValue = $this->m_oValue->GetAsScalar($aArgs); - } - else - { + } else { $this->m_oValue->ApplyParameters($aArgs); } } @@ -3319,7 +3069,7 @@ class IntervalExpression extends Expression public function ListRequiredFields() { - return array(); + return []; } public function CollectUsedParents(&$aTable) @@ -3328,7 +3078,7 @@ class IntervalExpression extends Expression public function ListConstantFields() { - return array(); + return []; } public function ListParameters() @@ -3354,7 +3104,7 @@ class IntervalExpression extends Expression return $aCriteria; } - public function Display($oSearch, &$aArgs = null, $oAttDef = null, &$aCtx = array()) + public function Display($oSearch, &$aArgs = null, $oAttDef = null, &$aCtx = []) { return $this->m_oValue->RenderExpression(false, $aArgs).' '.Dict::S('Expression:Unit:Long:'.$this->m_sUnit, $this->m_sUnit); } @@ -3383,9 +3133,8 @@ class CharConcatExpression extends Expression // recursive rendering public function RenderExpression($bForSQL = false, &$aArgs = null, $bRetrofitParams = false) { - $aRes = array(); - foreach ($this->m_aExpressions as $oExpr) - { + $aRes = []; + foreach ($this->m_aExpressions as $oExpr) { $sCol = $oExpr->RenderExpression($bForSQL, $aArgs, $bRetrofitParams); // Concat will be globally NULL if one single argument is null ! $aRes[] = "COALESCE($sCol, '')"; @@ -3401,8 +3150,7 @@ class CharConcatExpression extends Expression public function Evaluate(array $aArgs) { $sRet = ''; - foreach ($this->m_aExpressions as $oExpr) - { + foreach ($this->m_aExpressions as $oExpr) { $sRet .= $oExpr->Evaluate($aArgs); } return $sRet; @@ -3414,38 +3162,32 @@ class CharConcatExpression extends Expression */ public function ToJSON(&$aArgs = null, $bRetrofitParams = false) { - $aFields = array(); - foreach ($this->m_aExpressions as $oExpr) - { + $aFields = []; + foreach ($this->m_aExpressions as $oExpr) { $aFields[] = $oExpr->RenderExpression($aArgs, $bRetrofitParams); } - return array( + return [ 'type' => static::OPERATOR_FUNCTION, 'operator' => 'concat', 'fields' => $aFields, - ); + ]; } public function Browse(Closure $callback) { $callback($this); - foreach ($this->m_aExpressions as $oExpr) - { + foreach ($this->m_aExpressions as $oExpr) { $oExpr->Browse($callback); } } public function ApplyParameters($aArgs) { - foreach ($this->m_aExpressions as $idx => $oExpr) - { - if ($oExpr instanceof VariableExpression) - { + foreach ($this->m_aExpressions as $idx => $oExpr) { + if ($oExpr instanceof VariableExpression) { $this->m_aExpressions[$idx] = $oExpr->GetAsScalar($aArgs); - } - else - { + } else { $this->m_aExpressions->ApplyParameters($aArgs); } } @@ -3453,17 +3195,15 @@ class CharConcatExpression extends Expression public function GetUnresolvedFields($sAlias, &$aUnresolved) { - foreach ($this->m_aExpressions as $oExpr) - { + foreach ($this->m_aExpressions as $oExpr) { $oExpr->GetUnresolvedFields($sAlias, $aUnresolved); } } public function Translate($aTranslationData, $bMatchAll = true, $bMarkFieldsAsResolved = true) { - $aRes = array(); - foreach ($this->m_aExpressions as $oExpr) - { + $aRes = []; + foreach ($this->m_aExpressions as $oExpr) { $aRes[] = $oExpr->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved); } return new CharConcatExpression($aRes); @@ -3471,9 +3211,8 @@ class CharConcatExpression extends Expression public function ListRequiredFields() { - $aRes = array(); - foreach ($this->m_aExpressions as $oExpr) - { + $aRes = []; + foreach ($this->m_aExpressions as $oExpr) { $aRes = array_merge($aRes, $oExpr->ListRequiredFields()); } return $aRes; @@ -3481,17 +3220,15 @@ class CharConcatExpression extends Expression public function CollectUsedParents(&$aTable) { - foreach ($this->m_aExpressions as $oExpr) - { + foreach ($this->m_aExpressions as $oExpr) { $oExpr->CollectUsedParents($aTable); } } public function ListConstantFields() { - $aRes = array(); - foreach ($this->m_aExpressions as $oExpr) - { + $aRes = []; + foreach ($this->m_aExpressions as $oExpr) { $aRes = array_merge($aRes, $oExpr->ListConstantFields()); } return $aRes; @@ -3499,9 +3236,8 @@ class CharConcatExpression extends Expression public function ListParameters() { - $aRes = array(); - foreach ($this->m_aExpressions as $oExpr) - { + $aRes = []; + foreach ($this->m_aExpressions as $oExpr) { $aRes = array_merge($aRes, $oExpr->ListParameters()); } return $aRes; @@ -3509,22 +3245,19 @@ class CharConcatExpression extends Expression public function RenameParam($sOldName, $sNewName) { - foreach ($this->m_aExpressions as $key => $oExpr) - { + foreach ($this->m_aExpressions as $key => $oExpr) { $this->m_aExpressions[$key] = $oExpr->RenameParam($sOldName, $sNewName); } } public function RenameAlias($sOldName, $sNewName) { - foreach ($this->m_aExpressions as $key => $oExpr) - { + foreach ($this->m_aExpressions as $key => $oExpr) { $oExpr->RenameAlias($sOldName, $sNewName); } } } - class CharConcatWSExpression extends CharConcatExpression { protected $m_separator; @@ -3538,9 +3271,8 @@ class CharConcatWSExpression extends CharConcatExpression // recursive rendering public function RenderExpression($bForSQL = false, &$aArgs = null, $bRetrofitParams = false) { - $aRes = array(); - foreach ($this->m_aExpressions as $oExpr) - { + $aRes = []; + foreach ($this->m_aExpressions as $oExpr) { $sCol = $oExpr->RenderExpression($bForSQL, $aArgs, $bRetrofitParams); // Concat will be globally NULL if one single argument is null ! $aRes[] = "COALESCE($sCol, '')"; @@ -3556,9 +3288,8 @@ class CharConcatWSExpression extends CharConcatExpression */ public function Evaluate(array $aArgs) { - $aRes = array(); - foreach ($this->m_aExpressions as $oExpr) - { + $aRes = []; + foreach ($this->m_aExpressions as $oExpr) { // TODO: Seems weird, this should rather be $aRes[] = $oExpr->Evaluate($aArgs); $aRes .= $oExpr->Evaluate($aArgs); } @@ -3568,20 +3299,17 @@ class CharConcatWSExpression extends CharConcatExpression public function Browse(Closure $callback) { $callback($this); - foreach ($this->m_aExpressions as $oExpr) - { + foreach ($this->m_aExpressions as $oExpr) { $oExpr->Browse($callback); } } public function Translate($aTranslationData, $bMatchAll = true, $bMarkFieldsAsResolved = true) { - $aRes = array(); - foreach ($this->m_aExpressions as $oExpr) - { + $aRes = []; + foreach ($this->m_aExpressions as $oExpr) { $aRes[] = $oExpr->Translate($aTranslationData, $bMatchAll, $bMarkFieldsAsResolved); } return new CharConcatWSExpression($this->m_separator, $aRes); } } - diff --git a/core/sqlobjectquerybuilder.class.inc.php b/core/sqlobjectquerybuilder.class.inc.php index 893fbc032e..b84b81021b 100644 --- a/core/sqlobjectquerybuilder.class.inc.php +++ b/core/sqlobjectquerybuilder.class.inc.php @@ -29,13 +29,14 @@ class SQLObjectQueryBuilder * @param array $aGroupByExpr * @param array $aSelectedClasses * @param array $aSelectExpr + * @param bool $bSelectOnlyIds * * @return null|SQLObjectQuery * @throws \CoreException */ - public function BuildSQLQueryStruct($aAttToLoad, $bGetCount, $aModifierProperties, $aGroupByExpr = null, $aSelectedClasses = null, $aSelectExpr = null) + public function BuildSQLQueryStruct($aAttToLoad, $bGetCount, $aModifierProperties, $aGroupByExpr = null, $aSelectedClasses = null, $aSelectExpr = null, $bSelectOnlyIds = false) { - if ($bGetCount || !is_null($aGroupByExpr)) { + if ($bGetCount || !is_null($aGroupByExpr) || $bSelectOnlyIds) { // Avoid adding all the fields for counts or "group by" requests $aAttToLoad = []; foreach ($this->oDBObjetSearch->GetSelectedClasses() as $sClassAlias => $sClass) {