Show
Ignore:
Timestamp:
02/03/2008 02:00:03 AM (10 months ago)
Author:
Geert
Message:

Fixed #368:

  • Added magic get method, allowing you to access all pagination properties
  • Deprecated sql_offset and sql_limit methods, in favor of magic get
  • Updated comment style
Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/system/libraries/Pagination.php

    r1537 r1905  
    1111 
    1212        // Config values 
    13         private  $base_url       = ''; 
    14         private  $directory      = 'pagination'; 
    15         private  $style          = 'classic'; 
    16         private  $uri_segment    = 3; 
     13        protected $base_url       = ''; 
     14        protected $directory      = 'pagination'; 
     15        protected $style          = 'classic'; 
     16        protected $uri_segment    = 3; 
    1717        protected $items_per_page = 10; 
    1818        protected $total_items    = 0; 
     
    2828        protected $previous_page; 
    2929        protected $next_page; 
     30        protected $sql_offset; 
     31        protected $sql_limit; 
    3032 
    3133        /** 
    32          * Constructor: __construct 
    33          *  Sets up the config values. 
     34         * Constructs the Pagination object. 
    3435         * 
    35          * Parameters: 
    36          *  config - custom configuration 
     36         * @param  array  configuration 
     37         * @return void 
    3738         */ 
    3839        public function __construct($config = array()) 
     
    4748 
    4849        /** 
    49          * Method: initialize 
    50          *  Sets or overwrites (some) config values. 
     50         * Sets or overwrites (some) config values. 
    5151         * 
    52          * Parameters: 
    53          *  config - custom configuration 
     52         * @param  array  configuration 
     53         * @return void 
    5454         */ 
    5555        public function initialize($config = array()) 
     
    103103                $this->previous_page      = ($this->current_page > 1) ? $this->current_page - 1 : FALSE; 
    104104                $this->next_page          = ($this->current_page < $this->total_pages) ? $this->current_page + 1 : FALSE; 
     105 
     106                // SQL values 
     107                $this->sql_offset         = (int) ($this->current_page - 1) * $this->items_per_page; 
     108                $this->sql_limit          = sprintf(' LIMIT %d OFFSET %d ', $this->items_per_page, $this->sql_offset); 
    105109        } 
    106110 
    107111        /** 
    108          * Method: create_links 
    109          *  Generates the HTML for the chosen pagination style. 
     112         * Generates the HTML for the chosen pagination style. 
    110113         * 
    111          * Parameters: 
    112          *  style - style of generated links 
    113          * 
    114          * Returns: 
    115          *  Generated pagination HTML. 
     114         * @param  string  pagination style 
     115         * @return string  pagination html 
    116116         */ 
    117117        public function create_links($style = NULL) 
     
    123123 
    124124        /** 
    125          * Method: __toString 
    126          *  Magic method for converting an object to a string. 
     125         * Magically converts pagination object to string. 
    127126         * 
    128          * Returns: 
    129          *  The generated pagination HTML. 
     127         * @return string  pagination html 
    130128         */ 
    131129        public function __toString() 
     
    135133 
    136134        /** 
    137          * Method: sql_offset 
    138          *  Gets the SQL offset of the first row to return. 
     135         * Magically gets a pagination variable. 
    139136         * 
    140          * Returns: 
    141          *  SQL offset integer. 
     137         * @param  string  variable key 
     138         * @return mixed   variable value if the key is found 
     139         * @return void    if the key is not found 
     140         */ 
     141        public function __get($key) 
     142        { 
     143                if (isset($this->$key)) 
     144                        return $this->$key; 
     145        } 
     146 
     147        /** 
     148         * Gets the SQL offset of the first row to return. Deprecated. 
     149         * 
     150         * @return integer  sql offset 
    142151         */ 
    143152        public function sql_offset() 
    144153        { 
    145                 return (int) ($this->current_page - 1) * $this->items_per_page; 
     154                return $this->sql_offset; 
    146155        } 
    147156 
    148157        /** 
    149          * Method: sql_limit 
    150          *  Generates the complete SQL LIMIT clause. 
     158         * Generates the complete SQL LIMIT clause. Deprecated. 
    151159         * 
    152          * Returns: 
    153          *  SQL LIMIT clause. 
     160         * @return string  sql limit clause 
    154161         */ 
    155162        public function sql_limit() 
    156163        { 
    157                 return sprintf(' LIMIT %d OFFSET %d ', $this->items_per_page, $this->sql_offset()); 
     164                return $this->sql_limit; 
    158165        } 
    159166