/
split_page_results.php
241 lines (200 loc) · 9.88 KB
/
split_page_results.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
<?php
/**
* split_page_results Class.
*
* @copyright Copyright 2003-2023 Zen Cart Development Team
* @copyright Portions Copyright 2003 osCommerce
* @license http://www.zen-cart.com/license/2_0.txt GNU Public License V2.0
* @version $Id: dbltoe 2022 Nov 10 Modified in v1.5.8a $
*/
if (!defined('IS_ADMIN_FLAG')) {
die('Illegal Access');
}
/**
* Split Page Result Class
*
* An sql paging class, that allows for sql result to be shown over a number of pages using simple navigation system
* Overhaul scheduled for subsequent release
*
*/
class splitPageResults extends base {
/**
* Database field used to define unique item to count. Becomes the sql query.
*/
private $countQuery;
/**
* Current page number
*/
public $current_page_number;
/**
* Total number of pages;
*/
public $number_of_pages;
/**
* Total Number of unique rows from $countQuery
*/
public $number_of_rows;
/**
* maximum number of rows to display on a page
*/
private $number_of_rows_per_page;
/**
* The form name of field that holds the current page number
*/
private $page_name;
/**
* Query used to select items for page
*/
public $sql_query;
/* class constructor */
function __construct($query, $max_rows, $count_key = '*', $page_holder = 'page', $debug = false, $countQuery = "") {
global $db;
$max_rows = ($max_rows == '' || $max_rows == 0) ? 20 : $max_rows;
$this->sql_query = preg_replace("/\n\r|\r\n|\n|\r/", " ", $query);
if ($countQuery != "") $countQuery = preg_replace("/\n\r|\r\n|\n|\r/", " ", $countQuery);
$this->countQuery = ($countQuery != "") ? $countQuery : $this->sql_query;
$this->page_name = $page_holder;
if ($debug) {
echo '<br><br>';
echo 'original_query=' . $query . '<br><br>';
echo 'original_count_query=' . $countQuery . '<br><br>';
echo 'sql_query=' . $this->sql_query . '<br><br>';
echo 'count_query=' . $this->countQuery . '<br><br>';
}
if (isset($_GET[$page_holder])) {
$page = $_GET[$page_holder];
} elseif (isset($_POST[$page_holder])) {
$page = $_POST[$page_holder];
} else {
$page = '';
}
if (empty($page) || !is_numeric($page)) $page = 1;
$this->current_page_number = $page;
$this->number_of_rows_per_page = $max_rows;
$pos_to = strlen($this->countQuery);
$query_lower = strtolower($this->countQuery);
$pos_from = strpos($query_lower, ' from', 0);
$pos_group_by = strpos($query_lower, ' group by', $pos_from);
if (($pos_group_by < $pos_to) && ($pos_group_by != false)) $pos_to = $pos_group_by;
$pos_having = strpos($query_lower, ' having', $pos_from);
if (($pos_having < $pos_to) && ($pos_having != false)) $pos_to = $pos_having;
$pos_order_by = strrpos($query_lower, ' order by', $pos_from);
if (($pos_order_by < $pos_to) && ($pos_order_by != false)) $pos_to = $pos_order_by;
if (strpos($query_lower, 'distinct') || strpos($query_lower, 'group by')) {
$count_string = 'distinct ' . zen_db_input($count_key);
} else {
$count_string = zen_db_input($count_key);
}
$count_query = "select count(" . $count_string . ") as total " . substr($this->countQuery, $pos_from, ($pos_to - $pos_from));
if ($debug) {
echo 'count_query=' . $count_query . '<br><br>';
}
$count = $db->Execute($count_query);
$this->number_of_rows = $count->fields['total'];
$this->number_of_pages = ceil($this->number_of_rows / $this->number_of_rows_per_page);
if ($this->current_page_number > $this->number_of_pages) {
$this->current_page_number = $this->number_of_pages;
}
$offset = ($this->number_of_rows_per_page * ($this->current_page_number - 1));
// fix offset error on some versions
if ($offset <= 0) { $offset = 0; }
$this->sql_query .= " limit " . ($offset > 0 ? $offset . ", " : '') . $this->number_of_rows_per_page;
}
/* class functions */
// display split-page-number-links
function display_links($max_page_links, $parameters = '', $outputAsUnorderedList = false, $navElementLabel = '') {
global $request_type;
if ($max_page_links == '') $max_page_links = 1;
if ($this->number_of_pages <= 1) return;
$display_links_string = $ul_elements = '';
$counter_actual_page_links = 0;
$class = '';
if (!empty($parameters) && (substr($parameters, -1) != '&') && ($this->current_page_number > 1)) $parameters .= '&';
// previous button - not displayed on first page
$link = '<a href="' . zen_href_link($_GET['main_page'], $parameters . ($this->current_page_number > 2 ? $this->page_name . '=' . ($this->current_page_number - 1) : ''), $request_type) . '" title="' . PREVNEXT_TITLE_PREVIOUS_PAGE . '">' . PREVNEXT_BUTTON_PREV . '</a>';
if ($this->current_page_number > 1) {
$display_links_string .= $link . ' ';
$ul_elements .= ' <li class="pagination-previous" aria-label="' . ARIA_PAGINATION_PREVIOUS_PAGE . '">' . $link . '</li>' . "\n";
} else {
// $ul_elements .= ' <li class="disabled pagination-previous">' . $link . '</li>' . "\n";
}
// check if number_of_pages > $max_page_links
$cur_window_num = intval($this->current_page_number / $max_page_links);
if ($this->current_page_number % $max_page_links) $cur_window_num++;
$max_window_num = intval($this->number_of_pages / $max_page_links);
if ($this->number_of_pages % $max_page_links) $max_window_num++;
// previous group of pages
$link = '<a href="' . zen_href_link($_GET['main_page'], $parameters . ((($cur_window_num - 1) * $max_page_links) > 1 ? $this->page_name . '=' . (($cur_window_num - 1) * $max_page_links) : ''), $request_type) . '" title="' . sprintf(PREVNEXT_TITLE_PREV_SET_OF_NO_PAGE, $max_page_links) . '" aria-label="' . ARIA_PAGINATION_ELLIPSIS_PREVIOUS . '">...</a>';
if ($cur_window_num > 1) {
$display_links_string .= $link;
$ul_elements .= ' <li class="ellipsis">' . $link . '</li>' . "\n";
} else {
// $ul_elements .= ' <li class="ellipsis" aria-hidden="true">' . $link . '</li>' . "\n";
}
// page nn button
for ($jump_to_page = 1 + (($cur_window_num - 1) * $max_page_links); ($jump_to_page <= ($cur_window_num * $max_page_links)) && ($jump_to_page <= $this->number_of_pages); $jump_to_page++) {
if ($jump_to_page == $this->current_page_number) {
$display_links_string .= ' <strong class="current" aria-current="true" aria-label="' . ARIA_PAGINATION_CURRENT_PAGE . ', ' . sprintf(ARIA_PAGINATION_PAGE_NUM, $jump_to_page) . '">' . $jump_to_page . '</strong> ';
$ul_elements .= ' <li class="current active">' . $jump_to_page . '</li>' . "\n";
$counter_actual_page_links++;
} else {
$link = '<a href="' . zen_href_link($_GET['main_page'], $parameters . ($jump_to_page > 1 ? $this->page_name . '=' . $jump_to_page : ''), $request_type) . '" title="' . sprintf(PREVNEXT_TITLE_PAGE_NO, $jump_to_page) . '" aria-label="' . ARIA_PAGINATION_GOTO . sprintf(ARIA_PAGINATION_PAGE_NUM, $jump_to_page) . '">' . $jump_to_page . '</a>';
$display_links_string .= ' ' . $link . ' ';
$ul_elements .= ' <li>' . $link . '</li>' . "\n";
$counter_actual_page_links++;
}
}
// next group of pages
if ($cur_window_num < $max_window_num) {
$link = '<a href="' . zen_href_link($_GET['main_page'], $parameters . $this->page_name . '=' . (($cur_window_num) * $max_page_links + 1), $request_type) . '" title="' . sprintf(PREVNEXT_TITLE_NEXT_SET_OF_NO_PAGE, $max_page_links) . '" aria-label="' . ARIA_PAGINATION_ELLIPSIS_NEXT . '">...</a>';
$display_links_string .= $link . ' ';
$ul_elements .= ' <li class="ellipsis">' . $link . '</li>' . "\n";
} else {
// $ul_elements .= ' <li class="ellipsis" aria-hidden="true">' . $link . '</li>' . "\n";
}
// next button
if (($this->current_page_number < $this->number_of_pages) && ($this->number_of_pages != 1)) {
$link = '<a href="' . zen_href_link($_GET['main_page'], $parameters . $this->page_name . '=' . ($this->current_page_number + 1), $request_type) . '" title="' . PREVNEXT_TITLE_NEXT_PAGE . '" aria-label="' . ARIA_PAGINATION_NEXT_PAGE . '">' . PREVNEXT_BUTTON_NEXT . '</a>';
$display_links_string .= ' ' . $link . ' ';
$ul_elements .= ' <li class="pagination-next">' . $link . '</li>' . "\n";
} else {
// $ul_elements .= ' <li class="disabled pagination-next">' . $link . '</li>' . "\n";
}
// if no pagination needed, return blank
if ($counter_actual_page_links == 0) return;
// return <nav><ul> format with a-hrefs wrapped in <li>
// not setting role="navigation" because we're using a <nav> element already.
if ($outputAsUnorderedList) {
$aria_label = empty($navElementLabel) ? ARIA_PAGINATION_ROLE_LABEL_GENERAL : sprintf(ARIA_PAGINATION_ROLE_LABEL_FOR, zen_output_string_protected($navElementLabel));
$aria_label .= sprintf(ARIA_PAGINATION_CURRENTLY_ON, $this->current_page_number);
return '<nav class="pagination" aria-label="' . $aria_label . '">' . "\n" .
'<ul class="pagination">' . "\n" .
$ul_elements .
'</ul>' . "\n" .
'</nav>';
}
// return unformatted collection of a-hrefs
return $display_links_string;
}
// display number of total products found
function display_count($text_output) {
$to_num = ($this->number_of_rows_per_page * $this->current_page_number);
if ($to_num > $this->number_of_rows) $to_num = $this->number_of_rows;
$from_num = ($this->number_of_rows_per_page * ($this->current_page_number - 1));
if ($to_num == 0) {
$from_num = 0;
} else {
$from_num++;
}
if ($to_num <= 1) {
// don't show count when 1
return '';
} else {
return sprintf($text_output, $from_num, $to_num, $this->number_of_rows);
}
}
public function getSqlQuery()
{
return $this->sql_query;
}
}