Completed
Push — master ( de1426...00195f )
by Arjay
01:21
created

Button::addClass()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 10
Ratio 100 %

Importance

Changes 0
Metric Value
dl 10
loc 10
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
namespace Yajra\DataTables\Html;
4
5
use Illuminate\Support\Fluent;
6
use Illuminate\Contracts\Support\Arrayable;
7
8
class Button extends Fluent implements Arrayable
9
{
10
    use HasAuthorizations;
11
12
    /**
13
     * Make a new button instance.
14
     *
15
     * @param string|array $options
16
     * @return static
17
     */
18
    public static function make($options = [])
19
    {
20
        if (is_string($options)) {
21
            return new static(['extend' => $options]);
22
        }
23
24
        return new static($options);
25
    }
26
27
    /**
28
     * Make a raw button that does not extend anything.
29
     *
30
     * @param array $options
31
     * @return static
32
     */
33
    public static function raw($options = [])
34
    {
35
        if (is_string($options)) {
36
            return new static(['text' => $options]);
37
        }
38
39
        return new static($options);
40
    }
41
42
    /**
43
     * Set extend option value.
44
     *
45
     * @param string $value
46
     * @return $this
47
     */
48
    public function extend($value)
49
    {
50
        $this->attributes['extend'] = $value;
51
52
        return $this;
53
    }
54
55
    /**
56
     * Set editor option value.
57
     *
58
     * @param string $value
59
     * @return $this
60
     */
61
    public function editor($value)
62
    {
63
        $this->attributes['editor'] = $value;
64
65
        return $this;
66
    }
67
68
    /**
69
     * @param array $buttons
70
     * @return $this
71
     */
72 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...
73
    {
74
        foreach ($buttons as $key => $button) {
75
            if ($button instanceof Arrayable) {
76
                $buttons[$key] = $button->toArray();
77
            }
78
        }
79
80
        $this->attributes['buttons'] = $buttons;
81
82
        return $this;
83
    }
84
85
    /**
86
     * @param array $buttons
87
     * @return $this
88
     * @see https://editor.datatables.net/examples/api/cancelButton
89
     */
90 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...
91
    {
92
        foreach ($buttons as $key => $button) {
93
            if ($button instanceof Arrayable) {
94
                $buttons[$key] = $button->toArray();
95
            }
96
        }
97
98
        $this->attributes['formButtons'] = $buttons;
99
100
        return $this;
101
    }
102
103
    /**
104
     * @param mixed $message
105
     * @return $this
106
     * @see https://editor.datatables.net/examples/api/removeMessage
107
     * @see https://editor.datatables.net/reference/button/create
108
     * @see https://editor.datatables.net/reference/button/edit
109
     * @see https://editor.datatables.net/reference/button/remove
110
     */
111
    public function formMessage($message)
112
    {
113
        $this->attributes['formMessage'] = $message;
114
115
        return $this;
116
    }
117
118
    /**
119
     * @param mixed $title
120
     * @return $this
121
     * @see https://editor.datatables.net/reference/button/create
122
     * @see https://editor.datatables.net/reference/button/edit
123
     * @see https://editor.datatables.net/reference/button/remove
124
     */
125
    public function formTitle($title)
126
    {
127
        $this->attributes['formTitle'] = $title;
128
129
        return $this;
130
    }
131
132
    /**
133
     * Set className option value.
134
     *
135
     * @param string $value
136
     * @return $this
137
     */
138
    public function className($value)
139
    {
140
        $this->attributes['className'] = $value;
141
142
        return $this;
143
    }
144
145
    /**
146
     * Append a class name to column.
147
     *
148
     * @param string $class
149
     * @return $this
150
     */
151 View Code Duplication
    public function addClass($class)
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...
152
    {
153
        if (! isset($this->attributes['className'])) {
154
            $this->attributes['className'] = $class;
155
        } else {
156
            $this->attributes['className'] .= " $class";
157
        }
158
159
        return $this;
160
    }
161
162
    /**
163
     * Set text option value.
164
     *
165
     * @param string $value
166
     * @return $this
167
     */
168
    public function text($value)
169
    {
170
        $this->attributes['text'] = $value;
171
172
        return $this;
173
    }
174
175
    /**
176
     * Set name option value.
177
     *
178
     * @param string $value
179
     * @return $this
180
     */
181
    public function name($value)
182
    {
183
        $this->attributes['name'] = $value;
184
185
        return $this;
186
    }
187
188
    /**
189
     * Set columns option value.
190
     *
191
     * @param mixed $value
192
     * @return $this
193
     */
194
    public function columns($value)
195
    {
196
        $this->attributes['columns'] = $value;
197
198
        return $this;
199
    }
200
201
    /**
202
     * Set exportOptions option value.
203
     *
204
     * @param mixed $value
205
     * @return $this
206
     */
207
    public function exportOptions($value)
208
    {
209
        $this->attributes['exportOptions'] = $value;
210
211
        return $this;
212
    }
213
214
    /**
215
     * Set action to submit the form.
216
     *
217
     * @return \Yajra\DataTables\Html\Button
218
     */
219
    public function actionSubmit()
220
    {
221
        $this->attributes['action'] = 'function() { this.submit(); }';
222
223
        return $this;
224
    }
225
226
    /**
227
     * Set action option value.
228
     *
229
     * @param string $value
230
     * @return $this
231
     */
232
    public function action($value)
233
    {
234
        if (substr($value, 0, 8) == 'function') {
235
            $this->attributes['action'] = $value;
236
        } else {
237
            $this->attributes['action'] = "function(e, dt, node, config) { $value }";
238
        }
239
240
        return $this;
241
    }
242
243
    /**
244
     * Set editor class action handler.
245
     *
246
     * @param string $action
247
     * @return \Yajra\DataTables\Html\Button
248
     */
249
    public function actionHandler($action)
250
    {
251
        $this->attributes['action'] = "function() { this.submit(null, null, function(data) { data.action = '{$action}'; return data; }) }";
252
253
        return $this;
254
    }
255
256
    /**
257
     * Set action to close the form.
258
     *
259
     * @return \Yajra\DataTables\Html\Button
260
     */
261
    public function actionClose()
262
    {
263
        $this->attributes['action'] = 'function() { this.close(); }';
264
265
        return $this;
266
    }
267
}
268