public function providerParseExpressionWithVariables()
 {
     $variables = array(1 => 'test', 2 => 'test2', 3 => 5, 4 => 10);
     $field = new FieldExpression('field');
     $variable = new ScalarExpression('test');
     $variable2 = new ScalarExpression('test2');
     $maxFieldFunction = new FunctionExpression(FunctionExpression::FUNCTION_MAX);
     $maxFieldFunction->addArgument($variable2);
     $minFieldFunction = new FunctionExpression(FunctionExpression::FUNCTION_MIN, 'mf');
     $minFieldFunction->addArgument($field);
     $minFieldFunction->addArgument($variable);
     $minFieldFunction2 = new FunctionExpression(FunctionExpression::FUNCTION_MIN, 'mf');
     $minFieldFunction2->addArgument($field);
     $minFieldFunction2->addArgument($variable);
     $minFieldFunction2->addArgument($maxFieldFunction);
     $simpleMathematic = new MathematicalExpression();
     $simpleMathematic->addExpression(new ScalarExpression(5));
     $simpleMathematic->addExpression(new ScalarExpression(10), '+');
     $mediumMathematic = new MathematicalExpression();
     $mediumMathematic->addExpression(new ScalarExpression(5));
     $mediumMathematic->addExpression($simpleMathematic, '/');
     $minFunction = new FunctionExpression(FunctionExpression::FUNCTION_MIN);
     $minFunction->addArgument(new ScalarExpression(5));
     $maxFunction = new FunctionExpression(FunctionExpression::FUNCTION_MAX);
     $maxFunction->addArgument($simpleMathematic);
     $functionMathematic = new MathematicalExpression();
     $functionMathematic->addExpression($minFunction);
     $functionMathematic->addExpression($maxFunction);
     return array(array(new ScalarExpression('This is a test sentence'), '"This is a %1% sentence"', $variables), array(new ScalarExpression('test'), '%1%', $variables), array($maxFieldFunction, 'MAX(%2%)', $variables), array($minFieldFunction, 'MIN({field}, %1%) AS mf', $variables), array($minFieldFunction2, 'MIN({field}, %1%, MAX(%2%)) AS mf', $variables), array($simpleMathematic, '%3% + %4%', $variables), array($mediumMathematic, '%3% / (%3% + %4%)', $variables), array($functionMathematic, 'MIN(%3%) + MAX(%3% + %4%)', $variables));
 }
 private function parseMathematical($value)
 {
     $mathematicalOperators = $this->getMathematicalOperators();
     $hasMathematicalOperator = false;
     foreach ($mathematicalOperators as $mathematicalOperator) {
         $mathematicalOperatorPosition = strpos($value, ' ' . $mathematicalOperator . ' ');
         if ($mathematicalOperatorPosition !== false) {
             $hasMathematicalOperator = true;
             break;
         }
     }
     if (!$hasMathematicalOperator) {
         return false;
     }
     if (strpos($value, ' AS ') !== false) {
         list($value, $alias) = explode(' AS ', $value);
     } else {
         $alias = null;
     }
     $tokens = $this->mathematicTokenizer->tokenize($value);
     if (count($tokens) == 1) {
         return false;
     }
     $mathematicalExpression = new MathematicalExpression($alias);
     $currentExpression = '';
     $mathematicalOperator = null;
     foreach ($tokens as $token) {
         if (in_array($token, $mathematicalOperators)) {
             $mathematicalOperator = $token;
             continue;
         }
         $expression = $this->parseExpression($token);
         $mathematicalExpression->addExpression($expression, $mathematicalOperator);
     }
     return $mathematicalExpression;
 }
 /**
  * Create the SQL of a mathematical expression
  * @param zibo\library\database\manipulation\expression\MathematicalExpression $expression
  * @param boolean $useAlias
  * @return string SQL of the mathematical expression
  */
 protected function parseMathematicalExpression(MathematicalExpression $expression, $useAlias = true)
 {
     $parts = $expression->getParts();
     $sql = '';
     foreach ($parts as $part) {
         if ($sql) {
             $sql .= ' ' . $part->getOperator() . ' ';
         }
         $sql .= $this->parseExpression($part->getExpression(), false);
     }
     return '(' . $sql . ')';
 }
Beispiel #4
0
 /**
  * Copies a node
  * @param integer|Node $node Id of the node or the node to copy
  * @param boolean $recursive Set to true to also copy the children of the node
  * @param boolean $reorder Set to false to just copy the order index instead of adding the copied node after the source node
  * @param boolean $keepOriginalName Set to true to keep the name untouched, else a suffix like (copy) or (copy 2, 3 ...) will be added to the name of the copy
  * @param boolean $copyRoutes Set to true to copy the routes of the nodes. This will only work when copying a root node, else a validation error will occur
  * @param boolean $resetCopyTable Set to false for recursive copying
  * @param boolean $newParent Provide a new parent for the copy, needed for recursive copying
  * @return null
  */
 public function copy($node, $recursive = true, $reorder = true, $keepOriginalName = false, $copyRoutes = false, $resetCopyTable = true, $newParent = null)
 {
     $id = $this->getPrimaryKey($node);
     $node = $this->getNode($id, 1);
     if ($resetCopyTable) {
         $this->copyTable = array();
     }
     $copy = $this->createData();
     $nameSuffix = '';
     if (!$keepOriginalName) {
         $nameSuffix = $this->getNameSuffixForCopiedNode($node->parent, $node->name, $node->dataLocale);
     }
     $copy->name = $node->name . $nameSuffix;
     $copy->type = $node->type;
     if ($newParent) {
         $copy->parent = $newParent;
     } else {
         $copy->parent = $node->parent;
     }
     if ($reorder) {
         $copy->orderIndex = $node->orderIndex + 1;
     } else {
         $copy->orderIndex = $node->orderIndex;
     }
     if ($copyRoutes) {
         $copy->route = $node->route;
     }
     $isTransactionStarted = $this->startTransaction();
     try {
         $this->copyNodeSettings($node, $copy);
         $this->save($copy);
         $localizedModel = $this->meta->getLocalizedModel();
         $query = $localizedModel->createQuery();
         $query->addCondition('{dataId} = %1%', $node->id);
         $localizedNodes = $query->query();
         foreach ($localizedNodes as $localizedNode) {
             $nameSuffix = '';
             if (!$keepOriginalName) {
                 $nameSuffix = $this->getNameSuffixForCopiedNode($node->parent, $localizedNode->name, $localizedNode->dataLocale);
             }
             $copyLocalizedNode = $this->createData(false);
             $copyLocalizedNode->id = $copy->id;
             $copyLocalizedNode->name = $localizedNode->name . $nameSuffix;
             if ($copyRoutes) {
                 $copyLocalizedNode->route = $localizedNode->route;
             }
             $copyLocalizedNode->dataLocale = $localizedNode->dataLocale;
             $this->save($copyLocalizedNode);
         }
         if ($reorder) {
             $orderFieldExpression = new FieldExpression('orderIndex');
             $mathExpression = new MathematicalExpression();
             $mathExpression->addExpression($orderFieldExpression);
             $mathExpression->addExpression(new ScalarExpression(1));
             $idCondition = new SimpleCondition(new FieldExpression('id'), new ScalarExpression($copy->id), Condition::OPERATOR_NOT_EQUALS);
             $parentCondition = new SimpleCondition(new FieldExpression('parent'), new ScalarExpression($copy->parent), Condition::OPERATOR_EQUALS);
             $greaterCondition = new SimpleCondition($orderFieldExpression, new ScalarExpression($copy->orderIndex), Condition::OPERATOR_GREATER_OR_EQUALS);
             $condition = new NestedCondition();
             $condition->addCondition($idCondition);
             $condition->addCondition($parentCondition);
             $condition->addCondition($greaterCondition);
             $updateStatement = new UpdateStatement();
             $updateStatement->addTable(new TableExpression($this->getName()));
             $updateStatement->addValue($orderFieldExpression, $mathExpression);
             $updateStatement->addCondition($condition);
             $this->executeStatement($updateStatement);
         }
         if ($recursive) {
             $query = $this->createQuery();
             $query->setFields('{id}, {parent}, {orderIndex}');
             $query->addCondition('{parent} = \'' . $node->getPath() . '\'');
             // when copying a site, the path is numeric and won't be escaped by the orm
             $query->addOrderBy('{orderIndex} ASC');
             $children = $query->query();
             $path = $copy->getPath();
             foreach ($children as $child) {
                 $childCopy = $this->copy($child->id, true, false, true, $copyRoutes, false, $path);
             }
         }
         $this->copyTable[$id] = $copy->id;
         $this->commitTransaction($isTransactionStarted);
     } catch (Exception $exception) {
         $this->rollbackTransaction($isTransactionStarted);
         throw $exception;
     }
     return $copy;
 }