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

DataGridPagerBase::getFirst()   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\Layout;
26
27
use SP\Html\Assets\IconInterface;
28
use SP\Html\DataGrid\Action\DataGridActionSearch;
29
30
defined('APP_ROOT') || die();
31
32
/**
33
 * Class DataGridPagerBase para implementar los métodos del paginador
34
 *
35
 * @package SP\Html\DataGrid
36
 */
37
abstract class DataGridPagerBase implements DataGridPagerInterface
38
{
39
    /**
40
     * @var int
41
     */
42
    protected $sortKey = 0;
43
    /**
44
     * @var int
45
     */
46
    protected $sortOrder = 0;
47
    /**
48
     * @var int
49
     */
50
    protected $limitStart = 0;
51
    /**
52
     * @var int
53
     */
54
    protected $limitCount = 0;
55
    /**
56
     * @var int
57
     */
58
    protected $totalRows = 0;
59
    /**
60
     * @var bool
61
     */
62
    protected $filterOn = false;
63
    /**
64
     * @var string
65
     */
66
    protected $onClickFunction = '';
67
    /**
68
     * @var array
69
     */
70
    protected $onClickArgs = [];
71
    /**
72
     * @var IconInterface
73
     */
74
    protected $iconPrev;
75
    /**
76
     * @var IconInterface
77
     */
78
    protected $iconNext;
79
    /**
80
     * @var IconInterface
81
     */
82
    protected $iconFirst;
83
    /**
84
     * @var IconInterface
85
     */
86
    protected $iconLast;
87
    /**
88
     * @var DataGridActionSearch
89
     */
90
    protected $sourceAction;
91
    /**
92
     * @var string
93
     */
94
    protected $sk;
95
96
    /**
97
     * @return int
98
     */
99
    public function getSortOrder()
100
    {
101
        return $this->sortOrder;
102
    }
103
104
    /**
105
     * @param int $sortOrder
106
     *
107
     * @return $this
108
     */
109
    public function setSortOrder($sortOrder)
110
    {
111
        $this->sortOrder = $sortOrder;
112
113
        return $this;
114
    }
115
116
    /**
117
     * @param string $sk
118
     *
119
     * @return $this
120
     */
121
    public function setSk($sk)
122
    {
123
        $this->sk = $sk;
124
125
        return $this;
126
    }
127
128
    /**
129
     * @return IconInterface
130
     */
131
    public function getIconPrev()
132
    {
133
        return $this->iconPrev;
134
    }
135
136
    /**
137
     * @param IconInterface $iconPrev
138
     *
139
     * @return $this
140
     */
141
    public function setIconPrev(IconInterface $iconPrev)
142
    {
143
        $this->iconPrev = $iconPrev;
144
145
        return $this;
146
    }
147
148
    /**
149
     * @return IconInterface
150
     */
151
    public function getIconNext()
152
    {
153
        return $this->iconNext;
154
    }
155
156
    /**
157
     * @param IconInterface $iconNext
158
     *
159
     * @return $this
160
     */
161
    public function setIconNext(IconInterface $iconNext)
162
    {
163
        $this->iconNext = $iconNext;
164
165
        return $this;
166
    }
167
168
    /**
169
     * @return IconInterface
170
     */
171
    public function getIconFirst()
172
    {
173
        return $this->iconFirst;
174
    }
175
176
    /**
177
     * @param IconInterface $iconFirst
178
     *
179
     * @return $this
180
     */
181
    public function setIconFirst(IconInterface $iconFirst)
182
    {
183
        $this->iconFirst = $iconFirst;
184
185
        return $this;
186
    }
187
188
    /**
189
     * @return IconInterface
190
     */
191
    public function getIconLast()
192
    {
193
        return $this->iconLast;
194
    }
195
196
    /**
197
     * @param IconInterface $iconLast
198
     *
199
     * @return $this
200
     */
201
    public function setIconLast(IconInterface $iconLast)
202
    {
203
        $this->iconLast = $iconLast;
204
205
        return $this;
206
    }
207
208
    /**
209
     * Devolver el campo de la búsqueda
210
     *
211
     * @return int
212
     */
213
    public function getSortKey()
214
    {
215
        return $this->sortKey;
216
    }
217
218
    /**
219
     * Establecer el campo de la búsqueda
220
     *
221
     * @param int $sortKey
222
     *
223
     * @return $this
224
     */
225
    public function setSortKey($sortKey)
226
    {
227
        $this->sortKey = $sortKey;
228
229
        return $this;
230
    }
231
232
    /**
233
     * Devolver el registro de inicio de la página
234
     *
235
     * @return int
236
     */
237
    public function getLimitStart()
238
    {
239
        return $this->limitStart;
240
    }
241
242
    /**
243
     * Establecer el registro de inicio de la página
244
     *
245
     * @param int $limitStart
246
     *
247
     * @return $this
248
     */
249
    public function setLimitStart($limitStart)
250
    {
251
        $this->limitStart = $limitStart;
252
253
        return $this;
254
    }
255
256
    /**
257
     * Devolver el número de registros en una página
258
     *
259
     * @return mixed
260
     */
261
    public function getLimitCount()
262
    {
263
        return $this->limitCount;
264
    }
265
266
    /**
267
     * Establecer el número de registros en una página
268
     *
269
     * @param int $limitCount
270
     *
271
     * @return $this
272
     */
273
    public function setLimitCount($limitCount)
274
    {
275
        $this->limitCount = $limitCount;
276
277
        return $this;
278
    }
279
280
    /**
281
     * Devolver el número de página inicial
282
     *
283
     * @return int
284
     */
285
    public function getFirstPage()
286
    {
287
        return ceil(($this->limitStart + 1) / $this->limitCount);
288
    }
289
290
    /**
291
     * Devolver el número de página final
292
     *
293
     * @return int
294
     */
295
    public function getLastPage()
296
    {
297
        return ceil($this->totalRows / $this->limitCount);
298
    }
299
300
    /**
301
     * Devolver el número total de registros obtenidos
302
     *
303
     * @return int
304
     */
305
    public function getTotalRows()
306
    {
307
        return $this->totalRows;
308
    }
309
310
    /**
311
     * Establecer el número total de registros obtenidos
312
     *
313
     * @param int $totalRows
314
     *
315
     * @return $this
316
     */
317
    public function setTotalRows($totalRows)
318
    {
319
        $this->totalRows = $totalRows;
320
321
        return $this;
322
    }
323
324
    /**
325
     * Devolver si está activado el filtro
326
     *
327
     * @return bool
328
     */
329
    public function getFilterOn()
330
    {
331
        return $this->filterOn;
332
    }
333
334
    /**
335
     * Establecer si está activado el filtro
336
     *
337
     * @param bool $filterOn
338
     *
339
     * @return $this
340
     */
341
    public function setFilterOn($filterOn)
342
    {
343
        $this->filterOn = $filterOn;
344
345
        return $this;
346
    }
347
348
    /**
349
     * Establecer la función javascript para paginar
350
     *
351
     * @param string $function
352
     *
353
     * @return $this
354
     */
355
    public function setOnClickFunction($function)
356
    {
357
        $this->onClickFunction = $function;
358
359
        return $this;
360
    }
361
362
    /**
363
     * Devolver la función javascript para paginar
364
     *
365
     * @return string
366
     */
367
    public function getOnClick()
368
    {
369
        $args = $this->parseArgs();
370
371
        return count($args) > 0 ? $this->onClickFunction . '(' . implode(',', $args) . ')' : $this->onClickFunction;
372
    }
373
374
    /**
375
     * @return array
376
     */
377
    protected function parseArgs()
378
    {
379
        $args = array();
380
381
        foreach ($this->onClickArgs as $arg) {
382
            $args[] = (!is_numeric($arg) && $arg !== 'this') ? '\'' . $arg . '\'' : $arg;
383
        }
384
385
        return $args;
386
    }
387
388
    /**
389
     * Establecer los argumentos de la función OnClick
390
     *
391
     * @param mixed $args
392
     *
393
     * @return $this
394
     */
395
    public function setOnClickArgs($args)
396
    {
397
        $this->onClickArgs[] = $args;
398
399
        return $this;
400
    }
401
402
    /**
403
     * Devolver la funcion para ir a la primera página
404
     *
405
     * @return string
406
     */
407
    public function getOnClickFirst()
408
    {
409
        $args = $this->parseArgs();
410
        $args[] = $this->getFirst();
411
412
        return $this->onClickFunction . '(' . implode(',', $args) . ')';
413
    }
414
415
    /**
416
     * @return int
417
     */
418
    public function getFirst()
419
    {
420
        return 0;
421
    }
422
423
    /**
424
     * Devolver la funcion para ir a la última página
425
     *
426
     * @return string
427
     */
428
    public function getOnClickLast()
429
    {
430
        $args = $this->parseArgs();
431
        $args[] = $this->getLast();
432
433
        return $this->onClickFunction . '(' . implode(',', $args) . ')';
434
    }
435
436
    /**
437
     * @return float|int
438
     */
439
    public function getLast()
440
    {
441
        return (($this->totalRows % $this->limitCount) == 0) ? $this->totalRows - $this->limitCount : floor($this->totalRows / $this->limitCount) * $this->limitCount;
442
    }
443
444
    /**
445
     * Devolver la funcion para ir a la siguiente página
446
     *
447
     * @return string
448
     */
449
    public function getOnClickNext()
450
    {
451
        $args = $this->parseArgs();
452
        $args[] = $this->getNext();
453
454
        return $this->onClickFunction . '(' . implode(',', $args) . ')';
455
    }
456
457
    /**
458
     * @return int
459
     */
460
    public function getNext()
461
    {
462
        return ($this->limitStart + $this->limitCount);
463
    }
464
465
    /**
466
     * Devolver la funcion para ir a la página anterior
467
     *
468
     * @return string
469
     */
470
    public function getOnClickPrev()
471
    {
472
        $args = $this->parseArgs();
473
        $args[] = $this->getPrev();
474
475
        return $this->onClickFunction . '(' . implode(',', $args) . ')';
476
    }
477
478
    /**
479
     * @return int
480
     */
481
    public function getPrev()
482
    {
483
        return ($this->limitStart - $this->limitCount);
484
    }
485
486
    /**
487
     * @return DataGridActionSearch
488
     */
489
    public function getSourceAction()
490
    {
491
        return $this->sourceAction;
492
    }
493
494
    /**
495
     * @param DataGridActionSearch $sourceAction
496
     *
497
     * @return $this
498
     */
499
    public function setSourceAction($sourceAction)
500
    {
501
        $this->sourceAction = $sourceAction;
502
503
        return $this;
504
    }
505
}