source: pro-violet-viettel/sourcecode/system/libraries/Pagination.php @ 850

Last change on this file since 850 was 345, checked in by quyenla, 11 years ago

collaborator page

File size: 9.4 KB
Line 
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 */
27class 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                 =  0; // 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 $current_page               =  0;
38        var $use_page_numbers   = FALSE; // Use page number for segment instead of offset
39        var $first_link                 = '&lsaquo; First';
40        var $next_link                  = '&gt;';
41        var $prev_link                  = '&lt;';
42        var $last_link                  = 'Last &rsaquo;';
43        var $uri_segment                = 3;
44        var $full_tag_open              = '';
45        var $full_tag_close             = '';
46        var $first_tag_open             = '';
47        var $first_tag_close    = '&nbsp;';
48        var $last_tag_open              = '&nbsp;';
49        var $last_tag_close             = '';
50        var $first_url                  = ''; // Alternative URL for the First Page.
51        var $cur_tag_open               = '&nbsp;<strong>';
52        var $cur_tag_close              = '</strong>';
53        var $next_tag_open              = '&nbsp;';
54        var $next_tag_close             = '&nbsp;';
55        var $prev_tag_open              = '&nbsp;';
56        var $prev_tag_close             = '';
57        var $num_tag_open               = '&nbsp;';
58        var $num_tag_close              = '';
59        var $page_query_string  = FALSE;
60        var $query_string_segment = 'per_page';
61        var $display_pages              = TRUE;
62        var $ajax_class         = '';
63        var $anchor_class               = '';
64
65        /**
66         * Constructor
67         *
68         * @access      public
69         * @param       array   initialization parameters
70         */
71        public function __construct($params = array())
72        {
73                if (count($params) > 0)
74                {
75                        $this->initialize($params);
76                }
77
78                if ($this->anchor_class != '')
79                {
80                        $this->anchor_class = 'class="'.$this->anchor_class.'" ';
81                }
82
83                log_message('debug', "Pagination Class Initialized");
84        }
85
86        // --------------------------------------------------------------------
87
88        /**
89         * Initialize Preferences
90         *
91         * @access      public
92         * @param       array   initialization parameters
93         * @return      void
94         */
95        function initialize($params = array())
96        {
97                if (count($params) > 0)
98                {
99                        foreach ($params as $key => $val)
100                        {
101                                if (isset($this->$key))
102                                {
103                                        $this->$key = $val;
104                                       
105                                }
106                        }
107                }
108        }
109
110        // --------------------------------------------------------------------
111
112        /**
113         * Generate the pagination links
114         *
115         * @access      public
116         * @return      string
117         */
118        function create_links()
119        {
120                // If our item count or per-page total is zero there is no need to continue.
121                if ($this->total_rows == 0 OR $this->per_page == 0)
122                {
123                        return '';
124                }
125
126                // Calculate the total number of pages
127                $num_pages = ceil($this->total_rows / $this->per_page);
128
129                // Is there only one page? Hm... nothing more to do here then.
130                if ($num_pages == 1)
131                {
132                        return '';
133                }
134
135                // Set the base page index for starting page number
136                if ($this->use_page_numbers)
137                {
138                        $base_page = 1;
139                }
140                else
141                {
142                        $base_page = 0;
143                }
144
145                // Determine the current page number.
146                $CI =& get_instance();
147
148                if ($CI->config->item('enable_query_strings') === TRUE OR $this->page_query_string === TRUE)
149                {
150                        if ($CI->input->get($this->query_string_segment) != $base_page)
151                        {
152                                $this->cur_page = $CI->input->get($this->query_string_segment);
153
154                                // Prep the current page - no funny business!
155                                $this->cur_page = (int) $this->cur_page;
156                        }
157                }
158                else
159                {
160                        if ($CI->uri->segment($this->uri_segment) != $base_page)
161                        {
162                                $this->cur_page = $CI->uri->segment($this->uri_segment);
163
164                                // Prep the current page - no funny business!
165                                $this->cur_page = (int) $this->cur_page;
166                        }
167                       
168                       
169                        if ($this->current_page)
170                        {
171                                // Prep the current page - no funny business!
172                                $this->cur_page = (int) $this->current_page;
173                        }
174                }
175               
176                // Set current page to 1 if using page numbers instead of offset
177                if ($this->use_page_numbers AND $this->cur_page == 0)
178                {
179                        $this->cur_page = $base_page;
180                }
181
182                $this->num_links = (int)$this->num_links;
183
184                if ($this->num_links < 1)
185                {
186                        show_error('Your number of links must be a positive number.');
187                }
188
189                if ( ! is_numeric($this->cur_page))
190                {
191                        $this->cur_page = $base_page;
192                }
193
194                // Is the page number beyond the result range?
195                // If so we show the last page
196                if ($this->use_page_numbers)
197                {
198                        if ($this->cur_page > $num_pages)
199                        {
200                                $this->cur_page = $num_pages;
201                        }
202                }
203                else
204                {
205                        if ($this->cur_page > $this->total_rows)
206                        {
207                                $this->cur_page = ($num_pages - 1) * $this->per_page;
208                        }
209                }
210
211                $uri_page_number = $this->cur_page;
212               
213                if ( ! $this->use_page_numbers)
214                {
215                        $this->cur_page = floor(($this->cur_page/$this->per_page) + 1);
216                }
217
218                // Calculate the start and end numbers. These determine
219                // which number to start and end the digit links with
220                $start = (($this->cur_page - $this->num_links) > 0) ? $this->cur_page - ($this->num_links - 1) : 1;
221                $end   = (($this->cur_page + $this->num_links) < $num_pages) ? $this->cur_page + $this->num_links : $num_pages;
222
223                // Is pagination being used over GET or POST?  If get, add a per_page query
224                // string. If post, add a trailing slash to the base URL if needed
225                if ($CI->config->item('enable_query_strings') === TRUE OR $this->page_query_string === TRUE)
226                {
227                        $this->base_url = rtrim($this->base_url).'&amp;'.$this->query_string_segment.'=';
228                }
229                else
230                {
231                        $this->base_url = rtrim($this->base_url, '/') .'/';
232                }
233
234                // And here we go...
235                $output = '';
236
237                // Render the "First" link
238                if  ($this->first_link !== FALSE AND $this->cur_page > ($this->num_links + 1))
239                {
240                        $first_url = ($this->first_url == '') ? $this->base_url : $this->first_url;
241                        $output .= $this->first_tag_open.'<a class ="'.$this->ajax_class.'"'.$this->anchor_class.'href="'.$first_url.'">'.$this->first_link.'</a>'.$this->first_tag_close;
242                }
243
244                // Render the "previous" link
245                if  ($this->prev_link !== FALSE AND $this->cur_page != 1)
246                {
247                        if ($this->use_page_numbers)
248                        {
249                                $i = $uri_page_number - 1;
250                        }
251                        else
252                        {
253                                $i = $uri_page_number - $this->per_page;
254                        }
255
256                        if ($i == 0 && $this->first_url != '')
257                        {
258                                $output .= $this->prev_tag_open.'<a class ="'.$this->ajax_class.'"'.$this->anchor_class.'href="'.$this->first_url.'">'.$this->prev_link.'</a>'.$this->prev_tag_close;
259                        }
260                        else
261                        {
262                                $i = ($i == 0) ? '' : $this->prefix.$i.$this->suffix;
263                                $output .= $this->prev_tag_open.'<a class ="'.$this->ajax_class.'"'.$this->anchor_class.'href="'.$this->base_url.$i.'">'.$this->prev_link.'</a>'.$this->prev_tag_close;
264                        }
265
266                }
267
268                // Render the pages
269                if ($this->display_pages !== FALSE)
270                {
271                        // Write the digit links
272                        for ($loop = $start -1; $loop <= $end; $loop++)
273                        {
274                                if ($this->use_page_numbers)
275                                {
276                                        $i = $loop;
277                                }
278                                else
279                                {
280                                        $i = ($loop * $this->per_page) - $this->per_page;
281                                }
282
283                                if ($i >= $base_page)
284                                {
285                                        if ($this->cur_page == $loop)
286                                        {
287                                                $output .= $this->cur_tag_open.$loop.$this->cur_tag_close; // Current page
288                                        }
289                                        else
290                                        {
291                                                $n = ($i == $base_page) ? '' : $i;
292
293                                                if ($n == '' && $this->first_url != '')
294                                                {
295                                                        $output .= $this->num_tag_open.'<a class ="'.$this->ajax_class.'"'.$this->anchor_class.'href="'.$this->first_url.'">'.$loop.'</a>'.$this->num_tag_close;
296                                                }
297                                                else
298                                                {
299                                                        $n = ($n == '') ? '' : $this->prefix.$n.$this->suffix;
300
301                                                        $output .= $this->num_tag_open.'<a class ="'.$this->ajax_class.'"'.$this->anchor_class.'href="'.$this->base_url.$n.'">'.$loop.'</a>'.$this->num_tag_close;
302                                                }
303                                        }
304                                }
305                        }
306                }
307
308                       
309                if ($this->next_link !== FALSE AND $this->cur_page < $num_pages)
310                {
311                        if ($this->use_page_numbers)
312                        {
313                                $i = $this->cur_page + 1;
314                        }
315                        else
316                        {
317                                $i = ($this->cur_page * $this->per_page);
318                        }
319                       
320                        $output .= $this->next_tag_open.'<a  class ="'.$this->ajax_class.'"'.$this->anchor_class.'href="'.$this->base_url.$this->prefix.$i.$this->suffix.'">'.$this->next_link.'</a>'.$this->next_tag_close;
321                }
322
323                // Render the "Last" link
324                if ($this->last_link !== FALSE AND ($this->cur_page + $this->num_links) < $num_pages)
325                {
326                        if ($this->use_page_numbers)
327                        {
328                                $i = $num_pages;
329                        }
330                        else
331                        {
332                                $i = (($num_pages * $this->per_page) - $this->per_page);
333                        }
334                        $output .= $this->last_tag_open.'<a class ="'.$this->ajax_class.'"'.$this->anchor_class.'href="'.$this->base_url.$this->prefix.$i.$this->suffix.'">'.$this->last_link.'</a>'.$this->last_tag_close;
335                }
336
337                // Kill double slashes.  Note: Sometimes we can end up with a double slash
338                // in the penultimate link so we'll kill all double slashes.
339                $output = preg_replace("#([^:])//+#", "\\1/", $output);
340
341                // Add the wrapper HTML if exists
342                $output = $this->full_tag_open.$output.$this->full_tag_close;
343                return $output;
344        }
345}
346// END Pagination Class
347
348/* End of file Pagination.php */
349/* Location: ./system/libraries/Pagination.php */
Note: See TracBrowser for help on using the repository browser.