Completed
Push — master ( 8bf128...f3c232 )
by Yaro
03:51
created

AliasesTrait::setRowAttributes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Yaro\Jarboe\Http\Controllers\Traits;
4
5
use Closure;
6
use Yaro\Jarboe\Table\Actions\AbstractAction;
7
use Yaro\Jarboe\Table\CRUD;
8
use Yaro\Jarboe\Table\Fields\AbstractField;
9
use Yaro\Jarboe\Table\Toolbar\Interfaces\ToolInterface;
10
11
trait AliasesTrait
12
{
13
    /**
14
     * Get admin user object.
15
     *
16
     * @return \Illuminate\Contracts\Auth\Authenticatable|null
17
     */
18 21
    protected function admin()
19
    {
20 21
        return admin_user();
21
    }
22
23
    /**
24
     * Add locales for all translatable fields.
25
     *
26
     * @param array $locales
27
     */
28 1
    protected function locales(array $locales)
29
    {
30 1
        $this->crud()->locales($locales);
31 1
    }
32
33 1
    protected function addTools(array $tools)
34
    {
35 1
        foreach ($tools as $tool) {
36 1
            $this->addTool($tool);
37
        }
38 1
    }
39
40 2
    protected function addTool(ToolInterface $tool)
41
    {
42 2
        $tool->setCrud($this->crud());
43 2
        $this->crud()->addTool($tool);
44 2
    }
45
46
    /**
47
     * @param string|AbstractField $column
48
     */
49 5
    protected function addColumn($column)
50
    {
51 5
        $this->crud()->addColumn($column);
52 5
    }
53
54 1
    protected function addColumns(array $columns)
55
    {
56 1
        foreach ($columns as $column) {
57 1
            $this->addColumn($column);
58
        }
59 1
    }
60
61 50
    protected function addField(AbstractField $field)
62
    {
63 50
        $this->crud()->addField($field);
64 50
    }
65
66 50
    protected function addFields(array $fields)
67
    {
68 50
        foreach ($fields as $field) {
69 50
            $this->addField($field);
70
        }
71 50
    }
72
73
    protected function addTab($title, array $fields)
74
    {
75
        foreach ($fields as $field) {
76
            $field->tab($title);
77
            $this->addField($field);
78
        }
79
    }
80
81 49
    protected function setModel($model)
82
    {
83 49
        $this->crud()->setModel($model);
84 49
    }
85
86 2
    protected function paginate($perPage)
87
    {
88 2
        $this->crud()->paginate($perPage);
89 2
    }
90
91
    protected function order(string $column, string $direction = 'asc')
92
    {
93
        $this->crud()->order($column, $direction);
94
    }
95
96 49
    protected function filter(\Closure $callback)
97
    {
98 49
        $this->crud()->filter($callback);
99 49
    }
100
101
    /**
102
     * @param $ident
103
     * @return AbstractAction|null
104
     */
105
    protected function action($ident)
106
    {
107
        return $this->crud()->actions()->find($ident);
108
    }
109
110
    protected function removeAction($ident)
111
    {
112
        $this->crud()->actions()->remove($ident);
113
    }
114
115 1
    public function enableBatchCheckboxes(bool $enabled = true)
116
    {
117 1
        $this->crud()->enableBatchCheckboxes($enabled);
118 1
    }
119
120
    /**
121
     * Enable soft deletes for table.
122
     *
123
     * @param bool $enabled
124
     */
125 49
    public function softDeletes(bool $enabled = true)
126
    {
127 49
        $this->crud()->enableSoftDelete($enabled);
128 49
    }
129
130
    /**
131
     * Allows to reorder table rows.
132
     *
133
     * @param string $field
134
     */
135 1
    public function sortable(string $field)
136
    {
137 1
        $this->crud()->enableSortableByWeight($field);
138 1
    }
139
140
    /**
141
     * Add row action button with optional changing order.
142
     *
143
     * @param AbstractAction $action
144
     * @param null|string $moveDirection Move action 'before' or 'after' $baseActionIdent
145
     * @param null|string $baseActionIdent
146
     */
147
    protected function addAction(AbstractAction $action, $moveDirection = null, $baseActionIdent = null)
148
    {
149
        $this->crud()->actions()->add($action);
150
        if (is_null($moveDirection) || is_null($baseActionIdent)) {
151
            return;
152
        }
153
154
        if ($moveDirection == 'after') {
155
            $this->crud()->actions()->moveAfter($baseActionIdent, $action->identifier());
156
        } else {
157
            $this->crud()->actions()->moveBefore($baseActionIdent, $action->identifier());
158
        }
159
    }
160
161
    protected function addActions(array $actions)
162
    {
163
        $this->crud()->actions()->add($actions);
164
    }
165
166
    protected function setActions(array $actions = [])
167
    {
168
        $this->crud()->actions()->set($actions);
169
    }
170
171
    /**
172
     * Set closure for setting custom attributes to `<tr>`.
173
     *
174
     * @param Closure $closure
175
     */
176
    public function setRowAttributes(Closure $closure)
177
    {
178
        $this->crud()->setRowAttributes($closure);
179
    }
180
181
    abstract protected function crud(): CRUD;
182
}
183