1
|
|
|
<?php namespace Anomaly\Streams\Platform\Ui\Table\Command; |
2
|
|
|
|
3
|
|
|
use Anomaly\Streams\Platform\Addon\Module\ModuleCollection; |
4
|
|
|
use Anomaly\Streams\Platform\Ui\Table\TableBuilder; |
5
|
|
|
use Illuminate\Contracts\Bus\SelfHandling; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class SetDefaultOptions |
9
|
|
|
* |
10
|
|
|
* @link http://anomaly.is/streams-platform |
11
|
|
|
* @author AnomalyLabs, Inc. <[email protected]> |
12
|
|
|
* @author Ryan Thompson <[email protected]> |
13
|
|
|
* @package Anomaly\Streams\Platform\Ui\Table\Command |
14
|
|
|
*/ |
15
|
|
|
class SetDefaultOptions implements SelfHandling |
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* The table builder. |
20
|
|
|
* |
21
|
|
|
* @var TableBuilder |
22
|
|
|
*/ |
23
|
|
|
protected $builder; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Create a new SetDefaultOptions instance. |
27
|
|
|
* |
28
|
|
|
* @param TableBuilder $builder |
29
|
|
|
*/ |
30
|
|
|
public function __construct(TableBuilder $builder) |
31
|
|
|
{ |
32
|
|
|
$this->builder = $builder; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Handle the command. |
37
|
|
|
* |
38
|
|
|
* @param ModuleCollection $modules |
39
|
|
|
*/ |
40
|
|
|
public function handle(ModuleCollection $modules) |
41
|
|
|
{ |
42
|
|
|
$table = $this->builder->getTable(); |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Set the default sortable option. |
46
|
|
|
*/ |
47
|
|
|
if ($table->getOption('sortable') === null) { |
48
|
|
|
|
49
|
|
|
$stream = $table->getStream(); |
50
|
|
|
|
51
|
|
View Code Duplication |
if ($stream && $stream->isSortable()) { |
|
|
|
|
52
|
|
|
$table->setOption('sortable', true); |
53
|
|
|
$table->setOption('limit', $table->getOption('limit', 9999)); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Set the default limit if the table is sortable |
59
|
|
|
*/ |
60
|
|
View Code Duplication |
if (($table->getOption('sortable') === true) && ($table->getOption('limit') === null)) { |
|
|
|
|
61
|
|
|
$table->setOption('limit', $table->getOption('limit', 9999)); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Set the default breadcrumb. |
66
|
|
|
*/ |
67
|
|
|
if ($table->getOption('breadcrumb') === null && $title = $table->getOption('title')) { |
|
|
|
|
68
|
|
|
$table->setOption('breadcrumb', $title); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* If the table ordering is currently being overridden |
73
|
|
|
* then set the values from the request on the builder |
74
|
|
|
* last so it actually has an effect. |
75
|
|
|
*/ |
76
|
|
|
if ($orderBy = $this->builder->getRequestValue('order_by')) { |
77
|
|
|
$table->setOption('order_by', [$orderBy => $this->builder->getRequestValue('sort', 'asc')]); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* If the permission is not set then |
82
|
|
|
* try and automate it. |
83
|
|
|
*/ |
84
|
|
|
if ($table->getOption('permission') === null && ($module = $modules->active()) && ($stream = $this->builder->getTableStream()) |
85
|
|
|
) { |
86
|
|
|
$table->setOption('permission', $module->getNamespace($stream->getSlug() . '.read')); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.