/** * Constructs a new data format table * @param zibo\library\orm\definition\ModelTable $table Table containing the data formats * @param string $formatAction URL to the action for the format * @return null */ public function __construct(ModelTable $table, $formatAction = null) { $formats = $table->getDataFormats(); parent::__construct($formats); $this->setId(self::STYLE_ID); $this->addDecorator(new ZebraDecorator(new DataFormatDecorator($formatAction))); }
/** * Constructs a new model field table * @param zibo\library\orm\definition\ModelTable $table Table containing the fields * @param string $fieldAction URL to the action of a field * @param string $modelAction URL to the action of a model * @return null */ public function __construct(ModelTable $table, $tableAction, $fieldAction = null, $modelAction = null) { $fields = $table->getFields(); unset($fields[ModelTable::PRIMARY_KEY]); parent::__construct($fields, $tableAction, self::NAME); $this->addDecorator(new ZebraDecorator(new ModelFieldDecorator($fieldAction, $modelAction))); $this->addDecorator(new ModelFieldLabelDecorator()); $this->addDecorator(new ModelFieldFlagsDecorator()); }
/** * Constructs a new model field table * @param zibo\library\orm\definition\ModelTable $table Table containing the fields * @param string $fieldAction URL to the action for the field * @return null */ public function __construct(ModelTable $table, $fieldAction = null) { $fields = $table->getFields(); unset($fields[ModelTable::PRIMARY_KEY]); parent::__construct($fields); $this->setId(self::STYLE_ID); $this->addDecorator(new ZebraDecorator(new ModelFieldDecorator($fieldAction))); $this->addDecorator(new ModelFieldLabelDecorator()); }
protected function createModel($name, array $fields) { $table = new ModelTable($name); foreach ($fields as $field) { $table->addField($field); } $meta = new ModelMeta($table); return new SimpleModel($meta); }
/** * Constructs a new model index table * @param zibo\library\orm\definition\ModelTable $table Table containing the indexes * @param string $indexAction URL to the action for the index * @return null */ public function __construct(ModelTable $table, $indexAction = null) { $indexes = $table->getIndexes(); if (!$indexes) { $indexes = array(); } parent::__construct($indexes); $this->setId(self::STYLE_ID); $this->addDecorator(new ZebraDecorator(new IndexDecorator($indexAction))); }
/** * Parses a has field into the meta * @param zibo\library\orm\definition\field\HasField $field * @param zibo\library\orm\ModelManager $modelManager Instance of the model manager * @return null */ private function parseHasField(HasField $field, ModelManager $modelManager) { $name = $field->getName(); $modelName = $this->table->getName(); $relationModelName = $field->getRelationModelName(); $relationModel = $modelManager->getModel($relationModelName); $relation = new RelationMeta(); $relation->setIsRelationWithSelf($relationModelName == $modelName); $linkModelName = $field->getLinkModelName(); if ($field->isLocalized()) { $localizedMeta = $this->getLocalizedModel()->getMeta(); $localizedField = $localizedMeta->getField($name); $linkModelName = $localizedField->getLinkModelName(); $field->setLinkModelName($linkModelName); $relation->setLinkModelName($linkModelName); } elseif ($linkModelName) { $relation->setLinkModelName($linkModelName); $linkModel = $modelManager->getModel($linkModelName); $linkModelTable = $linkModel->getMeta()->getModelTable(); if (!$relation->isRelationWithSelf()) { $relation->setForeignKey($this->getForeignKey($linkModelTable, $relationModelName)); $relation->setForeignKeyToSelf($this->getForeignKey($linkModelTable, $modelName)); } else { $relation->setForeignKey($this->getForeignKeys($linkModelTable, $modelName)); } } else { $relationModelTable = $relationModel->getMeta()->getModelTable(); $foreignKey = $field->getForeignKeyName(); $relation->setForeignKey($this->getForeignKey($relationModelTable, $modelName, $foreignKey)); } $this->relations[$name] = $relation; if ($field instanceof HasOneField) { $this->hasOne[$name] = $field; return; } if ($linkModelName) { $relation->setIsHasManyAndBelongsToMany(true); } $this->hasMany[$name] = $field; }
/** * Copies a model table into another * @param zibo\library\orm\definition\ModelTable $source * @param zibo\library\orm\definition\ModelTable $destination * @return null */ private function copyModel(ModelTable $source, ModelTable $destination) { $destination->setWillBlockDeleteWhenUsed($source->willBlockDeleteWhenUsed); $fields = $source->getFields(); foreach ($fields as $field) { if ($field->getName() == ModelTable::PRIMARY_KEY) { continue; } $destination->addField($field); } $indexes = $source->getIndexes(); foreach ($indexes as $index) { $destination->addIndex($index); } $dataFormats = $source->getDataFormats(); foreach ($dataFormats as $name => $format) { $destination->setDataFormat($name, $format); } }
/** * Processes the remove action * @param zibo\library\orm\definition\ModelTable $modelTable * @return null */ private function processRemove(ModelTable $modelTable, array $remove) { $remove = array_keys($remove); foreach ($remove as $dataFormatName) { $modelTable->removeDataFormat($dataFormatName); } $this->wizard->setVariable(BuilderWizard::VARIABLE_MODEL_TABLE, $modelTable); $request = $this->wizard->getRequest(); $response = $this->wizard->getResponse(); $response->setRedirect($request->getBasePath()); return null; }
public function testGetDataFormats() { $name = 'name'; $table = new ModelTable('table'); $formats = $table->getDataFormats(false); $this->assertTrue(is_array($formats)); $this->assertEquals(0, count($formats)); $formats = $table->getDataFormats(true); $this->assertTrue(is_array($formats)); $this->assertEquals(1, count($formats)); $this->assertTrue(array_key_exists(DataFormatter::FORMAT_TITLE, $formats)); $table->setDataFormat(new DataFormat($name, 'format')); $formats = $table->getDataFormats(true); $this->assertTrue(is_array($formats)); $this->assertEquals(2, count($formats)); $this->assertTrue(array_key_exists(DataFormatter::FORMAT_TITLE, $formats)); $this->assertTrue(array_key_exists($name, $formats)); $formats = $table->getDataFormats(false); $this->assertTrue(is_array($formats)); $this->assertEquals(1, count($formats)); $this->assertFalse(array_key_exists(DataFormatter::FORMAT_TITLE, $formats)); $this->assertTrue(array_key_exists($name, $formats)); $table->setDataFormat(new DataFormat(DataFormatter::FORMAT_TITLE, 'format')); $formats = $table->getDataFormats(true); $this->assertTrue(is_array($formats)); $this->assertEquals(2, count($formats)); $this->assertTrue(array_key_exists(DataFormatter::FORMAT_TITLE, $formats)); $this->assertTrue(array_key_exists($name, $formats)); $formats = $table->getDataFormats(false); $this->assertTrue(is_array($formats)); $this->assertEquals(2, count($formats)); $this->assertTrue(array_key_exists(DataFormatter::FORMAT_TITLE, $formats)); $this->assertTrue(array_key_exists($name, $formats)); }
/** * Sets the submitted field to the model table * @param zibo\library\orm\definition\ModelTable $modelTable * @param zibo\library\orm\definition\field\ModelField $field * @return null */ private function setFieldToModelTable(ModelTable $modelTable, ModelField $field) { $modelTable->setField($field); $fieldName = $field->getName(); $currentFieldName = $this->wizard->getVariable(BuilderWizard::VARIABLE_FIELD); if (!$currentFieldName || $fieldName == $currentFieldName) { $this->wizard->setVariable(BuilderWizard::VARIABLE_MODEL_TABLE, $modelTable); return; } $order = array(); $fields = $modelTable->getFields(); foreach ($fields as $modelFieldName => $modelField) { if ($modelFieldName == $fieldName) { continue; } if ($modelFieldName == $currentFieldName) { $modelFieldName = $fieldName; } $order[] = $modelFieldName; } $modelTable->orderFields($order); $modelTable->removeField($currentFieldName); $this->wizard->setVariable(BuilderWizard::VARIABLE_MODEL_TABLE, $modelTable); }
/** * Deletes or clears, depending on the keepRecord argument, the values of the provided table * which have a relation with the provided data * @param zibo\library\orm\definition\ModelTable $modelTable Table definition of the model of the has many relation * @param integer $id Primary key of the data * @param boolean $keepRecord True to clear the link, false to delete the link * @return null */ private function clearHasMany(ModelTable $modelTable, $id, $keepRecord) { $table = new TableExpression($modelTable->getName()); $relationFields = $modelTable->getRelationFields($this->getName()); $fields = $relationFields[ModelTable::BELONGS_TO]; foreach ($fields as $field) { $fieldName = $field->getName(); if ($keepRecord) { $statement = new UpdateStatement(); $statement->addValue(new FieldExpression($fieldName), null); } else { $statement = new DeleteStatement(); } $condition = new SimpleCondition(new FieldExpression($fieldName), new SqlExpression($id), Condition::OPERATOR_EQUALS); $statement->addTable($table); $statement->addCondition($condition); $this->executeStatement($statement); } $model = $this->getModel($modelTable->getName()); $model->clearCache(); }
/** * Gets the foreign keys from the provided model table for the provided relation model. When no foreign keys are found and the relation * model is a localized model, the unlocalized model will be queried for the foreign keys. * @param zibo\library\orm\definition\ModelTable $modelTable Table definition of the model * @param string $relationModelName Model name to get the foreign keys of * @return array Array with ModelField objects * @throws zibo\library\orm\exception\ModelException when there are no foreign keys found the provided model */ private function getForeignKeys(ModelTable $modelTable, $relationModelName) { if (!$relationModelName) { throw new ModelException('Provided relation model name is empty'); } $foreignKeys = $modelTable->getRelationFields($relationModelName, ModelTable::BELONGS_TO); if (!$foreignKeys) { if (preg_match('/' . LocalizedModel::MODEL_SUFFIX . '$/', $relationModelName)) { $relationModelName = substr($relationModelName, 0, strlen(LocalizedModel::MODEL_SUFFIX) * -1); $foreignKeys = $modelTable->getRelationFields($relationModelName, ModelTable::BELONGS_TO); } if (!$foreignKeys) { throw new ModelException('No foreign key found for ' . $relationModelName . ' found in ' . $modelTable->getName()); } } return $foreignKeys; }
/** * Sets the the title and teaser format to the model table * @param DOMElement $modelElement Element of the model * @param zibo\library\orm\definition\ModelTable $modelTable Model table which is being read * @return null */ protected function setFormatsFromElement(DOMElement $modelElement, ModelTable $modelTable) { $formatElements = $modelElement->getElementsByTagName(self::TAG_FORMAT); foreach ($formatElements as $formatElement) { $name = $formatElement->getAttribute(self::ATTRIBUTE_NAME); $format = $formatElement->textContent; $modelTable->setDataFormat(new DataFormat($name, $format)); } }
/** * Creates a link model and registers it * @param string $linkModelName Name of the new link model * @param string $modelName1 Name of the first model * @param string $modelName2 Name of the second model * @return zibo\library\orm\model\Model The registered model */ private function createAndRegisterLinkModel($linkModelName, $modelName1, $modelName2) { $table = new ModelTable($linkModelName); $indexName = lcfirst($this->generateLinkModelName($modelName1, $modelName2)); $fieldName1 = lcfirst($modelName1); $fieldName2 = lcfirst($modelName2); if ($modelName1 == $modelName2) { $field1 = new BelongsToField($fieldName1 . '1', $modelName1); $field2 = new BelongsToField($fieldName1 . '2', $modelName2); } else { $field1 = new BelongsToField($fieldName1, $modelName1); $field2 = new BelongsToField($fieldName2, $modelName2); } $index = new Index($indexName, array($field1, $field2)); $table->addField($field1); $table->addField($field2); $table->addIndex($index); $linkModel = new SimpleModel(new ModelMeta($table)); $this->registerModel($linkModel, false); return $linkModel; }