|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* The Carbon Pagination number links item class. |
|
4
|
|
|
* Responsible for the "1, 2, 3, ..., 10, 20, 30" pagination item. |
|
5
|
|
|
* |
|
6
|
|
|
* @uses Carbon_Pagination_Item |
|
7
|
|
|
*/ |
|
8
|
|
|
class Carbon_Pagination_Item_Number_Links extends Carbon_Pagination_Item { |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Initialize the item. |
|
12
|
|
|
* Generate the sub items of this item. |
|
13
|
|
|
*/ |
|
14
|
2 |
|
public function init() { |
|
15
|
2 |
|
$pagination = $this->get_collection()->get_pagination(); |
|
16
|
|
|
|
|
17
|
|
|
// initialize subitems collection |
|
18
|
2 |
|
$subitems_collection = new Carbon_Pagination_Collection( $pagination, false ); |
|
19
|
2 |
|
$this->set_subitems_collection( $subitems_collection ); |
|
20
|
|
|
|
|
21
|
|
|
// generate large numbers - before |
|
22
|
2 |
|
$this->generate_large_number_pages_before(); |
|
23
|
|
|
|
|
24
|
|
|
// generate page numbers |
|
25
|
2 |
|
$this->generate_regular_number_pages(); |
|
26
|
|
|
|
|
27
|
|
|
// generate large numbers - after |
|
28
|
2 |
|
$this->generate_large_number_pages_after(); |
|
29
|
|
|
|
|
30
|
|
|
// generate & add limiters |
|
31
|
2 |
|
$this->generate_limiters(); |
|
32
|
|
|
|
|
33
|
|
|
// generate & add wrappers |
|
34
|
2 |
|
$this->generate_wrappers(); |
|
35
|
2 |
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Generate number pages (subitems) in a certain range, |
|
39
|
|
|
* with a specified interval and within a specific limit. |
|
40
|
|
|
* Can optionally generate the items starting from the end. |
|
41
|
|
|
* |
|
42
|
|
|
* @param int $from Index of the first page. |
|
43
|
|
|
* @param int $to Index of the last page. |
|
44
|
|
|
* @param int $interval Interval between pages. |
|
45
|
|
|
* @param int $limit Number of pages to create. |
|
46
|
|
|
* @param bool $from_end Whether to start from the end. |
|
47
|
|
|
*/ |
|
48
|
7 |
|
public function generate_pages( $from, $to, $interval = 1, $limit = 0, $from_end = false ) { |
|
49
|
|
|
// generate items for the current range, using the specified interval |
|
50
|
7 |
|
$new_subitems = $this->generate_pages_with_interval( $from, $to, $interval ); |
|
51
|
|
|
|
|
52
|
|
|
// limit items if necessary |
|
53
|
7 |
|
if ( $limit ) { |
|
54
|
2 |
|
$start = $from_end ? -1 * $limit : 0; |
|
55
|
2 |
|
$new_subitems = array_slice( $new_subitems, $start, $limit ); |
|
56
|
2 |
|
} |
|
57
|
|
|
|
|
58
|
|
|
// update the subitems collection with the new items |
|
59
|
7 |
|
$subitems_collection = $this->get_subitems_collection(); |
|
60
|
7 |
|
$subitems_collection->add_items( $new_subitems ); |
|
61
|
7 |
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Generate number pages (subitems) in a certain range with a specified interval. |
|
65
|
|
|
* |
|
66
|
|
|
* @param int $from Index of the first page. |
|
67
|
|
|
* @param int $to Index of the last page. |
|
68
|
|
|
* @param int $interval Interval between pages. |
|
69
|
|
|
* @return array $new_subitems Generated items. |
|
70
|
|
|
*/ |
|
71
|
4 |
|
public function generate_pages_with_interval( $from, $to, $interval = 1 ) { |
|
72
|
4 |
|
$collection = $this->get_collection(); |
|
73
|
4 |
|
$new_subitems = array(); |
|
74
|
|
|
|
|
75
|
4 |
|
for ( $i = $from; $i < $to; $i += $interval ) { |
|
76
|
4 |
|
$page_item = new Carbon_Pagination_Item_Page( $collection ); |
|
77
|
4 |
|
$page_item->set_page_number( $i ); |
|
78
|
4 |
|
$new_subitems[] = $page_item; |
|
79
|
4 |
|
} |
|
80
|
|
|
|
|
81
|
4 |
|
return $new_subitems; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Generate the regular consecutive number pages. |
|
86
|
|
|
*/ |
|
87
|
12 |
|
public function generate_regular_number_pages() { |
|
88
|
|
|
// get various pagination variables that we need |
|
89
|
12 |
|
$pagination = $this->get_collection()->get_pagination(); |
|
90
|
12 |
|
$current_page_idx = $pagination->get_current_page() - 1; |
|
91
|
12 |
|
$number_limit = $pagination->get_number_limit(); |
|
92
|
12 |
|
$total_pages = $pagination->get_total_pages(); |
|
93
|
|
|
|
|
94
|
|
|
// determine the range and generate the pages |
|
95
|
12 |
|
if ( $number_limit >= 0 ) { |
|
96
|
9 |
|
$from = max( 0, $current_page_idx - $number_limit ); |
|
97
|
9 |
|
$to = min( $total_pages, $current_page_idx + $number_limit + 1 ); |
|
98
|
9 |
|
} else { |
|
99
|
3 |
|
$from = 0; |
|
100
|
3 |
|
$to = $total_pages; |
|
101
|
|
|
} |
|
102
|
12 |
|
$this->generate_pages( $from, $to ); |
|
103
|
12 |
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Generate the large number page items - before the regular number pages. |
|
107
|
|
|
*/ |
|
108
|
15 |
|
public function generate_large_number_pages_before() { |
|
109
|
|
|
// get various pagination variables that we need |
|
110
|
15 |
|
$pagination = $this->get_collection()->get_pagination(); |
|
111
|
15 |
|
$current_page_idx = $pagination->get_current_page() - 1; |
|
112
|
15 |
|
$number_limit = $pagination->get_number_limit(); |
|
113
|
15 |
|
$large_page_number_limit = $pagination->get_large_page_number_limit(); |
|
114
|
15 |
|
$large_page_number_interval = $pagination->get_large_page_number_interval(); |
|
115
|
|
|
|
|
116
|
|
|
// if enabled, determine the range and generate the pages |
|
117
|
15 |
|
if ( $large_page_number_limit > 0 ) { |
|
118
|
13 |
|
$from = $large_page_number_interval - 1; |
|
119
|
13 |
|
$to = $current_page_idx - $number_limit; |
|
120
|
13 |
|
$this->generate_pages( $from, $to, $large_page_number_interval, $large_page_number_limit ); |
|
121
|
13 |
|
} |
|
122
|
15 |
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Generate the large number page items - after the regular number pages. |
|
126
|
|
|
*/ |
|
127
|
15 |
|
public function generate_large_number_pages_after() { |
|
128
|
|
|
// get various pagination variables that we need |
|
129
|
15 |
|
$pagination = $this->get_collection()->get_pagination(); |
|
130
|
15 |
|
$current_page_idx = $pagination->get_current_page() - 1; |
|
131
|
15 |
|
$total_pages = $pagination->get_total_pages(); |
|
132
|
15 |
|
$number_limit = $pagination->get_number_limit(); |
|
133
|
15 |
|
$large_page_number_limit = $pagination->get_large_page_number_limit(); |
|
134
|
15 |
|
$large_page_number_interval = $pagination->get_large_page_number_interval(); |
|
135
|
|
|
|
|
136
|
|
|
// if enabled, determine the range and generate the pages |
|
137
|
15 |
|
if ( $large_page_number_limit > 0 ) { |
|
138
|
13 |
|
$from_raw = $current_page_idx + $number_limit + 1; |
|
139
|
13 |
|
$from = intval( ceil( $from_raw / $large_page_number_interval ) ) * $large_page_number_interval - 1; |
|
140
|
13 |
|
if ( $from == $current_page_idx + $number_limit ) { |
|
141
|
4 |
|
$from += $large_page_number_interval; |
|
142
|
4 |
|
} |
|
143
|
|
|
|
|
144
|
13 |
|
$to = $total_pages; |
|
145
|
|
|
|
|
146
|
13 |
|
$this->generate_pages( $from, $to, $large_page_number_interval, $large_page_number_limit, true ); |
|
147
|
13 |
|
} |
|
148
|
15 |
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Generate and add limiters where necessary. |
|
152
|
|
|
*/ |
|
153
|
6 |
|
public function generate_limiters() { |
|
154
|
|
|
// get various pagination variables that we need |
|
155
|
6 |
|
$collection = $this->get_collection(); |
|
156
|
6 |
|
$pagination = $collection->get_pagination(); |
|
157
|
6 |
|
$subitems_collection = $this->get_subitems_collection(); |
|
158
|
6 |
|
$subitems = $subitems_collection->get_items(); |
|
159
|
6 |
|
$large_page_number_interval = $pagination->get_large_page_number_interval(); |
|
160
|
|
|
|
|
161
|
|
|
// generate a prototype limiter item |
|
162
|
6 |
|
$limiter_item = new Carbon_Pagination_Item_Limiter( $collection ); |
|
163
|
|
|
|
|
164
|
|
|
// insert limiters before & after the page numbers |
|
165
|
6 |
|
for ( $i = count( $subitems ) - 1; $i > 0; $i-- ) { |
|
166
|
5 |
|
$prev = $subitems[ $i - 1 ]->get_page_number(); |
|
167
|
5 |
|
$current = $subitems[ $i ]->get_page_number(); |
|
168
|
5 |
|
if ( $current > $prev + 1 && $current - $prev != $large_page_number_interval ) { |
|
169
|
3 |
|
$subitems_collection->insert_item_at( clone $limiter_item, $i ); |
|
170
|
3 |
|
} |
|
171
|
5 |
|
} |
|
172
|
6 |
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* Generate and add wrappers. |
|
176
|
|
|
*/ |
|
177
|
3 |
|
public function generate_wrappers() { |
|
178
|
|
|
// get various pagination variables that we need |
|
179
|
3 |
|
$collection = $this->get_collection(); |
|
180
|
3 |
|
$pagination = $collection->get_pagination(); |
|
181
|
3 |
|
$subitems_collection = $this->get_subitems_collection(); |
|
182
|
3 |
|
$total_subitems = count( $subitems_collection->get_items() ); |
|
183
|
|
|
|
|
184
|
|
|
// if there is at least one subitem in the collection |
|
185
|
3 |
|
if ( $total_subitems ) { |
|
186
|
|
|
// insert wrapper before the subitems |
|
187
|
2 |
|
$wrapper_before = new Carbon_Pagination_Item_HTML( $collection ); |
|
188
|
2 |
|
$wrapper_before->set_html( $pagination->get_numbers_wrapper_before() ); |
|
189
|
2 |
|
$subitems_collection->insert_item_at( $wrapper_before, 0 ); |
|
190
|
|
|
|
|
191
|
|
|
// insert wrapper after the subitems |
|
192
|
2 |
|
$wrapper_after = new Carbon_Pagination_Item_HTML( $collection ); |
|
193
|
2 |
|
$wrapper_after->set_html( $pagination->get_numbers_wrapper_after() ); |
|
194
|
2 |
|
$subitems_collection->insert_item_at( $wrapper_after, $total_subitems + 1 ); |
|
195
|
2 |
|
} |
|
196
|
|
|
} |
|
197
|
|
|
} |