コード例 #1
0
ファイル: audit.php プロジェクト: kira8565/ITOP203-ZHCN
/**
 * Adds the context parameters to the audit query
 */
function FilterByContext(DBObjectSearch &$oFilter, ApplicationContext $oAppContext)
{
    $sObjClass = $oFilter->GetClass();
    $aContextParams = $oAppContext->GetNames();
    $aCallSpec = array($sObjClass, 'MapContextParam');
    if (is_callable($aCallSpec)) {
        foreach ($aContextParams as $sParamName) {
            $sValue = $oAppContext->GetCurrentValue($sParamName, null);
            if ($sValue != null) {
                $sAttCode = call_user_func($aCallSpec, $sParamName);
                // Returns null when there is no mapping for this parameter
                if ($sAttCode != null && MetaModel::IsValidAttCode($sObjClass, $sAttCode)) {
                    // Check if the condition points to a hierarchical key
                    if ($sAttCode == 'id') {
                        // Filtering on the objects themselves
                        $sHierarchicalKeyCode = MetaModel::IsHierarchicalClass($sObjClass);
                        if ($sHierarchicalKeyCode !== false) {
                            $oRootFilter = new DBObjectSearch($sObjClass);
                            $oRootFilter->AddCondition($sAttCode, $sValue);
                            $oFilter->AddCondition_PointingTo($oRootFilter, $sHierarchicalKeyCode, TREE_OPERATOR_BELOW);
                            // Use the 'below' operator by default
                            $bConditionAdded = true;
                        }
                    } else {
                        $oAttDef = MetaModel::GetAttributeDef($sObjClass, $sAttCode);
                        $bConditionAdded = false;
                        if ($oAttDef->IsExternalKey()) {
                            $sHierarchicalKeyCode = MetaModel::IsHierarchicalClass($oAttDef->GetTargetClass());
                            if ($sHierarchicalKeyCode !== false) {
                                $oRootFilter = new DBObjectSearch($oAttDef->GetTargetClass());
                                $oRootFilter->AddCondition('id', $sValue);
                                $oHKFilter = new DBObjectSearch($oAttDef->GetTargetClass());
                                $oHKFilter->AddCondition_PointingTo($oRootFilter, $sHierarchicalKeyCode, TREE_OPERATOR_BELOW);
                                // Use the 'below' operator by default
                                $oFilter->AddCondition_PointingTo($oHKFilter, $sAttCode);
                                $bConditionAdded = true;
                            }
                        }
                    }
                    if (!$bConditionAdded) {
                        $oFilter->AddCondition($sAttCode, $sValue);
                    }
                }
            }
        }
    }
}
コード例 #2
0
 /**
  * Add a condition (restriction) to the current DBSearch on which the display block is based
  * taking into account the hierarchical keys for which the condition is based on the 'below' operator
  */
 protected function AddCondition($sFilterCode, $condition, $sOpCode = null)
 {
     // Workaround to an issue revealed whenever a condition on org_id is applied twice (with a hierarchy of organizations)
     // Moreover, it keeps the query as simple as possible
     if (isset($this->m_aConditions[$sFilterCode]) && $condition == $this->m_aConditions[$sFilterCode]) {
         // Skip
         return;
     }
     $this->m_aConditions[$sFilterCode] = $condition;
     $sClass = $this->m_oFilter->GetClass();
     $bConditionAdded = false;
     // If the condition is an external key with a class having a hierarchy, use a "below" criteria
     if (MetaModel::IsValidAttCode($sClass, $sFilterCode)) {
         $oAttDef = MetaModel::GetAttributeDef($sClass, $sFilterCode);
         if ($oAttDef->IsExternalKey()) {
             $sHierarchicalKeyCode = MetaModel::IsHierarchicalClass($oAttDef->GetTargetClass());
             if ($sHierarchicalKeyCode !== false) {
                 $oFilter = new DBObjectSearch($oAttDef->GetTargetClass());
                 if ($sOpCode == 'IN' && is_array($condition)) {
                     $oFilter->AddConditionExpression(self::GetConditionIN($oFilter, 'id', $condition));
                 } else {
                     $oFilter->AddCondition('id', $condition);
                 }
                 $oHKFilter = new DBObjectSearch($oAttDef->GetTargetClass());
                 $oHKFilter->AddCondition_PointingTo($oFilter, $sHierarchicalKeyCode, TREE_OPERATOR_BELOW);
                 // Use the 'below' operator by default
                 $this->m_oFilter->AddCondition_PointingTo($oHKFilter, $sFilterCode);
                 $bConditionAdded = true;
             } else {
                 if ($sOpCode == 'IN' && is_array($condition)) {
                     $this->m_oFilter->AddConditionExpression(self::GetConditionIN($this->m_oFilter, $sFilterCode, $condition));
                     $bConditionAdded = true;
                 }
             }
         } else {
             if ($sOpCode == 'IN' && is_array($condition)) {
                 $this->m_oFilter->AddConditionExpression(self::GetConditionIN($this->m_oFilter, $sFilterCode, $condition));
                 $bConditionAdded = true;
             }
         }
     }
     // In all other cases, just add the condition directly
     if (!$bConditionAdded) {
         $this->m_oFilter->AddCondition($sFilterCode, $condition);
         // Use the default 'loose' operator
     }
 }
コード例 #3
0
 public function Reload()
 {
     assert($this->m_bIsInDB);
     $aRow = MetaModel::MakeSingleRow(get_class($this), $this->m_iKey, false);
     if (empty($aRow)) {
         throw new CoreException("Failed to reload object of class '" . get_class($this) . "', id = " . $this->m_iKey);
     }
     $this->FromRow($aRow);
     // Process linked set attributes
     //
     foreach (MetaModel::ListAttributeDefs(get_class($this)) as $sAttCode => $oAttDef) {
         if (!$oAttDef->IsLinkSet()) {
             continue;
         }
         // Load the link information
         $sLinkClass = $oAttDef->GetLinkedClass();
         $sExtKeyToMe = $oAttDef->GetExtKeyToMe();
         // The class to target is not the current class, because if this is a derived class,
         // it may differ from the target class, then things start to become confusing
         $oRemoteExtKeyAtt = MetaModel::GetAttributeDef($sLinkClass, $sExtKeyToMe);
         $sMyClass = $oRemoteExtKeyAtt->GetTargetClass();
         $oMyselfSearch = new DBObjectSearch($sMyClass);
         $oMyselfSearch->AddCondition('id', $this->m_iKey, '=');
         $oLinkSearch = new DBObjectSearch($sLinkClass);
         $oLinkSearch->AddCondition_PointingTo($oMyselfSearch, $sExtKeyToMe);
         $oLinks = new DBObjectSet($oLinkSearch);
         $this->m_aCurrValues[$sAttCode] = $oLinks;
         $this->m_aOrigValues[$sAttCode] = clone $this->m_aCurrValues[$sAttCode];
         $this->m_aLoadedAtt[$sAttCode] = true;
     }
     $this->m_bFullyLoaded = true;
     $this->m_aModifiedAtt = array();
 }