/** * Handle the command. */ public function handle() { $model = $this->builder->getModel(); /* * If the builder has an entries handler * then call it through the container and * let it load the entries itself. */ if ($handler = $this->builder->getTreeOption('entries')) { app()->call($handler, ['builder' => $this->builder]); return; } $entries = $this->builder->getTreeEntries(); /* * If the entries have already been set on the * tree then return. Nothing to do here. * * If the model is not set then they need * to load the tree entries themselves. */ if (!$entries->isEmpty() || !$model) { return; } /* * Resolve the model out of the container. */ $repository = $this->builder->getTreeRepository(); /* * If the repository is an instance of * TreeRepositoryInterface use it. */ if ($repository instanceof TreeRepositoryInterface) { $this->builder->setTreeEntries($repository->get($this->builder)); } }
/** * Authorize the tree. * * @param TreeBuilder $builder */ public function authorize(TreeBuilder $builder) { // Try the option first. $permission = $builder->getTreeOption('permission'); /* * If the option is not set then * try and automate the permission. */ if (!$permission && ($module = $this->modules->active()) && ($stream = $builder->getTreeStream())) { $permission = $module->getNamespace($stream->getSlug() . '.read'); } if (!$this->authorizer->authorize($permission)) { abort(403); } }
/** * Return the item value. * * @param TreeBuilder $builder * @param $entry * @return View|mixed|null */ public function make(TreeBuilder $builder, $entry) { $value = $builder->getTreeOption('item_value', 'entry.title'); /* * If the entry is an instance of EntryInterface * then try getting the field value from the entry. */ if ($entry instanceof EntryInterface && $entry->getField($value)) { if ($entry->assignmentIsRelationship($value)) { $value = $entry->{camel_case($value)}->getTitle(); } else { $value = $entry->getFieldValue($value); } } /* * If the value matches a field with a relation * then parse the string using the eager loaded entry. */ if (preg_match("/^entry.([a-zA-Z\\_]+)/", $value, $match)) { $fieldSlug = camel_case($match[1]); if (method_exists($entry, $fieldSlug) && $entry->{$fieldSlug}() instanceof Relation) { $entry = $this->decorator->decorate($entry); $value = data_get(compact('entry'), str_replace("entry.{$match[1]}.", 'entry.' . camel_case($match[1]) . '.', $value)); } } /* * Decorate the entry object before * sending to decorate so that data_get() * can get into the presenter methods. */ $entry = $this->decorator->decorate($entry); /* * If the value matches a method in the presenter. */ if (preg_match("/^entry.([a-zA-Z\\_]+)/", $value, $match)) { if (method_exists($entry, camel_case($match[1]))) { $value = $entry->{camel_case($match[1])}(); } } /* * By default we can just pass the value through * the evaluator utility and be done with it. */ $value = $this->evaluator->evaluate($value, compact('builder', 'entry')); /* * Lastly, prepare the entry to be * parsed into the string. */ if ($entry instanceof Arrayable) { $entry = $entry->toArray(); } else { $entry = null; } /* * Parse the value with the entry. */ $value = $this->parser->render($builder->getTreeOption('item_wrapper', '{value}'), compact('value', 'entry')); /* * If the value looks like a language * key then try translating it. */ if (str_is('*.*.*::*', $value)) { $value = trans($value); } return $value; }
/** * Save the tree. * * @param TreeBuilder $builder * @param array $items * @param null $parent */ public function save(TreeBuilder $builder, array $items = [], $parent = null) { $model = $builder->getTreeModel(); $items = $items ?: $builder->getRequestValue('items'); foreach ($items as $index => $item) { /* @var EloquentModel $entry */ $entry = $model->find($item['id']); $entry->{$builder->getTreeOption('sort_column', 'sort_order')} = $index + 1; $entry->{$builder->getTreeOption('parent_column', 'parent_id')} = $parent; $entry->save(); if (isset($item['children'])) { $this->save($builder, $item['children'], $item['id']); } } }