Completed
Push — master ( 798e92...ea2dad )
by Yaro
04:12 queued 26s
created

AliasesTrait::addFields()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

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