Subversion Repositories Applications.papyrus

Rev

Details | Last modification | View Log | RSS feed

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