paginator.php 8.96 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
<?php namespace Laravel;

class Paginator {

	/**
	 * The results for the current page.
	 *
	 * @var array
	 */
	public $results;

	/**
13
	 * The current page.
14 15 16
	 *
	 * @var int
	 */
17
	public $page;
18 19

	/**
20
	 * The last page available for the result set.
21 22 23
	 *
	 * @var int
	 */
24
	public $last;
25 26

	/**
27
	 * The total number of results.
28 29 30
	 *
	 * @var int
	 */
31
	public $total;
32 33

	/**
34
	 * The number of items per page.
35 36 37
	 *
	 * @var int
	 */
38
	public $per_page;
39 40 41 42 43 44 45

	/**
	 * The values that should be appended to the end of the link query strings.
	 *
	 * @var array
	 */
	protected $appends;
46 47

	/**
48 49 50
	 * The compiled appendage that will be appended to the links.
	 *
	 * This consists of a sprintf format  with a page place-holder and query string.
51 52 53
	 *
	 * @var string
	 */
54
	protected $appendage;
55 56

	/**
57
	 * The "dots" element used in the pagination slider.
58
	 *
59
	 * @var string
60
	 */
61
	protected $dots = '<span class="dots">...</span>';
62 63

	/**
64 65 66
	 * Create a new Paginator instance.
	 *
	 * @param  array  $results
67
	 * @param  int    $last
68 69 70 71 72
	 * @param  int    $page
	 * @param  int    $total
	 * @param  int    $per_page
	 * @return void
	 */
73
	protected function __construct($results, $page, $total, $per_page, $last)
74 75
	{
		$this->page = $page;
76
		$this->last = $last;
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
		$this->total = $total;
		$this->results = $results;
		$this->per_page = $per_page;
	}

	/**
	 * Create a new Paginator instance.
	 *
	 * @param  array      $results
	 * @param  int        $total
	 * @param  int        $per_page
	 * @return Paginator
	 */
	public static function make($results, $total, $per_page)
	{
92 93
		$page = static::page($total, $per_page);

94
		$last = ceil($total / $per_page);
95

96
		return new static($results, $page, $total, $per_page, $last);
97 98 99 100 101 102 103 104 105 106 107
	}

	/**
	 * Get the current page from the request query string.
	 *
	 * @param  int  $total
	 * @param  int  $per_page
	 * @return int
	 */
	public static function page($total, $per_page)
	{
108
		$page = Input::get('page', 1);
109

110 111 112 113 114
		// The page will be validated and adjusted if it is less than one or greater
		// than the last page. For example, if the current page is not an integer or
		// less than one, one will be returned. If the current page is greater than
		// the last page, the last page will be returned.
		if (is_numeric($page) and $page > $last = ceil($total / $per_page))
115
		{
116
			return ($last > 0) ? $last : 1;
117 118
		}

119 120 121 122 123 124 125 126 127 128 129 130 131 132
		return (static::valid($page)) ? $page : 1;
	}

	/**
	 * Determine if a given page number is a valid page.
	 *
	 * A valid page must be greater than or equal to one and a valid integer.
	 *
	 * @param  int   $page
	 * @return bool
	 */
	protected static function valid($page)
	{
		return $page >= 1 and filter_var($page, FILTER_VALIDATE_INT) !== false;
133 134 135 136 137
	}

	/**
	 * Create the HTML pagination links.
	 *
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
	 * Typically, an intelligent, "sliding" window of links will be rendered based
	 * on the total number of pages, the current page, and the number of adjacent
	 * pages that should rendered. This creates a beautiful paginator similar to
	 * that of Google's.
	 *
	 * Example: 1 2 ... 23 24 25 [26] 27 28 29 ... 51 52
	 *
	 * If you wish to render only certain elements of the pagination control,
	 * explore some of the other public methods available on the instance.
	 *
	 * <code>
	 *		// Render the pagination links
	 *		echo $paginator->links();
	 *
	 *		// Render the pagination links using a given window size
	 *		echo $paginator->links(5);
	 * </code>
	 *
	 * @param  int     $adjacent
157 158
	 * @return string
	 */
159
	public function links($adjacent = 3)
160
	{
161
		if ($this->last <= 1) return '';
162

163 164 165
		// The hard-coded seven is to account for all of the constant elements in a
		// sliding range, such as the current page, the two ellipses, and the two
		// beginning and ending pages.
Taylor Otwell committed
166
		//
167 168 169 170 171
		// If there are not enough pages to make the creation of a slider possible
		// based on the adjacent pages, we will simply display all of the pages.
		// Otherwise, we will create a "truncating" slider which displays a nice
		// window of pages based on the current page.
		if ($this->last < 7 + ($adjacent * 2))
172
		{
173
			$links = $this->range(1, $this->last);
Taylor Otwell committed
174
		}
175
		else
Taylor Otwell committed
176
		{
177
			$links = $this->slider($adjacent);
178
		}
179

180
		$content = $this->previous().' '.$links.' '.$this->next();
181 182

		return '<div class="pagination">'.$content.'</div>';
183 184 185
	}

	/**
186
	 * Build sliding list of HTML numeric page links.
187
	 *
188 189 190 191 192 193 194 195 196 197 198 199
	 * This method is very similar to the "links" method, only it does not
	 * render the "first" and "last" pagination links, but only the pages.
	 *
	 * <code>
	 *		// Render the pagination slider
	 *		echo $paginator->slider();
	 *
	 *		// Render the pagination slider using a given window size
	 *		echo $paginator->slider(5);
	 * </code>
	 *
	 * @param  int     $adjacent
200 201
	 * @return string
	 */
202
	public function slider($adjacent = 3)
203
	{
204
		$window = $adjacent * 2;
Taylor Otwell committed
205

206 207 208 209 210
		// Example: 1 [2] 3 4 5 6 ... 23 24
		if ($this->page <= $window)
		{
			return $this->range(1, $window + 2).' '.$this->ending();
		}
211

212 213 214 215 216 217 218 219 220 221
		// Example: 1 2 ... 32 33 34 35 [36] 37
		elseif ($this->page >= $this->last - $window)
		{
			return $this->beginning().' '.$this->range($this->last - $window - 2, $this->last);
		}

		// Example: 1 2 ... 23 24 25 [26] 27 28 29 ... 51 52
		$content = $this->range($this->page - $adjacent, $this->page + $adjacent);

		return $this->beginning().' '.$content.' '.$this->ending();
222 223 224
	}

	/**
225 226 227 228 229 230 231 232 233
	 * Generate the "previous" HTML link.
	 *
	 * <code>
	 *		// Create the "previous" pagination element
	 *		echo $paginator->previous();
	 *
	 *		// Create the "previous" pagination element with custom text
	 *		echo $paginator->previous('Go Back');
	 * </code>
234 235 236
	 *
	 * @return string
	 */
Taylor Otwell committed
237
	public function previous($text = null)
238
	{
239 240 241
		$disabled = function($page) { return $page <= 1; };

		return $this->element(__FUNCTION__, $this->page - 1, $text, $disabled);
242
	}
243

244
	/**
245 246 247 248 249 250 251 252 253
	 * Generate the "next" HTML link.
	 *
	 * <code>
	 *		// Create the "next" pagination element
	 *		echo $paginator->next();
	 *
	 *		// Create the "next" pagination element with custom text
	 *		echo $paginator->next('Skip Forwards');
	 * </code>
254 255 256
	 *
	 * @return string
	 */
Taylor Otwell committed
257
	public function next($text = null)
258
	{
259 260 261
		$disabled = function($page, $last) { return $page >= $last; };

		return $this->element(__FUNCTION__, $this->page + 1, $text, $disabled);
262 263 264
	}

	/**
265
	 * Create a chronological pagination element, such as a "previous" or "next" link.
266
	 *
267 268 269 270
	 * @param  string   $element
	 * @param  int      $page
	 * @param  string   $text
	 * @param  Closure  $disabled
271 272
	 * @return string
	 */
273
	protected function element($element, $page, $text, $disabled)
274
	{
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
		$class = "{$element}_page";

		if (is_null($text)) $text = Lang::line("pagination.{$element}")->get();

		// Each consumer of this method provides a "disabled" Closure which can
		// be used to determine if the element should be a span element or an
		// actual link. For example, if the current page is the first page,
		// the "first" element should be a span instead of a link.
		if ($disabled($this->page, $this->last))
		{
			return HTML::span($text, array('class' => "{$class} disabled"));
		}
		else
		{
			return $this->link($page, $text, $class);
		}
291 292 293
	}

	/**
294
	 * Build the first two page links for a sliding page range.
295
	 *
296 297
	 * @return string
	 */
298
	protected function beginning()
299
	{
300
		return $this->range(1, 2).' '.$this->dots;
301 302 303
	}

	/**
304
	 * Build the last two page links for a sliding page range.
305 306 307
	 *
	 * @return string
	 */
308
	protected function ending()
309
	{
310
		return $this->dots.' '.$this->range($this->last - 1, $this->last);
311 312 313
	}

	/**
314
	 * Build a range of numeric pagination links.
315
	 *
316 317 318 319
	 * For the current page, an HTML span element will be generated instead of a link.
	 *
	 * @param  int     $start
	 * @param  int     $end
320 321
	 * @return string
	 */
322
	protected function range($start, $end)
323
	{
324
		$pages = array();
Taylor Otwell committed
325

326
		for ($page = $start; $page <= $end; $page++)
327
		{
328 329 330 331 332 333 334 335
			if ($this->page == $page)
			{
				$pages[] = HTML::span($page, array('class' => 'current'));
			}
			else
			{
				$pages[] = $this->link($page, $page, null);
			}
336 337
		}

338
		return implode(' ', $pages);
339 340 341
	}

	/**
342
	 * Create a HTML page link.
343 344
	 *
	 * @param  int     $page
345 346
	 * @param  string  $text
	 * @param  string  $attributes
347 348
	 * @return string
	 */
349
	protected function link($page, $text, $class)
350
	{
351
		$url = URI::current().'?page='.$page.$this->appendage($this->appends);
Taylor Otwell committed
352

353 354
		return HTML::link($url, $text, compact('class'), Request::secure());
	}
355

356 357 358 359 360 361 362 363 364 365 366 367 368
	/**
	 * Create the "appendage" that should be attached to every pagination link.
	 *
	 * The developer may assign an array of values that will be converted to a
	 * query string and attached to every pagination link. This allows simple
	 * implementation of sorting or other things the developer may need.
	 *
	 * @param  array   $appends
	 * @return string
	 */
	protected function appendage($appends)
	{
		if ( ! is_null($this->appendage))
369
		{
370
			return $this->appendage;
371 372
		}

373
		return $this->appendage = (count($appends) > 0) ? '&'.http_build_query($appends) : '';
374 375 376
	}

	/**
377 378 379
	 * Set the items that should be appended to the link query strings.
	 *
	 * @param  array      $values
380 381
	 * @return Paginator
	 */
382
	public function appends($values)
383
	{
384
		$this->appends = $values;
385 386 387 388
		return $this;
	}

}