Passed
Push — devel-3.0 ( 3dfe81...25adf3 )
by Rubén
03:07
created

DataGridActionBase::addClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * sysPass
4
 *
5
 * @author nuxsmin
6
 * @link https://syspass.org
7
 * @copyright 2012-2018, Rubén Domínguez nuxsmin@$syspass.org
8
 *
9
 * This file is part of sysPass.
10
 *
11
 * sysPass is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU General Public License as published by
13
 * the Free Software Foundation, either version 3 of the License, or
14
 * (at your option) any later version.
15
 *
16
 * sysPass is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU General Public License
22
 *  along with sysPass.  If not, see <http://www.gnu.org/licenses/>.
23
 */
24
25
namespace SP\Html\DataGrid\Action;
26
27
use SP\Html\Assets\IconInterface;
28
29
defined('APP_ROOT') || die();
30
31
/**
32
 * Class DataGridAction para crear una acción para cada elemento de la matriz de datos
33
 *
34
 * @package SP\Html\DataGrid
35
 */
36
abstract class DataGridActionBase implements DataGridActionInterface
37
{
38
    /**
39
     * El objeto reflexivo que determina si se muestra la acción
40
     *
41
     * @var callable
42
     */
43
    protected $runtimeFilter;
44
    /**
45
     * El nombre de la acción
46
     *
47
     * @var string
48
     */
49
    protected $name = '';
50
    /**
51
     * El título de la acción
52
     *
53
     * @var string
54
     */
55
    protected $title = '';
56
    /**
57
     * El id de la acción
58
     *
59
     * @var int
60
     */
61
    protected $id = 0;
62
    /**
63
     * La función javascript del evento OnClick
64
     *
65
     * @var string
66
     */
67
    protected $onClickFunction = '';
68
    /**
69
     * Los argumentos de la función OnClick
70
     *
71
     * @var array
72
     */
73
    protected $onClickArgs = [];
74
    /**
75
     * El icono de la acción
76
     *
77
     * @var IconInterface
78
     */
79
    protected $icon;
80
    /**
81
     * Si se debe de omitir para los elementos del listado
82
     *
83
     * @var bool
84
     */
85
    protected $isSkip = false;
86
    /**
87
     * La columna de origen de datos que condiciona esta acción
88
     *
89
     * @var string
90
     */
91
    protected $filterRowSource;
92
    /**
93
     * Si es una acción de ayuda
94
     *
95
     * @var bool
96
     */
97
    protected $isHelper;
98
    /**
99
     * El tipo de acción
100
     *
101
     * @var int
102
     */
103
    protected $type = 0;
104
    /**
105
     * Atributos de datos adicionales
106
     *
107
     * @var array
108
     */
109
    protected $data = [];
110
    /**
111
     * Atributos adicionales
112
     *
113
     * @var array
114
     */
115
    protected $attributes = [];
116
    /**
117
     * @var array
118
     */
119
    protected $classes = [];
120
121
    /**
122
     * DataGridActionBase constructor.
123
     *
124
     * @param int $id EL id de la acción
125
     */
126
    public function __construct($id = null)
127
    {
128
        $this->id = $id;
129
    }
130
131
    /**
132
     * Devolver el método reflexivo que determina si se muestra la acción
133
     *
134
     * @return callable
135
     */
136
    public function getRuntimeFilter()
137
    {
138
        return $this->runtimeFilter;
139
    }
140
141
    /**
142
     * Establecer el método reflexivo que determina si se muestra la acción
143
     *
144
     * @param string $class
145
     * @param string $method
146
     *
147
     * @throws \RuntimeException
148
     * @return $this
149
     */
150
    public function setRuntimeFilter($class, $method)
151
    {
152
        if (method_exists($class, $method)) {
153
            $this->runtimeFilter = function ($filter) use ($method) {
154
//                new \ReflectionMethod($class, $method);
155
                return $filter->{$method}();
156
            };
157
        } else {
158
            throw new \RuntimeException('Method does not exist');
159
        }
160
161
        return $this;
162
    }
163
164
    /**
165
     * @return string
166
     */
167
    public function getName()
168
    {
169
        return $this->name;
170
    }
171
172
    /**
173
     * @param $name string
174
     *
175
     * @return $this
176
     */
177
    public function setName($name)
178
    {
179
        $this->name = $name;
180
181
        return $this;
182
    }
183
184
    /**
185
     * @return string
186
     */
187
    public function getId()
188
    {
189
        return $this->id;
190
    }
191
192
    /**
193
     * @param int $id
194
     *
195
     * @return $this
196
     */
197
    public function setId($id)
198
    {
199
        $this->id = $id;
200
201
        return $this;
202
    }
203
204
    /**
205
     * @return string
206
     */
207
    public function getTitle()
208
    {
209
        return $this->title;
210
    }
211
212
    /**
213
     * @param $title string
214
     *
215
     * @return $this
216
     */
217
    public function setTitle($title)
218
    {
219
        $this->title = $title;
220
221
        return $this;
222
    }
223
224
    /**
225
     * @param $function string
226
     *
227
     * @return $this
228
     */
229
    public function setOnClickFunction($function)
230
    {
231
        $this->onClickFunction = $function;
232
233
        return $this;
234
    }
235
236
    /**
237
     * @param $args string
238
     *
239
     * @return $this
240
     */
241
    public function setOnClickArgs($args)
242
    {
243
        $this->onClickArgs[] = $args;
244
245
        return $this;
246
    }
247
248
    /**
249
     * @return string
250
     */
251
    public function getOnClick()
252
    {
253
        $args = [];
254
255
        foreach ($this->onClickArgs as $arg) {
256
            $args[] = (!is_numeric($arg) && $arg !== 'this') ? '\'' . $arg . '\'' : $arg;
257
        }
258
259
        return count($args) > 0 ? $this->onClickFunction . '(' . implode(',', $args) . ')' : $this->onClickFunction;
260
    }
261
262
    /**
263
     * @return IconInterface
264
     */
265
    public function getIcon()
266
    {
267
        return $this->icon;
268
    }
269
270
    /**
271
     * @param $icon IconInterface
272
     *
273
     * @return $this
274
     */
275
    public function setIcon($icon)
276
    {
277
        $this->icon = $icon;
278
279
        return $this;
280
    }
281
282
    /**
283
     * @param $skip bool
284
     *
285
     * @return $this
286
     */
287
    public function setSkip($skip)
288
    {
289
        $this->isSkip = $skip;
290
291
        return $this;
292
    }
293
294
    /**
295
     * @return bool
296
     */
297
    public function isSkip()
298
    {
299
        return $this->isSkip;
300
    }
301
302
    /**
303
     * @return bool
304
     */
305
    public function isHelper()
306
    {
307
        return $this->isHelper;
308
    }
309
310
    /**
311
     * @param bool $helper
312
     *
313
     * @return $this
314
     */
315
    public function setIsHelper($helper)
316
    {
317
        $this->isHelper = $helper;
318
319
        return $this;
320
    }
321
322
    /**
323
     * @return string
324
     */
325
    public function getFilterRowSource()
326
    {
327
        return $this->filterRowSource;
328
    }
329
330
    /**
331
     * Filtro para mostrar la acción
332
     *
333
     * @param       $rowSource string
334
     * @param mixed $value     Valor a filtrar
335
     *
336
     * @return $this
337
     */
338
    public function setFilterRowSource($rowSource, $value = 1)
339
    {
340
        $this->filterRowSource[] = ['field' => $rowSource, 'value' => $value];
341
342
        return $this;
343
    }
344
345
    /**
346
     * @return int El tipo de acción
347
     */
348
    public function getType()
349
    {
350
        return $this->type;
351
    }
352
353
    /**
354
     * @param int $type El tipo de acción definido en DataGridActionType
355
     *
356
     * @return $this
357
     */
358
    public function setType($type)
359
    {
360
        $this->type = $type;
361
362
        return $this;
363
    }
364
365
    /**
366
     * @return array
367
     */
368
    public function getData()
369
    {
370
        return $this->data;
371
    }
372
373
    /**
374
     * @param array $data Los datos de los atributos
375
     *
376
     * @return $this
377
     */
378
    public function setData(array $data)
379
    {
380
        $this->data = $data;
381
382
        return $this;
383
    }
384
385
    /**
386
     * Añadir nuevo atributo de datos
387
     *
388
     * @param string $name El nombe del atributo
389
     * @param mixed  $data Los datos del atributo
390
     *
391
     * @return $this
392
     */
393
    public function addData($name, $data)
394
    {
395
        $this->data[$name] = $data;
396
397
        return $this;
398
    }
399
400
    /**
401
     * @return array
402
     */
403
    public function getAttributes()
404
    {
405
        return $this->attributes;
406
    }
407
408
    /**
409
     * Establecer atributos
410
     *
411
     * @param array $attributes Los datos de los atributos
412
     *
413
     * @return $this
414
     */
415
    public function setAttributes(array $attributes)
416
    {
417
        $this->attributes = $attributes;
418
419
        return $this;
420
    }
421
422
    /**
423
     * Añadir nuevo atributo
424
     *
425
     * @param string $name El nombe del atributo
426
     * @param mixed  $value
427
     *
428
     * @return $this
429
     */
430
    public function addAttribute($name, $value)
431
    {
432
        $this->attributes[$name] = $value;
433
434
        return $this;
435
    }
436
437
    /**
438
     * Returns classes as a string
439
     *
440
     * @return array
441
     */
442
    public function getClassesAsString()
443
    {
444
        return implode(' ', $this->classes);
0 ignored issues
show
Bug Best Practice introduced by
The expression return implode(' ', $this->classes) returns the type string which is incompatible with the documented return type array.
Loading history...
445
    }
446
447
    /**
448
     * Returns classes
449
     *
450
     * @return array
451
     */
452
    public function getClasses()
453
    {
454
        return $this->classes;
455
    }
456
457
    /**
458
     * Set classes
459
     *
460
     * @param array $classes
461
     */
462
    public function setClasses(array $classes)
463
    {
464
        $this->classes = $classes;
465
    }
466
467
    /**
468
     * Adds a new class
469
     *
470
     * @param mixed $value
471
     *
472
     * @return $this
473
     */
474
    public function addClass($value)
475
    {
476
        $this->classes[] = $value;
477
478
        return $this;
479
    }
480
}
481