public function bootRunTime(Object $object, Configs $configs) { if (!$object->hasRelation($this->getFieldDefinition()->getId())) { $relation = new RelationDefinition(); $relation->setName($this->getFieldDefinition()->getId()); $relation->setType(AbstractStorage::ONE_TO_MANY); $relation->setForeignObjectKey('jarves/content'); $relation->setRefName($object->getId()); $object->addRelation($relation); $configs->addReboot('Added auto relation because of PageContents type.'); } if (!$object->hasField('layout')) { $field = new Field(); $field->setId('layout'); $field->setType('text'); $object->addField($field); $configs->addReboot('PageContents needs `layout` field.'); } if (!$object->hasField('theme')) { $field = new Field(); $field->setId('theme'); $field->setType('text'); $object->addField($field); $configs->addReboot('PageContents needs `theme field.'); } }
/** * @param \Jarves\Configuration\Object $object * @param \SimpleXMLElement $objectTable * * @return \SimpleXMLElement * * @throws \Jarves\Exceptions\ModelBuildException */ protected function getXmlTable(Object $object, \SimpleXMLElement $objectTable = null) { if (!$objectTable) { $objectTable = new \SimpleXMLElement('<table />'); //simplexml_load_string('<database></database>'); } if (!$object->getTable()) { throw new ModelBuildException(sprintf('The object `%s` has no table defined', $object->getId())); } $objectTable['name'] = $object->getTable(); $objectTable['phpName'] = ucfirst($object->getId()); if ($object->isCrossRef()) { $objectTable['isCrossRef'] = 'true'; } $columnsDefined = array(); if (!$object->getFields()) { throw new ModelBuildException(sprintf('The object `%s` has no fields defined', $object->getId())); } foreach ($object->getFields() as $field) { if ($columns = $field->getFieldType()->getColumns()) { foreach ($columns as $column) { $name = Tools::camelcase2Underscore($column->getName()); //column exist? $eColumns = $objectTable->xpath('column[@name =\'' . $name . '\']'); if ($eColumns) { $newCol = current($eColumns); if ($newCol['custom'] == true) { continue; } } else { $newCol = $objectTable->addChild('column'); } $columnsDefined[] = $name; $this->setupColumnAttributes($column, $newCol); if ($field->isRequired()) { $newCol['required'] = 'true'; } if ($field->isPrimaryKey()) { $newCol['primaryKey'] = 'true'; } if ($field->isAutoIncrement()) { $newCol['autoIncrement'] = 'true'; } } } } if ($relations = $object->getRelations()) { foreach ($relations as $relation) { $this->addRelation($object, $relation, $objectTable); } } if ($object->isNested()) { $behaviors = $objectTable->xpath('behavior[@name=\'nested_set\']'); if ($behaviors) { $behavior = current($behaviors); } else { $behavior = $objectTable->addChild('behavior'); } if (!$behavior['custom']) { $behavior['name'] = 'nested_set'; $parameters = ['left_column' => 'lft', 'right_column' => 'rgt', 'level_column' => 'lvl']; if ($object->getNestedRootAsObject()) { $parameters['use_scope'] = 'true'; $parameters['scope_column'] = Tools::camelcase2Underscore($object->getNestedRootObjectField()); } foreach ($parameters as $k => $v) { $parameter = $behavior->addChild('parameter'); $parameter['name'] = $k; $parameter['value'] = $v; } } } if ($object['workspace']) { $behaviors = $objectTable->xpath('behavior[@name=\'Jarves\\Propel\\Behavior\\WorkspaceBehavior\']'); if ($behaviors) { $behavior = current($behaviors); } else { $behavior = $objectTable->addChild('behavior'); } $behavior['name'] = 'Jarves\\Propel\\Behavior\\WorkspaceBehavior'; } $vendors = $objectTable->xpath('vendor[@type=\'mysql\']'); if ($vendors) { foreach ($vendors as $k => $v) { unset($vendors[$k][0]); } } $vendor = $objectTable->addChild('vendor'); $vendor['type'] = 'mysql'; $params = $vendor->xpath('parameter[@name=\'Charset\']'); if ($params) { $param = current($params); } else { $param = $vendor->addChild('parameter'); } $param['name'] = 'Charset'; $param['value'] = 'utf8'; return $objectTable; // $dom = new \DOMDocument; // $dom->preserveWhiteSpace = false; // $dom->loadXML($xml->asXML()); // $dom->formatOutput = true; // // $xml = $dom->saveXML(); // $prefix = '<?xml version="1.0"? >'; // if (0 === strpos($xml, $prefix)) { // $xml = substr($xml, strlen($prefix)); // } // // return trim($xml); }
public function bootRunTime(Object $object, Configs $configs) { $contentsObjectName = $object->getId() . ucfirst($this->getFieldDefinition()->getId()); $contentsObject = $object->getBundle()->getObject($contentsObjectName); if (!$contentsObject) { $contentsObject = new Object(); $contentsObject->setId($contentsObjectName); if ($object->getWorkspace()) { $contentsObject->setWorkspace(true); } $contentsObject->setAutoCrud(false); $contentsObject->setSearchable(false); $contentsObject->setExcludeFromREST(true); $contentsObject->setNested(true); $contentsObject->setNestedRootAsObject(true); $contentsObject->setNestedRootObject($object->getKey()); $contentsObject->setNestedRootObjectField('foreignId'); $contentsObject->setTable($object->getTable() . '_' . Tools::camelcase2Underscore($this->getFieldDefinition()->getId())); $contentsObject->setStorageService($object->getStorageService()); } $fields = ['id' => ['type' => 'number', 'autoIncrement' => true, 'primaryKey' => true], 'foreignId' => ['type' => 'number'], 'slotId' => ['type' => 'number'], 'sort' => ['type' => 'number'], 'content' => ['type' => 'textarea'], 'template' => ['type' => 'view'], 'type' => ['type' => 'text'], 'hide' => ['type' => 'checkbox'], 'unsearchable' => ['type' => 'checkbox'], 'access_from' => ['type' => 'datetime'], 'access_to' => ['type' => 'datetime'], 'access_from_groups' => ['type' => 'text']]; foreach ($fields as $k => $def) { if (!$contentsObject->getField($k)) { $def['id'] = $k; $field = new Field($def, $object->getJarves()); $contentsObject->addField($field); $configs->addReboot(sprintf('[ContentElements] Added field %s to %s', $k, $contentsObject->getKey())); } } if (!$contentsObject->hasRelation('ForeignObject')) { $relation = new RelationDefinition(); $relation->setName('ForeignObject'); $relation->setType(AbstractStorage::MANY_TO_ONE); $relation->setForeignObjectKey($object->getKey()); $relation->setRefName(ucfirst($this->getFieldDefinition()->getId())); $reference = new RelationReferenceDefinition(); $primaryFields = $object->getPrimaryKeys(); if (1 < count($primaryFields)) { throw new ModelBuildException(sprintf('FieldType `ContentElements` can not be used on the object `%s` with composite PrimaryKey', $object->getId())); } if (0 === count($primaryFields)) { throw new ModelBuildException(sprintf('FieldType `ContentElements` can not be used on the object `%s` with no PrimaryKey', $object->getId())); } $columns = $primaryFields[0]->getFieldType()->getColumns(); if (1 < count($columns)) { throw new ModelBuildException(sprintf('FieldType `ContentElements` can not be used on the object `%s` with composite PrimaryKey', $object->getId())); } $reference->setForeignColumn($columns[0]); $field = $contentsObject->getField('foreignId'); $columns = $field->getFieldType()->getColumns(); $reference->setLocalColumn($columns[0]); $relation->setReferences([$reference]); $contentsObject->addRelation($relation); $configs->addReboot(sprintf('[ContentElements] Added relation ForeignObject to %s', $contentsObject->getKey())); } if (!$contentsObject->getBundle()) { $object->getBundle()->addObject($contentsObject); } if (!$object->hasRelation($this->getFieldDefinition()->getId())) { $relation = new RelationDefinition(); $relation->setName(ucfirst($this->getFieldDefinition()->getId())); $relation->setType(AbstractStorage::ONE_TO_MANY); $relation->setForeignObjectKey($contentsObject->getKey()); $relation->setRefName('ForeignObject'); $reference = new RelationReferenceDefinition(); $primaryFields = $object->getPrimaryKeys(); $columns = $primaryFields[0]->getFieldType()->getColumns(); $reference->setLocalColumn($columns[0]); $field = $contentsObject->getField('foreignId'); $columns = $field->getFieldType()->getColumns(); $reference->setForeignColumn($columns[0]); $relation->setReferences([$reference]); $object->addRelation($relation); $configs->addReboot(sprintf('[ContentElements] Added relation %s to %s', ucfirst($this->getFieldDefinition()->getId()), $object->getKey())); } }
/** * @param \Jarves\Configuration\Object $objectDefinition * @param Configs $configs * @return bool */ protected function defineCrossObject(Object $objectDefinition, Configs $configs) { $changed = false; $bundle = $objectDefinition->getBundle(); $foreignObjectDefinition = $configs->getObject($this->getFieldDefinition()->getObject()); $possibleObjectName = ucfirst($objectDefinition->getId()) . ucfirst($foreignObjectDefinition->getId()); $possibleObjectKey = $bundle->getName() . '/' . $possibleObjectName; if (!($crossObjectKey = $this->getFieldDefinition()->getObjectRelationCrossObjectKey())) { $crossObjectKey = $possibleObjectKey; } $crossObject = $configs->getObject($crossObjectKey); if (!$crossObject) { if (!($crossObject = $configs->getObject($possibleObjectKey))) { $crossObject = new Object(null, $objectDefinition->getJarves()); $crossObject->setId($possibleObjectName); $crossObject->setSearchable(false); $crossObject->setAutoCrud(false); $crossObject->setExcludeFromREST(true); $crossObject->setTable($objectDefinition->getTable() . '_' . Tools::camelcase2Underscore($foreignObjectDefinition->getId())); $changed = true; } } if (!$crossObject->isCrossRef()) { $crossObject->setCrossRef(true); $changed = true; } $leftFieldName = $this->getFieldDefinition()->getObjectRefRelationName() ?: $objectDefinition->getId(); if (!$crossObject->getField($leftFieldName)) { $leftObjectField = new Field(null, $objectDefinition->getJarves()); $leftObjectField->setId($leftFieldName); $leftObjectField->setType('object'); $leftObjectField->setObject($objectDefinition->getKey()); $leftObjectField->setObjectRelation(AbstractStorage::ONE_TO_ONE); $leftObjectField->setPrimaryKey(true); $crossObject->addField($leftObjectField); $changed = true; } if (!$crossObject->getField($this->getFieldDefinition()->getId())) { $rightObjectField = new Field(null, $objectDefinition->getJarves()); $rightObjectField->setId($this->getFieldDefinition()->getId()); $rightObjectField->setType('object'); $rightObjectField->setObject($foreignObjectDefinition->getKey()); $rightObjectField->setObjectRelation(AbstractStorage::ONE_TO_ONE); $rightObjectField->setPrimaryKey(true); $crossObject->addField($rightObjectField); $changed = true; } if (!$crossObject->getBundle()) { //we created a new object $bundle->addObject($crossObject); } return $changed; }
/** * @param \Jarves\Configuration\Object $objectDefinition */ public function addObject(Object $objectDefinition) { $this->objects[strtolower($objectDefinition->getId())] = $objectDefinition; }
public function testObjectItemArray() { $xml = ' <object id="Item"> <label>title</label> <table>test_item</table> <labelField>title</labelField> <nested>false</nested> <multiLanguage>false</multiLanguage> <workspace>true</workspace> <domainDepended>false</domainDepended> <treeFixedIcon>false</treeFixedIcon> <fields> <field id="id" type="number" primaryKey="true" autoIncrement="true"> </field> <field id="title" type="text"> </field> <field id="category" type="object"> <object>test/itemCategory</object> <objectRelation>nToM</objectRelation> </field> <field id="oneCategory" type="object"> <object>test/itemCategory</object> <objectRelation>nTo1</objectRelation> </field> </fields> </object>'; $object = new Object($xml, $this->getJarves()); $array = $object->toArray(); $this->assertEquals('Item', $object->getId()); $this->assertEquals('title', $object->getLabel()); $this->assertEquals('test_item', $object->getTable()); $this->assertTrue($object->getWorkspace()); $this->assertCount(4, $object->getFields()); $this->assertEquals('Item', $array['id']); $this->assertEquals('title', $array['label']); $this->assertEquals('test_item', $array['table']); $this->assertTrue($array['workspace']); $this->assertCount(4, $array['fields']); }
/** * Maybe in v1.1 * * @param $pattern * @param Object $object */ public function setupRelationRoutes($pattern, Object $object) { $objectName = $object->getBundle()->getBundleName() . '/' . lcfirst($object->getId()); $pattern = $pattern . '/{pk}/'; foreach ($object->getFields() as $field) { if ('object' === $field->getType()) { $foreignObject = $this->jarves->getObjects()->getDefinition($field->getObject()); if (!$foreignObject) { continue; } $this->setupRoutes($object->getBundle(), $object->getFinalApiController(), $pattern . lcfirst($field->getObjectRelationName() ?: $field->getId()), $objectName, $foreignObject, $object, $field); } } }
/** * @param \Jarves\Configuration\Object $object */ public function addObject(Object $object) { $object->setBundle($this); $this->objects[strtolower($object->getId())] = $object; }