private function smartIncludeMenuItems($main, $config) { foreach ($this->aujaConfigurator->getModels() as $model) { if ($this->aujaConfigurator->shouldSmartIncludeInMain($model, $config)) { $item = new Item(); $item->setTitle($model->getName()); $item->setIcon($this->aujaConfigurator->getIcon($model, $config)); $item->setTarget(Url::route($this->aujaRouter->getMenuName($model->getName()))); $main->addItem($item); } } }
/** * 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; }