Completed
Push — master ( d35733...78d2ff )
by Arjay
01:13
created

Button::actionClose()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Yajra\DataTables\Html;
4
5
use Illuminate\Support\Fluent;
6
use Illuminate\Contracts\Support\Arrayable;
7
use Illuminate\Contracts\Auth\Access\Authorizable;
8
9
class Button extends Fluent implements Arrayable
10
{
11
    /**
12
     * Flag to check if user is authorized to use the button.
13
     *
14
     * @var bool
15
     */
16
    protected $authorized = true;
17
18
    /**
19
     * Make a button if condition is true.
20
     *
21
     * @param bool|callable $condition
22
     * @param string|array $options
23
     * @return Button
24
     */
25
    public static function makeIf($condition, $options)
26
    {
27
        if (value($condition)) {
28
            return static::make($options);
29
        }
30
31
        return static::make()->authorized(false);
32
    }
33
34
    /**
35
     * Make a new button instance.
36
     *
37
     * @param string|array $options
38
     * @return Button
39
     */
40
    public static function make($options = [])
41
    {
42
        if (is_string($options)) {
43
            return new static(['extend' => $options]);
44
        }
45
46
        return new static($options);
47
    }
48
49
    /**
50
     * Set authotization status of the button.
51
     *
52
     * @param bool|callable $bool
53
     * @return $this
54
     */
55
    public function authorized($bool)
56
    {
57
        $this->authorized = value($bool);
58
59
        return $this;
60
    }
61
62
    /**
63
     * Make a raw button that does not extend anything.
64
     *
65
     * @param array $options
66
     * @return \Yajra\DataTables\Html\Button
67
     */
68
    public static function raw($options = [])
69
    {
70
        if (is_string($options)) {
71
            return new static(['text' => $options]);
72
        }
73
74
        return new static($options);
75
    }
76
77
    /**
78
     * Make a button if condition is true.
79
     *
80
     * @param string $permission
81
     * @param string|array $options
82
     * @param Authorizable|null $user
83
     * @return Button
84
     */
85
    public static function makeIfCan($permission, $options, Authorizable $user = null)
86
    {
87
        if (is_null($user)) {
88
            $user = auth()->user();
89
        }
90
91
        if ($user->can($permission)) {
92
            return static::make($options);
93
        }
94
95
        return static::make()->authorized(false);
96
    }
97
98
    /**
99
     * Set extend option value.
100
     *
101
     * @param string $value
102
     * @return $this
103
     */
104
    public function extend($value)
105
    {
106
        $this->attributes['extend'] = $value;
107
108
        return $this;
109
    }
110
111
    /**
112
     * Set editor option value.
113
     *
114
     * @param string $value
115
     * @return $this
116
     */
117
    public function editor($value)
118
    {
119
        $this->attributes['editor'] = $value;
120
121
        return $this;
122
    }
123
124
    /**
125
     * @param array $buttons
126
     * @return $this
127
     */
128 View Code Duplication
    public function buttons(array $buttons)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
129
    {
130
        foreach ($buttons as $key => $button) {
131
            if ($button instanceof Arrayable) {
132
                $buttons[$key] = $button->toArray();
133
            }
134
        }
135
136
        $this->attributes['buttons'] = $buttons;
137
138
        return $this;
139
    }
140
141
    /**
142
     * @param array $buttons
143
     * @return $this
144
     * @see https://editor.datatables.net/examples/api/cancelButton
145
     */
146 View Code Duplication
    public function formButtons(array $buttons)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
147
    {
148
        foreach ($buttons as $key => $button) {
149
            if ($button instanceof Arrayable) {
150
                $buttons[$key] = $button->toArray();
151
            }
152
        }
153
154
        $this->attributes['formButtons'] = $buttons;
155
156
        return $this;
157
    }
158
159
    /**
160
     * Set className option value.
161
     *
162
     * @param string $value
163
     * @return $this
164
     */
165
    public function className($value)
166
    {
167
        $this->attributes['className'] = $value;
168
169
        return $this;
170
    }
171
172
    /**
173
     * Set text option value.
174
     *
175
     * @param string $value
176
     * @return $this
177
     */
178
    public function text($value)
179
    {
180
        $this->attributes['text'] = $value;
181
182
        return $this;
183
    }
184
185
    /**
186
     * Set columns option value.
187
     *
188
     * @param mixed $value
189
     * @return $this
190
     */
191
    public function columns($value)
192
    {
193
        $this->attributes['columns'] = $value;
194
195
        return $this;
196
    }
197
198
    /**
199
     * Set exportOptions option value.
200
     *
201
     * @param mixed $value
202
     * @return $this
203
     */
204
    public function exportOptions($value)
205
    {
206
        $this->attributes['exportOptions'] = $value;
207
208
        return $this;
209
    }
210
211
    /**
212
     * Convert the Fluent instance to an array.
213
     *
214
     * @return array
215
     */
216
    public function toArray()
217
    {
218
        if ($this->authorized) {
219
            return parent::toArray();
220
        }
221
222
        return [];
223
    }
224
225
    /**
226
     * Set action to submit the form.
227
     *
228
     * @return \Yajra\DataTables\Html\Button
229
     */
230
    public function actionSubmit()
231
    {
232
        return $this->action('function() { this.submit(); }');
233
    }
234
235
    /**
236
     * Set action option value.
237
     *
238
     * @param string $value
239
     * @return $this
240
     */
241
    public function action($value)
242
    {
243
        $this->attributes['action'] = $value;
244
245
        return $this;
246
    }
247
248
    /**
249
     * Set editor class action handler.
250
     *
251
     * @param string $action
252
     * @return \Yajra\DataTables\Html\Button
253
     */
254
    public function actionHandler($action)
255
    {
256
        return $this->action("function() { this.submit(null, null, function(data) { data.action = '{$action}'; return data; }) }");
257
    }
258
259
    /**
260
     * Set action to close the form.
261
     *
262
     * @return \Yajra\DataTables\Html\Button
263
     */
264
    public function actionClose()
265
    {
266
        return $this->action('function() { this.close(); }');
267
    }
268
}
269