forked from Iddah/informea.plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
imea.functions.php
783 lines (701 loc) · 26.2 KB
/
imea.functions.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
<?php
/**
* This code provides utility functions to the InforMEA plugin.
* Put your generic functions here.
*/
if (!function_exists('imea_log')) {
/**
* Log information
*/
function imea_log($message, $warn = FALSE) {
$message = 'IMEA: ' . ($warn ? '[WARN] ' : '') . $message;
if (WP_DEBUG === true) {
if (is_array($message) || is_object($message)) {
error_log(print_r($message, true));
} else {
error_log($message);
}
}
}
}
if (!function_exists('imea_debug')) {
/**
* Output debug information. Works if user enables 'Debug' in plugin configuration page from administration area.
*/
function imea_debug($message, $warn = FALSE) {
$options = get_option('informea_options');
if ($options['debug']) {
$message = 'IMEA: [DEBUG] ' . $message;
if (WP_DEBUG === true) {
if (is_array($message) || is_object($message)) {
error_log(print_r($message, true));
} else {
error_log($message);
}
}
}
}
}
if (!function_exists('echo_trace')) {
function echo_trace() {
echo '<pre>';
$trace = debug_backtrace();
foreach ($trace as $t) {
echo $t['file'] . '(' . $t['line'] . ')';
echo "\n";
}
echo "
-- Trace end --
</pre>";
}
}
if (!function_exists('html_script')) {
/**
* Write <script ...>
* @param $rel_path Path relative to template_directory
* @return String so you can echo
*/
function html_script($rel_path) {
return '<script type="text/javascript" src="' . get_bloginfo('template_directory') . '/' . $rel_path . '"></script>' . "\n";
}
}
if (!function_exists('html_style')) {
/**
* Write <link stylesheet ...>
* @param $rel_path Path relative to template_directory
* @return String so you can echo
*/
function html_style($rel_path) {
return '<link href="' . get_bloginfo('template_directory') . '/' . $rel_path . '" rel="stylesheet" type="text/css" />' . "\n";
}
}
if (!function_exists('subwords')) {
/**
* Extract the first $len words from a string
* @param $len Length in words
* @return Sliced string
*/
function subwords($s, $len = 10, $suffix = ' ...') {
if ($s !== NULL && $s != '') {
$put_suffix = FALSE;
$arr = explode(' ', $s);
if (count($arr) > $len) {
$arr = array_slice($arr, 0, $len);
$put_suffix = TRUE;
}
return implode(' ', $arr) . ($put_suffix ? $suffix : '');
}
}
}
if (!function_exists('slugify')) {
function slugify($phrase, $maxLength = 50) {
$result = strtolower($phrase);
$result = preg_replace("/[^a-z0-9\s-]/", "", $result);
$result = trim(preg_replace("/[\s-]+/", " ", $result));
$result = preg_replace("/\s/", "-", $result);
return $result;
}
}
if (!function_exists('stdclass_copy')) {
/**
* Copy subset of object properties to other object
* @param object $source Source object
* @param array $properties Properties to copy to the new object
* @return New stdClass object with copied properties. References are kept intact. NULL if $source is not object, $source is NULL or $properties is empty array
*/
function stdclass_copy($source, $properties) {
if (!is_object($source) || empty($source)) {
error_log("shallow_copy(): Invalid source object passed");
return NULL;
}
if (empty($properties)) {
error_log("shallow_copy(): Refusing to return object without properties");
return NULL;
}
$ob = new stdClass();
foreach ($properties as $property) {
if (isset($source->$property)) {
$ob->$property = $source->$property;
} else {
$ob->$property = NULL;
error_log("shallow_copy(): Object does not have property $property, ignoring");
}
}
return $ob;
}
}
if (!function_exists('number_order_human')) {
/**
* Extract the first $len words from a string
* //TODO: Fix for various languages?
* @param $no Number to transform
* @return Transformed number
*/
function number_order_human($no) {
if ($no == 1) {
return '1st';
}
if ($no == 2) {
return '2nd';
}
if ($no == 3) {
return '3rd';
}
}
}
/**
* Pretty print a date interval
*
* @param integer $start
* (required) Start date as timestamp or string. If string, format would be 'YYYY-MM-dd'
* @param integer $end
* (optional) End date as timestamp or string. If string, format is 'YYYY-MM-dd'
* @param string $month
* Month format. %B - Full month name, %b - Abbreviated month etc. See strftime() manual for more details
* @param string $year
* Year format. %Y - 4-digit year, %y - 2-digit year. See date() manual for more details
* @param string $separator
* Separator between dates. For instance ' - ' in "16 - 19 October 2012"
*
* @return Date interval formatted, depending wether $end is null or not. Examples:
* * 19 - 23 January 2012 - same month, different month format
* * 28 Oct - 29 Nov 2012 - different months
* * 15 Dec 2012 - 3 Jan 2013 - different years
* Returns empty string if $start is invalid
*/
if (!function_exists('date_interval')) {
function date_interval($start, $end = null, $month = '%B', $year = '%Y', $separator = ' - ') {
$ret = '';
if (empty($start)) {
return $ret;
}
$df = '%e'; // day format - default single digit
$fmt = '%Y-%m-%d';
if (is_string($start)) {
$a = strptime($start, $fmt);
$start = mktime(0, 0, 0, $a['tm_mon'] + 1, $a['tm_mday'], $a['tm_year'] + 1900);
}
if (!empty($end)) {
if (is_string($end)) {
$a = strptime($end, $fmt);
$end = mktime(0, 0, 0, $a['tm_mon'] + 1, $a['tm_mday'], $a['tm_year'] + 1900);
}
} else {
$end = false;
}
$full_start_date = strftime("$df $month $year", $start);
if ($end) {
$full_end_date = trim(strftime("$df $month $year", $end));
$ey = strftime('%Y', $end);
$sy = strftime('%Y', $start);
if ($ey != $sy) {
$ret = sprintf('%s%s%s', $full_start_date, $separator, $full_end_date);
} else {
// Same year
$em = strftime('%m', $end);
$sm = strftime('%m', $start);
if ($em != $sm) {
$ret = sprintf('%s%s%s', strftime("$df $month", $start), $separator, $full_end_date);
} else {
// Same month
$ret = sprintf('%s%s%s', strftime("$df", $start), $separator, $full_end_date);
}
}
} else {
$ret = $full_start_date;
}
return trim($ret);
}
}
/**
* Mobile Detect
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/
class Mobile_Detect {
protected $accept;
protected $userAgent;
protected $isMobile = false;
protected $isAndroid = null;
protected $isAndroidtablet = null;
protected $isIphone = null;
protected $isIpad = null;
protected $isBlackberry = null;
protected $isBlackberrytablet = null;
protected $isOpera = null;
protected $isPalm = null;
protected $isWindows = null;
protected $isWindowsphone = null;
protected $isGeneric = null;
protected $devices = array(
"android" => "android.*mobile",
"androidtablet" => "android(?!.*mobile)",
"blackberry" => "blackberry",
"blackberrytablet" => "rim tablet os",
"iphone" => "(iphone|ipod)",
"ipad" => "(ipad)",
"palm" => "(avantgo|blazer|elaine|hiptop|palm|plucker|xiino)",
"windows" => "windows ce; (iemobile|ppc|smartphone)",
"windowsphone" => "windows phone os",
"generic" => "(kindle|mobile|mmp|midp|pocket|psp|symbian|smartphone|treo|up.browser|up.link|vodafone|wap|opera mini)"
);
public function __construct() {
if (isset($_SERVER['HTTP_USER_AGENT'])) {
$this->userAgent = $_SERVER['HTTP_USER_AGENT'];
}
if (isset($_SERVER['HTTP_ACCEPT'])) {
$this->accept = $_SERVER['HTTP_ACCEPT'];
}
if (isset($_SERVER['HTTP_X_WAP_PROFILE']) || isset($_SERVER['HTTP_PROFILE'])) {
$this->isMobile = true;
} elseif (strpos($this->accept, 'text/vnd.wap.wml') > 0 || strpos($this->accept, 'application/vnd.wap.xhtml+xml') > 0) {
$this->isMobile = true;
} else {
foreach ($this->devices as $device => $regexp) {
if ($this->isDevice($device)) {
$this->isMobile = true;
}
}
}
}
/**
* Overloads isAndroid() | isAndroidtablet() | isIphone() | isIpad() | isBlackberry() | isBlackberrytablet() | isPalm() | isWindowsphone() | isWindows() | isGeneric() through isDevice()
*
* @param string $name
* @param array $arguments
* @return bool
*/
public function __call($name, $arguments) {
$device = substr($name, 2);
if ($name == "is" . ucfirst($device) && array_key_exists(strtolower($device), $this->devices)) {
return $this->isDevice($device);
} else {
trigger_error("Method $name not defined", E_USER_WARNING);
}
}
/**
* Returns true if any type of mobile device detected, including special ones
* @return bool
*/
public function isMobile() {
return $this->isMobile;
}
protected function isDevice($device) {
$var = "is" . ucfirst($device);
$return = $this->$var === null ? (bool)preg_match("/" . $this->devices[strtolower($device)] . "/i", $this->userAgent) : $this->$var;
if ($device != 'generic' && $return == true) {
$this->isGeneric = false;
}
return $return;
}
}
if (!function_exists('microtime_float')) {
function microtime_float($start = null, $msg = 'Execution took') {
list ($msec, $sec) = explode(' ', microtime());
$microtime = (float)$msec + (float)$sec;
if ($start) {
imea_debug($msg . ': ' . (round($microtime - $start, 3) * 1000) . ' ms');
}
return $microtime;
}
}
# PHP Calendar (version 2.3), written by Keith Devens
# http://keithdevens.com/software/php_calendar
# see example at http://keithdevens.com/weblog
# License: http://keithdevens.com/software/license
if (!function_exists('generate_calendar')) {
function generate_calendar($year, $month, $days = array(), $day_name_length = 3, $month_href = NULL, $first_day = 0,
$pn = array()) {
$first_of_month = gmmktime(0, 0, 0, $month, 1, $year);
#remember that mktime will automatically correct if invalid dates are entered
# for instance, mktime(0,0,0,12,32,1997) will be the date for Jan 1, 1998
# this provides a built in "rounding" feature to generate_calendar()
$day_names = array(); #generate all the day names according to the current locale
for ($n = 0, $t = (3 + $first_day) * 86400; $n < 7; $n++, $t += 86400) #January 4, 1970 was a Sunday
$day_names[$n] = ucfirst(gmstrftime('%A', $t)); #%A means full textual day name
list($month, $year, $month_name, $weekday) = explode(',', gmstrftime('%m,%Y,%B,%w', $first_of_month));
$weekday = ($weekday + 7 - $first_day) % 7; #adjust for $first_day
$title = htmlentities(ucfirst($month_name)) . ' ' . $year; #note that some locales don't capitalize month and day names
#Begin calendar. Uses a real <caption>. See http://diveintomark.org/archives/2002/07/03
@list($p, $pl) = each($pn);
@list($n, $nl) = each($pn); #previous and next links, if applicable
if ($p) {
$p = '<span class="calendar-prev">' . ($pl ? '<a href="' . htmlspecialchars($pl) . '">' . $p . '</a>' : $p) . '</span> ';
}
if ($n) {
$n = ' <span class="calendar-next">' . ($nl ? '<a href="' . htmlspecialchars($nl) . '">' . $n . '</a>' : $n) . '</span>';
}
$calendar = '<table class="calendar">' . "\n" .
'<caption class="calendar-month">' . $p . ($month_href ? '<a href="' . htmlspecialchars($month_href) . '">' . $title . '</a>' : $title) . $n . "</caption>\n<tr>";
if ($day_name_length) { #if the day names should be shown ($day_name_length > 0)
#if day_name_length is >3, the full name of the day will be printed
foreach ($day_names as $d)
$calendar .= '<th abbr="' . htmlentities($d) . '">' . htmlentities($day_name_length < 4 ? substr($d, 0, $day_name_length) : $d) . '</th>';
$calendar .= "</tr>\n<tr>";
}
if ($weekday > 0) {
$calendar .= '<td colspan="' . $weekday . '"> </td>';
} #initial 'empty' days
for ($day = 1, $days_in_month = gmdate('t', $first_of_month); $day <= $days_in_month; $day++, $weekday++) {
if ($weekday == 7) {
$weekday = 0; #start a new week
$calendar .= "</tr>\n<tr>";
}
if (isset($days[$day]) and is_array($days[$day])) {
@list($link, $link_title, $external, $classes, $content) = $days[$day];
if (is_null($content)) {
$content = $day;
}
$calendar .= '<td' . ($classes ? ' class="' . htmlspecialchars($classes) . '">' : '>') .
($link ? '<a href="' . htmlspecialchars($link) . '" title="' . $link_title . '"' . ($external ? ' target="_blank" ' : '') . '>' . $content . '</a>' : $content) . '</td>';
} else {
$calendar .= "<td>$day</td>";
}
}
if ($weekday != 7) {
$calendar .= '<td colspan="' . (7 - $weekday) . '"> </td>';
} #remaining "empty" days
return $calendar . "</tr>\n</table>\n";
}
}
if (!function_exists('replace_enter_br')) {
function replace_enter_br($strval) {
if ($strval !== NULL) {
$r = array("\r\n", "\n", "\r");
return str_replace($r, '<br />', $strval);
}
}
}
if (!function_exists('get_administrators')) {
function get_administrators() {
global $wpdb;
$sql = "SELECT user_email, meta_value FROM wp_users a INNER JOIN wp_usermeta b ON a.id = b.user_id WHERE meta_key = 'wp_capabilities'"; //TODO: replace wp_ with prefix_
return $wpdb->get_results($sql);
}
}
if (!class_exists('paginated_query')) {
/**
* Pagination class used to retrieve the dataset from database in chunks of data.
*/
class paginated_query {
private $sql;
private $sql_count;
private $page_size = 10;
private $page = 0;
/** Total number of results */
private $count = 0;
/** Accepted request parameters to build URLs for pagination */
private $req_parameters = array();
private $method = 'get';
private $tpl_post = "
<input type='hidden' name='%s' value='%s' />";
/**
* Where the base_url / action form will point to. Default to current page ''.
*/
private $target_url = '';
/**
* Constructor
* @param sql SQL statement that retrieves the data
* @param req_parameters HTTP request parameters specific to the page in order to correctly reconstruct the URL and add specific parameters (i.e. page)
* @param page Page of results to display (ex. 0, 1, 2, 3, 4 ...)
* @param page_size Size of the results page (ex. 10)
* @param sql_count The SQL statement used for counting.
* For simple queries `SELECT * FROM table` there is no need, class will figure it out by replacing `*` with `COUNT(*)`.
* For complex selects write here the statement.
*/
function __construct($sql, $req_parameters, $page = NULL, $page_size = 10, $sql_count = NULL) {
$this->sql = $sql;
$this->sql_count = $sql_count;
$this->page_size = $page_size;
$this->req_parameters = $req_parameters;
$this->page = $page;
if ($this->page === NULL) {
$this->page = get_request_value('page', 0);
}
// Fixe page to avoid SQL errors
if ($this->page < 0) {
$this->page = 0;
}
}
function __destruct() {
return true;
}
function _count_results() {
global $wpdb;
$ret = 0;
$sql = $this->sql_count;
if ($sql === NULL) {
// Gues the count sql by replacing `SELECT ... FROM table` with `SELECT COUNT(*) FROM table`.
// This should work for most simple statements.
$sql = preg_replace('/^SELECT .* FROM/', 'SELECT COUNT(*) AS cnt FROM', $this->sql, 1);
}
$count = $wpdb->get_results($sql);
if (count($count)) {
$ret = $count[0]->cnt;
}
$this->count = $ret;
return $ret;
}
/**
* Retrieve the page of results
* @return A list of rows from the database. object with properties as column names.
*/
function results() {
global $wpdb;
$this->_count_results();
$start = $this->page * $this->page_size;
// Normalize the start value to last page if erroneous
if ($start > $this->count) {
$start = $this->count - ($this->count % $this->page_size);
}
$this->sql = $this->sql . " LIMIT " . $start . ", " . $this->page_size;
return $wpdb->get_results($this->sql);
}
/**
* Get the starting index for this page (10th, 20th row etc.)
* @return integer with first row index
*/
function start() {
return $this->page * $this->page_size + 1;
}
/**
* Get the last index for this page (21th, 31th etc.)
* @return integer with last row index
*/
function end() {
if ($this->is_last_page()) {
return $this->count;
}
return $this->page * $this->page_size + $this->page_size;
}
/**
* Check if we are on last page of results.
* @return TRUE if this is the last page
*/
function is_last_page() {
return $this->page == ceil($this->count / $this->page_size) - 1;
}
/**
* Retrieve the total number of results.
* @return integer with count of all records from the query.
*/
function total() {
return $this->count;
}
/**
* Check if we have a next page of results.
* @return TRUE if we have next page
*/
function has_next() {
$pages = ceil($this->count / $this->page_size);
return $this->page < $pages - 1;
}
/**
* Get the URL for the next page of results. Useful for templating.
* @return string with next's page URL
*/
function next_url() {
$url = $this->base_url();
return $url . '&page=' . ($this->page + 1);
}
function next_form() {
$ret = $this->base_form();
$ret .= sprintf($this->tpl_post, 'page', $this->page + 1);
return $ret;
}
/**
* Check if we have a previous page of results.
* @return TRUE if we have previous page
*/
function has_previous() {
$pages = ceil($this->count / $this->page_size);
return $this->page > 0;
}
/**
* Get the URL for the previous page of results. Useful for templating.
* @return string with previous's page URL
*/
function previous_url() {
$url = $this->base_url();
return $url . '&page=' . ($this->page - 1);
}
function previous_form() {
$ret = $this->base_form();
$ret .= sprintf($this->tpl_post, 'page', $this->page - 1);
return $ret;
}
/**
* Retrieve the HTTP base URL to the current page and reconstruct the GET parameters.
* @return string with URL to the page.
*/
function base_url() {
$ret = $this->target_url . '?';
foreach ($this->req_parameters as $name => $type) {
if (isset($_GET[$name])) {
$value = $_GET[$name];
if (is_int($type) && !empty($value)) {
$value = intval($value);
}
$ret .= "&$name=$value";
}
}
return $ret;
}
/**
* Retrieve the HTTP base URL to the current page and reconstruct the GET parameters.
* @return string with URL to the page.
*/
function base_form() {
$ret = '';
foreach ($this->req_parameters as $name => $type) {
if (isset($_POST[$name])) {
$value = $_POST[$name];
if (is_int($type)) {
$ret .= sprintf($this->tpl_post, $name, intval($value));
} else {
if (is_array($type)) {
$value = $_POST[$name];
foreach ($value as $item) {
// Go on safe side and make them integers for now, as I am transmiting only integers on array of values
$ret .= sprintf($this->tpl_post, $name . '[]', intval($item));
}
} else {
$ret .= sprintf($this->tpl_post, $name, $value);
}
}
}
}
return $ret;
}
/**
* Set the page size
* @param size New page size (Results per page)
*/
function set_page_size($size) {
if ($size !== NULL) {
$this->page_size = $size;
}
}
/**
* HTTP method 'post' or 'get'
*/
function get_method() {
return $this->method;
}
/**
* HTTP method 'post' or 'get'
*/
function set_method($method) {
$this->method = $method;
}
function get_target_url() {
return $this->target_url;
}
function set_target_url($url) {
$this->target_url = $url;
}
function get_sql() {
return $this->sql;
}
}
}
if (!class_exists('paginated_array')) {
/**
* Pagination class used to retrieve the dataset from an array of results in chunks of data.
*/
class paginated_array {
private $page_size = 10;
private $page = 0;
/** Total number of results */
private $count = 0;
/**
* Constructor
* @param array array that holds the data
* @param page Page of results to display (ex. 0, 1, 2, 3, 4 ...)
* @param page_size Size of the results page (ex. 10)
*/
function __construct($array, $page = 0, $page_size = 10) {
$this->array = $array;
$this->count = count($array);
$this->page = $page;
$this->page_size = $page_size;
}
function __destruct() {
return true;
}
/**
* Retrieve the page of results
* @return sub-array
*/
function results() {
$start = $this->page * $this->page_size;
// Normalize the start value to last page if erroneous
if ($start > $this->count) {
$start = $this->count - ($this->count % $this->page_size);
}
return array_slice($this->array, $start, $this->page_size, TRUE);
}
/**
* Get the starting index for this page (10th, 20th row etc.)
* @return integer with first row index
*/
function start() {
return $this->page * $this->page_size + 1;
}
/**
* Get the last index for this page (21th, 31th etc.)
* @return integer with last row index
*/
function end() {
if ($this->is_last_page()) {
return $this->count;
}
return $this->page * $this->page_size + $this->page_size;
}
/**
* Check if we are on last page of results.
* @return TRUE if this is the first page
*/
function is_first_page() {
return $this->page == 0;
}
/**
* Check if we are on last page of results.
* @return TRUE if this is the last page
*/
function is_last_page() {
return $this->page == ceil($this->count / $this->page_size) - 1;
}
/**
* Retrieve the total number of results.
* @return integer with count of all records from the query.
*/
function total() {
return $this->count;
}
/**
* Check if we have a next page of results.
* @return TRUE if we have next page
*/
function has_next() {
$pages = ceil($this->count / $this->page_size);
return $this->page < $pages - 1;
}
/**
* Check if we have a previous page of results.
* @return TRUE if we have previous page
*/
function has_previous() {
$pages = ceil($this->count / $this->page_size);
return $this->page > 0;
}
/**
* Set the page size
* @param size New page size (Results per page)
*/
function set_page_size($size) {
if ($size !== NULL) {
$this->page_size = $size;
}
}
}
}