PageRenderTime 98ms CodeModel.GetById 25ms app.highlight 15ms RepoModel.GetById 53ms app.codeStats 0ms

/View/Helper/BootstrapPaginatorHelper.php

https://github.com/webacteo/Platform-Twitter-Bootstrap
PHP | 158 lines | 112 code | 26 blank | 20 comment | 31 complexity | acc1f40deeccc5930b21312badb89897 MD5 | raw file
  1<?php
  2App::uses('PaginatorHelper', 'View/Helper');
  3/**
  4 * Twitter Bootstrap Paginator Helper
  5 */
  6class BootstrapPaginatorHelper extends PaginatorHelper {
  7
  8	/**
  9	 * Make table headers sortable
 10	 *
 11	 * @param string $key
 12	 * @param mixed $title
 13	 * @param array $options
 14	 *
 15	 * @return string
 16	 */
 17	public function sortTableHeader($key, $title = null, $options = array()) {
 18		$content = parent::sort($key, $title, $options + array('block' => 'script'));
 19
 20		$options = array_merge(array('url' => array(), 'model' => null), $options);
 21
 22		$class = "";
 23
 24		$sortKey = $this->sortKey($options['model']);
 25		$defaultModel = $this->defaultModel();
 26		$isSorted = (
 27			$sortKey === $key ||
 28			$sortKey === $defaultModel . '.' . $key ||
 29			$key === $defaultModel . '.' . $sortKey
 30		);
 31
 32		if ($isSorted) {
 33			$dir = $this->sortDir($options['model']) === 'asc' ? 'desc' : 'asc';
 34			$class = ($dir === 'asc') ? 'headerSortDown' : 'headerSortUp';
 35		}
 36
 37		return sprintf('<th class="blue header %s">%s</th>', $class, $content);
 38	}
 39
 40	/**
 41	 * Numbers paginator as we know it
 42	 * @see parent "numbers" function for docblock
 43	 * @changes $currentClass & and now active, is wrapped as a link aswell to support TwitterBootstrap
 44	 *
 45	 * @param mixed $options Options for the numbers, (before, after, model, modulus, separator)
 46	 * @return string numbers string.
 47	 */
 48	public function numbers($options = array()) {
 49		if ($options === true) {
 50			$options = array(
 51				'before' => ' | ', 'after' => ' | ', 'first' => 'first', 'last' => 'last'
 52			);
 53		}
 54
 55		$defaults = array(
 56			'tag' => 'span', 'before' => null, 'after' => null, 'model' => $this->defaultModel(), 'class' => null,
 57			'modulus' => '8', 'separator' => ' | ', 'first' => null, 'last' => null, 'ellipsis' => '...',
 58		);
 59		$options += $defaults;
 60
 61		$params = (array)$this->params($options['model']) + array('page'=> 1);
 62		unset($options['model']);
 63
 64		if ($params['pageCount'] <= 1) {
 65			return false;
 66		}
 67
 68		extract($options);
 69		unset($options['tag'], $options['before'], $options['after'], $options['model'],
 70			$options['modulus'], $options['separator'], $options['first'], $options['last'],
 71			$options['ellipsis'], $options['class']
 72		);
 73
 74		$out = '';
 75
 76		if ($modulus && $params['pageCount'] > $modulus) {
 77			$half = intval($modulus / 2);
 78			$end = $params['page'] + $half;
 79
 80			if ($end > $params['pageCount']) {
 81				$end = $params['pageCount'];
 82			}
 83			$start = $params['page'] - ($modulus - ($end - $params['page']));
 84			if ($start <= 1) {
 85				$start = 1;
 86				$end = $params['page'] + ($modulus	- $params['page']) + 1;
 87			}
 88
 89			if ($first && $start > 1) {
 90				$offset = ($start <= (int)$first) ? $start - 1 : $first;
 91				if ($offset < $start - 1) {
 92					$out .= $this->first($offset, compact('tag', 'separator', 'ellipsis', 'class'));
 93				} else {
 94					$out .= $this->first($offset, compact('tag', 'separator', 'class') + array('after' => $separator));
 95				}
 96			}
 97
 98			$out .= $before;
 99
100			for ($i = $start; $i < $params['page']; $i++) {
101				$out .= $this->Html->tag($tag, $this->link($i, array('page' => $i), $options), compact('class'))
102					. $separator;
103			}
104
105			$currentClass = 'current';
106			if ($class) {
107				$currentClass .= ' ' . $class;
108			}
109			$out .= $this->Html->tag($tag, $params['page'], array('class' => $currentClass));
110			if ($i != $params['pageCount']) {
111				$out .= $separator;
112			}
113
114			$start = $params['page'] + 1;
115			for ($i = $start; $i < $end; $i++) {
116				$out .= $this->Html->tag($tag, $this->link($i, array('page' => $i), $options), compact('class'))
117					. $separator;
118			}
119
120			if ($end != $params['page']) {
121				$out .= $this->Html->tag($tag, $this->link($i, array('page' => $end), $options), compact('class'));
122			}
123
124			$out .= $after;
125
126			if ($last && $end < $params['pageCount']) {
127				$offset = ($params['pageCount'] < $end + (int)$last) ? $params['pageCount'] - $end : $last;
128				if ($offset <= $last && $params['pageCount'] - $end > $offset) {
129					$out .= $this->last($offset, compact('tag', 'separator', 'ellipsis', 'class'));
130				} else {
131					$out .= $this->last($offset, compact('tag', 'separator', 'class') + array('before' => $separator));
132				}
133			}
134
135		} else {
136			$out .= $before;
137
138			for ($i = 1; $i <= $params['pageCount']; $i++) {
139				if ($i == $params['page']) {
140					$currentClass = 'active'; // Changed to support TwitterBootstrap
141					if ($class) {
142						$currentClass .= ' ' . $class;
143					}
144					$out .= $this->Html->tag($tag, $this->link($i, array('page' => $i), $options), array('class' => $currentClass)); // Changed to support TwitterBootstrap
145				} else {
146					$out .= $this->Html->tag($tag, $this->link($i, array('page' => $i), $options), compact('class'));
147				}
148				if ($i != $params['pageCount']) {
149					$out .= $separator;
150				}
151			}
152
153			$out .= $after;
154		}
155
156		return $out;
157	}
158}