コード例 #1
0
ファイル: PageFactory.php プロジェクト: hramose/Auja-Laravel
 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;
 }
コード例 #2
0
 /**
  * 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;
 }
コード例 #3
0
 /**
  * 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;
 }