/** * Parses condition tokens into a nested database condition * @param array $conditionTokens Array with the tokens from the condition tokenizer * @return zibo\library\database\manipulation\condition\NestedCondition */ private function parseNestedCondition(array $conditionTokens) { $nestedCondition = new NestedCondition($operator); $operator = null; foreach ($conditionTokens as $token) { if ($token == Condition::OPERATOR_AND || $token == Condition::OPERATOR_OR) { $operator = $token; continue; } if (is_array($token)) { $nestedCondition->addCondition($this->parseNestedCondition($token), $operator); } else { $nestedCondition->addCondition($this->parseSimpleCondition($token), $operator); } $operator = null; } return $nestedCondition; }
/** * Create the SQL of a nested condition * @param zibo\library\database\manipulation\expression\condition\NestedCondition $condition * @param boolean useAlias * @return string sql of the nested condition */ protected function parseNestedCondition(NestedCondition $condition, $useAlias = true) { $parts = $condition->getParts(); $sql = ''; foreach ($parts as $part) { if ($sql) { $sql .= ' ' . $part->getOperator() . ' '; } $sql .= $this->parseCondition($part->getCondition(), $useAlias); } return '(' . $sql . ')'; }
/** * Create a condition to localize a model * @param zibo\library\database\manipulation\expression\TableExpression $table Table expression of the table with the shared data * @param zibo\library\database\manipulation\expression\TableExpression $localizedTable Table expression of the table with the localized data * @return zibo\library\database\manipulation\condition\NestedCondition Condition to localize the provided table */ private function createLocalizeCondition(TableExpression $table, TableExpression $localizedTable) { $expressionPrimaryKey = new FieldExpression(ModelTable::PRIMARY_KEY, $table, $table->getAlias() . self::ALIAS_SEPARATOR . ModelTable::PRIMARY_KEY); $expressionData = new FieldExpression(LocalizedModel::FIELD_DATA, $localizedTable, $localizedTable->getAlias() . self::ALIAS_SEPARATOR . LocalizedModel::FIELD_DATA); $expressionLocaleField = new FieldExpression(LocalizedModel::FIELD_LOCALE, $localizedTable, $localizedTable->getAlias() . self::ALIAS_SEPARATOR . LocalizedModel::FIELD_LOCALE); $expressionLocale = new ScalarExpression($this->locale); $dataCondition = new SimpleCondition($expressionPrimaryKey, $expressionData, Condition::OPERATOR_EQUALS); $localeCondition = new SimpleCondition($expressionLocaleField, $expressionLocale, Condition::OPERATOR_EQUALS); $localizeCondition = new NestedCondition(); $localizeCondition->addCondition($dataCondition); $localizeCondition->addCondition($localeCondition); return $localizeCondition; }
/** * @dataProvider providerAddConditionThrowsExceptionWhenInvalidOperatorPassed * @expectedException zibo\ZiboException */ public function testAddConditionThrowsExceptionWhenInvalidOperatorPassed($operator) { $condition = new NestedCondition(); $condition->addCondition($this->condition, $operator); }
/** * 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; }