/**
  * Handle the command.
  */
 public function handle()
 {
     /*
      * Set the default buttons handler based
      * on the builder class. Defaulting to
      * no handler.
      */
     if (!$this->builder->getButtons()) {
         $buttons = str_replace('TreeBuilder', 'TreeButtons', get_class($this->builder));
         if (class_exists($buttons)) {
             $this->builder->setButtons($buttons . '@handle');
         }
     }
     /*
      * Set the default segments handler based
      * on the builder class. Defaulting to
      * no handler.
      */
     if (!$this->builder->getSegments()) {
         $segments = str_replace('TreeBuilder', 'TreeSegments', get_class($this->builder));
         if (class_exists($segments)) {
             $this->builder->setSegments($segments . '@handle');
         }
     }
 }
 /**
  * Handle the command.
  */
 public function handle()
 {
     $tree = $this->builder->getTree();
     $model = $this->builder->getModel();
     /**
      * If the model is already instantiated
      * then use it as is.
      */
     if (is_object($model)) {
         $tree->setModel($model);
         return;
     }
     /**
      * If no model is set, try guessing the
      * model based on best practices.
      */
     if (!$model) {
         $parts = explode('\\', str_replace('TreeBuilder', 'Model', get_class($this->builder)));
         unset($parts[count($parts) - 2]);
         $model = implode('\\', $parts);
         $this->builder->setModel($model);
     }
     /**
      * If the model is not set then skip it.
      */
     if (!class_exists($model)) {
         return;
     }
     /**
      * Set the model on the tree!
      */
     $tree->setModel(app($model));
 }
 /**
  * Handle the command.
  */
 public function handle()
 {
     $tree = $this->builder->getTree();
     $model = $tree->getModel();
     /**
      * If a repository is set
      * then we don't have
      * anything to do.
      */
     if ($this->builder->getTreeRepository()) {
         return;
     }
     $repository = $tree->getOption('repository');
     /**
      * If there is no repository
      * then skip this step.
      */
     if (!$repository) {
         return;
     }
     /**
      * Set the repository on the form!
      */
     $tree->setRepository(app()->make($repository, compact('model', 'tree')));
 }
 /**
  * Handle the command.
  *
  * @param ResponseFactory $response
  */
 public function handle(ResponseFactory $response)
 {
     $tree = $this->builder->getTree();
     $options = $tree->getOptions();
     $data = $tree->getData();
     $tree->setResponse($response->view($options->get('wrapper_view', 'streams::blank'), $data));
 }
Example #5
0
 /**
  * Handle the command.
  */
 public function handle()
 {
     $this->builder->fire('saving', ['builder' => $this->builder]);
     $repository = $this->builder->getTreeRepository();
     $repository->save($this->builder);
     $this->builder->fire('saved', ['builder' => $this->builder]);
 }
Example #6
0
 /**
  * Handle the command.
  */
 public function handle()
 {
     $tree = $this->builder->getTree();
     $options = $tree->getOptions();
     $data = $tree->getData();
     $content = view($options->get('tree_view', 'streams::tree/tree'), $data)->render();
     $tree->setContent($content);
     $tree->addData('content', $content);
 }
 /**
  * Normalize button input.
  *
  * @param TreeBuilder $builder
  */
 public function normalize(TreeBuilder $builder)
 {
     $buttons = $builder->getButtons();
     foreach ($buttons as $key => &$button) {
         /*
          * If the button is a string then use
          * it as the button parameter.
          */
         if (is_string($button)) {
             $button = ['button' => $button];
         }
         /*
          * If the key is a string and the button
          * is an array without a button param then
          * move the key into the button as that param.
          */
         if (!is_integer($key) && !isset($button['button'])) {
             $button['button'] = $key;
         }
         /*
          * Make sure some default parameters exist.
          */
         $button['attributes'] = array_get($button, 'attributes', []);
         /*
          * Move the HREF if any to the attributes.
          */
         if (isset($button['href'])) {
             array_set($button['attributes'], 'href', array_pull($button, 'href'));
         }
         /*
          * Move the target if any to the attributes.
          */
         if (isset($button['target'])) {
             array_set($button['attributes'], 'target', array_pull($button, 'target'));
         }
         /*
          * Move all data-* keys
          * to attributes.
          */
         foreach ($button as $attribute => $value) {
             if (str_is('data-*', $attribute)) {
                 array_set($button, 'attributes.' . $attribute, array_pull($button, $attribute));
             }
         }
         /*
          * Make sure the HREF is absolute.
          */
         if (isset($button['attributes']['href']) && is_string($button['attributes']['href']) && !starts_with($button['attributes']['href'], 'http')) {
             $button['attributes']['href'] = url($button['attributes']['href']);
         }
         /*
          * Use small buttons for trees.
          */
         $button['size'] = array_get($button, 'size', 'xs');
     }
     $builder->setButtons($buttons);
 }
 /**
  * Merge in registered properties.
  *
  * @param TreeBuilder $builder
  */
 public function merge(TreeBuilder $builder)
 {
     $buttons = $builder->getButtons();
     foreach ($buttons as &$parameters) {
         if ($button = $this->buttons->get(array_get($parameters, 'button'))) {
             $parameters = array_replace_recursive($button, $parameters);
         }
     }
     $builder->setButtons($buttons);
 }
 /**
  * Handle the command.
  *
  * @param Resolver  $resolver
  * @param Evaluator $evaluator
  */
 public function handle(Resolver $resolver, Evaluator $evaluator)
 {
     $arguments = ['builder' => $this->builder];
     $tree = $this->builder->getTree();
     $options = $this->builder->getOptions();
     $options = $resolver->resolve($options, $arguments);
     $options = $evaluator->evaluate($options, $arguments);
     foreach ($options as $key => $value) {
         $tree->setOption($key, $value);
     }
 }
 /**
  * Handle the command.
  */
 public function handle()
 {
     $tree = $this->builder->getTree();
     /*
      * Set the default options handler based
      * on the builder class. Defaulting to
      * no handler.
      */
     if (!$tree->getOption('options')) {
         $options = str_replace('TreeBuilder', 'TreeOptions', get_class($this->builder));
         if (class_exists($options)) {
             app()->call($options . '@handle', compact('builder', 'tree'));
         }
     }
     /*
      * Set the default data handler based
      * on the builder class. Defaulting to
      * no handler.
      */
     if (!$tree->getOption('data')) {
         $options = str_replace('TreeBuilder', 'TreeData', get_class($this->builder));
         if (class_exists($options)) {
             $tree->setOption('data', $options . '@handle');
         }
     }
     /*
      * Set a optional entries handler based
      * on the builder class. Defaulting to
      * no handler in which case we will use
      * the model and included repositories.
      */
     if (!$tree->getOption('entries')) {
         $entries = str_replace('TreeBuilder', 'TreeEntries', get_class($this->builder));
         if (class_exists($entries)) {
             $tree->setOption('entries', $entries . '@handle');
         }
     }
     /*
      * Set the default options handler based
      * on the builder class. Defaulting to
      * no handler.
      */
     if (!$tree->getOption('repository')) {
         $model = $tree->getModel();
         if (!$tree->getOption('repository') && $model instanceof EntryModel) {
             $tree->setOption('repository', EntryTreeRepository::class);
         }
         if (!$tree->getOption('repository') && $model instanceof EloquentModel) {
             $tree->setOption('repository', EloquentTreeRepository::class);
         }
     }
 }
 /**
  * Build the items.
  *
  * @param TreeBuilder $builder
  */
 public function build(TreeBuilder $builder)
 {
     foreach ($builder->getTreeEntries() as $entry) {
         $segments = $this->segments->build($builder, $entry);
         $buttons = $this->buttons->build($builder, $entry);
         $buttons = $buttons->enabled();
         $id = $entry->getId();
         $parent = $entry->{$builder->getTreeOption('parent_segment', 'parent_id')};
         $item = compact('builder', 'segments', 'buttons', 'entry', 'parent', 'id');
         $item = $this->evaluator->evaluate($item, compact('builder', 'entry'));
         $builder->addTreeItem($this->factory->make($item));
     }
 }
Example #12
0
 /**
  * Handle the command.
  *
  * @param  Asset      $asset
  * @throws \Exception
  */
 public function handle(Asset $asset)
 {
     foreach ($this->builder->getAssets() as $collection => $assets) {
         if (!is_array($assets)) {
             $assets = [$assets];
         }
         foreach ($assets as $file) {
             $filters = explode('|', $file);
             $file = array_shift($filters);
             $asset->add($collection, $file, $filters);
         }
     }
 }
 /**
  * Handle the command.
  */
 public function handle()
 {
     $tree = $this->builder->getTree();
     $model = $this->builder->getModel();
     if (is_string($model) && !class_exists($model)) {
         return;
     }
     if (is_string($model)) {
         $model = app($model);
     }
     if ($model instanceof EntryInterface) {
         $tree->setStream($model->getStream());
     }
 }
 /**
  * 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);
     }
 }
 /**
  * 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']);
         }
     }
 }
 /**
  * Normalize the segment input.
  *
  * @param TreeBuilder $builder
  */
 public function normalize(TreeBuilder $builder)
 {
     $segments = $builder->getSegments();
     foreach ($segments as $key => &$segment) {
         /**
          * If the key is non-numerical then
          * use it as the header and use the
          * segment as the segment if it's a class.
          */
         if (!is_numeric($key) && !is_array($segment) && class_exists($segment)) {
             $segment = ['heading' => $key, 'segment' => $segment];
         }
         /**
          * If the key is non-numerical then
          * use it as the header and use the
          * segment as the value.
          */
         if (!is_numeric($key) && !is_array($segment) && !class_exists($segment)) {
             $segment = ['heading' => $key, 'value' => $segment];
         }
         /**
          * If the segment is not already an
          * array then treat it as the value.
          */
         if (!is_array($segment)) {
             $segment = ['value' => $segment];
         }
         /**
          * Move all data-* keys
          * to attributes.
          */
         foreach ($segment as $attribute => $value) {
             if (str_is('data-*', $attribute)) {
                 array_set($segment, 'attributes.' . $attribute, array_pull($segment, $attribute));
             }
         }
         /**
          * If no value wrap is set
          * then use a default.
          */
         array_set($segment, 'wrapper', array_get($segment, 'wrapper', '{value}'));
         /**
          * If there is no value then use NULL
          */
         array_set($segment, 'value', array_get($segment, 'value', null));
     }
     $builder->setSegments($segments);
 }
 /**
  * Build the buttons.
  *
  * @param TreeBuilder  $builder
  * @param              $entry
  * @return ButtonCollection
  */
 public function build(TreeBuilder $builder, $entry)
 {
     $tree = $builder->getTree();
     $buttons = new ButtonCollection();
     $this->input->read($builder, $entry);
     foreach ($builder->getButtons() as $button) {
         if (!array_get($button, 'enabled', true)) {
             continue;
         }
         $button = $this->evaluator->evaluate($button, compact('entry', 'tree'));
         $button = $this->parser->parse($button, $entry);
         $button = $this->factory->make($button);
         $buttons->push($button);
     }
     return $buttons;
 }
Example #18
0
 /**
  * Handle the command.
  *
  * @param Container            $container
  * @param ViewTemplate         $template
  * @param BreadcrumbCollection $breadcrumbs
  */
 public function handle(Container $container, ViewTemplate $template, BreadcrumbCollection $breadcrumbs)
 {
     $tree = $this->builder->getTree();
     $tree->addData('tree', $tree);
     if ($handler = $tree->getOption('data')) {
         $container->call($handler, compact('tree'));
     }
     if ($layout = $tree->getOption('layout_view')) {
         $template->put('layout', $layout);
     }
     if ($title = $tree->getOption('title')) {
         $template->put('title', $title);
     }
     if ($breadcrumb = $tree->getOption('breadcrumb')) {
         $breadcrumbs->put($breadcrumb, '#');
     }
 }
 /**
  * Guess the HREF for a button.
  *
  * @param TreeBuilder $builder
  */
 public function guess(TreeBuilder $builder)
 {
     $buttons = $builder->getButtons();
     // Nothing to do if empty.
     if (!($section = $this->sections->active())) {
         return;
     }
     foreach ($buttons as &$button) {
         // If we already have an HREF then skip it.
         if (isset($button['attributes']['href'])) {
             continue;
         }
         // Determine the HREF based on the button type.
         if ($type = array_get($button, 'button')) {
             $button['attributes']['href'] = $section->getHref($type . '/{entry.id}');
         }
     }
     $builder->setButtons($buttons);
 }
 /**
  * Build the segments.
  *
  * @param  TreeBuilder       $builder
  * @param                    $entry
  * @return SegmentCollection
  */
 public function build(TreeBuilder $builder, $entry)
 {
     $tree = $builder->getTree();
     $segments = new SegmentCollection();
     if (!$builder->getSegments()) {
         $builder->setSegments(['entry.edit_link']);
     }
     $this->input->read($builder, $entry);
     foreach ($builder->getSegments() as $segment) {
         array_set($segment, 'entry', $entry);
         $segment = $this->evaluator->evaluate($segment, compact('entry', 'tree'));
         if (array_get($segment, 'enabled', null) === false) {
             continue;
         }
         $segment['value'] = $this->value->make($tree, $segment, $entry);
         $segments->push($this->factory->make($segment));
     }
     return $segments;
 }
 /**
  * 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));
     }
 }
 /**
  * Translate the tree segments.
  *
  * @param TreeBuilder $builder
  */
 public function translate(TreeBuilder $builder)
 {
     $builder->setSegments($this->translator->translate($builder->getSegments()));
 }
Example #23
0
 /**
  * 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;
 }
 /**
  * Parse the segments.
  *
  * @param TreeBuilder $builder
  */
 public function parse(TreeBuilder $builder)
 {
     $builder->setSegments($this->parser->parse($builder->getSegments()));
 }
 /**
  * Set the tree entries.
  *
  * @param \Illuminate\Support\Collection $entries
  * @return $this
  */
 public function setTreeEntries(\Illuminate\Support\Collection $entries)
 {
     if (!$this->getFieldType()) {
         $entries = $entries->sort(function ($a, $b) {
             return array_search($a->id, $this->getSelected()) - array_search($b->id, $this->getSelected());
         });
     }
     return parent::setTreeEntries($entries);
 }
 /**
  * Resolve tree views.
  *
  * @param TreeBuilder $builder
  */
 public function resolve(TreeBuilder $builder)
 {
     $this->resolver->resolve($builder->getButtons(), compact('builder'));
 }