|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | +* Bootstrap Paginator Helper |
| 5 | +* |
| 6 | +* |
| 7 | +* PHP 5 |
| 8 | +* |
| 9 | +* Licensed under the Apache License, Version 2.0 (the "License"); |
| 10 | +* you may not use this file except in compliance with the License. |
| 11 | +* You may obtain a copy of the License at |
| 12 | +* |
| 13 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | +* |
| 15 | +* |
| 16 | +* @copyright Copyright (c) Mikaël Capelle (http://mikael-capelle.fr) |
| 17 | +* @link http://mikael-capelle.fr |
| 18 | +* @package app.View.Helper |
| 19 | +* @since Apache v2 |
| 20 | +* @license http://www.apache.org/licenses/LICENSE-2.0 |
| 21 | +*/ |
| 22 | + |
| 23 | +namespace Bootstrap\View\Helper; |
| 24 | + |
| 25 | +use Cake\View\Helper\PaginatorHelper; |
| 26 | + |
| 27 | +class BootstrapPaginatorHelper extends PaginatorHelper { |
| 28 | + |
| 29 | + use BootstrapTrait ; |
| 30 | + |
| 31 | + /** |
| 32 | + * Default config for this class |
| 33 | + * |
| 34 | + * Options: Holds the default options for pagination links |
| 35 | + * |
| 36 | + * The values that may be specified are: |
| 37 | + * |
| 38 | + * - `url` Url of the action. See Router::url() |
| 39 | + * - `url['sort']` the key that the recordset is sorted. |
| 40 | + * - `url['direction']` Direction of the sorting (default: 'asc'). |
| 41 | + * - `url['page']` Page number to use in links. |
| 42 | + * - `model` The name of the model. |
| 43 | + * - `escape` Defines if the title field for the link should be escaped (default: true). |
| 44 | + * |
| 45 | + * Templates: the templates used by this class |
| 46 | + * |
| 47 | + * @var array |
| 48 | + */ |
| 49 | + protected $_defaultConfig = [ |
| 50 | + 'options' => [], |
| 51 | + 'templates' => [ |
| 52 | + 'nextActive' => '<li><a href="{{url}}">{{text}}</a></li>', |
| 53 | + 'nextDisabled' => '<li class="disabled"><a>{{text}}</a></li>', |
| 54 | + 'prevActive' => '<li><a href="{{url}}">{{text}}</a></li>', |
| 55 | + 'prevDisabled' => '<li class="disabled"><a>{{text}}</a></li>', |
| 56 | + 'counterRange' => '{{start}} - {{end}} of {{count}}', |
| 57 | + 'counterPages' => '{{page}} of {{pages}}', |
| 58 | + 'first' => '<li><a href="{{url}}">{{text}}</a></li>', |
| 59 | + 'last' => '<li><a href="{{url}}">{{text}}</a></li>', |
| 60 | + 'number' => '<li><a href="{{url}}">{{text}}</a></li>', |
| 61 | + 'current' => '<li class="active"><a href="{{url}}">{{text}}</a></li>', |
| 62 | + 'ellipsis' => '<li class="ellipsis disabled"><a>...</a></li>', |
| 63 | + 'sort' => '<a href="{{url}}">{{text}}</a>', |
| 64 | + 'sortAsc' => '<a class="asc" href="{{url}}">{{text}}</a>', |
| 65 | + 'sortDesc' => '<a class="desc" href="{{url}}">{{text}}</a>', |
| 66 | + 'sortAscLocked' => '<a class="asc locked" href="{{url}}">{{text}}</a>', |
| 67 | + 'sortDescLocked' => '<a class="desc locked" href="{{url}}">{{text}}</a>', |
| 68 | + ] |
| 69 | + ]; |
| 70 | + |
| 71 | + /** |
| 72 | + * |
| 73 | + * Get pagination link list. |
| 74 | + * |
| 75 | + * @param $options Options for link element |
| 76 | + * |
| 77 | + * Extra options: |
| 78 | + * - size small/normal/large (default normal) |
| 79 | + * |
| 80 | + **/ |
| 81 | + public function numbers (array $options = []) { |
| 82 | + |
| 83 | + $defaults = [ |
| 84 | + 'before' => null, 'after' => null, 'model' => $this->defaultModel(), |
| 85 | + 'modulus' => 8, 'first' => null, 'last' => null, 'url' => [], |
| 86 | + 'prev' => null, 'next' => null, 'class' => '', 'size' => false |
| 87 | + ]; |
| 88 | + $options += $defaults; |
| 89 | + |
| 90 | + $options = $this->addClass($options, 'pagination'); |
| 91 | + |
| 92 | + switch ($options['size']) { |
| 93 | + case 'small': |
| 94 | + $options = $this->addClass($options, 'pagination-sm') ; |
| 95 | + break ; |
| 96 | + case 'large': |
| 97 | + $options = $this->addClass($options, 'pagination-lg') ; |
| 98 | + break ; |
| 99 | + } |
| 100 | + unset($options['size']) ; |
| 101 | + |
| 102 | + $options['before'] .= $this->Html->tag('ul', null, ['class' => $options['class']]); |
| 103 | + $options['after'] = '</ul>'.$options['after'] ; |
| 104 | + unset($options['class']); |
| 105 | + |
| 106 | + $params = (array)$this->params($options['model']) + ['page' => 1]; |
| 107 | + if ($params['pageCount'] <= 1) { |
| 108 | + return false; |
| 109 | + } |
| 110 | + |
| 111 | + $templater = $this->templater(); |
| 112 | + if (isset($options['templates'])) { |
| 113 | + $templater->push(); |
| 114 | + $method = is_string($options['templates']) ? 'load' : 'add'; |
| 115 | + $templater->{$method}($options['templates']); |
| 116 | + } |
| 117 | + |
| 118 | + $first = $prev = $next = $last = ''; |
| 119 | + |
| 120 | + /* Previous and Next buttons (addition from standard PaginatorHelper). */ |
| 121 | + |
| 122 | + if ($options['prev']) { |
| 123 | + $title = $options['prev'] ; |
| 124 | + $opts = [] ; |
| 125 | + if (is_array($title)) { |
| 126 | + $title = $title['title'] ; |
| 127 | + unset ($options['prev']['title']) ; |
| 128 | + $opts = $options['prev'] ; |
| 129 | + } |
| 130 | + $prev = $this->prev($title, $opts) ; |
| 131 | + } |
| 132 | + unset($options['prev']); |
| 133 | + |
| 134 | + if ($options['next']) { |
| 135 | + $title = $options['next'] ; |
| 136 | + $opts = [] ; |
| 137 | + if (is_array($title)) { |
| 138 | + $title = $title['title']; |
| 139 | + unset ($options['next']['title']); |
| 140 | + $opts = $options['next']; |
| 141 | + } |
| 142 | + $next = $this->next($title, $opts); |
| 143 | + } |
| 144 | + unset($options['next']); |
| 145 | + |
| 146 | + /* Custom First and Last. */ |
| 147 | + |
| 148 | + list($start, $end) = $this->_getNumbersStartAndEnd($params, $options); |
| 149 | + |
| 150 | + if ($options['last']) { |
| 151 | + $ellipsis = isset($options['ellipsis']) ? |
| 152 | + $options['ellipsis'] : is_int($options['last']); |
| 153 | + $ellipsis = $ellipsis ? $templater->format('ellipsis', []) : ''; |
| 154 | + $last = $this->_lastNumber($ellipsis, $params, $end, $options); |
| 155 | + } |
| 156 | + |
| 157 | + if ($options['first']) { |
| 158 | + $ellipsis = isset($options['ellipsis']) ? |
| 159 | + $options['ellipsis'] : is_int($options['first']); |
| 160 | + $ellipsis = $ellipsis ? $templater->format('ellipsis', []) : ''; |
| 161 | + $first = $this->_firstNumber($ellipsis, $params, $start, $options); |
| 162 | + } |
| 163 | + |
| 164 | + unset($options['ellipsis']); |
| 165 | + |
| 166 | + $before = is_int($options['first']) ? $prev.$first : $first.$prev; |
| 167 | + $after = is_int($options['last']) ? $last.$next : $next.$last; |
| 168 | + $options['before'] = $options['before'].$before;; |
| 169 | + $options['after'] = $after.$options['after']; |
| 170 | + $options['first'] = $options['last'] = false; |
| 171 | + |
| 172 | + if ($options['modulus'] !== false && $params['pageCount'] > $options['modulus']) { |
| 173 | + $out = $this->_modulusNumbers($templater, $params, $options); |
| 174 | + } else { |
| 175 | + $out = $this->_numbers($templater, $params, $options); |
| 176 | + } |
| 177 | + |
| 178 | + if (isset($options['templates'])) { |
| 179 | + $templater->pop(); |
| 180 | + } |
| 181 | + |
| 182 | + |
| 183 | + return $out; |
| 184 | + } |
| 185 | + |
| 186 | + public function prev ($title = '<< Previous', array $options = []) { |
| 187 | + return $this->_easyIcon('parent::prev', $title, $options); |
| 188 | + } |
| 189 | + |
| 190 | + public function next ($title = 'Next >>', array $options = []) { |
| 191 | + return $this->_easyIcon('parent::next', $title, $options); |
| 192 | + } |
| 193 | + |
| 194 | + public function first($first = '<< first', array $options = []) { |
| 195 | + return $this->_easyIcon('parent::first', $first, $options); |
| 196 | + } |
| 197 | + |
| 198 | + public function last($last = 'last >>', array $options = []) { |
| 199 | + return $this->_easyIcon('parent::last', $last, $options); |
| 200 | + } |
| 201 | + |
| 202 | +} |
| 203 | + |
| 204 | +?> |
0 commit comments