/** * @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())); } }
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']); }
/** * @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 bool $withoutObjectCheck * * @throws ObjectNotFoundException */ public function initialize($withoutObjectCheck = false) { if ($this->objectDefinition) { return; } if (!$this->getObject()) { return; } $this->objectDefinition = $this->objects->getDefinition($this->getObject()); if (!$this->objectDefinition && $this->getObject() && !$withoutObjectCheck) { throw new ObjectNotFoundException("Can not find object '" . $this->getObject() . "'"); } if ($this->objectDefinition) { if ($apiControllerDefinition = $this->objectDefinition->getApiControllerDefinition()) { $path = $this->jarves->resolvePath($apiControllerDefinition); $definitionContent = file_get_contents($path); $yaml = new Parser(); $parsedDefinition = $yaml->parse($definitionContent); if ($parsedDefinition) { foreach ($parsedDefinition as $key => $val) { $setter = 'set' . ucfirst($key); if (method_exists($this, $setter)) { $this->{$setter}($val); } } } } $this->asNested = $this->objectDefinition->getNested(); if (!$this->table) { $this->table = $this->objectDefinition->getTable(); } if (!$this->fields) { $this->fields = []; foreach ($this->objectDefinition->getFields() as $field) { if (!$field->isAutoIncrement()) { $this->fields[] = $field; } } } if (!$this->columns) { foreach ($this->fields as $field) { if ($field->isPrimaryKey() || $field->isAutoIncrement()) { continue; } if ('object' !== $field->getType()) { $this->columns[$field->getId()] = $field; } } if ($labelField = $this->objectDefinition->getLabelField()) { $field = $this->objectDefinition->getField($labelField); $this->columns[$field->getId()] = $field; } } //we need to call it, no matter if it's already defined, because of multiLanguage field. $this->prepareFieldItem($this->fields); $this->translateFields($this->fields); $this->translateFields($this->columns); if (!isset($this->titleField)) { $this->titleField = $this->objectDefinition->getLabel(); } } else { //resolve shortcuts if ($this->fields) { $this->prepareFieldDefinition($this->fields); $this->convertToFieldObjects($this->fields); $this->prepareFieldItem($this->fields); $this->translateFields($this->fields); } if ($this->columns) { $this->prepareFieldDefinition($this->columns); $this->convertToFieldObjects($this->columns); $this->translateFields($this->columns); } } $this->ensureLanguageField(); $this->fields = $this->toIdIndex($this->fields); $this->columns = $this->toIdIndex($this->columns); if ($this->addMultipleFields) { $this->prepareFieldDefinition($this->addMultipleFields); $this->convertToFieldObjects($this->addMultipleFields); $this->translateFields($this->addMultipleFields); } if ($this->addMultipleFixedFields) { $this->prepareFieldDefinition($this->addMultipleFixedFields); $this->convertToFieldObjects($this->addMultipleFixedFields); $this->translateFields($this->addMultipleFixedFields); } if (is_string($this->primary)) { $this->primary = explode(',', str_replace(' ', '', $this->primary)); } if (!$this->order || count($this->order) == 0) { /* compatibility */ $this->orderByDirection = strtolower($this->orderByDirection) == 'asc' ? 'asc' : 'desc'; if ($this->orderBy) { $this->order = array($this->orderBy => $this->orderByDirection); } } if ((!$this->order || count($this->order) == 0) && $this->columns) { reset($this->columns); $field = current($this->columns); if ($field instanceof Field) { $this->order[$field->getId()] = 'asc'; } } //normalize order array if (count($this->order) > 0 && is_numeric(key($this->order))) { $newOrder = array(); foreach ($this->order as $order) { $newOrder[$order['field']] = $order['direction']; } $this->order = $newOrder; } $this->filterFields = array(); if ($this->filter) { foreach ($this->filter as $key => $val) { if (is_numeric($key)) { //no special definition $fieldKey = $val; $field = $this->fields[$val]; } else { $field = $val; $fieldKey = $key; } $this->prepareFieldItem($field); $this->filterFields[$fieldKey] = $field; } } if (!$this->primary) { $this->primary = array(); if ($this->objectDefinition) { foreach ($this->objectDefinition->getPrimaryKeys() as $sfield) { $this->primary[] = $sfield->getId(); } } } $this->translate($this->nestedRootAddLabel); $this->translate($this->newLabel); }