Exemplo n.º 1
0
/**
 * Adds the context parameters to the audit query
 */
function FilterByContext(DBSearch &$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);
                    }
                }
            }
        }
    }
}
Exemplo n.º 2
0
 /**
  * Create a new DBObjectSearch from $oSearch with a new alias $sAlias
  *
  * Note : This has not be tested with UNION queries.
  *
  * @param DBSearch $oSearch
  * @param string $sAlias
  * @return DBObjectSearch
  */
 public static function CloneWithAlias(DBSearch $oSearch, $sAlias)
 {
     $oSearchWithAlias = new DBObjectSearch($oSearch->GetClass(), $sAlias);
     $oSearchWithAlias = $oSearchWithAlias->Intersect($oSearch);
     return $oSearchWithAlias;
 }
 protected function AddCondition_ReferencedBy_InNameSpace(DBSearch $oFilter, $sForeignExtKeyAttCode, &$aClassAliases, &$aAliasTranslation)
 {
     $sForeignClass = $oFilter->GetClass();
     // Find the node on which the new tree must be attached (most of the time it is "this")
     $oReceivingFilter = $this->GetNode($this->GetClassAlias());
     if (array_key_exists($sForeignClass, $this->m_aReferencedBy) && array_key_exists($sForeignExtKeyAttCode, $this->m_aReferencedBy[$sForeignClass])) {
         $oReceivingFilter->m_aReferencedBy[$sForeignClass][$sForeignExtKeyAttCode]->MergeWith_InNamespace($oFilter, $aClassAliases, $aAliasTranslation);
     } else {
         $oFilter->AddToNamespace($aClassAliases, $aAliasTranslation);
         // #@# The condition expression found in that filter should not be used - could be another kind of structure like a join spec tree !!!!
         //$oNewFilter = $oFilter->DeepClone();
         //$oNewFilter->ResetCondition();
         $oReceivingFilter->m_aReferencedBy[$sForeignClass][$sForeignExtKeyAttCode] = $oFilter;
     }
 }
 /**
  * Initializes the default search parameters based on 1) a 'current' object and 2) the silos defined by the context
  * @param DBObject $oSourceObj
  * @param DBSearch $oSearch
  */
 protected function SetSearchDefaultFromContext($oSourceObj, &$oSearch)
 {
     $oAppContext = new ApplicationContext();
     $sSrcClass = get_class($oSourceObj);
     $sDestClass = $oSearch->GetClass();
     foreach ($oAppContext->GetNames() as $key) {
         // Find the value of the object corresponding to each 'context' parameter
         $aCallSpec = array($sSrcClass, 'MapContextParam');
         $sAttCode = '';
         if (is_callable($aCallSpec)) {
             $sAttCode = call_user_func($aCallSpec, $key);
             // Returns null when there is no mapping for this parameter
         }
         if (MetaModel::IsValidAttCode($sSrcClass, $sAttCode)) {
             $oAttDef = MetaModel::GetAttributeDef($sSrcClass, $sAttCode);
             $defaultValue = $oSourceObj->Get($sAttCode);
             // Find the attcode for the same 'context' parameter in the destination class
             // and sets its value as the default value for the search condition
             $aCallSpec = array($sDestClass, 'MapContextParam');
             $sAttCode = '';
             if (is_callable($aCallSpec)) {
                 $sAttCode = call_user_func($aCallSpec, $key);
                 // Returns null when there is no mapping for this parameter
             }
             if (MetaModel::IsValidAttCode($sDestClass, $sAttCode) && !empty($defaultValue)) {
                 $oSearch->AddCondition($sAttCode, $defaultValue);
             }
         }
     }
 }