Changeset 1229

Show
Ignore:
Timestamp:
11/21/2007 01:31:28 PM (11 months ago)
Author:
Geert
Message:

Pagination library update:

  • All class variables are now protected
  • initialize() method added by which you can overwrite (some) pagination config values if you want to (Thanks for the suggestion, Dynom)
  • Various small cleanups and comments
Files:
1 modified

Legend:

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

    r1227 r1229  
    1010class Pagination_Core { 
    1111 
    12         public $base_url       = ''; 
    13         public $directory      = 'pagination'; 
    14         public $style          = 'classic'; 
    15         public $uri_segment    = 3; 
    16         public $items_per_page = 10; 
     12        // Config values 
     13        protected $base_url       = ''; 
     14        protected $directory      = 'pagination'; 
     15        protected $style          = 'classic'; 
     16        protected $uri_segment    = 3; 
     17        protected $items_per_page = 10; 
    1718 
    18         public $current_page; 
    19         public $total_pages; 
    20         public $total_items; 
    21         public $current_first_item; 
    22         public $current_last_item; 
    23  
    24         public $first_page; 
    25         public $last_page; 
    26         public $previous_page; 
    27         public $next_page; 
     19        // Automatically calculated values 
     20        protected $current_page; 
     21        protected $total_pages; 
     22        protected $total_items; 
     23        protected $current_first_item; 
     24        protected $current_last_item; 
     25        protected $first_page; 
     26        protected $last_page; 
     27        protected $previous_page; 
     28        protected $next_page; 
    2829 
    2930        /* 
     
    3940                $config = array_merge(Config::load('pagination', FALSE), (array) $config); 
    4041 
     42                $this->initialize($config); 
     43 
     44                Log::add('debug', 'Pagination Library initialized'); 
     45        } 
     46 
     47        /* 
     48         * Method: initialize 
     49         *  Sets or overwrites (some) config values. 
     50         * 
     51         * Parameters: 
     52         *  config - custom configuration 
     53         */ 
     54        public function initialize($config = array()) 
     55        { 
    4156                // Assign config values to the object 
    4257                foreach ($config as $key => $value) 
     
    4863                } 
    4964 
    50                 // Set a default base_url if none given via config 
    51                 if ($this->base_url == '') 
     65                // Clean view directory 
     66                $this->directory = trim($this->directory, '/').'/'; 
     67 
     68                // Don't bother about full base_urls 
     69                if (strpos($this->base_url, '://') === FALSE) 
    5270                { 
    53                         $this->base_url = Kohana::instance()->uri->string(); 
     71                        // Default base_url 
     72                        if ($this->base_url == '') 
     73                        { 
     74                                $this->base_url = url::current(); 
     75                        } 
     76 
     77                        // Explode base_url into segments 
     78                        $this->base_url = explode('/', trim($this->base_url, '/')); 
     79 
     80                        // Convert uri 'label' to corresponding integer if needed 
     81                        if (is_string($this->uri_segment)) 
     82                        { 
     83                                if (($key = array_search($this->uri_segment, $this->base_url)) === FALSE) 
     84                                { 
     85                                        // If uri 'label' is not found, auto add it to base_url 
     86                                        $this->base_url[] = $this->uri_segment; 
     87                                        $this->uri_segment = count($this->base_url) + 1; 
     88                                } 
     89                                else 
     90                                { 
     91                                        $this->uri_segment = $key + 2; 
     92                                } 
     93                        } 
     94 
     95                        // Create a generic base_url with {page} placeholder 
     96                        $this->base_url[$this->uri_segment - 1] = '{page}'; 
     97                        $this->base_url = url::site(implode('/', $this->base_url)); 
    5498                } 
    55  
    56                 // Explode base_url into segments 
    57                 $this->base_url = explode('/', trim($this->base_url, '/')); 
    58  
    59                 // Convert uri 'label' to corresponding integer if needed 
    60                 if (is_string($this->uri_segment)) 
    61                 { 
    62                         if (($key = array_search($this->uri_segment, $this->base_url)) === FALSE) 
    63                         { 
    64                                 // If uri 'label' is not found, auto add it to base_url 
    65                                 $this->base_url[] = $this->uri_segment; 
    66                                 $this->uri_segment = count($this->base_url) + 1; 
    67                         } 
    68                         else 
    69                         { 
    70                                 $this->uri_segment = $key + 2; 
    71                         } 
    72                 } 
    73  
    74                 // Create a generic base_url with {page} placeholder 
    75                 $this->base_url[$this->uri_segment - 1] = '{page}'; 
    76                 $this->base_url = url::site(implode('/', $this->base_url)); 
    7799 
    78100                // Core pagination values 
     
    84106                $this->current_last_item  = (int) min($this->current_first_item + $this->items_per_page - 1, $this->total_items); 
    85107 
    86                 // Helper variables 
    87                 // - first_page/last_page     FALSE if the current page is the first/last page 
    88                 // - previous_page/next_page  FALSE if that page doesn't exist relative to the current page 
     108                // If there is no first/last/previous/next page, relative to the 
     109                // current page, value is set to FALSE. Valid page number otherwise. 
    89110                $this->first_page         = ($this->current_page == 1) ? FALSE : 1; 
    90111                $this->last_page          = ($this->current_page >= $this->total_pages) ? FALSE : $this->total_pages; 
    91112                $this->previous_page      = ($this->current_page > 1) ? $this->current_page - 1 : FALSE; 
    92113                $this->next_page          = ($this->current_page < $this->total_pages) ? $this->current_page + 1 : FALSE; 
    93  
    94                 // Initialization done 
    95                 Log::add('debug', 'Pagination Library initialized'); 
    96114        } 
    97115 
     
    104122         * 
    105123         * Returns: 
    106          *  Generated pagination HTML 
     124         *  Generated pagination HTML. 
    107125         */ 
    108126        public function create_links($style = NULL) 
     
    110128                $style = (isset($style)) ? $style : $this->style; 
    111129 
    112                 $view = new View(trim($this->directory, '/').'/'.$style, get_object_vars($this)); 
     130                $view = new View($this->directory.$style, get_object_vars($this)); 
    113131                return $view->render(); 
    114132        } 
    115133 
     134        /* 
     135         * Method: __toString 
     136         *  Magic method for converting an object to a string. 
     137         * 
     138         * Returns: 
     139         *  The generated pagination HTML. 
     140         */ 
    116141        public function __toString() 
    117142        { 
     
    127152         * 
    128153         * Returns: 
    129          *  Base URL with specified page number 
     154         *  Base URL with specified page number. 
    130155         */ 
    131156        public function url($page = NULL) 
     
    141166         * 
    142167         * Returns: 
    143          *  SQL offset 
     168         *  SQL offset integer. 
    144169         */ 
    145170        public function sql_offset() 
     
    153178         * 
    154179         * Returns: 
    155          *  SQL LIMIT clause 
     180         *  SQL LIMIT clause. 
    156181         */ 
    157182        public function sql_limit()