initialize($config); Kohana::log('debug', 'Pagination Library initialized'); } public function initialize($config = array()) { if (isset($config['group'])) { if ( ! is_array($group_config = Kohana::config('pagination.'.$config['group']))) throw new Kohana_Exception('pagination.undefined_group', $config['group']); if ($config['group'] !== 'default') { if ( ! is_array($default_config = Kohana::config('pagination.default'))) throw new Kohana_Exception('pagination.undefined_group', 'default'); $group_config += $default_config; } $config += $group_config; } foreach ($config as $key => $value) { if (property_exists($this, $key)) { $this->$key = $value; } } $this->directory = trim($this->directory, '/').'/'; if ($this->query_string !== '') { $this->current_page = isset($_GET[$this->query_string]) ? (int) $_GET[$this->query_string] : 1; $_GET[$this->query_string] = '{page}'; $base_url = ($this->base_url === '') ? Router::$current_uri : $this->base_url; $this->url = url::site($base_url).'?'.str_replace('%7Bpage%7D', '{page}', http_build_query($_GET)); $_GET[$this->query_string] = $this->current_page; } else { $this->url = ($this->base_url === '') ? Router::$segments : explode('/', trim($this->base_url, '/')); if (is_string($this->uri_segment)) { if (($key = array_search($this->uri_segment, $this->url)) === FALSE) { $this->url[] = $this->uri_segment; $this->uri_segment = count($this->url) + 1; } else { $this->uri_segment = $key + 2; } } $this->url[$this->uri_segment - 1] = '{page}'; $this->url = url::site(implode('/', $this->url)).Router::$query_string; $this->current_page = URI::instance()->segment($this->uri_segment); } $this->total_items = (int) max(0, $this->total_items); $this->items_per_page = (int) max(1, $this->items_per_page); $this->total_pages = (int) ceil($this->total_items / $this->items_per_page); $this->current_page = (int) min(max(1, $this->current_page), max(1, $this->total_pages)); $this->current_first_item = (int) min((($this->current_page - 1) * $this->items_per_page) + 1, $this->total_items); $this->current_last_item = (int) min($this->current_first_item + $this->items_per_page - 1, $this->total_items); $this->first_page = ($this->current_page === 1) ? FALSE : 1; $this->last_page = ($this->current_page >= $this->total_pages) ? FALSE : $this->total_pages; $this->previous_page = ($this->current_page > 1) ? $this->current_page - 1 : FALSE; $this->next_page = ($this->current_page < $this->total_pages) ? $this->current_page + 1 : FALSE; $this->sql_offset = (int) ($this->current_page - 1) * $this->items_per_page; $this->sql_limit = sprintf(' LIMIT %d OFFSET %d ', $this->items_per_page, $this->sql_offset); } public function render($style = NULL) { if ($this->auto_hide === TRUE AND $this->total_pages <= 1) return ''; if ($style === NULL) { $style = $this->style; } return View::factory($this->directory.$style, get_object_vars($this))->render(); } public function __toString() { return $this->render(); } public function __get($key) { if (isset($this->$key)) return $this->$key; } public function __call($func, $args = NULL) { return $this->__get($func); } }