/** * Handle the command. */ public function handle() { $stream = $this->builder->getTableStream(); if (!$stream instanceof StreamInterface) { return; } $eager = []; if ($stream->isTranslatable()) { $eager[] = 'translations'; } $assignments = $stream->getRelationshipAssignments(); foreach ($this->builder->getColumns() as $column) { /** * If the column value is a string and uses a dot * format then check if it's a relation. */ if (isset($column['value']) && is_string($column['value']) && preg_match("/^entry.([a-zA-Z\\_]+)./", $column['value'], $match)) { if ($assignment = $assignments->findByFieldSlug($match[1])) { if ($assignment->getFieldType()->getNamespace() == 'anomaly.field_type.polymorphic') { continue; } $eager[] = camel_case($match[1]); } } } $this->builder->setTableOption('eager', array_unique($this->builder->getTableOption('eager', []) + $eager)); }
/** * Set the active action. * * @param SetActiveAction $command */ public function handle() { $prefix = $this->builder->getTableOption('prefix'); $actions = $this->builder->getTableActions(); if ($action = $actions->findBySlug(app('request')->get($prefix . 'action'))) { $action->setActive(true); } }
/** * Authorize the table. * * @param TableBuilder $builder */ public function authorize(TableBuilder $builder) { // Try the option first. $permission = $builder->getTableOption('permission'); if ($permission && !$this->authorizer->authorize($permission)) { abort(403); } }
/** * Normalize action input. * * @param TableBuilder $builder */ public function normalize(TableBuilder $builder) { $actions = $builder->getActions(); $prefix = $builder->getTableOption('prefix'); foreach ($actions as $slug => &$action) { $action = $this->process($prefix, $slug, $action); } $builder->setActions($actions); }
/** * Save the order of the entries. * * @param SectionCollection $sections * @param TableBuilder $builder * @param array $selected */ public function handle(SectionCollection $sections, Redirector $redirector, TableBuilder $builder, array $selected) { $prefix = $builder->getTableOption('prefix'); $edit = array_shift($selected); $ids = implode(',', $selected); if ($section = $sections->active()) { $builder->setTableResponse($redirector->to($section->getHref('edit/' . $edit . '?' . $prefix . 'edit_next=' . $ids))); } }
/** * Set the active filter. * * @param $slug * @param TableBuilder $builder */ protected function setActiveFilter($slug, TableBuilder $builder) { /* @var FilterInterface $filter */ foreach ($builder->getTableFilters() as $filter) { if ($filter->getSlug() === $slug) { $filter->setPrefix($builder->getTableOption('prefix')); $filter->setActive(true); break; } } }
/** * Set the active action. * * @param $slug * @param TableBuilder $builder */ protected function setActiveAction($slug, TableBuilder $builder) { /* @var ActionInterface $action */ foreach ($builder->getTableActions() as $action) { if ($action->getSlug() === $slug) { $action->setPrefix($builder->getTableOption('prefix')); $action->setActive(true); break; } } }
/** * Save the order of the entries. * * @param TableBuilder $builder * @param Request $request */ public function handle(TableBuilder $builder, Request $request) { $count = 0; $model = $builder->getTableModel(); /* @var EloquentModel $entry */ foreach ($request->get($builder->getTableOption('prefix') . 'order', []) as $k => $id) { if ($entry = $model->find($id)) { $entry->sort_order = $k + 1; $entry->save(); $count++; } } $builder->fire('reordered', compact('count', 'builder')); $this->messages->success(trans('streams::message.reorder_success', compact('count'))); }
/** * Guess the sortable flags for headers. * * @param TableBuilder $builder */ public function guess(TableBuilder $builder) { $columns = $builder->getColumns(); $stream = $builder->getTableStream(); foreach ($columns as &$column) { if ($builder->getTableOption('sortable_headers') === false) { $column['sortable'] = false; continue; } /* * If the heading is false or does not exist * then the intent was to not have * heading text at all. */ if (!isset($column['heading']) || $column['heading'] === false) { continue; } /* * If sortable is already set the we don't * need to guess anything. */ if (isset($column['sortable'])) { continue; } /* * If the sort column is set and * sortable is not yet, set it. */ if (isset($column['sort_column'])) { $column['sortable'] = true; continue; } /* * No stream means we can't * really do much here. */ if (!$stream instanceof StreamInterface) { continue; } /* * We're going to be using the value to * try and determine if a streams field is * being used. No value, no guess. */ if (!isset($column['value']) || !$column['value'] || !is_string($column['value'])) { continue; } /* * Now we're going to try and determine * what streams field this column if * using if any at all. */ $field = $column['value']; /* * If the value matches a field * with dot format then reduce it. */ if (preg_match("/^entry.([a-zA-Z\\_]+)/", $column['value'], $match)) { $field = $match[1]; } /* * If we can't determine a field type * then we don't have anything to base * our guess off of. */ if (!($assignment = $stream->getAssignment($field))) { continue; } $type = $assignment->getFieldType(); /* * If the field type has a database * column type then we can sort on it * by default! * * @todo: Allow sorting of translatable fields. */ if ($type->getColumnType() && !$assignment->isTranslatable()) { $column['sortable'] = true; $column['sort_column'] = $type->getColumnName(); } else { $column['sortable'] = false; } } $builder->setColumns($columns); }
/** * Predict the presence of of the sortable action. * * @param TableBuilder $builder */ public function predict(TableBuilder $builder) { if ($builder->getTableOption('sortable')) { $builder->setActions(array_merge(['reorder'], $builder->getActions())); } }
/** * Get the table entries. * * @param TableBuilder $builder * @return Collection */ public function get(TableBuilder $builder) { // Grab any stream we have. $stream = $builder->getTableStream(); // Start a new query. $query = $this->model->newQuery(); /* * Prevent joins from overriding intended columns * by prefixing with the model's table name. */ $query = $query->select($this->model->getTable() . '.*'); /* * Eager load any relations to * save resources and queries. */ $query = $query->with($builder->getTableOption('eager', [])); /* * Raise and fire an event here to allow * other things (including filters / views) * to modify the query before proceeding. */ $builder->fire('querying', compact('builder', 'query')); app('events')->fire(new TableIsQuerying($builder, $query)); /* * Before we actually adjust the baseline query * set the total amount of entries possible back * on the table so it can be used later. */ $total = $query->count(); $builder->setTableOption('total_results', $total); /* * Assure that our page exists. If the page does * not exist then start walking backwards until * we find a page that is has something to show us. */ $limit = (int) $builder->getTableOption('limit', config('streams::system.per_page', 15)); $page = app('request')->get($builder->getTableOption('prefix') . 'page', 1); $offset = $limit * ($page - 1); if ($total < $offset && $page > 1) { $url = str_replace($builder->getTableOption('prefix') . 'page=' . $page, $builder->getTableOption('prefix') . 'page=' . ($page - 1), app('request')->fullUrl()); header('Location: ' . $url); } /* * Limit the results to the limit and offset * based on the page if any. */ $offset = $limit * (app('request')->get($builder->getTableOption('prefix') . 'page', 1) - 1); $query = $query->take($limit)->offset($offset); /* * Order the query results. */ if ($order = $builder->getTableOption('order_by')) { foreach ($order as $column => $direction) { if ($stream && ($utility = $stream->getFieldTypeQuery($column))) { $utility->orderBy($query, $direction); } else { $query = $query->orderBy($column, $direction); } } } if ($builder->getTableOption('sortable')) { $query = $query->orderBy('sort_order', 'ASC'); } return $query->get(); }