Completed
Push — master ( f066c0...72d72a )
by Yaro
01:50 queued 10s
created

AliasesTrait::softDeletes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
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
    protected function admin()
18
    {
19
        return admin_user();
20
    }
21
22
    /**
23
     * Add locales for all translatable fields.
24
     *
25
     * @param array $locales
26
     */
27
    protected function locales(array $locales)
28
    {
29
        $this->crud()->locales($locales);
30
    }
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
    protected function addColumn($column)
49
    {
50
        $this->crud()->addColumn($column);
51
    }
52
53
    protected function addColumns(array $columns)
54
    {
55
        foreach ($columns as $column) {
56
            $this->addColumn($column);
57
        }
58
    }
59
60 13
    protected function addField(AbstractField $field)
61
    {
62 13
        $this->crud()->addField($field);
63 13
    }
64
65 13
    protected function addFields(array $fields)
66
    {
67 13
        foreach ($fields as $field) {
68 13
            $this->addField($field);
69
        }
70 13
    }
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 13
    protected function setModel($model)
81
    {
82 13
        $this->crud()->setModel($model);
83 13
    }
84
85
    protected function paginate($perPage)
86
    {
87
        $this->crud()->paginate($perPage);
88
    }
89
90
    protected function order(string $column, string $direction = 'asc')
91
    {
92
        $this->crud()->order($column, $direction);
93
    }
94
95 13
    protected function filter(\Closure $callback)
96
    {
97 13
        $this->crud()->filter($callback);
98 13
    }
99
100
    protected function action($ident)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
101
    {
102
        return $this->crud()->actions()->find($ident);
103
    }
104
105
    protected function removeAction($ident)
106
    {
107
        $this->crud()->actions()->remove($ident);
108
    }
109
110
    public function enableBatchCheckboxes(bool $enabled = true)
111
    {
112
        $this->crud()->enableBatchCheckboxes($enabled);
113
    }
114
115
    /**
116
     * Enable soft deletes for table.
117
     *
118
     * @param bool $enabled
119
     */
120 13
    public function softDeletes(bool $enabled = true)
121
    {
122 13
        $this->crud()->enableSoftDelete($enabled);
123 13
    }
124
125
    /**
126
     * Allows to reorder table rows.
127
     *
128
     * @param string $field
129
     */
130
    public function sortable(string $field)
131
    {
132
        $this->crud()->enableSortableByWeight($field);
133
    }
134
135
    /**
136
     * Add row action button with optional changing order.
137
     *
138
     * @param AbstractAction $action
139
     * @param null|string $moveDirection Move action 'before' or 'after' $baseActionIdent
140
     * @param null|string $baseActionIdent
141
     */
142
    protected function addAction(AbstractAction $action, $moveDirection = null, $baseActionIdent = null)
143
    {
144
        $this->crud()->actions()->add($action);
145
        if (!$moveDirection || !$baseActionIdent) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $moveDirection of type null|string is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
Bug Best Practice introduced by
The expression $baseActionIdent of type null|string is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
146
            return;
147
        }
148
149
        if ($moveDirection == 'after') {
150
            $this->crud()->actions()->moveAfter($baseActionIdent, $action->identifier());
151
        } else {
152
            $this->crud()->actions()->moveBefore($baseActionIdent, $action->identifier());
153
        }
154
    }
155
156
    protected function addActions(array $actions)
157
    {
158
        $this->crud()->actions()->add($actions);
159
    }
160
161
    protected function setActions(array $actions = [])
162
    {
163
        $this->crud()->actions()->set($actions);
164
    }
165
166
    abstract protected function crud(): CRUD;
167
}
168