1113f862f76aa5334192a1d718744a1495a09d25
[living-lab-site.git] / system / libraries / Pagination.php
1 <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2 /**
3  * CodeIgniter
4  *
5  * An open source application development framework for PHP 5.1.6 or newer
6  *
7  * @package             CodeIgniter
8  * @author              ExpressionEngine Dev Team
9  * @copyright   Copyright (c) 2008 - 2011, EllisLab, Inc.
10  * @license             http://codeigniter.com/user_guide/license.html
11  * @link                http://codeigniter.com
12  * @since               Version 1.0
13  * @filesource
14  */
15
16 // ------------------------------------------------------------------------
17
18 /**
19  * Pagination Class
20  *
21  * @package             CodeIgniter
22  * @subpackage  Libraries
23  * @category    Pagination
24  * @author              ExpressionEngine Dev Team
25  * @link                http://codeigniter.com/user_guide/libraries/pagination.html
26  */
27 class CI_Pagination {
28
29         var $base_url                   = ''; // The page we are linking to
30         var $prefix                             = ''; // A custom prefix added to the path.
31         var $suffix                             = ''; // A custom suffix added to the path.
32
33         var $total_rows                 = ''; // Total number of items (database results)
34         var $per_page                   = 10; // Max number of items you want shown per page
35         var $num_links                  =  2; // Number of "digit" links to show before/after the currently viewed page
36         var $cur_page                   =  0; // The current page being viewed
37         var $first_link                 = '&lsaquo; First';
38         var $next_link                  = '&gt;';
39         var $prev_link                  = '&lt;';
40         var $last_link                  = 'Last &rsaquo;';
41         var $uri_segment                = 3;
42         var $full_tag_open              = '';
43         var $full_tag_close             = '';
44         var $first_tag_open             = '';
45         var $first_tag_close    = '&nbsp;';
46         var $last_tag_open              = '&nbsp;';
47         var $last_tag_close             = '';
48         var $first_url                  = ''; // Alternative URL for the First Page.
49         var $cur_tag_open               = '&nbsp;<strong>';
50         var $cur_tag_close              = '</strong>';
51         var $next_tag_open              = '&nbsp;';
52         var $next_tag_close             = '&nbsp;';
53         var $prev_tag_open              = '&nbsp;';
54         var $prev_tag_close             = '';
55         var $num_tag_open               = '&nbsp;';
56         var $num_tag_close              = '';
57         var $page_query_string  = FALSE;
58         var $query_string_segment = 'per_page';
59         var $display_pages              = TRUE;
60         var $anchor_class               = '';
61
62         /**
63          * Constructor
64          *
65          * @access      public
66          * @param       array   initialization parameters
67          */
68         public function __construct($params = array())
69         {
70                 if (count($params) > 0)
71                 {
72                         $this->initialize($params);
73                 }
74
75                 if ($this->anchor_class != '')
76                 {
77                         $this->anchor_class = 'class="'.$this->anchor_class.'" ';
78                 }
79
80                 log_message('debug', "Pagination Class Initialized");
81         }
82
83         // --------------------------------------------------------------------
84
85         /**
86          * Initialize Preferences
87          *
88          * @access      public
89          * @param       array   initialization parameters
90          * @return      void
91          */
92         function initialize($params = array())
93         {
94                 if (count($params) > 0)
95                 {
96                         foreach ($params as $key => $val)
97                         {
98                                 if (isset($this->$key))
99                                 {
100                                         $this->$key = $val;
101                                 }
102                         }
103                 }
104         }
105
106         // --------------------------------------------------------------------
107
108         /**
109          * Generate the pagination links
110          *
111          * @access      public
112          * @return      string
113          */
114         function create_links()
115         {
116                 // If our item count or per-page total is zero there is no need to continue.
117                 if ($this->total_rows == 0 OR $this->per_page == 0)
118                 {
119                         return '';
120                 }
121
122                 // Calculate the total number of pages
123                 $num_pages = ceil($this->total_rows / $this->per_page);
124
125                 // Is there only one page? Hm... nothing more to do here then.
126                 if ($num_pages == 1)
127                 {
128                         return '';
129                 }
130
131                 // Determine the current page number.
132                 $CI =& get_instance();
133
134                 if ($CI->config->item('enable_query_strings') === TRUE OR $this->page_query_string === TRUE)
135                 {
136                         if ($CI->input->get($this->query_string_segment) != 0)
137                         {
138                                 $this->cur_page = $CI->input->get($this->query_string_segment);
139
140                                 // Prep the current page - no funny business!
141                                 $this->cur_page = (int) $this->cur_page;
142                         }
143                 }
144                 else
145                 {
146                         if ($CI->uri->segment($this->uri_segment) != 0)
147                         {
148                                 $this->cur_page = $CI->uri->segment($this->uri_segment);
149
150                                 // Prep the current page - no funny business!
151                                 $this->cur_page = (int) $this->cur_page;
152                         }
153                 }
154
155                 $this->num_links = (int)$this->num_links;
156
157                 if ($this->num_links < 1)
158                 {
159                         show_error('Your number of links must be a positive number.');
160                 }
161
162                 if ( ! is_numeric($this->cur_page))
163                 {
164                         $this->cur_page = 0;
165                 }
166
167                 // Is the page number beyond the result range?
168                 // If so we show the last page
169                 if ($this->cur_page > $this->total_rows)
170                 {
171                         $this->cur_page = ($num_pages - 1) * $this->per_page;
172                 }
173
174                 $uri_page_number = $this->cur_page;
175                 $this->cur_page = floor(($this->cur_page/$this->per_page) + 1);
176
177                 // Calculate the start and end numbers. These determine
178                 // which number to start and end the digit links with
179                 $start = (($this->cur_page - $this->num_links) > 0) ? $this->cur_page - ($this->num_links - 1) : 1;
180                 $end   = (($this->cur_page + $this->num_links) < $num_pages) ? $this->cur_page + $this->num_links : $num_pages;
181
182                 // Is pagination being used over GET or POST?  If get, add a per_page query
183                 // string. If post, add a trailing slash to the base URL if needed
184                 if ($CI->config->item('enable_query_strings') === TRUE OR $this->page_query_string === TRUE)
185                 {
186                         $this->base_url = rtrim($this->base_url).'&amp;'.$this->query_string_segment.'=';
187                 }
188                 else
189                 {
190                         $this->base_url = rtrim($this->base_url, '/') .'/';
191                 }
192
193                 // And here we go...
194                 $output = '';
195
196                 // Render the "First" link
197                 if  ($this->first_link !== FALSE AND $this->cur_page > ($this->num_links + 1))
198                 {
199                         $first_url = ($this->first_url == '') ? $this->base_url : $this->first_url;
200                         $output .= $this->first_tag_open.'<a '.$this->anchor_class.'href="'.$first_url.'">'.$this->first_link.'</a>'.$this->first_tag_close;
201                 }
202
203                 // Render the "previous" link
204                 if  ($this->prev_link !== FALSE AND $this->cur_page != 1)
205                 {
206                         $i = $uri_page_number - $this->per_page;
207
208                         if ($i == 0 && $this->first_url != '')
209                         {
210                                 $output .= $this->prev_tag_open.'<a '.$this->anchor_class.'href="'.$this->first_url.'">'.$this->prev_link.'</a>'.$this->prev_tag_close;
211                         }
212                         else
213                         {
214                                 $i = ($i == 0) ? '' : $this->prefix.$i.$this->suffix;
215                                 $output .= $this->prev_tag_open.'<a '.$this->anchor_class.'href="'.$this->base_url.$i.'">'.$this->prev_link.'</a>'.$this->prev_tag_close;
216                         }
217
218                 }
219
220                 // Render the pages
221                 if ($this->display_pages !== FALSE)
222                 {
223                         // Write the digit links
224                         for ($loop = $start -1; $loop <= $end; $loop++)
225                         {
226                                 $i = ($loop * $this->per_page) - $this->per_page;
227
228                                 if ($i >= 0)
229                                 {
230                                         if ($this->cur_page == $loop)
231                                         {
232                                                 $output .= $this->cur_tag_open.$loop.$this->cur_tag_close; // Current page
233                                         }
234                                         else
235                                         {
236                                                 $n = ($i == 0) ? '' : $i;
237
238                                                 if ($n == '' && $this->first_url != '')
239                                                 {
240                                                         $output .= $this->num_tag_open.'<a '.$this->anchor_class.'href="'.$this->first_url.'">'.$loop.'</a>'.$this->num_tag_close;
241                                                 }
242                                                 else
243                                                 {
244                                                         $n = ($n == '') ? '' : $this->prefix.$n.$this->suffix;
245
246                                                         $output .= $this->num_tag_open.'<a '.$this->anchor_class.'href="'.$this->base_url.$n.'">'.$loop.'</a>'.$this->num_tag_close;
247                                                 }
248                                         }
249                                 }
250                         }
251                 }
252
253                 // Render the "next" link
254                 if ($this->next_link !== FALSE AND $this->cur_page < $num_pages)
255                 {
256                         $output .= $this->next_tag_open.'<a '.$this->anchor_class.'href="'.$this->base_url.$this->prefix.($this->cur_page * $this->per_page).$this->suffix.'">'.$this->next_link.'</a>'.$this->next_tag_close;
257                 }
258
259                 // Render the "Last" link
260                 if ($this->last_link !== FALSE AND ($this->cur_page + $this->num_links) < $num_pages)
261                 {
262                         $i = (($num_pages * $this->per_page) - $this->per_page);
263                         $output .= $this->last_tag_open.'<a '.$this->anchor_class.'href="'.$this->base_url.$this->prefix.$i.$this->suffix.'">'.$this->last_link.'</a>'.$this->last_tag_close;
264                 }
265
266                 // Kill double slashes.  Note: Sometimes we can end up with a double slash
267                 // in the penultimate link so we'll kill all double slashes.
268                 $output = preg_replace("#([^:])//+#", "\\1/", $output);
269
270                 // Add the wrapper HTML if exists
271                 $output = $this->full_tag_open.$output.$this->full_tag_close;
272
273                 return $output;
274         }
275 }
276 // END Pagination Class
277
278 /* End of file Pagination.php */
279 /* Location: ./system/libraries/Pagination.php */