Browse Source

Core: Join any table to an OQL query (prerequisite to Data Exchange)

git-svn-id: http://svn.code.sf.net/p/itop/code/trunk@1061 a333f486-631f-4898-b8df-5754b55c2be0
romainq 14 năm trước cách đây
mục cha
commit
c8ea6adcff

+ 27 - 3
core/dbobject.class.php

@@ -39,6 +39,8 @@ abstract class DBObject
 	private $m_aCurrValues = array();
 	protected $m_aOrigValues = array();
 
+	protected $m_aExtendedData = null;
+
 	private $m_bDirty = false; // Means: "a modification is ongoing"
 										// The object may have incorrect external keys, then any attempt of reload must be avoided
 	private $m_bCheckStatus = null; // Means: the object has been verified and is consistent with integrity rules
@@ -50,11 +52,11 @@ abstract class DBObject
 	private $m_aLoadedAtt = array(); // Compound objects can be partially loaded, array of sAttCode
 
 	// Use the MetaModel::NewObject to build an object (do we have to force it?)
-	public function __construct($aRow = null, $sClassAlias = '')
+	public function __construct($aRow = null, $sClassAlias = '', $aExtendedDataSpec = null)
 	{
 		if (!empty($aRow))
 		{
-			$this->FromRow($aRow, $sClassAlias);
+			$this->FromRow($aRow, $sClassAlias, $aExtendedDataSpec);
 			$this->m_bFullyLoaded = $this->IsFullyLoaded();
 			return;
 		}
@@ -181,7 +183,7 @@ abstract class DBObject
 		$this->m_bFullyLoaded = true;
 	}
 
-	protected function FromRow($aRow, $sClassAlias = '')
+	protected function FromRow($aRow, $sClassAlias = '', $aExtendedDataSpec = null)
 	{
 		if (strlen($sClassAlias) == 0)
 		{
@@ -245,6 +247,20 @@ abstract class DBObject
 				$bFullyLoaded = false;
 			}
 		}
+		
+		// Load extended data
+		if ($aExtendedDataSpec != null)
+		{
+			$aExtendedDataSpec['table'];
+			foreach($aExtendedDataSpec['fields'] as $sColumn)
+			{
+				$sColRef = $sClassAlias.'_extdata_'.$sColumn;
+				if (array_key_exists($sColRef, $aRow))
+				{
+					$this->m_aExtendedData[$sColumn] = $aRow[$sColRef];
+				}
+			}
+		}
 		return $bFullyLoaded;
 	}
 	
@@ -346,6 +362,14 @@ abstract class DBObject
 	}
 
 	/**
+	 * Returns data loaded by the mean of a dynamic and explicit JOIN
+	 */	 
+	public function GetExtendedData()
+	{
+		return $this->m_aExtendedData;
+	}
+
+	/**
 	 * Updates the value of an external field by (re)loading the object
 	 * corresponding to the external key and getting the value from it
 	 * @param string $sAttCode Attribute code of the external field to update

+ 6 - 5
core/dbobjectset.class.php

@@ -38,11 +38,12 @@ class DBObjectSet
 	private $m_aId2Row;
 	private $m_iCurrRow;
 
-	public function __construct(DBObjectSearch $oFilter, $aOrderBy = array(), $aArgs = array(), $iLimitCount = 0, $iLimitStart = 0)
+	public function __construct(DBObjectSearch $oFilter, $aOrderBy = array(), $aArgs = array(), $aExtendedDataSpec = null, $iLimitCount = 0, $iLimitStart = 0)
 	{
 		$this->m_oFilter = $oFilter;
 		$this->m_aOrderBy = $aOrderBy;
 		$this->m_aArgs = $aArgs;
+		$this->m_aExtendedDataSpec = $aExtendedDataSpec;
 		$this->m_iLimitCount = $iLimitCount;
 		$this->m_iLimitStart = $iLimitStart;
 
@@ -241,11 +242,11 @@ class DBObjectSet
 		if ($this->m_bLoaded) return;
 		if ($this->m_iLimitCount > 0)
 		{
-			$sSQL = MetaModel::MakeSelectQuery($this->m_oFilter, $this->m_aOrderBy, $this->m_aArgs, $this->m_iLimitCount, $this->m_iLimitStart);
+			$sSQL = MetaModel::MakeSelectQuery($this->m_oFilter, $this->m_aOrderBy, $this->m_aArgs, $this->m_aExtendedDataSpec, $this->m_iLimitCount, $this->m_iLimitStart);
 		}
 		else
 		{
-			$sSQL = MetaModel::MakeSelectQuery($this->m_oFilter, $this->m_aOrderBy, $this->m_aArgs);
+			$sSQL = MetaModel::MakeSelectQuery($this->m_oFilter, $this->m_aOrderBy, $this->m_aArgs, $this->m_aExtendedDataSpec);
 		}
 		$resQuery = CMDBSource::Query($sSQL);
 		if (!$resQuery) return;
@@ -256,7 +257,7 @@ class DBObjectSet
 			$aObjects = array();
 			foreach ($this->m_oFilter->GetSelectedClasses() as $sClassAlias => $sClass)
 			{
-				$oObject = MetaModel::GetObjectByRow($sClass, $aRow, $sClassAlias);
+				$oObject = MetaModel::GetObjectByRow($sClass, $aRow, $sClassAlias, $this->m_aExtendedDataSpec);
 				$aObjects[$sClassAlias] = $oObject;
 			}
 			$this->AddObjectExtended($aObjects);
@@ -268,7 +269,7 @@ class DBObjectSet
 
 	public function Count()
 	{
-		$sSQL = MetaModel::MakeSelectQuery($this->m_oFilter, $this->m_aOrderBy, $this->m_aArgs, 0, 0, true);
+		$sSQL = MetaModel::MakeSelectQuery($this->m_oFilter, $this->m_aOrderBy, $this->m_aArgs, null, 0, 0, true);
 		$resQuery = CMDBSource::Query($sSQL);
 		if (!$resQuery) return 0;
 

+ 18 - 3
core/metamodel.class.php

@@ -1708,7 +1708,7 @@ abstract class MetaModel
 		return $aScalarArgs;
 	}
 
-	public static function MakeSelectQuery(DBObjectSearch $oFilter, $aOrderBy = array(), $aArgs = array(), $iLimitCount = 0, $iLimitStart = 0, $bGetCount = false)
+	public static function MakeSelectQuery(DBObjectSearch $oFilter, $aOrderBy = array(), $aArgs = array(), $aExtendedDataSpec = null, $iLimitCount = 0, $iLimitStart = 0, $bGetCount = false)
 	{
 		// Hide objects that are not visible to the current user
 		//
@@ -1800,6 +1800,21 @@ abstract class MetaModel
 				$aOrderSpec[$sSelectedAlias."friendlyname"] = true;
 			}
 		}
+
+		// Join to an additional table, if required...
+		//
+		if ($aExtendedDataSpec != null)
+		{
+			$sTableAlias = '_extended_data_';
+			$aExtendedFields = array();
+			foreach($aExtendedDataSpec['fields'] as $sColumn)
+			{
+				$sColRef = $oFilter->GetClassAlias().'_extdata_'.$sColumn;
+				$aExtendedFields[$sColRef] = new FieldExpressionResolved($sColumn, $sTableAlias);;
+			}
+			$oSelectExt = new SQLQuery($aExtendedDataSpec['table'], $sTableAlias, $aExtendedFields);
+			$oSelect->AddInnerJoin($oSelectExt, 'id', $aExtendedDataSpec['join_key'] /*, $sTableAlias*/);
+		}
 		
 		// Go
 		//
@@ -3656,7 +3671,7 @@ abstract class MetaModel
 		return $aRow;
 	}
 
-	public static function GetObjectByRow($sClass, $aRow, $sClassAlias = '')
+	public static function GetObjectByRow($sClass, $aRow, $sClassAlias = '', $aExtendedDataSpec = null)
 	{
 		self::_check_subclass($sClass);	
 
@@ -3685,7 +3700,7 @@ abstract class MetaModel
 			// do the job for the real target class
 			$sClass = $aRow[$sClassAlias."finalclass"];
 		}
-		return new $sClass($aRow, $sClassAlias);
+		return new $sClass($aRow, $sClassAlias, $aExtendedDataSpec);
 	}
 
 	public static function GetObject($sClass, $iKey, $bMustBeFound = true, $bAllowAllData = false)

+ 1 - 1
core/sqlquery.class.inc.php

@@ -46,7 +46,7 @@ class SQLQuery
 	private $m_aValues = array(); // Values to set in case of an update query
 	private $m_aJoinSelects = array();
 
-	public function __construct($sTable, $sTableAlias, $aFields, $aFullTextNeedles, $bToDelete = true, $aValues = array())
+	public function __construct($sTable, $sTableAlias, $aFields, $aFullTextNeedles = array(), $bToDelete = true, $aValues = array())
 	{
 		// This check is not needed but for developping purposes
 		//if (!CMDBSource::IsTable($sTable))

+ 3 - 0
css/light-grey.css

@@ -917,3 +917,6 @@ span.form_validation {
 	width: 95%;
 	-moz-border-radius: 0.5em;
 }
+.wiki_broken_link {
+	text-decoration: line-through;
+}