PluginProbe
Redirection / 2.2.11
Redirection v2.2.11
5.10.0 5.9.0 5.8.1 5.8.0 3.7.2 3.7.3 4.0 4.0.1 4.1 4.1.1 4.2 4.2.1 4.2.2 4.2.3 4.3 4.3.1 4.3.2 4.3.3 4.4 4.4.1 4.4.2 4.5 4.5.1 4.6.2 4.7.1 All 130 releases
redirection / models / pager.php

pager.php in Redirection 2.2.11, at models/pager.php

483 lines 12.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 // ======================================================================================
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License, or (at your option) any later version.
8 //
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
13 // ======================================================================================
14 // @author John Godley (http://urbangiraffe.com)
15 // @version 0.2.8
16 // @copyright Copyright &copy; 2007 John Godley, All Rights Reserved
17 // ======================================================================================
18 // 0.2.3 - Remember pager details in user data
19 // 0.2.4 - Add phpdoc comments
20 // 0.2.5 - Allow orderby to use tags to hide database columns
21 // 0.2.6 - Fix sortable columns with only 1 page
22 // 0.2.7 - Add a GROUP BY feature, make search work when position not 0
23 // 0.2.8 - WP 2.7 functions
24 // ======================================================================================
25
26
27 /**
28 * Provides pagination, column-based ordering, searching, and filtering
29 *
30 * The class does no database queries itself but instead relies on the user modifying their queries with data
31 * from the pager. For correct pagination you must set the total number of results
32 *
33 * For example,
34 *
35 * $pager->set_total ($wpdb->get_var ("SELECT COUNT(*) FROM wp_posts").$pager->to_conditions ());
36 * $rows = $wpdb->get_results ("SELECT * FROM wp_posts".$pager->to_limits ("post_type=page"));
37 *
38 * Searching is achieved by specifying the columns that can be searched:
39 *
40 * $rows = $wpdb->get_results ("SELECT * FROM wp_posts".$pager->to_limits ("post_type=page", array ('post_content', 'post_excerpt')));
41 *
42 * Additionally you can output column headings with correct URLs:
43 * <th><?php echo $pager->sortable ('post_username', 'Username') ?></th>
44 *
45 * @package default
46 **/
47
48 class RE_Pager
49 {
50 var $url = null;
51 var $current_page = 1;
52 var $per_page = 25;
53 var $total = 0;
54 var $order_by = null;
55 var $order_original = null;
56 var $order_direction = null;
57 var $order_tags = array ();
58 var $steps = array ();
59 var $search = null;
60 var $filters = array ();
61 var $id;
62
63
64 /**
65 * Construct a pager object using the $_GET data, the current URL, and default preferences
66 *
67 * @param array $data Array of values, typically from $_GET
68 * @param string $url The current URL
69 * @param string $orderby Default database column to order data by
70 * @param string $direction Default direction of ordering (DESC or ASC)
71 * @param string $id An ID for the pager to separate it from other pagers (typically the plugin name)
72 * @return void
73 **/
74 function RE_Pager ($data, $url, $orderby = '', $direction = 'DESC', $id = 'default', $tags = '')
75 {
76 // Remove all pager params from the url
77 $this->id = $id;
78 $this->url = $url;
79
80 if (isset ($data['curpage']))
81 $this->current_page = intval ($data['curpage']);
82
83 global $user_ID;
84
85 if (isset ($data['perpage']))
86 {
87 $this->per_page = intval ($data['perpage']);
88 $per_page[get_class ($this)][$this->id] = $this->per_page;
89 }
90 else if (isset ($per_page[get_class ($this)]) && isset ($per_page[get_class ($this)][$this->id]))
91 $this->per_page = $per_page[get_class ($this)][$this->id];
92
93 if ($orderby != '')
94 $this->order_by = $orderby;
95
96 if (isset ($data['orderby']))
97 $this->order_by = $data['orderby'];
98
99 if (!empty ($tags))
100 {
101 $this->order_tags = $tags;
102 if (isset ($this->order_tags[$this->order_by]))
103 $this->order_by = $this->order_tags[$this->order_by];
104 }
105
106 $this->order_direction = $direction;
107 $this->order_original = $orderby;
108 if (isset ($data['order']))
109 $this->order_direction = $data['order'];
110
111 $this->search = isset($data['search']) ? $data['search'] : '';
112 $this->steps = array (10, 25, 50, 100, 250);
113 $this->url = str_replace ('&', '&amp;', $this->url);
114 $this->url = str_replace ('&&amp;', '&amp;', $this->url);
115 }
116
117
118 /**
119 * Set the total number of entries that match the conditions
120 *
121 * @param int $total Count
122 * @return void
123 **/
124
125 function set_total ($total)
126 {
127 $this->total = $total;
128
129 if ($this->current_page <= 0 || $this->current_page > $this->total_pages ())
130 $this->current_page = 1;
131 }
132
133
134 /**
135 * Return the current page offset
136 *
137 * @return int Current page offset
138 **/
139
140 function offset ()
141 {
142 return ($this->current_page - 1) * $this->per_page;
143 }
144
145
146 /**
147 * @todo explain
148 * @return void
149 **/
150 // XXX
151
152 function is_secondary_sort ()
153 {
154 return substr ($this->order_by, 0, 1) == '_' ? true : false;
155 }
156
157
158 /**
159 * Returns a set of conditions without any limits. This is suitable for a COUNT SQL
160 *
161 * @param string $conditions WHERE conditions
162 * @param array $searches Array of columns to search on
163 * @param array $filters Array of columns to filter on
164 * @return string SQL
165 **/
166
167 function to_conditions ($conditions, $searches = '', $filters = '') {
168 global $wpdb;
169
170 $sql = '';
171 if ($conditions != '')
172 $sql .= ' WHERE '.$conditions;
173
174 // Add on search conditions
175 if (is_array ($searches) && $this->search != '')
176 {
177 if ($sql == '')
178 $sql .= ' WHERE (';
179 else
180 $sql .= ' AND (';
181
182 $searchbits = array ();
183 foreach ($searches AS $search)
184 $searchbits[] = $wpdb->prepare( $search.' LIKE "%s"', '%'.$this->search.'%' );
185
186 $sql .= implode (' OR ', $searchbits);
187 $sql .= ')';
188 }
189
190 // Add filters
191 if (is_array ($filters) && !empty ($this->filters))
192 {
193 $searchbits = array ();
194 foreach ($filters AS $filter)
195 {
196 if (isset ($this->filters[$filter]))
197 {
198 if ($this->filters[$filter] != '')
199 $searchbits[] = $wpdb->prepare( $filter." = %s", $this->filters[$filter] );
200 }
201 }
202
203 if (count ($searchbits) > 0)
204 {
205 if ($sql == '')
206 $sql .= ' WHERE (';
207 else
208 $sql .= ' AND (';
209
210 $sql .= implode (' AND ', $searchbits);
211 $sql .= ')';
212 }
213 }
214
215 return $sql;
216 }
217
218
219 /**
220 * Returns a set of conditions with limits.
221 *
222 * @param string $conditions WHERE conditions
223 * @param array $searches Array of columns to search on
224 * @param array $filters Array of columns to filter on
225 * @return string SQL
226 **/
227
228 function to_limits ($conditions = '', $searches = '', $filters = '', $group_by = '') {
229 global $wpdb;
230
231 $sql = $this->to_conditions ($conditions, $searches, $filters);
232
233 if ($group_by)
234 $sql .= ' '.$group_by.' ';
235
236 if (strlen ($this->order_by) > 0)
237 {
238 if (!$this->is_secondary_sort ())
239 $sql .= " ORDER BY ".$this->order_by.' '.$this->order_direction;
240 else
241 $sql .= " ORDER BY ".$this->order_original.' '.$this->order_direction;
242 }
243
244 if ($this->per_page > 0)
245 $sql .= $wpdb->prepare( ' LIMIT %d,%d', $this->offset(), $this->per_page );
246 return $sql;
247 }
248
249
250 /**
251 * Return the url with all the params added back
252 *
253 * @param int Page offset
254 * @param string $orderby Optional order
255 * @return string URL
256 **/
257
258 function url ($offset, $orderby = '')
259 {
260 // Position
261 if (strpos ($this->url, 'curpage=') !== false)
262 $url = preg_replace ('/curpage=\d*/', 'curpage='.$offset, $this->url);
263 else
264 $url = $this->url.'&amp;curpage='.$offset;
265
266 // Order
267 if ($orderby != '')
268 {
269 if (strpos ($url, 'orderby=') !== false)
270 $url = preg_replace ('/orderby=\w*/', 'orderby='.$orderby, $url);
271 else
272 $url = $url.'&amp;orderby='.$orderby;
273
274 if (!empty ($this->order_tags) && isset ($this->order_tags[$orderby]))
275 $dir = $this->order_direction == 'ASC' ? 'DESC' : 'ASC';
276 else if ($this->order_by == $orderby)
277 $dir = $this->order_direction == 'ASC' ? 'DESC' : 'ASC';
278 else
279 $dir = $this->order_direction;
280
281 if (strpos ($url, 'order=') !== false)
282 $url = preg_replace ('/order=\w*/', 'order='.$dir, $url);
283 else
284 $url = $url.'&amp;order='.$dir;
285 }
286
287 return str_replace ('&go=go', '', $url);
288 }
289
290
291 /**
292 * Return current page
293 *
294 * @return int
295 **/
296
297 function current_page () { return $this->current_page; }
298
299
300 /**
301 * Return total number of pages
302 *
303 * @return int
304 **/
305
306 function total_pages ()
307 {
308 if ($this->per_page == 0)
309 return 1;
310 return ceil ($this->total / $this->per_page);
311 }
312
313
314 /**
315 * Determine if we have a next page
316 *
317 * @return boolean
318 **/
319
320 function have_next_page ()
321 {
322 if ($this->current_page < $this->total_pages ())
323 return true;
324 return false;
325 }
326
327
328 /**
329 * Determine if we have a previous page
330 *
331 * @return boolean
332 **/
333
334 function have_previous_page ()
335 {
336 if ($this->current_page > 1)
337 return true;
338 return false;
339 }
340
341
342 function sortable_class ($column, $class = true)
343 {
344 if ($column == $this->order_by)
345 {
346 if ($class)
347 printf (' class="sortedd"');
348 else
349 echo ' sortedd';
350 }
351 }
352
353 /**
354 * Return a string suitable for a sortable column heading
355 *
356 * @param string $column Column to search upon
357 * @param string $text Text to display for the column
358 * @param boolean $image Whether to show a direction image
359 * @return string URL
360 **/
361
362 function sortable ($column, $text, $image = true)
363 {
364 $url = $this->url ($this->current_page, $column);
365 $img = '';
366
367 if (isset ($this->order_tags[$column]))
368 $column = $this->order_tags[$column];
369
370 if ($column == $this->order_by)
371 {
372 if (defined ('WP_PLUGIN_URL'))
373 $dir = WP_PLUGIN_URL.'/'.basename (dirname (dirname (__FILE__)));
374 else
375 $dir = get_bloginfo ('wpurl').'/wp-content/plugins/'.basename (dirname (dirname (__FILE__)));
376
377 if (strpos ($url, 'ASC') !== false)
378 $img = '<img align="bottom" src="'.$dir.'/images/up.gif" alt="dir" width="16" height="7"/>';
379 else
380 $img = '<img align="bottom" src="'.$dir.'/images/down.gif" alt="dir" width="16" height="7"/>';
381
382 if ($image == false)
383 $img = '';
384 }
385
386 return '<a href="'.$url.'">'.$text.'</a>'.$img;
387 }
388
389
390 /**
391 * Returns an array of page numbers => link, given the current page (next and previous etc)
392 *
393 * @return array Array of page links
394 **/
395
396 function area_pages ()
397 {
398 // First page
399 $allow_dot = true;
400 $pages = array ();
401
402 if ($this->total_pages () > 1)
403 {
404 $previous = __ ('Previous', 'redirection');
405 $next = __ ('Next', 'redirection');
406
407 if ($this->have_previous_page ())
408 $pages[] = '<a href="'.$this->url ($this->current_page - 1).'">'.$previous.'</a> |';
409 else
410 $pages[] = $previous.' |';
411
412 for ($pos = 1; $pos <= $this->total_pages (); $pos++)
413 {
414 if ($pos == $this->current_page)
415 {
416 $pages[] = '<span class="active">'.$pos.'</span>';
417 $allow_dot = true;
418 }
419 else if ($pos == 1 || abs ($this->current_page - $pos) <= 2 || $pos == $this->total_pages ())
420 $pages[] = '<a href="'.$this->url ($pos).'">'.$pos."</a>";
421 else if ($allow_dot)
422 {
423 $allow_dot = false;
424 $pages[] = '&hellip;';
425 }
426 }
427
428 if ($this->have_next_page ())
429 $pages[] = '| <a href="'.$this->url ($this->current_page + 1).'">'.$next.'</a>';
430 else
431 $pages[] = '| '.$next;
432 }
433
434 return $pages;
435 }
436
437
438 /**
439 * @todo
440 * @return boolean
441 **/
442
443 function filtered ($field, $value)
444 {
445 if (isset ($this->filters[$field]) && $this->filters[$field] == $value)
446 return true;
447 return false;
448 }
449
450
451 /**
452 * Display a SELECT box suitable for a per-page
453 *
454 * @return void
455 **/
456
457 function per_page ($plugin = '')
458 {
459 ?>
460 <select name="perpage">
461 <?php foreach ($this->steps AS $step) : ?>
462 <option value="<?php echo $step ?>"<?php if ($this->per_page == $step) echo ' selected="selected"' ?>>
463 <?php printf (__ ('%d per-page', $plugin), $step) ?>
464 </option>
465 <?php endforeach; ?>
466 </select>
467 <?php
468 }
469
470 function page_links ()
471 {
472 $text = sprintf( '<span class="displaying-num">' . __( 'Displaying %s&#8211;%s of %s' ) . '</span>',
473 number_format_i18n (($this->current_page () - 1) * $this->per_page + 1),
474 number_format_i18n ($this->current_page () * $this->per_page > $this->total ? $this->total : $this->current_page () * $this->per_page),
475 number_format_i18n ($this->total));
476
477 $links = paginate_links (array ('base' => str_replace ('99', '%#%', $this->url (99)), 'format' => '%#%', 'current' => $this->current_page (), 'total' => $this->total_pages (), 'end_size' => 3, 'mid_size' => 2, 'prev_next' => true));
478 return $text.$links;
479 }
480 }
481
482 ?>
483