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

DataGridBase::getDataActionsTemplate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
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;
26
27
defined('APP_ROOT') || die();
28
29
use SP\Core\Acl\ActionsInterface;
30
use SP\Core\Exceptions\FileNotFoundException;
31
use SP\Core\UI\Theme;
32
use SP\Core\UI\ThemeInterface;
33
use SP\Html\DataGrid\Action\DataGridActionInterface;
34
use SP\Html\DataGrid\Layout\DataGridHeader;
35
use SP\Html\DataGrid\Layout\DataGridHeaderInterface;
36
use SP\Html\DataGrid\Layout\DataGridHeaderSort;
37
use SP\Html\DataGrid\Layout\DataGridPagerBase;
38
use SP\Html\DataGrid\Layout\DataGridPagerInterface;
39
40
/**
41
 * Class DataGridBase para crear una matriz de datos
42
 *
43
 * @package SP\Html\DataGrid
44
 */
45
abstract class DataGridBase implements DataGridInterface
46
{
47
    /**
48
     * Tiempo de ejecución
49
     *
50
     * @var int
51
     */
52
    protected $time = 0;
53
    /**
54
     * El id de la matriz
55
     *
56
     * @var string
57
     */
58
    protected $id = '';
59
    /**
60
     * La cabecera de la matriz
61
     *
62
     * @var DataGridHeaderInterface
63
     */
64
    protected $header;
65
    /**
66
     * Los datos de la matriz
67
     *
68
     * @var DataGridData
69
     */
70
    protected $data;
71
    /**
72
     * El paginador
73
     *
74
     * @var DataGridPagerBase
75
     */
76
    protected $pager;
77
    /**
78
     * Las acciones asociadas a los elementos de la matriz
79
     *
80
     * @var DataGridActionInterface[]
81
     */
82
    protected $actions = [];
83
    /**
84
     * @var int
85
     */
86
    protected $actionsCount = 0;
87
    /**
88
     * Las acciones asociadas a los elementos de la matriz que se muestran en un menú
89
     *
90
     * @var DataGridActionInterface[]
91
     */
92
    protected $actionsMenu = [];
93
    /**
94
     * @var int
95
     */
96
    protected $actionsMenuCount = 0;
97
    /**
98
     * La acción a realizar al cerrar la matriz
99
     *
100
     * @var int
101
     */
102
    protected $onCloseAction = 0;
103
    /**
104
     * La plantilla a utilizar para presentar la cabecera
105
     *
106
     * @var string
107
     */
108
    protected $headerTemplate;
109
    /**
110
     * La plantilla a utilizar para presentar las acciones
111
     *
112
     * @var string
113
     */
114
    protected $actionsTemplate;
115
    /**
116
     * La plantilla a utilizar para presentar el paginador
117
     *
118
     * @var string
119
     */
120
    protected $pagerTemplate;
121
    /**
122
     * La plantilla a utilizar para presentar los datos
123
     *
124
     * @var string
125
     */
126
    protected $rowsTemplate;
127
    /**
128
     * La plantilla a utilizar para presentar la tabla
129
     *
130
     * @var string
131
     */
132
    protected $tableTemplate;
133
    /**
134
     * @var Theme
135
     */
136
    protected $theme;
137
138
    /**
139
     * DataGridBase constructor.
140
     *
141
     * @param ThemeInterface $theme
142
     */
143
    public function __construct(ThemeInterface $theme)
144
    {
145
        $this->theme = $theme;
0 ignored issues
show
Documentation Bug introduced by
$theme is of type SP\Core\UI\ThemeInterface, but the property $theme was declared to be of type SP\Core\UI\Theme. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
146
    }
147
148
    /**
149
     * @return int
150
     */
151
    public function getOnCloseAction()
152
    {
153
        return $this->onCloseAction;
154
    }
155
156
    /**
157
     * @param \SP\Core\Acl\ActionsInterface $action
158
     *
159
     * @return $this
160
     */
161
    public function setOnCloseAction(ActionsInterface $action)
162
    {
163
        $this->onCloseAction = $action;
0 ignored issues
show
Documentation Bug introduced by
It seems like $action of type SP\Core\Acl\ActionsInterface is incompatible with the declared type integer of property $onCloseAction.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
164
165
        return $this;
166
    }
167
168
    /**
169
     * @return string
170
     */
171
    public function getId()
172
    {
173
        return $this->id;
174
    }
175
176
    /**
177
     * @param $id string
178
     *
179
     * @return $this
180
     */
181
    public function setId($id)
182
    {
183
        $this->id = $id;
184
185
        return $this;
186
    }
187
188
    /**
189
     * @return DataGridHeader|DataGridHeaderSort|DataGridHeaderInterface
190
     */
191
    public function getHeader()
192
    {
193
        return $this->header;
194
    }
195
196
    /**
197
     * @param DataGridHeaderInterface $header
198
     *
199
     * @return $this
200
     */
201
    public function setHeader(DataGridHeaderInterface $header)
202
    {
203
        $this->header = $header;
204
205
        return $this;
206
    }
207
208
    /**
209
     * @return DataGridDataInterface
210
     */
211
    public function getData()
212
    {
213
        return $this->data;
214
    }
215
216
    /**
217
     * @param DataGridDataInterface $data
218
     *
219
     * @return $this
220
     */
221
    public function setData(DataGridDataInterface $data)
222
    {
223
        $this->data = $data;
0 ignored issues
show
Documentation Bug introduced by
$data is of type SP\Html\DataGrid\DataGridDataInterface, but the property $data was declared to be of type SP\Html\DataGrid\DataGridData. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
224
225
        return $this;
226
    }
227
228
    /**
229
     * @param DataGridActionInterface $action
230
     * @param bool                    $isMenu Añadir al menu de acciones
231
     *
232
     * @return $this
233
     */
234
    public function addDataAction(DataGridActionInterface $action, $isMenu = false)
235
    {
236
        if ($isMenu === false) {
237
            $this->actions[] = $action;
238
239
            if (!$action->isSkip()) {
240
                $this->actionsCount++;
241
            }
242
        } else {
243
            $this->actionsMenu[] = $action;
244
245
            if (!$action->isSkip()) {
246
                $this->actionsMenuCount++;
247
            }
248
        }
249
250
        return $this;
251
    }
252
253
    /**
254
     * @return DataGridActionInterface[]
255
     */
256
    public function getDataActions()
257
    {
258
        return $this->actions;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->actions returns the type SP\Html\DataGrid\Action\DataGridActionInterface[] which is incompatible with the return type mandated by SP\Html\DataGrid\DataGri...rface::getDataActions() of SP\Html\DataGrid\Action\DataGridActionInterface.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
259
    }
260
261
    /**
262
     * @return $this
263
     */
264
    public function getGrid()
265
    {
266
        return $this;
267
    }
268
269
    /**
270
     * Establecer la plantilla utilizada para la cabecera
271
     *
272
     * @param string $template El nombre de la plantilla a utilizar
273
     * @param string $base     Directorio base para la plantilla
274
     *
275
     * @return $this
276
     */
277
    public function setDataHeaderTemplate($template, $base = null)
278
    {
279
        try {
280
            $this->headerTemplate = $this->checkTemplate($template, $base);
0 ignored issues
show
Bug introduced by
It seems like $base can also be of type string; however, parameter $base of SP\Html\DataGrid\DataGridBase::checkTemplate() does only seem to accept null, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

280
            $this->headerTemplate = $this->checkTemplate($template, /** @scrutinizer ignore-type */ $base);
Loading history...
281
        } catch (FileNotFoundException $e) {
282
            processException($e);
283
        }
284
285
        return $this;
286
    }
287
288
    /**
289
     * Comprobar si existe una plantilla y devolver la ruta completa
290
     *
291
     * @param      $template
292
     * @param null $base
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $base is correct as it would always require null to be passed?
Loading history...
293
     *
294
     * @return string
295
     * @throws FileNotFoundException
296
     */
297
    protected function checkTemplate($template, $base = null)
298
    {
299
        $template = null === $base ? $template . '.inc' : $base . DIRECTORY_SEPARATOR . $template . '.inc';
300
        $file = $this->theme->getViewsPath() . DIRECTORY_SEPARATOR . $template;
301
302
        if (!is_readable($file)) {
303
            throw new FileNotFoundException(sprintf(__('No es posible obtener la plantilla "%s" : %s'), $template, $file));
304
        }
305
306
        return $file;
307
    }
308
309
    /**
310
     * Devolver la plantilla utilizada para la cabecera
311
     *
312
     * @return string
313
     */
314
    public function getDataHeaderTemplate()
315
    {
316
        return $this->headerTemplate;
317
    }
318
319
    /**
320
     * Establecer la plantilla utilizada para las acciones
321
     *
322
     * @param string $template El nombre de la plantilla a utilizar
323
     *
324
     * @return $this
325
     */
326
    public function setDataActionsTemplate($template)
327
    {
328
        try {
329
            $this->actionsTemplate = $this->checkTemplate($template);
330
        } catch (FileNotFoundException $e) {
331
            logger($e->getMessage());
332
        }
333
334
        return $this;
335
    }
336
337
    /**
338
     * Devolver la plantilla utilizada para las acciones
339
     *
340
     * @return string
341
     */
342
    public function getDataActionsTemplate()
343
    {
344
        return $this->actionsTemplate;
345
    }
346
347
    /**
348
     * Establecer la plantilla utilizada para el paginador
349
     *
350
     * @param string $template El nombre de la plantilla a utilizar
351
     * @param string $base     Directorio base para la plantilla
352
     *
353
     * @return $this
354
     */
355
    public function setDataPagerTemplate($template, $base = null)
356
    {
357
        try {
358
            $this->pagerTemplate = $this->checkTemplate($template, $base);
0 ignored issues
show
Bug introduced by
It seems like $base can also be of type string; however, parameter $base of SP\Html\DataGrid\DataGridBase::checkTemplate() does only seem to accept null, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

358
            $this->pagerTemplate = $this->checkTemplate($template, /** @scrutinizer ignore-type */ $base);
Loading history...
359
        } catch (FileNotFoundException $e) {
360
            logger($e->getMessage());
361
        }
362
363
        return $this;
364
    }
365
366
    /**
367
     * Devolver la plantilla utilizada para el paginador
368
     *
369
     * @return string
370
     */
371
    public function getDataPagerTemplate()
372
    {
373
        return $this->pagerTemplate;
374
    }
375
376
    /**
377
     * @param string $template El nombre de la plantilla a utilizar
378
     * @param string $base     Directorio base para la plantilla
379
     *
380
     * @return mixed
381
     */
382
    public function setDataRowTemplate($template, $base = null)
383
    {
384
        try {
385
            $this->rowsTemplate = $this->checkTemplate($template, $base);
0 ignored issues
show
Bug introduced by
It seems like $base can also be of type string; however, parameter $base of SP\Html\DataGrid\DataGridBase::checkTemplate() does only seem to accept null, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

385
            $this->rowsTemplate = $this->checkTemplate($template, /** @scrutinizer ignore-type */ $base);
Loading history...
386
        } catch (FileNotFoundException $e) {
387
            processException($e);
388
        }
389
390
        return $this;
391
    }
392
393
    /**
394
     * @return string
395
     */
396
    public function getDataRowTemplate()
397
    {
398
        return $this->rowsTemplate;
399
    }
400
401
    /**
402
     * Devolver el paginador
403
     *
404
     * @return DataGridPagerInterface
405
     */
406
    public function getPager()
407
    {
408
        return $this->pager;
409
    }
410
411
    /**
412
     * Establecer el paginador
413
     *
414
     * @param DataGridPagerInterface $pager
415
     *
416
     * @return $this
417
     */
418
    public function setPager(DataGridPagerInterface $pager)
419
    {
420
        $this->pager = $pager;
0 ignored issues
show
Documentation Bug introduced by
$pager is of type SP\Html\DataGrid\Layout\DataGridPagerInterface, but the property $pager was declared to be of type SP\Html\DataGrid\Layout\DataGridPagerBase. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
421
422
        return $this;
423
    }
424
425
    /**
426
     * Actualizar los datos del paginador
427
     */
428
    public function updatePager()
429
    {
430
        if ($this->pager instanceof DataGridPagerInterface) {
0 ignored issues
show
introduced by
$this->pager is always a sub-type of SP\Html\DataGrid\Layout\DataGridPagerInterface. If $this->pager can have other possible types, add them to lib/SP/Html/DataGrid/DataGridBase.php:74.
Loading history...
431
            $this->pager->setTotalRows($this->data->getDataCount());
432
        }
433
434
        return $this;
435
    }
436
437
    /**
438
     * @return int
439
     */
440
    public function getTime()
441
    {
442
        return abs($this->time);
443
    }
444
445
    /**
446
     * @param int $time
447
     *
448
     * @return $this
449
     */
450
    public function setTime($time)
451
    {
452
        $this->time = $time;
453
454
        return $this;
455
    }
456
457
    /**
458
     * Devolver las acciones que se muestran en un menu
459
     *
460
     * @return DataGridActionInterface[]
461
     */
462
    public function getDataActionsMenu()
463
    {
464
        return $this->actionsMenu;
465
    }
466
467
    /**
468
     * Devolver las acciones filtradas
469
     *
470
     * @param $filter
471
     *
472
     * @return DataGridActionInterface[]
473
     */
474
    public function getDataActionsFiltered($filter)
475
    {
476
        $actions = [];
477
478
        foreach ($this->actions as $action) {
479
            if ($action->getRuntimeFilter()($filter)) {
480
                $actions[] = $action;
481
            }
482
        }
483
484
        return $actions;
485
    }
486
487
    /**
488
     * Devolver las acciones de menu filtradas
489
     *
490
     * @param $filter
491
     *
492
     * @return DataGridActionInterface[]
493
     */
494
    public function getDataActionsMenuFiltered($filter)
495
    {
496
        $actions = [];
497
498
        foreach ($this->actionsMenu as $action) {
499
            if ($action->getRuntimeFilter()($filter)) {
500
                $actions[] = $action;
501
            }
502
        }
503
504
        return $actions;
505
    }
506
507
    /**
508
     * @return string
509
     */
510
    public function getDataTableTemplate()
511
    {
512
        return $this->tableTemplate;
513
    }
514
515
    /**
516
     * @param      $template
517
     * @param null $base
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $base is correct as it would always require null to be passed?
Loading history...
518
     *
519
     * @return DataGridBase
520
     */
521
    public function setDataTableTemplate($template, $base = null)
522
    {
523
        try {
524
            $this->tableTemplate = $this->checkTemplate($template, $base);
525
        } catch (FileNotFoundException $e) {
526
            processException($e);
527
        }
528
529
        return $this;
530
    }
531
532
    /**
533
     * @return int
534
     */
535
    public function getDataActionsMenuCount()
536
    {
537
        return $this->actionsMenuCount;
538
    }
539
540
    /**
541
     * @return int
542
     */
543
    public function getDataActionsCount()
544
    {
545
        return $this->actionsCount;
546
    }
547
}