1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @package Cadmium\System\Utils |
5
|
|
|
* @author Anton Romanov |
6
|
|
|
* @copyright Copyright (c) 2015-2017, Anton Romanov |
7
|
|
|
* @link http://cadmium-cms.com |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Utils { |
11
|
|
|
|
12
|
|
|
use Template, Url; |
13
|
|
|
|
14
|
|
|
abstract class Pagination { |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Set pagination items |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
private static function setItems(Template\Block $pagination, int $active, int $count, Url $url) { |
21
|
|
|
|
22
|
|
|
$items = $pagination->getLoop('items'); |
23
|
|
|
|
24
|
|
|
for ($index = ($active - 2); $index <= ($active + 2); $index++) { |
25
|
|
|
|
26
|
|
|
if (!($index > 0 && $index <= $count)) continue; |
27
|
|
|
|
28
|
|
|
$class = (($index === $active) ? 'active item' : 'item'); |
29
|
|
|
|
30
|
|
|
$url->setAttribute('index', $index); $link = $url->getString(); |
31
|
|
|
|
32
|
|
|
$items->addItem(['class' => $class, 'link' => $link, 'index' => $index]); |
33
|
|
|
} |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Set first/last buttons |
38
|
|
|
*/ |
39
|
|
|
|
40
|
|
|
private static function setExtremeButtons(Template\Block $pagination, int $active, int $count, Url $url) { |
41
|
|
|
|
42
|
|
|
$extremums = [1, $count]; $buttons = []; |
43
|
|
|
|
44
|
|
|
$buttons['first'] = [$extremums[0], ($active < ($extremums[0] + 4)), ($active < ($extremums[0] + 3))]; |
45
|
|
|
$buttons['last'] = [$extremums[1], ($active > ($extremums[1] - 4)), ($active > ($extremums[1] - 3))]; |
46
|
|
|
|
47
|
|
|
foreach ($buttons as $class => $data) { |
48
|
|
|
|
49
|
|
|
list ($index, $closest, $disabled) = $data; $block = $pagination->getBlock($class); |
50
|
|
|
|
51
|
|
|
if ($disabled) { $block->disable(); continue; } |
52
|
|
|
|
53
|
|
|
$block->link = $url->setAttribute('index', $index)->getString(); $block->index = $index; |
|
|
|
|
54
|
|
|
|
55
|
|
|
if ($closest) $block->getBlock('ellipsis')->disable(); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Set prev/next buttons |
61
|
|
|
*/ |
62
|
|
|
|
63
|
|
|
private static function setStepButtons(Template\Block $pagination, int $active, int $count, Url $url) { |
64
|
|
|
|
65
|
|
|
$extremums = [1, $count]; $buttons = []; |
66
|
|
|
|
67
|
|
|
$buttons['prev'] = [$extremums[0], ($active - 1)]; |
68
|
|
|
$buttons['next'] = [$extremums[1], ($active + 1)]; |
69
|
|
|
|
70
|
|
|
foreach ($buttons as $class => $data) { |
71
|
|
|
|
72
|
|
|
list ($extremum, $index) = $data; $block = $pagination->getBlock($class); |
73
|
|
|
|
74
|
|
|
if ($active === $extremum) { $block->disable(); $pagination->getBlock($class . '_disabled')->enable(); } |
75
|
|
|
|
76
|
|
|
else $block->link = $url->setAttribute('index', $index)->getString(); |
|
|
|
|
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Get a pagination block |
82
|
|
|
* |
83
|
|
|
* @param $index an active page index |
84
|
|
|
* @param $display a number of entries per page |
85
|
|
|
* @param $total a total number of entries |
86
|
|
|
* @param $url a url object |
87
|
|
|
* |
88
|
|
|
* @return Template\Block|false : a block object or false if incorrect values given |
89
|
|
|
*/ |
90
|
|
|
|
91
|
|
|
public static function getBlock(int $index, int $display, int $total, Url $url) { |
92
|
|
|
|
93
|
|
|
if (($index <= 0) || ($display <= 0) || ($total <= 0)) return false; |
94
|
|
|
|
95
|
|
|
if (($display >= $total) || ($index > ($count = ceil($total / $display)))) return false; |
96
|
|
|
|
97
|
|
|
# Create block |
98
|
|
|
|
99
|
|
|
$pagination = View::get('Blocks/Utils/Pagination'); |
100
|
|
|
|
101
|
|
|
# Set items |
102
|
|
|
|
103
|
|
|
self::setItems($pagination, $index, $count, $url); |
104
|
|
|
|
105
|
|
|
# Set buttons |
106
|
|
|
|
107
|
|
|
self::setExtremeButtons($pagination, $index, $count, $url); |
108
|
|
|
|
109
|
|
|
self::setStepButtons($pagination, $index, $count, $url); |
110
|
|
|
|
111
|
|
|
# ------------------------ |
112
|
|
|
|
113
|
|
|
return $pagination; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
Since your code implements the magic setter
_set
, this function will be called for any write access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.