public function createStorage(\Jarves\Configuration\Object $object) { $storageService = $object->getStorageService(); $instance = $this->container->get($storageService); $instance->configure($object->getKey(), $object); return $instance; }
/** * Important call directly after the creation of this class. * * @param string $objectKey * @param Object $definition */ public function configure($objectKey, $definition) { $this->objectKey = \Jarves\Objects::normalizeObjectKey($objectKey); $this->definition = $definition; foreach ($this->definition->getFields() as $field) { if ($field->isPrimaryKey()) { $this->primaryKeys[] = $field->getId(); } } }
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.'); } }
protected function addForeignKey(Object $object, RelationDefinitionInterface $relation, &$xmlTable) { $relationName = $relation->getName(); $foreignObject = $this->objects->getDefinition($relation->getForeignObjectKey()); if (!$foreignObject) { throw new ModelBuildException(sprintf('Foreign object `%s` does not exist in relation `%s`', $relation->getForeignObjectKey(), $relation->getName())); } if ($object->getStorageService() !== $foreignObject->getStorageService()) { throw new ModelBuildException(sprintf('Can not create a relation between two different dataModels. Got `%s` but `%s` is needed.', $foreignObject->getStorageService(), $object->getStorageService())); } $pluralizer = new StandardEnglishPluralizer(); $foreignPhpName = ucfirst($pluralizer->getSingularForm(lcfirst($relationName))); $foreigns = $xmlTable->xpath('foreign-key[@phpName=\'' . $foreignPhpName . '\']'); if ($foreigns) { $foreignKey = current($foreigns); } else { $foreignKey = $xmlTable->addChild('foreign-key'); } $foreignKey['phpName'] = $foreignPhpName; $foreignKey['foreignTable'] = $foreignObject->getTable(); if ($refName = $relation->getRefName()) { $foreignKey['refPhpName'] = ucfirst($pluralizer->getSingularForm(lcfirst($refName))); } $foreignKey['onDelete'] = $relation->getOnDelete(); $foreignKey['onUpdate'] = $relation->getOnUpdate(); if (!$relation->getWithConstraint()) { $foreignKey['skipSql'] = 'true'; } $references = $foreignKey->xpath("reference[not(@custom='true')]"); foreach ($references as $i => $ref) { unset($references[$i][0]); } foreach ($relation->getReferences() as $reference) { $localName = Tools::camelcase2Underscore($reference->getLocalColumn()->getName()); $references = $foreignKey->xpath('reference[@local=\'' . $localName . '\']'); if ($references) { $xmlReference = current($references); } else { $xmlReference = $foreignKey->addChild('reference'); } $xmlReference['local'] = $localName; $xmlReference['foreign'] = Tools::camelcase2Underscore($reference->getForeignColumn()->getName()); } if ($foreignObject->getWorkspace()) { if (!$object->getWorkspace()) { $columns = $xmlTable->xpath('column[@name=\'workspace_id\']'); if (!$columns) { $newCol = $xmlTable->addChild('column'); $newCol['name'] = 'workspace_id'; $newCol['type'] = 'INTEGER'; $newCol['defaultValue'] = '1'; } } $localName = 'workspace_id'; $references = $foreignKey->xpath('reference[@local=\'' . $localName . '\']'); if ($references) { $xmlReference = current($references); } else { $xmlReference = $foreignKey->addChild('reference'); } $xmlReference['local'] = $localName; $xmlReference['foreign'] = 'workspace_id'; } }
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())); } }
protected function defineRelation(Object $objectDefinition, Configs $configs) { $relation = $this->getRelation($configs); if ($relation && !$objectDefinition->hasRelation($relation->getName())) { $objectDefinition->addRelation($relation); return true; } }
/** * @param \Jarves\Configuration\Object $objectDefinition */ public function addObject(Object $objectDefinition) { $this->objects[strtolower($objectDefinition->getId())] = $objectDefinition; }
public function testOptions() { $xml = '<object id="File"> <label>File</label> <class>Admin\\Models\\ObjectFile</class> <table>system_file</table> <labelField>path</labelField> <nested>true</nested> <treeLabel>name</treeLabel> <treeIcon>type</treeIcon> <treeIconMapping> <icon id="dir">#icon-folder-4</icon> </treeIconMapping> <treeDefaultIcon>#icon-paper</treeDefaultIcon> <browserInterfaceClass>jarves.Files</browserInterfaceClass> <browserInterface>custom</browserInterface> </object>'; $object = new Object($xml, $this->getJarves()); $this->assertEquals(['dir' => '#icon-folder-4'], $object->toArray()['treeIconMapping']); }
/** * * array( * 'items' => $items, * 'count' => $maxItems, * 'pages' => $maxPages * ); * * @param array $filter * @param integer $limit * @param integer $offset * @param string $query * @param string $fields * @param array $orderBy * * @param bool $withAcl * @param array $primaryKeys * * @return array * @throws ObjectNotFoundException * @throws \Exception */ public function getItems($filter = null, $limit = null, $offset = null, $query = '', $fields = null, $orderBy = [], $withAcl = false, array $primaryKeys = []) { $options = array(); $storageController = $this->objects->getStorageController($this->getObject()); $options['offset'] = $offset; $options['limit'] = $limit ? $limit : $this->defaultLimit; $condition = $this->getCondition(); if ($extraCondition = $this->getCustomListingCondition()) { $condition->mergeAnd($extraCondition); } $options['order'] = $orderBy ?: $this->getOrder(); $options['fields'] = $this->getItemsSelection($fields); $options['permissionCheck'] = $this->getPermissionCheck(); $aclRequest = ACLRequest::create($this->getObject())->onlyListingMode(); if ($this->getPermissionCheck() && !$this->acl->check($aclRequest)) { return null; } if ($limit = $this->getObjectDefinition()->getLimitDataSets()) { $condition->mergeAnd($limit); } if ($this->getMultiLanguage() && $this->getLanguage()) { if ($this->getObjectDefinition()->getNestedRootAsObject() && ($rootObjectKey = $this->getObjectDefinition()->getNestedRootObject())) { $rootObjects = $this->objects->getList($rootObjectKey, null, ['lang' => $this->getLanguage(), 'fields' => 'id']); $langConditions = new Condition(); foreach ($rootObjects as $item) { $langConditions->addOr(['domain', '=', $item['id']]); } $condition->addAnd($langConditions); } else { //does the object have a lang field? if ($this->objectDefinition->hasField('lang') && !isset($filter['lang'])) { $filter['lang'] = $this->getLanguage(); } } } if ($query) { if ($queryCondition = $this->getQueryCondition($query, $options['fields'])) { $condition->mergeAnd($queryCondition); } } if ($primaryKeys) { $primaryKeyCondition = Condition::create(); foreach ($primaryKeys as $pk) { $primaryKeyConditionItem = Condition::create(); foreach ($this->getObjectDefinition()->getPrimaryKeyNames() as $primaryKeyName) { if (!isset($pk[$primaryKeyName])) { throw new \LogicException(sprintf('Field %s not found in primaryKey parameter (%s)', $primaryKeyName, json_encode($pk))); } $primaryKeyConditionItem->addAnd([$primaryKeyName, '=', $pk[$primaryKeyName]]); } $primaryKeyCondition->mergeOr($primaryKeyConditionItem); } $condition->mergeAndBegin($primaryKeyCondition); } if ($this->getPermissionCheck() && ($aclCondition = $this->acl->getListingCondition($this->getObject()))) { $condition->mergeAndBegin($aclCondition); } $items = $storageController->getItems($filter, $condition, $options); if ($withAcl && is_array($items)) { foreach ($items as &$item) { if ($item) { $this->prepareRow($item); } } } return $items ?: null; }
/** * 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); } } }
/** * Creates a new `Object` object and sets Object's Bundle to this instance. * * @param string $id * * @return Object */ public function newObject($id) { $object = new Object(null, $this->getJarves()); $object->setId($id); $this->addObject($id); return $object; }