/**
  * Handle the command.
  *
  * @param Request   $request
  * @param Container $container
  */
 public function handle(Request $request, ViewHandler $handler)
 {
     $table = $this->builder->getTable();
     $options = $table->getOptions();
     $views = $table->getViews();
     if ($views->active()) {
         return;
     }
     if ($view = $views->findBySlug($request->get($options->get('prefix') . 'view'))) {
         $view->setActive(true);
     }
     if (!$view && ($view = $views->first())) {
         $view->setActive(true);
     }
     // Nothing to do.
     if (!$view) {
         return;
     }
     // Set columns from active view.
     if (($columns = $view->getColumns()) !== null) {
         $this->builder->setColumns($columns);
     }
     // Set buttons from active view.
     if (($buttons = $view->getButtons()) !== null) {
         $this->builder->setButtons($buttons);
     }
     // Set actions from active view.
     if (($actions = $view->getActions()) !== null) {
         $this->builder->setActions($actions);
     }
     $handler->handle($this->builder, $view);
 }
 /**
  * Set defaults.
  *
  * @param TableBuilder $builder
  */
 public function defaults(TableBuilder $builder)
 {
     $stream = $builder->getTableStream();
     if ($builder->getColumns() == []) {
         $builder->setColumns([$stream->getTitleColumn()]);
     }
 }
 /**
  * Normalize the column input.
  *
  * @param TableBuilder $builder
  */
 public function normalize(TableBuilder $builder)
 {
     $columns = $builder->getColumns();
     foreach ($columns as $key => &$column) {
         /*
          * If the key is non-numerical then
          * use it as the header and use the
          * column as the column if it's a class.
          */
         if (!is_numeric($key) && !is_array($column) && class_exists($column)) {
             $column = ['heading' => $key, 'column' => $column];
         }
         /*
          * If the key is non-numerical then
          * use it as the header and use the
          * column as the value.
          */
         if (!is_numeric($key) && !is_array($column) && !class_exists($column)) {
             $column = ['heading' => $key, 'value' => $column];
         }
         /*
          * If the column is not already an
          * array then treat it as the value.
          */
         if (!is_array($column)) {
             $column = ['value' => $column];
         }
         /*
          * If the key is non-numerical and
          * the column is an array without
          * a heading then use the key.
          */
         if (!is_numeric($key) && is_array($column) && !array_has($column, 'field')) {
             $column['field'] = $key;
         }
         /*
          * If the key is non-numerical and
          * the column is an array without
          * a value then use the key.
          */
         if (!is_numeric($key) && is_array($column) && !isset($column['value'])) {
             $column['value'] = $key;
         }
         /*
          * If no value wrap is set
          * then use a default.
          */
         array_set($column, 'wrapper', array_get($column, 'wrapper', '{value}'));
         /*
          * If there is no value then use NULL
          */
         array_set($column, 'value', array_get($column, 'value', null));
     }
     $builder->setColumns($columns);
 }
 /**
  * Evaluate the table columns.
  *
  * @param TableBuilder $builder
  */
 public function evaluate(TableBuilder $builder)
 {
     $builder->setColumns($this->evaluator->evaluate($builder->getColumns(), compact('builder')));
 }
 /**
  * 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);
 }
 /**
  * Guess the field for a column.
  *
  * @param TableBuilder $builder
  */
 public function guess(TableBuilder $builder)
 {
     $columns = $builder->getColumns();
     $stream = $builder->getTableStream();
     $module = $this->modules->active();
     foreach ($columns as &$column) {
         /*
          * If the heading is already set then
          * we don't have anything to do.
          */
         if (isset($column['heading'])) {
             continue;
         }
         /*
          * If the heading is false, then no
          * header is desired at all.
          */
         if (isset($column['heading']) && $column['heading'] === false) {
             continue;
         }
         /*
          * No stream means we can't
          * really do much here.
          */
         if (!$stream instanceof StreamInterface) {
             continue;
         }
         if (!isset($column['field']) && is_string($column['value'])) {
             $column['field'] = $column['value'];
         }
         /*
          * If the heading matches a field
          * with dot format then reduce it.
          */
         if (isset($column['field']) && preg_match("/^entry.([a-zA-Z\\_]+)/", $column['field'], $match)) {
             $column['field'] = $match[1];
         }
         /*
          * Detect some built in columns.
          */
         if (in_array($column['field'], ['id', 'created_at', 'created_by', 'updated_at', 'updated_by'])) {
             $column['heading'] = 'streams::entry.' . $column['field'];
             continue;
         }
         /*
          * Detect entry title.
          */
         if (in_array($column['field'], ['view_link', 'edit_link']) && ($field = $stream->getTitleField())) {
             $column['heading'] = $field->getName();
             continue;
         }
         $field = $stream->getField(array_get($column, 'field'));
         /*
          * Detect the title column.
          */
         $title = $stream->getTitleField();
         if ($title && !$field && $column['field'] == 'title' && $this->translator->has($heading = $title->getName())) {
             $column['heading'] = $heading;
         }
         /*
          * Use the name from the field.
          */
         if ($field && ($heading = $field->getName())) {
             $column['heading'] = $heading;
         }
         /*
          * If no field look for
          * a name anyways.
          */
         if ($module && !$field && $this->translator->has($heading = $module->getNamespace('field.' . $column['field'] . '.name'))) {
             $column['heading'] = $heading;
         }
         /*
          * If no translatable heading yet and
          * the heading matches the value (default)
          * then humanize the heading value.
          */
         if (!isset($column['heading']) && $this->config->get('streams::system.lazy_translations')) {
             $column['heading'] = ucwords($this->string->humanize($column['field']));
         }
         /*
          * If we have a translatable heading and
          * the heading does not have a translation
          * then humanize the heading value.
          */
         if (isset($column['heading']) && str_is('*.*.*::*', $column['heading']) && !$this->translator->has($column['heading']) && $this->config->get('streams::system.lazy_translations')) {
             $column['heading'] = ucwords($this->string->humanize($column['field']));
         }
         /*
          * Last resort.
          */
         if ($module && !isset($column['heading'])) {
             $column['heading'] = $module->getNamespace('field.' . $column['field'] . '.name');
         }
     }
     $builder->setColumns($columns);
 }