PageRenderTime 83ms CodeModel.GetById 40ms app.highlight 4ms RepoModel.GetById 37ms app.codeStats 0ms

/halogy/application/libraries/MY_Pagination.php

https://bitbucket.org/haloweb/halogy-1.0/
PHP | 110 lines | 33 code | 21 blank | 56 comment | 5 complexity | 05f31461877416b2c7b2253971034752 MD5 | raw file
  1<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');
  2
  3/**
  4* @name MY_Pagination.php
  5* @version 1.0
  6* @author Joost van Veen www.accentinteractive.nl
  7* @created: Sun Jul 27 16:27:26 GMT 2008 16:27:26
  8*
  9* Extends CI's pagination class (http://codeigniter.com/user_guide/libraries/pagination.html)
 10* It sets some variables for configuration of the pagination class dynamically,
 11* depending on the URI, so we don't have to substract the offset from the URI,
 12* or set $config['base_url'] and $config['uri_segment'] manually in the controller
 13*
 14* Here is what is set by this extension class:
 15* 1. $this->offset - the current offset
 16* 2. $this->uri_segment - the URI segment to be used for pagination
 17* 3. $this->base_url - the base url to be used for pagination
 18* (where $this refers to the pagination class)
 19*
 20* The way this works is simple:
 21* If there we use pagination, it must ALWAYS follow the following syntax and be
 22* located at the END of the URI:
 23* PAGINATION_SELECTOR/offset
 24*
 25* The PAGINATION_SELECTOR is a special string which we know will ONLY be in the
 26* URI when paging is set. Let's say the PAGINATION_SELECTOR is 'Page' (since most
 27* coders never use any capitals in the URI, most of the times any string with
 28* a single capital character in it will suffice).
 29*
 30* Example use (in controller):
 31* // Initialize pagination
 32* $config['total_rows'] = $this->db->count_all_results('my_table');
 33* $config['per_page'] = 10; // You'd best set this in a config file, but hey
 34* $this->pagination->initialize($config);
 35* $this->data['pagination'] = $this->pagination->create_links();
 36*
 37* // Retrieve paginated results, using the dynamically determined offset
 38* $this->db->limit($config['per_page'], $this->pagination->offset);
 39* $query = $this->db->get('my_table');
 40*
 41*/
 42class MY_Pagination extends CI_Pagination {
 43
 44    var $offset = 0;
 45    var $pagination_selector = 'page';
 46
 47    function MY_Pagination()
 48    {
 49        parent::CI_Pagination();
 50        log_message('debug', "MY_Pagination Class Initialized");
 51
 52        $this->_set_pagination_offset();
 53
 54    }
 55
 56    /**
 57     * Set dynamic pagination variables in $CI->data['pagvars']
 58     *
 59     */
 60    function _set_pagination_offset()
 61    {
 62
 63        // Instantiate the CI super object so we have access to the uri class
 64        $CI =& get_instance();
 65
 66		// parse uri
 67		preg_match('/\/'.$this->pagination_selector.'(\/)?([0-9]+)?$/i', $CI->uri->uri_string(), $matches);
 68
 69        // Store pagination offset if it is set
 70        if ($matches) {
 71
 72			// set uri based on matches
 73			$uri = substr($CI->uri->uri_string(), 0, strrpos($CI->uri->uri_string(), $matches[0]));
 74
 75            // Get the segment offset for the pagination selector
 76            $segments = $CI->uri->segment_array();
 77
 78            // Loop through segments to retrieve pagination offset
 79            foreach ($segments as $key => $value) {
 80
 81                // Find the pagination_selector and work from there
 82                if ($value == $this->pagination_selector) {
 83
 84                    // Store pagination offset
 85                    $this->offset = $CI->uri->segment($key + 1);
 86
 87                    // Store pagination segment
 88                    $this->uri_segment = $key + 1;
 89
 90                    // Set base url for paging. This only works if the
 91                    // pagination_selector and paging offset are AT THE END of
 92                    // the URI!
 93                    //$pos = strrpos($uri, $this->pagination_selector);
 94                    $this->base_url = $CI->config->item('base_url') . $uri . '/' . $this->pagination_selector;
 95                }
 96
 97            }
 98
 99        }
100        else { // Pagination selector was not found in URI string. So offset is 0
101            $this->offset = 0;
102            $this->uri_segment = 0;
103            $this->base_url = $CI->config->item('base_url') . $CI->uri->uri_string() . '/' . $this->pagination_selector;
104
105        }
106
107    }
108
109}
110?>