From 18a28df55f5b5edec4ec9236f92a362e863a5d03 Mon Sep 17 00:00:00 2001 From: Romain Quetiez Date: Tue, 20 Mar 2012 10:52:11 +0000 Subject: [PATCH] #520 Capability to define a default sort order (PHP/XML) SVN:trunk[1900] --- core/metamodel.class.php | 13 +++++++- pages/ajax.render.php | 9 ------ setup/compiler.class.inc.php | 58 ++++++++++++++++++++++++++---------- 3 files changed, 54 insertions(+), 26 deletions(-) diff --git a/core/metamodel.class.php b/core/metamodel.class.php index bf2e09812..4d15771dd 100644 --- a/core/metamodel.class.php +++ b/core/metamodel.class.php @@ -494,6 +494,11 @@ abstract class MetaModel self::_check_subclass($sClass); return array_key_exists("display_template", self::$m_aClassParams[$sClass]) ? self::$m_aClassParams[$sClass]["display_template"]: ''; } + final static public function GetOrderByDefault($sClass) + { + self::_check_subclass($sClass); + return array_key_exists("order_by_default", self::$m_aClassParams[$sClass]) ? self::$m_aClassParams[$sClass]["order_by_default"]: array(); + } final static public function GetAttributeOrigin($sClass, $sAttCode) { self::_check_subclass($sClass); @@ -2009,6 +2014,12 @@ abstract class MetaModel } } + // Get the class default sort order if not specified with the API + // + if (empty($aOrderBy)) + { + $aOrderBy = self::GetOrderByDefault($oFilter->GetClass()); + } // Check the order by specification, and prefix with the class alias // and make sure that the ordering columns are going to be selected // @@ -2043,7 +2054,7 @@ abstract class MetaModel $aAttToLoad[$sFirstClassAlias][$sFieldAlias] = MetaModel::GetAttributeDef($oFilter->GetFirstJoinedClass(), $sFieldAlias); } } - // By default, force the name attribute to be the ordering key + // At least, force the name attribute to be the ordering key // if (empty($aOrderSpec)) { diff --git a/pages/ajax.render.php b/pages/ajax.render.php index a45920c18..fcfa21eaf 100644 --- a/pages/ajax.render.php +++ b/pages/ajax.render.php @@ -122,15 +122,6 @@ try $iSortIndex++; } - if (count($aOrderBy) == 0) - { - $aOrderBy['friendlyname'] = true; // By default, sort by name - } - else - { -// $oPage->add("

ICI: OrderBy already set to:
'".print_r($aOrderBy, true)."'

\n"); - } - // Load only the requested columns $oSet = new DBObjectSet($oFilter, $aOrderBy, $aExtraParams, null, $iEnd-$iStart, $iStart); $sClassAlias = $oSet->GetFilter()->GetClassAlias(); diff --git a/setup/compiler.class.inc.php b/setup/compiler.class.inc.php index d41d937c1..5fcc5aa5c 100644 --- a/setup/compiler.class.inc.php +++ b/setup/compiler.class.inc.php @@ -223,12 +223,15 @@ EOF; protected function GetUniqueElement($oDOMNode, $sTagName, $bMustExist = true) { $oNode = null; - foreach($oDOMNode->childNodes as $oChildNode) + if ($oDOMNode->hasChildNodes()) { - if ($oChildNode->nodeName == $sTagName) + foreach($oDOMNode->childNodes as $oChildNode) { - $oNode = $oChildNode; - break; + if ($oChildNode->nodeName == $sTagName) + { + $oNode = $oChildNode; + break; + } } } if ($bMustExist && is_null($oNode)) @@ -255,11 +258,14 @@ EOF; protected function GetNodeText($oNode) { $sText = ''; - foreach($oNode->childNodes as $oChildNode) + if ($oNode->hasChildNodes()) { - if ($oChildNode instanceof DOMCharacterData) // Base class of DOMText and DOMCdataSection + foreach($oNode->childNodes as $oChildNode) { - $sText .= $oChildNode->wholeText; + if ($oChildNode instanceof DOMCharacterData) // Base class of DOMText and DOMCdataSection + { + $sText .= $oChildNode->wholeText; + } } } return $sText; @@ -281,17 +287,20 @@ EOF; if ($oItems) { $res = array(); - foreach($oItems->childNodes as $oItem) + if ($oItems->hasChildNodes()) { - // When an attribute is msising - if ($oItem->hasAttribute('key')) + foreach($oItems->childNodes as $oItem) { - $key = $oItem->getAttribute('key'); - $res[$key] = $this->GetNodeAsArrayOfItems($oItem); - } - else - { - $res[] = $this->GetNodeAsArrayOfItems($oItem); + // When an attribute is msising + if ($oItem->hasAttributes() && $oItem->hasAttribute('key')) + { + $key = $oItem->getAttribute('key'); + $res[$key] = $this->GetNodeAsArrayOfItems($oItem); + } + else + { + $res[] = $this->GetNodeAsArrayOfItems($oItem); + } } } } @@ -407,6 +416,23 @@ EOF; $aClassParams['icon'] = "utils::GetAbsoluteUrlModulesRoot().'$sIcon'"; } + $oOrder = $this->GetOptionalElement($oProperties, 'order'); + if ($oOrder) + { + $oColumnsNode = $this->GetUniqueElement($oOrder, 'columns'); + $oColumns = $oColumnsNode->getElementsByTagName('column'); + $aSortColumns = array(); + foreach($oColumns as $oColumn) + { + $aSortColumns[] = "'".$oColumn->getAttribute('name')."' => ".(($oColumn->getAttribute('ascending') == 'true') ? 'true' : 'false'); + } + if (count($aSortColumns) > 0) + { + $aClassParams['order_by_default'] = "array(".implode(", ", $aSortColumns).")"; + } + } + + // Finalize class params declaration // $aClassParamsPHP = array();