Changeset 1229 for trunk/system/libraries/Pagination.php
- Timestamp:
- 11/21/2007 01:31:28 PM (12 months ago)
- Files:
-
- 1 modified
-
trunk/system/libraries/Pagination.php (modified) (9 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/system/libraries/Pagination.php
r1227 r1229 10 10 class Pagination_Core { 11 11 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; 17 18 18 public $current_page;19 p ublic $total_pages;20 p ublic $total_items;21 p ublic $current_first_item;22 p ublic $current_last_item;23 24 p ublic$first_page;25 p ublic$last_page;26 p ublic$previous_page;27 p ublic$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; 28 29 29 30 /* … … 39 40 $config = array_merge(Config::load('pagination', FALSE), (array) $config); 40 41 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 { 41 56 // Assign config values to the object 42 57 foreach ($config as $key => $value) … … 48 63 } 49 64 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) 52 70 { 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)); 54 98 } 55 56 // Explode base_url into segments57 $this->base_url = explode('/', trim($this->base_url, '/'));58 59 // Convert uri 'label' to corresponding integer if needed60 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_url65 $this->base_url[] = $this->uri_segment;66 $this->uri_segment = count($this->base_url) + 1;67 }68 else69 {70 $this->uri_segment = $key + 2;71 }72 }73 74 // Create a generic base_url with {page} placeholder75 $this->base_url[$this->uri_segment - 1] = '{page}';76 $this->base_url = url::site(implode('/', $this->base_url));77 99 78 100 // Core pagination values … … 84 106 $this->current_last_item = (int) min($this->current_first_item + $this->items_per_page - 1, $this->total_items); 85 107 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. 89 110 $this->first_page = ($this->current_page == 1) ? FALSE : 1; 90 111 $this->last_page = ($this->current_page >= $this->total_pages) ? FALSE : $this->total_pages; 91 112 $this->previous_page = ($this->current_page > 1) ? $this->current_page - 1 : FALSE; 92 113 $this->next_page = ($this->current_page < $this->total_pages) ? $this->current_page + 1 : FALSE; 93 94 // Initialization done95 Log::add('debug', 'Pagination Library initialized');96 114 } 97 115 … … 104 122 * 105 123 * Returns: 106 * Generated pagination HTML 124 * Generated pagination HTML. 107 125 */ 108 126 public function create_links($style = NULL) … … 110 128 $style = (isset($style)) ? $style : $this->style; 111 129 112 $view = new View( trim($this->directory, '/').'/'.$style, get_object_vars($this));130 $view = new View($this->directory.$style, get_object_vars($this)); 113 131 return $view->render(); 114 132 } 115 133 134 /* 135 * Method: __toString 136 * Magic method for converting an object to a string. 137 * 138 * Returns: 139 * The generated pagination HTML. 140 */ 116 141 public function __toString() 117 142 { … … 127 152 * 128 153 * Returns: 129 * Base URL with specified page number 154 * Base URL with specified page number. 130 155 */ 131 156 public function url($page = NULL) … … 141 166 * 142 167 * Returns: 143 * SQL offset 168 * SQL offset integer. 144 169 */ 145 170 public function sql_offset() … … 153 178 * 154 179 * Returns: 155 * SQL LIMIT clause 180 * SQL LIMIT clause. 156 181 */ 157 182 public function sql_limit()
