-
Notifications
You must be signed in to change notification settings - Fork 18
/
zca_split_page_results.php
209 lines (172 loc) · 8.95 KB
/
zca_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
<?php
/**
* split_page_results Class.
*
* BOOTSTRAP v1.0.BETA
*
* @package classes
* @copyright Copyright 2003-2016 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: Author: DrByte Sat Jan 9 13:13:41 2016 -0500 Modified in v1.5.5 $
*/
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
*
* @package classes
*/
// -----
// Provides a modified version of the splitPageResults formatting; loaded by
// /includes/init_includes/init_zca_bootstrap.php if the 'bootstrap' template is the
// currently-active template. The template-specific processing will use this formatting
// when the ZCA Bootstrap template is active.
//
class zca_splitPageResults extends base {
var $sql_query, $number_of_rows, $current_page_number, $number_of_pages, $number_of_rows_per_page, $page_name;
/* 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 = strpos($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) {
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 (zen_not_null($parameters) && (substr($parameters, -1) != '&')) $parameters .= '&';
// previous button - not displayed on first page
$link = '<a class="page-link" href="' . zen_href_link($_GET['main_page'], $parameters . $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 .= '<li class="page-item">' . $link . '</li>';
$ul_elements .= '<li class="page-item">' . $link . '</li>';
} 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 class="page-link" href="' . zen_href_link($_GET['main_page'], $parameters . $this->page_name . '=' . (($cur_window_num - 1) * $max_page_links), $request_type) . '" title=" ' . sprintf(PREVNEXT_TITLE_PREV_SET_OF_NO_PAGE, $max_page_links) . ' ">...</a>';
if ($cur_window_num > 1) {
$display_links_string .= '<li class="page-item">' . $link . '</li>';
$ul_elements .= '<li class="page-item">' . $link . '</li>';
} 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 .= '<li class="page-item active"><span class="page-link">' . $jump_to_page . '</span></li>';
$ul_elements .= '<li class="page-item active"><span class="page-link">' . $jump_to_page . '</span></li>';
$counter_actual_page_links++;
} else {
$link = '<a class="page-link" href="' . zen_href_link($_GET['main_page'], $parameters . $this->page_name . '=' . $jump_to_page, $request_type) . '" title=" ' . sprintf(PREVNEXT_TITLE_PAGE_NO, $jump_to_page) . ' ">' . $jump_to_page . '</a>';
$display_links_string .= '<li class="page-item">' . $link . '</li>';
$ul_elements .= '<li class="page-item">' . $link . '</li>';
$counter_actual_page_links++;
}
}
// next group of pages
if ($cur_window_num < $max_window_num) {
$link = '<a class="page-link" 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) . ' ">...</a>';
$display_links_string .= '<li class="page-item">' . $link . '</li>';
$ul_elements .= '<li class="page-item">' . $link . '</li>';
} 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 class="page-link" href="' . zen_href_link($_GET['main_page'], $parameters . 'page=' . ($this->current_page_number + 1), $request_type) . '" title=" ' . PREVNEXT_TITLE_NEXT_PAGE . ' ">' . PREVNEXT_BUTTON_NEXT . '</a>';
$display_links_string .= '<li class="page-item">' . $link . '</li>';
$ul_elements .= '<li class="page-item">' . $link . '</li>';
} 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>
if ($outputAsUnorderedList) {
return '<nav aria-label="Pagination"><ul class="pagination float-right">' . $ul_elements . '</ul></nav>';
}
// return unformatted collection of a-hrefs
return '<nav aria-label="Pagination"><ul class="pagination float-right">' . $display_links_string . '</ul></nav>';
}
// 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);
}
}
}