/** * Returns a `SelectFormItem` which is filled with instances of the model given `Column` represents. * * @param Model $model The `Model` which contains given `Column`. * @param Column $column The `Column` which represents a related model. * @param \Eloquent $item The instance to retrieve information from for filling the `SelectFormItem`. * * @return SelectFormItem The created `SelectFormItem`. */ private function createSelectAssociationFormItem(Model $model, Column $column, $item) { $result = new SelectFormItem(); $relations = $this->aujaConfigurator->getRelationsForModel($model); $relatedModel = null; foreach ($relations as $relation) { $rightModel = $relation->getRight(); if (starts_with($column->getName(), camel_case($rightModel->getName()))) { $relatedModel = $rightModel; } } if ($relatedModel != null) { $displayName = $this->aujaConfigurator->getDisplayName($relatedModel); $result->setName($displayName); $result->setValue($item->id); $items = call_user_func(array($relatedModel->getName(), 'all')); $displayField = $this->aujaConfigurator->getDisplayField($relatedModel); foreach ($items as $item) { $label = isset($item->{$displayField}) ? $item->{$displayField} : ''; $value = $item->id; $option = new SelectOption($label, $value); $result->addOption($option); } } return $result; }
private function assertRelationshipsExist(TableNode $table, $type) { $relations = $this->aujaConfigurator->getRelations(); foreach ($table->getHash() as $relationHash) { $leftModelName = $relationHash['left']; $rightModelName = $relationHash['right']; if (!isset($relations[$leftModelName])) { throw new Exception(sprintf('No relations for model %s', $leftModelName)); } $modelRelations = $this->aujaConfigurator->getRelationsForModel($this->aujaConfigurator->getModel($leftModelName)); $relationshipExists = false; foreach ($modelRelations as $relation) { if ($relation->getType() == $type && $relation->getRight()->getName() == $rightModelName) { $relationshipExists = true; } } if (!$relationshipExists) { throw new Exception(sprintf('There is no %s relationship between %s and %s.', $type, $leftModelName, $rightModelName)); } } }
/** * Creates a `Menu` for given model, with a layout depending on its relations. * * @param String $modelName The name of the model to create a `Menu` for. * @param int $modelId The id of an instance of the model, 0 for none. * @param ModelConfig $config (optional) The `ModelConfig` to use. * * @return Menu The built `Menu` instance, which can be configured further. */ private function buildComplexIndexMenu($modelName, $modelId, ModelConfig $config = null) { $model = $this->aujaConfigurator->getModel($modelName); $relations = $this->aujaConfigurator->getRelationsForModel($model); $associationRelations = array(); foreach ($relations as $relation) { if ($relation->getType() == Relation::HAS_MANY || $relation->getType() == Relation::HAS_AND_BELONGS_TO) { // TODO: What to do with one-to-one relations? $associationRelations[] = $relation; } } switch (count($associationRelations)) { case 0: $menu = $this->noAssociationsMenuFor($modelName, $config); break; case 1: $menu = $this->singleAssociationMenuFor($modelName, $modelId, $associationRelations[0], $config); break; default: $menu = $this->multipleAssociationsMenuFor($modelName, $modelId, $associationRelations, $config); break; } return $menu; }
/** * Builds a Resource instance for given items. * This is typically used when a ResourceMenuItem triggers a call for items. * * This method also supports pagination, either manually or automatically. * To automatically use pagination, simply provide a Paginator as items. * * @param String $modelName the name of the model the items represent. * @param array|Paginator $items an array of instances of the model to be shown, or a Paginator containing the instances. * @param String $targetUrl (optional) The target url for the items. Must contain '%d' in the place of the item id. * @param String $nextPageUrl (optional) The url to the next page, if any. * @param int $offset (optional) The offset to start the order from. * @param ModelConfig $config (optional) The `ModelConfig` to use. * * @return Resource The built LinkMenuItems. */ public function create($modelName, $items, $targetUrl = null, $nextPageUrl = null, $offset = -1, ModelConfig $config = null) { // TODO: create separate methods for pagination and no pagination? /* Extract items from Paginator if necessary */ $paginator = null; if ($items instanceof Paginator) { $paginator = $items; $items = $paginator->getCollection(); if ($offset == -1) { $offset = ($paginator->getCurrentPage() - 1) * $paginator->getPerPage(); } } /* If the offset is not set, use no offset */ if ($offset == -1) { $offset = 0; } /* No items. */ if (count($items) == 0) { return new Resource(); } /* If the items are not iterable */ if (!$items instanceof \IteratorAggregate) { $items = new Collection([$items]); } $model = $this->aujaConfigurator->getModel($modelName); /* Find relations for this model, so we can know the target */ $relations = $this->aujaConfigurator->getRelationsForModel($model); $associationRelations = array(); foreach ($relations as $relation) { if ($relation->getType() == Relation::HAS_MANY || $relation->getType() == Relation::HAS_AND_BELONGS_TO) { $associationRelations[] = $relation; } } /* Build the actual items to return */ $resourceItems = new Resource(); $displayField = $this->aujaConfigurator->getDisplayField($model, $config); $icon = $this->aujaConfigurator->getIcon($model, $config); for ($i = 0; $i < count($items); $i++) { if ($targetUrl != null) { $target = sprintf($targetUrl, $items[$i]->id); } else { if (count($associationRelations) == 0) { $target = URL::route($this->aujaRouter->getEditName($modelName), $items[$i]->id); } else { $target = URL::route($this->aujaRouter->getShowMenuName($modelName), $items[$i]->id); } } $menuItem = new LinkMenuItem(); $menuItem->setText($items[$i]->{$displayField}); $menuItem->setTarget($target); $menuItem->setOrder($offset + $i); $menuItem->setIcon($icon); $resourceItems->addItem($menuItem); } /* Add pagination if necessary */ if ($nextPageUrl != null) { $resourceItems->setNextPageUrl($nextPageUrl); } else { if ($paginator != null && $paginator->getCurrentPage() != $paginator->getLastPage()) { $target = route($this->aujaRouter->getIndexName($modelName), ['page' => $paginator->getCurrentPage() + 1]); $resourceItems->setNextPageUrl($target); } } return $resourceItems; }