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 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); }