| 1 |
<?php defined('SYSPATH') or die('No direct script access.'); |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
class Pagination_Core { |
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
protected $base_url = ''; |
|---|
| 16 |
protected $directory = 'pagination'; |
|---|
| 17 |
protected $style = 'classic'; |
|---|
| 18 |
protected $uri_segment = 3; |
|---|
| 19 |
protected $query_string = ''; |
|---|
| 20 |
protected $items_per_page = 20; |
|---|
| 21 |
protected $total_items = 0; |
|---|
| 22 |
protected $auto_hide = FALSE; |
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
protected $url; |
|---|
| 26 |
protected $current_page; |
|---|
| 27 |
protected $total_pages; |
|---|
| 28 |
protected $current_first_item; |
|---|
| 29 |
protected $current_last_item; |
|---|
| 30 |
protected $first_page; |
|---|
| 31 |
protected $last_page; |
|---|
| 32 |
protected $previous_page; |
|---|
| 33 |
protected $next_page; |
|---|
| 34 |
protected $sql_offset; |
|---|
| 35 |
protected $sql_limit; |
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 |
* Constructs and returns a new Pagination object. |
|---|
| 39 |
* |
|---|
| 40 |
* @param array configuration settings |
|---|
| 41 |
* @return object |
|---|
| 42 |
*/ |
|---|
| 43 |
public function factory($config = array()) |
|---|
| 44 |
{ |
|---|
| 45 |
return new Pagination($config); |
|---|
| 46 |
} |
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 |
* Constructs a new Pagination object. |
|---|
| 50 |
* |
|---|
| 51 |
* @param array configuration settings |
|---|
| 52 |
* @return void |
|---|
| 53 |
*/ |
|---|
| 54 |
public function __construct($config = array()) |
|---|
| 55 |
{ |
|---|
| 56 |
|
|---|
| 57 |
if ( ! isset($config['group'])) |
|---|
| 58 |
{ |
|---|
| 59 |
$config['group'] = 'default'; |
|---|
| 60 |
} |
|---|
| 61 |
|
|---|
| 62 |
|
|---|
| 63 |
$this->initialize($config); |
|---|
| 64 |
|
|---|
| 65 |
Kohana::log('debug', 'Pagination Library initialized'); |
|---|
| 66 |
} |
|---|
| 67 |
|
|---|
| 68 |
|
|---|
| 69 |
* Sets config values. |
|---|
| 70 |
* |
|---|
| 71 |
* @throws Kohana_Exception |
|---|
| 72 |
* @param array configuration settings |
|---|
| 73 |
* @return void |
|---|
| 74 |
*/ |
|---|
| 75 |
public function initialize($config = array()) |
|---|
| 76 |
{ |
|---|
| 77 |
|
|---|
| 78 |
if (isset($config['group'])) |
|---|
| 79 |
{ |
|---|
| 80 |
|
|---|
| 81 |
if ( ! is_array($group_config = Kohana::config('pagination.'.$config['group']))) |
|---|
| 82 |
throw new Kohana_Exception('pagination.undefined_group', $config['group']); |
|---|
| 83 |
|
|---|
| 84 |
|
|---|
| 85 |
if ($config['group'] !== 'default') |
|---|
| 86 |
{ |
|---|
| 87 |
|
|---|
| 88 |
if ( ! is_array($default_config = Kohana::config('pagination.default'))) |
|---|
| 89 |
throw new Kohana_Exception('pagination.undefined_group', 'default'); |
|---|
| 90 |
|
|---|
| 91 |
|
|---|
| 92 |
$group_config += $default_config; |
|---|
| 93 |
} |
|---|
| 94 |
|
|---|
| 95 |
|
|---|
| 96 |
$config += $group_config; |
|---|
| 97 |
} |
|---|
| 98 |
|
|---|
| 99 |
|
|---|
| 100 |
foreach ($config as $key => $value) |
|---|
| 101 |
{ |
|---|
| 102 |
if (property_exists($this, $key)) |
|---|
| 103 |
{ |
|---|
| 104 |
$this->$key = $value; |
|---|
| 105 |
} |
|---|
| 106 |
} |
|---|
| 107 |
|
|---|
| 108 |
|
|---|
| 109 |
$this->directory = trim($this->directory, '/').'/'; |
|---|
| 110 |
|
|---|
| 111 |
|
|---|
| 112 |
if ($this->query_string !== '') |
|---|
| 113 |
{ |
|---|
| 114 |
|
|---|
| 115 |
$this->current_page = isset($_GET[$this->query_string]) ? (int) $_GET[$this->query_string] : 1; |
|---|
| 116 |
|
|---|
| 117 |
|
|---|
| 118 |
$_GET[$this->query_string] = '{page}'; |
|---|
| 119 |
|
|---|
| 120 |
|
|---|
| 121 |
$this->url = url::site(Router::$current_uri).'?'.str_replace('%7Bpage%7D', '{page}', http_build_query($_GET)); |
|---|
| 122 |
|
|---|
| 123 |
|
|---|
| 124 |
$_GET[$this->query_string] = $this->current_page; |
|---|
| 125 |
} |
|---|
| 126 |
|
|---|
| 127 |
|
|---|
| 128 |
else |
|---|
| 129 |
{ |
|---|
| 130 |
|
|---|
| 131 |
$this->url = ($this->base_url === '') ? Router::$segments : explode('/', trim($this->base_url, '/')); |
|---|
| 132 |
|
|---|
| 133 |
|
|---|
| 134 |
if (is_string($this->uri_segment)) |
|---|
| 135 |
{ |
|---|
| 136 |
if (($key = array_search($this->uri_segment, $this->url)) === FALSE) |
|---|
| 137 |
{ |
|---|
| 138 |
|
|---|
| 139 |
$this->url[] = $this->uri_segment; |
|---|
| 140 |
$this->uri_segment = count($this->url) + 1; |
|---|
| 141 |
} |
|---|
| 142 |
else |
|---|
| 143 |
{ |
|---|
| 144 |
$this->uri_segment = $key + 2; |
|---|
| 145 |
} |
|---|
| 146 |
} |
|---|
| 147 |
|
|---|
| 148 |
|
|---|
| 149 |
$this->url[$this->uri_segment - 1] = '{page}'; |
|---|
| 150 |
|
|---|
| 151 |
|
|---|
| 152 |
$this->url = url::site(implode('/', $this->url)).Router::$query_string; |
|---|
| 153 |
|
|---|
| 154 |
|
|---|
| 155 |
$this->current_page = URI::instance()->segment($this->uri_segment); |
|---|
| 156 |
} |
|---|
| 157 |
|
|---|
| 158 |
|
|---|
| 159 |
$this->total_items = (int) max(0, $this->total_items); |
|---|
| 160 |
$this->items_per_page = (int) max(1, $this->items_per_page); |
|---|
| 161 |
$this->total_pages = (int) ceil($this->total_items / $this->items_per_page); |
|---|
| 162 |
$this->current_page = (int) min(max(1, $this->current_page), max(1, $this->total_pages)); |
|---|
| 163 |
$this->current_first_item = (int) min((($this->current_page - 1) * $this->items_per_page) + 1, $this->total_items); |
|---|
| 164 |
$this->current_last_item = (int) min($this->current_first_item + $this->items_per_page - 1, $this->total_items); |
|---|
| 165 |
|
|---|
| 166 |
|
|---|
| 167 |
// current page, value is set to FALSE. Valid page number otherwise. |
|---|
| 168 |
$this->first_page = ($this->current_page === 1) ? FALSE : 1; |
|---|
| 169 |
$this->last_page = ($this->current_page >= $this->total_pages) ? FALSE : $this->total_pages; |
|---|
| 170 |
$this->previous_page = ($this->current_page > 1) ? $this->current_page - 1 : FALSE; |
|---|
| 171 |
$this->next_page = ($this->current_page < $this->total_pages) ? $this->current_page + 1 : FALSE; |
|---|
| 172 |
|
|---|
| 173 |
|
|---|
| 174 |
$this->sql_offset = (int) ($this->current_page - 1) * $this->items_per_page; |
|---|
| 175 |
$this->sql_limit = sprintf(' LIMIT %d OFFSET %d ', $this->items_per_page, $this->sql_offset); |
|---|
| 176 |
} |
|---|
| 177 |
|
|---|
| 178 |
|
|---|
| 179 |
* Generates the HTML for the chosen pagination style. |
|---|
| 180 |
* |
|---|
| 181 |
* @param string pagination style |
|---|
| 182 |
* @return string pagination html |
|---|
| 183 |
*/ |
|---|
| 184 |
public function render($style = NULL) |
|---|
| 185 |
{ |
|---|
| 186 |
|
|---|
| 187 |
if ($this->auto_hide === TRUE AND $this->total_pages <= 1) |
|---|
| 188 |
return ''; |
|---|
| 189 |
|
|---|
| 190 |
if ($style === NULL) |
|---|
| 191 |
{ |
|---|
| 192 |
|
|---|
| 193 |
$style = $this->style; |
|---|
| 194 |
} |
|---|
| 195 |
|
|---|
| 196 |
|
|---|
| 197 |
return View::factory($this->directory.$style, get_object_vars($this))->render(); |
|---|
| 198 |
} |
|---|
| 199 |
|
|---|
| 200 |
|
|---|
| 201 |
* Magically converts Pagination object to string. |
|---|
| 202 |
* |
|---|
| 203 |
* @return string pagination html |
|---|
| 204 |
*/ |
|---|
| 205 |
public function __toString() |
|---|
| 206 |
{ |
|---|
| 207 |
return $this->render(); |
|---|
| 208 |
} |
|---|
| 209 |
|
|---|
| 210 |
|
|---|
| 211 |
* Magically gets a pagination variable. |
|---|
| 212 |
* |
|---|
| 213 |
* @param string variable key |
|---|
| 214 |
* @return mixed variable value if the key is found |
|---|
| 215 |
* @return void if the key is not found |
|---|
| 216 |
*/ |
|---|
| 217 |
public function __get($key) |
|---|
| 218 |
{ |
|---|
| 219 |
if (isset($this->$key)) |
|---|
| 220 |
return $this->$key; |
|---|
| 221 |
} |
|---|
| 222 |
|
|---|
| 223 |
|
|---|
| 224 |
* Adds a secondary interface for accessing properties, e.g. $pagination->total_pages(). |
|---|
| 225 |
* Note that $pagination->total_pages is the recommended way to access properties. |
|---|
| 226 |
* |
|---|
| 227 |
* @param string function name |
|---|
| 228 |
* @return string |
|---|
| 229 |
*/ |
|---|
| 230 |
public function __call($func, $args = NULL) |
|---|
| 231 |
{ |
|---|
| 232 |
return $this->__get($func); |
|---|
| 233 |
} |
|---|
| 234 |
|
|---|
| 235 |
} |
|---|