Browse Source

#520 Capability to define a default sort order (PHP/XML)

git-svn-id: http://svn.code.sf.net/p/itop/code/trunk@1900 a333f486-631f-4898-b8df-5754b55c2be0
romainq 13 years ago
parent
commit
34a29ab219
3 changed files with 54 additions and 26 deletions
  1. 12 1
      core/metamodel.class.php
  2. 0 9
      pages/ajax.render.php
  3. 42 16
      setup/compiler.class.inc.php

+ 12 - 1
core/metamodel.class.php

@@ -494,6 +494,11 @@ abstract class MetaModel
 		self::_check_subclass($sClass);	
 		self::_check_subclass($sClass);	
 		return array_key_exists("display_template", self::$m_aClassParams[$sClass]) ? self::$m_aClassParams[$sClass]["display_template"]: '';
 		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)
 	final static public function GetAttributeOrigin($sClass, $sAttCode)
 	{
 	{
 		self::_check_subclass($sClass);
 		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
 		// Check the order by specification, and prefix with the class alias
 		// and make sure that the ordering columns are going to be selected
 		// 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);
 				$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))
 		if (empty($aOrderSpec))
 		{
 		{

+ 0 - 9
pages/ajax.render.php

@@ -122,15 +122,6 @@ try
 			$iSortIndex++;
 			$iSortIndex++;
 		}
 		}
 		
 		
-		if (count($aOrderBy) == 0)
-		{
-			$aOrderBy['friendlyname'] = true; // By default, sort by name
-		}
-		else
-		{
-//			$oPage->add("</p>ICI: OrderBy already set to: <pre>'".print_r($aOrderBy, true)."'</pre></p>\n");		
-		}
-
 		// Load only the requested columns
 		// Load only the requested columns
 		$oSet = new DBObjectSet($oFilter, $aOrderBy, $aExtraParams, null, $iEnd-$iStart, $iStart);
 		$oSet = new DBObjectSet($oFilter, $aOrderBy, $aExtraParams, null, $iEnd-$iStart, $iStart);
 		$sClassAlias = $oSet->GetFilter()->GetClassAlias();
 		$sClassAlias = $oSet->GetFilter()->GetClassAlias();

+ 42 - 16
setup/compiler.class.inc.php

@@ -223,12 +223,15 @@ EOF;
 	protected function GetUniqueElement($oDOMNode, $sTagName, $bMustExist = true)
 	protected function GetUniqueElement($oDOMNode, $sTagName, $bMustExist = true)
 	{
 	{
 		$oNode = null;
 		$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))
 		if ($bMustExist && is_null($oNode))
@@ -255,11 +258,14 @@ EOF;
 	protected function GetNodeText($oNode)
 	protected function GetNodeText($oNode)
 	{
 	{
 		$sText = '';
 		$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;
 		return $sText;
@@ -281,17 +287,20 @@ EOF;
 		if ($oItems)
 		if ($oItems)
 		{
 		{
 			$res = array();
 			$res = array();
-			foreach($oItems->childNodes as $oItem)
+			if ($oItems->hasChildNodes())
 			{
 			{
-				// When an attribute is msising
-				if ($oItem->hasAttribute('key'))
-				{
-					$key = $oItem->getAttribute('key');
-					$res[$key] = $this->GetNodeAsArrayOfItems($oItem);
-				}
-				else
+				foreach($oItems->childNodes as $oItem)
 				{
 				{
-					$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'";
 			$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
 		// Finalize class params declaration
 		//
 		//
 		$aClassParamsPHP = array();
 		$aClassParamsPHP = array();