Exemple #1
0
 protected function prepareJoinReference($reference, $alias_this, $alias_ref, $baseDefinition, $refDefenition, $isBackReference)
 {
     $new = array();
     foreach ($reference as $k => $v) {
         if ($k === 'LOGIC') {
             $new[$k] = $v;
             continue;
         }
         if (is_numeric($k)) {
             // subfilter, recursive call
             $new[$k] = $this->prepareJoinReference($v, $alias_this, $alias_ref, $baseDefinition, $refDefenition, $isBackReference);
         } else {
             // key
             $csw_result = \CSQLWhere::makeOperation($k);
             list($field, $operation) = array_values($csw_result);
             if (strpos($field, 'this.') === 0) {
                 // parse the chain
                 $definition = str_replace(\CSQLWhere::getOperationByCode($operation) . 'this.', '', $k);
                 $absDefinition = strlen($baseDefinition) ? $baseDefinition . '.' . $definition : $definition;
                 $chain = $this->getRegisteredChain($absDefinition, true);
                 if (!$isBackReference) {
                     // make sure these fields will be joined bebore the main join
                     $this->buildJoinMap(array($chain));
                 } else {
                     $chain->getLastElement()->setParameter('talias', $alias_this);
                 }
                 // recursively collect all "build_from" fields
                 if ($chain->getLastElement()->getValue() instanceof ExpressionField) {
                     $this->collectExprChains($chain);
                     $this->buildJoinMap($chain->getLastElement()->getValue()->getBuildFromChains());
                 }
                 $k = \CSQLWhere::getOperationByCode($operation) . $chain->getSqlDefinition();
             } elseif (strpos($field, 'ref.') === 0) {
                 $definition = str_replace(\CSQLWhere::getOperationByCode($operation) . 'ref.', '', $k);
                 $absDefinition = strlen($refDefenition) ? $refDefenition . '.' . $definition : $definition;
                 $chain = $this->getRegisteredChain($absDefinition, true);
                 if ($isBackReference) {
                     // make sure these fields will be joined bebore the main join
                     $this->buildJoinMap(array($chain));
                 } else {
                     $chain->getLastElement()->setParameter('talias', $alias_ref);
                 }
                 // recursively collect all "build_from" fields
                 if ($chain->getLastElement()->getValue() instanceof ExpressionField) {
                     $this->collectExprChains($chain);
                     $this->buildJoinMap($chain->getLastElement()->getValue()->getBuildFromChains());
                 }
                 $k = \CSQLWhere::getOperationByCode($operation) . $chain->getSqlDefinition();
             } else {
                 throw new Main\SystemException(sprintf('Unknown reference key `%s`', $k));
             }
             // value
             if (is_array($v)) {
                 // field = expression
                 $v = new \CSQLWhereExpression($v[0], array_slice($v, 1));
             } elseif ($v instanceof Main\DB\SqlExpression) {
                 // it's ok, nothing to do
             } elseif (!is_object($v)) {
                 if (strpos($v, 'this.') === 0) {
                     $definition = str_replace('this.', '', $v);
                     $absDefinition = strlen($baseDefinition) ? $baseDefinition . '.' . $definition : $definition;
                     $chain = $this->getRegisteredChain($absDefinition, true);
                     if (!$isBackReference) {
                         // make sure these fields will be joined bebore the main join
                         $this->buildJoinMap(array($chain));
                     } else {
                         $chain->getLastElement()->setParameter('talias', $alias_this);
                     }
                     // recursively collect all "build_from" fields
                     if ($chain->getLastElement()->getValue() instanceof ExpressionField) {
                         $this->collectExprChains($chain);
                         $this->buildJoinMap($chain->getLastElement()->getValue()->getBuildFromChains());
                     }
                     $field_def = $chain->getSqlDefinition();
                 } elseif (strpos($v, 'ref.') === 0) {
                     $definition = str_replace('ref.', '', $v);
                     $absDefinition = strlen($refDefenition) ? $refDefenition . '.' . $definition : $definition;
                     $chain = $this->getRegisteredChain($absDefinition, true);
                     if ($isBackReference) {
                         // make sure these fields will be joined bebore the main join
                         $this->buildJoinMap(array($chain));
                     } else {
                         $chain->getLastElement()->setParameter('talias', $alias_ref);
                     }
                     $this->buildJoinMap(array($chain));
                     // recursively collect all "build_from" fields
                     if ($chain->getLastElement()->getValue() instanceof ExpressionField) {
                         $this->collectExprChains($chain);
                         $this->buildJoinMap($chain->getLastElement()->getValue()->getBuildFromChains());
                     }
                     $field_def = $chain->getSqlDefinition();
                 } else {
                     throw new Main\SystemException(sprintf('Unknown reference value `%s`', $v));
                 }
                 $v = new \CSQLWhereExpression('?#', $field_def);
             } else {
                 throw new Main\SystemException(sprintf('Unknown reference value `%s`', $v));
             }
             $new[$k] = $v;
         }
     }
     return $new;
 }