1<?php 2 /* libraries/pagination.php 3 * 4 * Copyright (C) by Hugo Leisink <hugo@leisink.net> 5 * This file is part of the Banshee PHP framework 6 * http://www.banshee-php.org/ 7 */ 8 9 class pagination { 10 private $output = null; 11 private $name = null; 12 private $page = 0; 13 private $max_page = null; 14 private $page_size = null; 15 private $list_size = null; 16 private $error = false; 17 18 /* Constructor 19 * 20 * INPUT: object output, string name, int page size, int list size 21 * OUTPUT: - 22 * ERROR: - 23 */ 24 public function __construct($output, $name, $page_size, $list_size) { 25 $this->output = $output; 26 $this->name = $name; 27 $this->page_size = $page_size; 28 $this->list_size = $list_size; 29 30 if (($this->page_size <= 0) || ($this->list_size <= 0)) { 31 $this->error = true; 32 return; 33 } 34 35 /* Calculate maximum page number 36 */ 37 $this->max_page = $this->list_size / $this->page_size; 38 if ($this->max_page == floor($this->max_page)) { 39 $this->max_page -= 1; 40 } else { 41 $this->max_page = floor($this->max_page); 42 } 43 44 /* Initialize session storage 45 */ 46 if (is_array($_SESSION["pagination"]) == false) { 47 $_SESSION["pagination"] = array(); 48 } 49 if (isset($_SESSION["pagination"][$name]) == false) { 50 $_SESSION["pagination"][$name] = $this->page; 51 } 52 53 /* Calulate page number 54 */ 55 $this->page = &$_SESSION["pagination"][$name]; 56 if (isset($_GET["offset"])) { 57 if (valid_input($_GET["offset"], VALIDATE_NUMBERS, VALIDATE_NONEMPTY) == false) { 58 $this->page = 0; 59 } else if (($this->page = (int)$_GET["offset"]) > $this->max_page) { 60 $this->page = $this->max_page; 61 } 62 } 63 64 #$this->output->add_css("banshee/pagination.css"); 65 } 66 67 /* Magic method get 68 * 69 * INPUT: string key 70 * OUTPUT: mixed value 71 * ERROR: null 72 */ 73 public function __get($key) { 74 switch ($key) { 75 case "offset": return $this->page * $this->page_size; 76 case "size": return $this->page_size; 77 } 78 79 return null; 80 } 81 82 /* Set active page to 0 83 * 84 * INPUT: - 85 * OUTPUT: - 86 * ERROR: - 87 */ 88 public function reset() { 89 $this->page = 0; 90 } 91 92 /* Generate XML for the browse links 93 * 94 * INPUT: int number of page links, int step size of arrow links 95 * OUTPUT: boolean xml generated 96 * ERROR: - 97 */ 98 public function show_browse_links($max_links = 7, $step = 7) { 99 if ($this->error) { 100 return false; 101 } 102 $max_links = (floor($max_links / 2) * 2) + 1; 103 104 /* Calculate minimum and maximum page number 105 */ 106 if ($this->max_page > $max_links) { 107 $min = $this->page - floor($max_links / 2); 108 $max = $this->page + floor($max_links / 2); 109 110 if ($min < 0) { 111 $max -= $min; 112 $min = 0; 113 } else if ($max > $this->max_page) { 114 $min -= ($max - $this->max_page); 115 $max = $this->max_page; 116 } 117 } else { 118 $min = 0; 119 $max = $this->max_page; 120 } 121 122 /* Generate XML for browse links 123 */ 124 $this->output->open_tag("pagination", array( 125 "page" => $this->page, 126 "max" => $this->max_page, 127 "step" => $step)); 128 for ($page = $min; $page <= $max; $page++) { 129 $this->output->add_tag("page", $page); 130 } 131 $this->output->close_tag(); 132 133 return true; 134 } 135 136 /* Returns content of table for current page 137 * 138 * INPUT: object database, string table name[, string column name for ordering] 139 * OUTPUT: array table content 140 * ERROR: false 141 */ 142 public function get_items($db, $table, $order = "id") { 143 $query = "select * from %S order by %S limit %d,%d"; 144 145 return $db->execute($query, $table, $order, $this->offset, $this->size); 146 } 147 } 148?> 149