diff --git a/application/applicationcontext.class.inc.php b/application/applicationcontext.class.inc.php index a82553ef3..1f0fe8e94 100644 --- a/application/applicationcontext.class.inc.php +++ b/application/applicationcontext.class.inc.php @@ -118,7 +118,7 @@ class ApplicationContext $oSearchFilter = new DBObjectSearch('Organization'); $oSearchFilter->SetModifierProperty('UserRightsGetSelectFilter', 'bSearchMode', true); $oSet = new CMDBObjectSet($oSearchFilter); - $iCount = $oSet->Count(); + $iCount = $oSet->CountWithLimit(2); if ($iCount == 1) { // Only one possible value for org_id, set it in the context diff --git a/application/cmdbabstract.class.inc.php b/application/cmdbabstract.class.inc.php index 2d8b548ad..a2a7e2654 100644 --- a/application/cmdbabstract.class.inc.php +++ b/application/cmdbabstract.class.inc.php @@ -1768,11 +1768,11 @@ EOF case 'LinkedSet': if ($oAttDef->IsIndirect()) { - $oWidget = new UILinksWidget($sClass, $sAttCode, $iId, $sNameSuffix, $oAttDef->DuplicatesAllowed(), $aArgs); + $oWidget = new UILinksWidget($sClass, $sAttCode, $iId, $sNameSuffix, $oAttDef->DuplicatesAllowed()); } else { - $oWidget = new UILinksWidgetDirect($sClass, $sAttCode, $iId, $sNameSuffix, $aArgs); + $oWidget = new UILinksWidgetDirect($sClass, $sAttCode, $iId, $sNameSuffix); } $aEventsList[] ='validate'; $aEventsList[] ='change'; @@ -2045,7 +2045,7 @@ EOF $LockEnabled = MetaModel::GetConfig()->Get('concurrent_lock_enabled'); if ($LockEnabled) { - $sOwnershipToken = utils::ReadPostedParam('ownership_token', null, false, 'raw_data'); + $sOwnershipToken = utils::ReadPostedParam('ownership_token', null, 'raw_data'); if ($sOwnershipToken !== null) { // We're probably inside something like "apply_modify" where the validation failed and we must prompt the user again to edit the object @@ -2357,8 +2357,9 @@ EOF { if ($oAttDef->IsExternalKey()) { + /** @var DBObjectSet $oAllowedValues */ $oAllowedValues = MetaModel::GetAllowedValuesAsObjectSet($sClass, $sAttCode, $aArgs); - if ($oAllowedValues->Count() == 1) + if ($oAllowedValues->CountWithLimit(2) == 1) { $oRemoteObj = $oAllowedValues->Fetch(); $oObj->Set($sAttCode, $oRemoteObj->GetKey()); @@ -2395,7 +2396,7 @@ EOF $sOwnershipToken = null; if ($LockEnabled) { - $sOwnershipToken = utils::ReadPostedParam('ownership_token', null, false, 'raw_data'); + $sOwnershipToken = utils::ReadPostedParam('ownership_token', null, 'raw_data'); $aLockInfo = iTopOwnershipLock::AcquireLock($sClass, $iKey); if ($aLockInfo['success']) { @@ -2476,8 +2477,9 @@ EOF { if ($oAttDef->IsExternalKey()) { + /** @var DBObjectSet $oAllowedValues */ $oAllowedValues = MetaModel::GetAllowedValuesAsObjectSet($sClass, $sAttCode, $aArgs, '', $this->Get($sAttCode)); - if ($oAllowedValues->Count() == 1) + if ($oAllowedValues->CountWithLimit(2) == 1) { $oRemoteObj = $oAllowedValues->Fetch(); $this->Set($sAttCode, $oRemoteObj->GetKey()); @@ -3883,7 +3885,7 @@ EOF $bResult = (count($aErrors) == 0); if ($bResult) { - list($bResult, $aErrors) = $oObj->CheckToWrite(true /* Enforce Read-only fields */); + list($bResult, $aErrors) = $oObj->CheckToWrite(); } if ($bPreview) { diff --git a/application/displayblock.class.inc.php b/application/displayblock.class.inc.php index 98caa513c..26aba4b25 100644 --- a/application/displayblock.class.inc.php +++ b/application/displayblock.class.inc.php @@ -594,7 +594,7 @@ class DisplayBlock } if (count($aAuthorizedClasses) > 0) { - if($this->m_oSet->Count() > 0) + if($this->m_oSet->CountWithLimit(1) > 0) { $sHtml .= cmdbAbstractObject::GetDisplayExtendedSet($oPage, $this->m_oSet, $aExtraParams); } @@ -613,7 +613,7 @@ class DisplayBlock else { // The list is made of only 1 class of objects, actions on the list are possible - if ( ($this->m_oSet->Count()> 0) && (UserRights::IsActionAllowed($this->m_oSet->GetClass(), UR_ACTION_READ, $this->m_oSet) == UR_ALLOWED_YES) ) + if ( ($this->m_oSet->CountWithLimit(1)> 0) && (UserRights::IsActionAllowed($this->m_oSet->GetClass(), UR_ACTION_READ, $this->m_oSet) == UR_ALLOWED_YES) ) { $sHtml .= cmdbAbstractObject::GetDisplaySet($oPage, $this->m_oSet, $aExtraParams); } @@ -668,7 +668,7 @@ class DisplayBlock case 'links': //$bDashboardMode = isset($aExtraParams['dashboard']) ? ($aExtraParams['dashboard'] == 'true') : false; //$bSelectMode = isset($aExtraParams['select']) ? ($aExtraParams['select'] == 'true') : false; - if ( ($this->m_oSet->Count()> 0) && (UserRights::IsActionAllowed($this->m_oSet->GetClass(), UR_ACTION_READ, $this->m_oSet) == UR_ALLOWED_YES) ) + if ( ($this->m_oSet->CountWithLimit(1) > 0) && (UserRights::IsActionAllowed($this->m_oSet->GetClass(), UR_ACTION_READ, $this->m_oSet) == UR_ALLOWED_YES) ) { //$sLinkage = isset($aExtraParams['linkage']) ? $aExtraParams['linkage'] : ''; $sHtml .= cmdbAbstractObject::GetDisplaySet($oPage, $this->m_oSet, $aExtraParams); @@ -1702,7 +1702,7 @@ class MenuBlock extends DisplayBlock // Do not perform time consuming computations if there are too may objects in the list $iLimit = MetaModel::GetConfig()->Get('complex_actions_limit'); - if ((count($aStates) > 0) && (($iLimit == 0) || ($oSet->Count() < $iLimit))) + if ((count($aStates) > 0) && (($iLimit == 0) || ($oSet->CountWithLimit($iLimit + 1) < $iLimit))) { // Life cycle actions may be available... if all objects are in the same state // diff --git a/core/dbobject.class.php b/core/dbobject.class.php index 598fed8f7..cc55628e8 100644 --- a/core/dbobject.class.php +++ b/core/dbobject.class.php @@ -2939,7 +2939,7 @@ abstract class DBObject implements iDisplay $oSearch->AllowAllData(); } $oSet = new CMDBObjectSet($oSearch); - if ($oSet->Count() > 0) + if ($oSet->CountExceeds(0)) { $aDependentObjects[$sRemoteClass][$sExtKeyAttCode] = array( 'attribute' => $oExtKeyAttDef, diff --git a/core/dbobjectset.class.php b/core/dbobjectset.class.php index 8c6fecc6e..1d88f52f5 100644 --- a/core/dbobjectset.class.php +++ b/core/dbobjectset.class.php @@ -570,7 +570,7 @@ class DBObjectSet implements iDBObjectSetIterator $aAttributes = array(); foreach ($aAliases as $sAlias => $bClassDirection) { - foreach (MetaModel::GetOrderByDefault($this->m_oFilter->GetClass($sAlias)) as $sAttCode => $bAttributeDirection) + foreach (MetaModel::GetOrderByDefault($this->m_oFilter->GetClass()) as $sAttCode => $bAttributeDirection) { $bDirection = $bClassDirection ? $bAttributeDirection : !$bAttributeDirection; $aAttributes[$sAlias.'.'.$sAttCode] = $bDirection; @@ -728,16 +728,72 @@ class DBObjectSet implements iDBObjectSetIterator return $this->m_iNumTotalDBRows + count($this->m_aAddedObjects); // Does it fix Trac #887 ?? } + /** Check if the count exceeds a given limit + * @param $iLimit + * + * @return bool + * @throws \CoreException + * @throws \MissingQueryArgument + * @throws \MySQLException + * @throws \MySQLHasGoneAwayException + */ public function CountExceeds($iLimit) { - $sSQL = $this->m_oFilter->MakeSelectQuery(array(), $this->m_aArgs, null, null, $iLimit + 2, 0, true); - $resQuery = CMDBSource::Query($sSQL); - if (!$resQuery) return (0 > $iLimit); + if (is_null($this->m_iNumTotalDBRows)) + { + $sSQL = $this->m_oFilter->MakeSelectQuery(array(), $this->m_aArgs, null, null, $iLimit + 2, 0, true); + $resQuery = CMDBSource::Query($sSQL); + if ($resQuery) + { + $aRow = CMDBSource::FetchArray($resQuery); + CMDBSource::FreeResult($resQuery); + $iCount = intval($aRow['COUNT']); + } + else + { + $iCount = 0; + } + } + else + { + $iCount = $this->m_iNumTotalDBRows; + } - $aRow = CMDBSource::FetchArray($resQuery); - CMDBSource::FreeResult($resQuery); + return ($iCount > $iLimit); + } - return (intval($aRow['COUNT']) > $iLimit); + /** Count only up to the given limit + * @param $iLimit + * + * @return int + * @throws \CoreException + * @throws \MissingQueryArgument + * @throws \MySQLException + * @throws \MySQLHasGoneAwayException + */ + public function CountWithLimit($iLimit) + { + if (is_null($this->m_iNumTotalDBRows)) + { + $sSQL = $this->m_oFilter->MakeSelectQuery(array(), $this->m_aArgs, null, null, $iLimit + 2, 0, true); + $resQuery = CMDBSource::Query($sSQL); + if ($resQuery) + { + $aRow = CMDBSource::FetchArray($resQuery); + CMDBSource::FreeResult($resQuery); + $iCount = intval($aRow['COUNT']); + } + else + { + $iCount = 0; + } + } + else + { + $iCount = $this->m_iNumTotalDBRows; + } + + return $iCount; } /**