Explorar o código

N.1108 return exception if $bMustBeFound and result is archived

git-svn-id: http://svn.code.sf.net/p/itop/code/trunk@5025 a333f486-631f-4898-b8df-5754b55c2be0
pgoiffon %!s(int64=7) %!d(string=hai) anos
pai
achega
dc1cda03ef
Modificáronse 3 ficheiros con 74 adicións e 15 borrados
  1. 10 0
      application/utils.inc.php
  2. 9 0
      core/coreexception.class.inc.php
  3. 55 15
      core/metamodel.class.php

+ 10 - 0
application/utils.inc.php

@@ -141,6 +141,9 @@ class utils
 	}
 	}
 
 
 	protected static $bPageMode = null;
 	protected static $bPageMode = null;
+	/**
+	 * @var boolean[]
+	 */
 	protected static $aModes = array();
 	protected static $aModes = array();
 
 
 	public static function InitArchiveMode()
 	public static function InitArchiveMode()
@@ -164,6 +167,10 @@ class utils
 		self::$bPageMode = ($iCurrent == 1);
 		self::$bPageMode = ($iCurrent == 1);
 	}
 	}
 
 
+	/**
+	 * @param boolean $bMode if true then activate archive mode (archived objects are visible), otherwise archived objects are
+	 *     hidden (archive = "soft deletion")
+	 */
 	public static function PushArchiveMode($bMode)
 	public static function PushArchiveMode($bMode)
 	{
 	{
 		array_push(self::$aModes, $bMode);
 		array_push(self::$aModes, $bMode);
@@ -174,6 +181,9 @@ class utils
 		array_pop(self::$aModes);
 		array_pop(self::$aModes);
 	}
 	}
 
 
+	/**
+	 * @return boolean true if archive mode is enabled
+	 */
 	public static function IsArchiveMode()
 	public static function IsArchiveMode()
 	{
 	{
 		if (count(self::$aModes) > 0)
 		if (count(self::$aModes) > 0)

+ 9 - 0
core/coreexception.class.inc.php

@@ -112,4 +112,13 @@ class SecurityException extends CoreException
 {
 {
 }
 }
 
 
+/**
+ * Throwned when querying on an object that exists in the database but is archived
+ *
+ * @see N.1108
+ */
+class ArchivedObjectException extends CoreException
+{
+}
+
 ?>
 ?>

+ 55 - 15
core/metamodel.class.php

@@ -4941,8 +4941,10 @@ abstract class MetaModel
 	 * @param bool $bAllowAllData if true then no rights filtering
 	 * @param bool $bAllowAllData if true then no rights filtering
 	 * @param array $aModifierProperties
 	 * @param array $aModifierProperties
 	 *
 	 *
-	 * @return DBObject|null
+	 * @return string[] column name / value array
 	 * @throws CoreException if no result found and $bMustBeFound=true
 	 * @throws CoreException if no result found and $bMustBeFound=true
+	 *
+	 * @see utils::PushArchiveMode() to enable search on archived objects
 	 */
 	 */
 	public static function MakeSingleRow($sClass, $iKey, $bMustBeFound = true, $bAllowAllData = false, $aModifierProperties = null)
 	public static function MakeSingleRow($sClass, $iKey, $bMustBeFound = true, $bAllowAllData = false, $aModifierProperties = null)
 	{
 	{
@@ -5000,13 +5002,27 @@ abstract class MetaModel
 
 
 		$aRow = CMDBSource::FetchArray($res);
 		$aRow = CMDBSource::FetchArray($res);
 		CMDBSource::FreeResult($res);
 		CMDBSource::FreeResult($res);
+
 		if ($bMustBeFound && empty($aRow))
 		if ($bMustBeFound && empty($aRow))
 		{
 		{
 			throw new CoreException("No result for the single row query: '$sSQL'");
 			throw new CoreException("No result for the single row query: '$sSQL'");
 		}
 		}
+
 		return $aRow;
 		return $aRow;
 	}
 	}
 
 
+	/**
+	 * Converts a column name / value array to a {@link DBObject}
+	 *
+	 * @param string $sClass
+	 * @param string[] $aRow column name / value array
+	 * @param string $sClassAlias
+	 * @param string[] $aAttToLoad
+	 * @param array $aExtendedDataSpec
+	 *
+	 * @return DBObject
+	 * @throws CoreException if finalClass cannot be found
+	 */
 	public static function GetObjectByRow($sClass, $aRow, $sClassAlias = '', $aAttToLoad = null, $aExtendedDataSpec = null)
 	public static function GetObjectByRow($sClass, $aRow, $sClassAlias = '', $aAttToLoad = null, $aExtendedDataSpec = null)
 	{
 	{
 		self::_check_subclass($sClass);
 		self::_check_subclass($sClass);
@@ -5040,31 +5056,43 @@ abstract class MetaModel
 	}
 	}
 
 
 	/**
 	/**
-	 * Search for the specified class and id. <strong>Warning</strong>. If the object is archived then null is
-	 * returned.
+	 * Search for the specified class and id.
 	 *
 	 *
-	 * @param $sClass
+	 * @param string $sClass
 	 * @param int $iKey id value of the object to retrieve
 	 * @param int $iKey id value of the object to retrieve
 	 * @param bool $bMustBeFound
 	 * @param bool $bMustBeFound
 	 * @param bool $bAllowAllData if true then no rights filtering
 	 * @param bool $bAllowAllData if true then no rights filtering
 	 * @param null $aModifierProperties
 	 * @param null $aModifierProperties
 	 *
 	 *
-	 * @return DBObject|null
+	 * @return DBObject|null null if : (the object is not found) or (archive mode disabled and object is archived and $bMustBeFound=false)
 	 * @throws CoreException if no result found and $bMustBeFound=true
 	 * @throws CoreException if no result found and $bMustBeFound=true
+	 * @throws ArchivedObjectException if archive mode disabled and result is archived and $bMustBeFound=true
 	 *
 	 *
-	 * @since 2.4 introduction of the archive functionalities, and creation of this method.
-	 *     {@link MetaModel::GetObjectWithArchive} remains as a default behavior for modules (see N.1108).
-	 * @see MetaModel::GetObjectWithArchive to get also archived object
+	 * @see MetaModel::GetObjectWithArchive to get object even if it's archived
+	 * @see utils::PushArchiveMode() to enable search on archived objects
 	 */
 	 */
 	public static function GetObject($sClass, $iKey, $bMustBeFound = true, $bAllowAllData = false, $aModifierProperties = null)
 	public static function GetObject($sClass, $iKey, $bMustBeFound = true, $bAllowAllData = false, $aModifierProperties = null)
 	{
 	{
-		self::_check_subclass($sClass);
-		$aRow = self::MakeSingleRow($sClass, $iKey, $bMustBeFound, $bAllowAllData, $aModifierProperties);
-		if (empty($aRow))
+		$oObject = self::GetObjectWithArchive($sClass, $iKey, $bMustBeFound, $bAllowAllData, $aModifierProperties);
+
+		if (empty($oObject))
 		{
 		{
 			return null;
 			return null;
 		}
 		}
-		return self::GetObjectByRow($sClass, $aRow);
+
+		if (!utils::IsArchiveMode() && $oObject->IsArchived())
+		{
+			if ($bMustBeFound)
+			{
+				throw new ArchivedObjectException("The object $sClass::$iKey is archived");
+			}
+			else
+			{
+				return null;
+			}
+		}
+
+		return $oObject;
 	}
 	}
 
 
 	/**
 	/**
@@ -5078,14 +5106,26 @@ abstract class MetaModel
 	 * @param array $aModifierProperties
 	 * @param array $aModifierProperties
 	 *
 	 *
 	 * @return DBObject|null
 	 * @return DBObject|null
-	 * @see MetaModel::GetObject()
+	 * @throws CoreException if no result found and $bMustBeFound=true
+	 *
+	 * @since 2.4 introduction of the archive functionalities
+	 *
+	 * @see MetaModel::GetObject() same but returns null or ArchivedObjectFoundException if object exists but is archived
 	 */
 	 */
 	public static function GetObjectWithArchive($sClass, $iKey, $bMustBeFound = true, $bAllowAllData = false, $aModifierProperties = null)
 	public static function GetObjectWithArchive($sClass, $iKey, $bMustBeFound = true, $bAllowAllData = false, $aModifierProperties = null)
 	{
 	{
+		self::_check_subclass($sClass);
+
 		utils::PushArchiveMode(true);
 		utils::PushArchiveMode(true);
-		$oObject = static::GetObject($sClass, $iKey, $bMustBeFound, $bAllowAllData, $aModifierProperties);
+		$aRow = self::MakeSingleRow($sClass, $iKey, $bMustBeFound, $bAllowAllData, $aModifierProperties);
 		utils::PopArchiveMode();
 		utils::PopArchiveMode();
-		return $oObject;
+
+		if (empty($aRow))
+		{
+			return null;
+		}
+
+		return self::GetObjectByRow($sClass, $aRow); // null should not be returned, this is handled in the callee
 	}
 	}
 
 
 	public static function GetObjectByName($sClass, $sName, $bMustBeFound = true)
 	public static function GetObjectByName($sClass, $sName, $bMustBeFound = true)