public function create($modelName, $item = null, ModelConfig $config = null) { $page = new Page(); $header = new PageHeader(); $header->setText('Create ' . $modelName); if ($item != null && isset($item->id)) { $model = $this->aujaConfigurator->getModel($modelName); $displayField = $this->aujaConfigurator->getDisplayField($model, $config); $header->setText('Edit ' . (isset($item->{$displayField}) ? $item->{$displayField} : $modelName)); $deleteButton = new Button(); $deleteButton->setText(Lang::trans('Delete')); $deleteButton->setConfirmationMessage(Lang::trans('Are you sure?')); $deleteButton->setTarget(URL::route($this->aujaRouter->getDeleteName($modelName), $item->id)); $deleteButton->setMethod('delete'); $header->addButton($deleteButton); } $page->addPageComponent($header); $form = new Form(); $action = $item == null || !isset($item->id) ? URL::route($this->aujaRouter->getStoreName($modelName)) : URL::route($this->aujaRouter->getUpdateName($modelName), $item->id); $form->setAction($action); $form->setMethod($item == null ? 'POST' : 'PUT'); $model = $this->aujaConfigurator->getModel($modelName); $visibleFields = $this->aujaConfigurator->getVisibleFields($model, $config); foreach ($visibleFields as $columnName) { $column = $model->getColumn($columnName); $formItem = $this->formItemFactory->getFormItem($model, $column, $item); $form->addFormItem($formItem); } $submit = new SubmitFormItem(); $submit->setText(Lang::trans('Submit')); $form->addFormItem($submit); $page->addPageComponent($form); return $page; }
/** * Builds a simple menu for given model, where typically this model should not have any relations to other models. * * The menu will include: * - An Add LinkMenuItem; * - A SpacerMenuItem with the model's name; * - A ResourceMenuItem to hold entries of the model. * * @param String $modelName The name of the model. * @param ModelConfig $config (optional) The `ModelConfig` to use. * * @return Menu the Menu, which can be configured further. */ public function create($modelName, ModelConfig $config = null) { $menu = new Menu(); $addMenuItem = new LinkMenuItem(); $addMenuItem->setText(Lang::trans('Add')); $addMenuItem->setIcon(Icons::ion_plus); $addMenuItem->setTarget(URL::route($this->aujaRouter->getCreateName($modelName))); $menu->addMenuItem($addMenuItem); $spacerMenuItem = new SpacerMenuItem(); $spacerMenuItem->setText(Lang::trans($modelName)); $menu->addMenuItem($spacerMenuItem); $resourceMenuItem = new ResourceMenuItem(); $resourceMenuItem->setTarget(URL::route($this->aujaRouter->getIndexName($modelName))); $model = $this->aujaConfigurator->getModel($modelName); if ($this->aujaConfigurator->isSearchable($model, $config)) { $target = urldecode(URL::route($this->aujaRouter->getIndexName($modelName), ['q' => '%s'])); /* urldecode because the '%' gets escaped. */ $property = new Searchable($target); $resourceMenuItem->addProperty($property); } $menu->addMenuItem($resourceMenuItem); return $menu; }
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; }