CakePHP
  • Documentation
    • Book
    • API
    • Videos
    • Reporting Security Issues
    • Privacy Policy
    • Logos & Trademarks
  • Business Solutions
  • Swag
  • Road Trip
  • Team
  • Community
    • Community
    • Get Involved
    • Issues (GitHub)
    • Bakery
    • Featured Resources
    • Training
    • Meetups
    • My CakePHP
    • CakeFest
    • Newsletter
    • Linkedin
    • YouTube
    • Facebook
    • Twitter
    • Mastodon
    • Help & Support
    • Forum
    • Stack Overflow
    • Slack
    • Paid Support
CakePHP

C CakePHP 1.2 API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 1.2
      • 4.2
      • 4.1
      • 4.0
      • 3.9
      • 3.8
      • 3.7
      • 3.6
      • 3.5
      • 3.4
      • 3.3
      • 3.2
      • 3.1
      • 3.0
      • 2.10
      • 2.9
      • 2.8
      • 2.7
      • 2.6
      • 2.5
      • 2.4
      • 2.3
      • 2.2
      • 2.1
      • 2.0
      • 1.3
      • 1.2

Classes

  • AclBase
  • AclBehavior
  • AclComponent
  • AclNode
  • AclShell
  • Aco
  • AcoAction
  • AjaxHelper
  • ApcEngine
  • ApiShell
  • App
  • AppController
  • AppHelper
  • AppModel
  • Aro
  • AuthComponent
  • BakeShell
  • BehaviorCollection
  • Cache
  • CacheEngine
  • CacheHelper
  • CakeErrorController
  • CakeLog
  • CakeSchema
  • CakeSession
  • CakeSocket
  • ClassRegistry
  • Component
  • Configure
  • ConnectionManager
  • ConsoleShell
  • ContainableBehavior
  • Controller
  • ControllerTask
  • CookieComponent
  • DataSource
  • DbAcl
  • DbAclSchema
  • DbConfigTask
  • DboAdodb
  • DboDb2
  • DboFirebird
  • DboMssql
  • DboMysql
  • DboMysqlBase
  • DboMysqli
  • DboOdbc
  • DboOracle
  • DboPostgres
  • DboSource
  • DboSqlite
  • DboSybase
  • Debugger
  • EmailComponent
  • ErrorHandler
  • ExtractTask
  • File
  • FileEngine
  • Flay
  • Folder
  • FormHelper
  • Helper
  • HtmlHelper
  • HttpSocket
  • I18n
  • I18nModel
  • i18nSchema
  • I18nShell
  • Inflector
  • IniAcl
  • JavascriptHelper
  • JsHelper
  • JsHelperObject
  • L10n
  • MagicDb
  • MagicFileResource
  • MediaView
  • MemcacheEngine
  • Model
  • ModelBehavior
  • ModelTask
  • Multibyte
  • NumberHelper
  • Object
  • Overloadable
  • Overloadable2
  • PagesController
  • PaginatorHelper
  • Permission
  • PluginTask
  • ProjectTask
  • RequestHandlerComponent
  • Router
  • RssHelper
  • Sanitize
  • Scaffold
  • ScaffoldView
  • SchemaShell
  • Security
  • SecurityComponent
  • SessionComponent
  • SessionHelper
  • SessionsSchema
  • Set
  • Shell
  • String
  • TestSuiteShell
  • TestTask
  • TextHelper
  • ThemeView
  • TimeHelper
  • TranslateBehavior
  • TreeBehavior
  • Validation
  • View
  • ViewTask
  • XcacheEngine
  • Xml
  • XmlElement
  • XmlHelper
  • XmlManager
  • XmlNode
  • XmlTextNode

Functions

  • __enclose
  • make_clean_css
  • mb_encode_mimeheader
  • mb_stripos
  • mb_stristr
  • mb_strlen
  • mb_strpos
  • mb_strrchr
  • mb_strrichr
  • mb_strripos
  • mb_strrpos
  • mb_strstr
  • mb_strtolower
  • mb_strtoupper
  • mb_substr
  • mb_substr_count
  • write_css_cache
  1: <?php
  2: /* SVN FILE: $Id$ */
  3: /**
  4:  * Pagination Helper class file.
  5:  *
  6:  * Generates pagination links
  7:  *
  8:  * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  9:  * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
 10:  *
 11:  * Licensed under The MIT License
 12:  * Redistributions of files must retain the above copyright notice.
 13:  *
 14:  * @copyright     Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
 15:  * @link          http://cakephp.org CakePHP(tm) Project
 16:  * @package       cake
 17:  * @subpackage    cake.cake.libs.view.helpers
 18:  * @since         CakePHP(tm) v 1.2.0
 19:  * @version       $Revision$
 20:  * @modifiedby    $LastChangedBy$
 21:  * @lastmodified  $Date$
 22:  * @license       http://www.opensource.org/licenses/mit-license.php The MIT License
 23:  */
 24: /**
 25:  * Pagination Helper class for easy generation of pagination links.
 26:  *
 27:  * PaginationHelper encloses all methods needed when working with pagination.
 28:  *
 29:  * @package       cake
 30:  * @subpackage    cake.cake.libs.view.helpers
 31:  */
 32: class PaginatorHelper extends AppHelper {
 33: /**
 34:  * Helper dependencies
 35:  *
 36:  * @var array
 37:  */
 38:     var $helpers = array('Html', 'Ajax');
 39: /**
 40:  * Holds the default model for paged recordsets
 41:  *
 42:  * @var string
 43:  */
 44:     var $__defaultModel = null;
 45: /**
 46:  * Holds the default options for pagination links
 47:  *
 48:  * The values that may be specified are:
 49:  *
 50:  *  - `$options['format']` Format of the counter. Supported formats are 'range' and 'pages'
 51:  *    and custom (default). In the default mode the supplied string is parsed and constants are replaced
 52:  *    by their actual values.
 53:  *    Constants: %page%, %pages%, %current%, %count%, %start%, %end% .
 54:  *  - `$options['separator']` The separator of the actual page and number of pages (default: ' of ').
 55:  *  - `$options['url']` Url of the action. See Router::url()
 56:  *  - `$options['url']['sort']`  the key that the recordset is sorted.
 57:  *  - `$options['url']['direction']` Direction of the sorting (default: 'asc').
 58:  *  - `$options['url']['page']` Page # to display.
 59:  *  - `$options['model']` The name of the model.
 60:  *  - `$options['escape']` Defines if the title field for the link should be escaped (default: true).
 61:  *  - `$options['update']` DOM id of the element updated with the results of the AJAX call.
 62:  *     If this key isn't specified Paginator will use plain HTML links.
 63:  *  - `$options['indicator']` DOM id of the element that will be shown when doing AJAX requests.
 64:  *
 65:  * @var array
 66:  */
 67:     var $options = array();
 68: /**
 69:  * Gets the current paging parameters from the resultset for the given model
 70:  *
 71:  * @param string $model Optional model name.  Uses the default if none is specified.
 72:  * @return array The array of paging parameters for the paginated resultset.
 73:  */
 74:     function params($model = null) {
 75:         if (empty($model)) {
 76:             $model = $this->defaultModel();
 77:         }
 78:         if (!isset($this->params['paging']) || empty($this->params['paging'][$model])) {
 79:             return null;
 80:         }
 81:         return $this->params['paging'][$model];
 82:     }
 83: /**
 84:  * Sets default options for all pagination links
 85:  *
 86:  * @param mixed $options Default options for pagination links. If a string is supplied - it
 87:  * is used as the DOM id element to update. See #options for list of keys.
 88:  */
 89:     function options($options = array()) {
 90:         if (is_string($options)) {
 91:             $options = array('update' => $options);
 92:         }
 93: 
 94:         if (!empty($options['paging'])) {
 95:             if (!isset($this->params['paging'])) {
 96:                 $this->params['paging'] = array();
 97:             }
 98:             $this->params['paging'] = array_merge($this->params['paging'], $options['paging']);
 99:             unset($options['paging']);
100:         }
101:         $model = $this->defaultModel();
102: 
103:         if (!empty($options[$model])) {
104:             if (!isset($this->params['paging'][$model])) {
105:                 $this->params['paging'][$model] = array();
106:             }
107:             $this->params['paging'][$model] = array_merge($this->params['paging'][$model], $options[$model]);
108:             unset($options[$model]);
109:         }
110:         $this->options = array_filter(array_merge($this->options, $options));
111:     }
112: /**
113:  * Gets the current page of the recordset for the given model
114:  *
115:  * @param string $model Optional model name.  Uses the default if none is specified.
116:  * @return string The current page number of the recordset.
117:  */
118:     function current($model = null) {
119:         $params = $this->params($model);
120: 
121:         if (isset($params['page'])) {
122:             return $params['page'];
123:         }
124:         return 1;
125:     }
126: /**
127:  * Gets the current key by which the recordset is sorted
128:  *
129:  * @param string $model Optional model name.  Uses the default if none is specified.
130:  * @param mixed $options Options for pagination links. See #options for list of keys.
131:  * @return string The name of the key by which the recordset is being sorted, or
132:  *  null if the results are not currently sorted.
133:  */
134:     function sortKey($model = null, $options = array()) {
135:         if (empty($options)) {
136:             $params = $this->params($model);
137:             $options = array_merge($params['defaults'], $params['options']);
138:         }
139: 
140:         if (isset($options['sort']) && !empty($options['sort'])) {
141:             if (preg_match('/(?:\w+\.)?(\w+)/', $options['sort'], $result) && isset($result[1])) {
142:                 if ($result[0] == $this->defaultModel()) {
143:                     return $result[1];
144:                 }
145:             }
146:             return $options['sort'];
147:         } elseif (isset($options['order']) && is_array($options['order'])) {
148:             return key($options['order']);
149:         } elseif (isset($options['order']) && is_string($options['order'])) {
150:             if (preg_match('/(?:\w+\.)?(\w+)/', $options['order'], $result) && isset($result[1])) {
151:                 return $result[1];
152:             }
153:             return $options['order'];
154:         }
155:         return null;
156:     }
157: /**
158:  * Gets the current direction the recordset is sorted
159:  *
160:  * @param string $model Optional model name.  Uses the default if none is specified.
161:  * @param mixed $options Options for pagination links. See #options for list of keys.
162:  * @return string The direction by which the recordset is being sorted, or
163:  *  null if the results are not currently sorted.
164:  */
165:     function sortDir($model = null, $options = array()) {
166:         $dir = null;
167: 
168:         if (empty($options)) {
169:             $params = $this->params($model);
170:             $options = array_merge($params['defaults'], $params['options']);
171:         }
172: 
173:         if (isset($options['direction'])) {
174:             $dir = strtolower($options['direction']);
175:         } elseif (isset($options['order']) && is_array($options['order'])) {
176:             $dir = strtolower(current($options['order']));
177:         }
178: 
179:         if ($dir == 'desc') {
180:             return 'desc';
181:         }
182:         return 'asc';
183:     }
184: /**
185:  * Generates a "previous" link for a set of paged records
186:  *
187:  * Options:
188:  *
189:  * - `tag` The tag wrapping tag you want to use, defaults to 'span'
190:  * - `escape` Whether you want the contents html entity encoded, defaults to true
191:  * - `model` The model to use, defaults to PaginatorHelper::defaultModel()
192:  *
193:  * @param  string $title Title for the link. Defaults to '<< Previous'.
194:  * @param  mixed $options Options for pagination link. See #options for list of keys.
195:  * @param  string $disabledTitle Title when the link is disabled.
196:  * @param  mixed $disabledOptions Options for the disabled pagination link. See #options for list of keys.
197:  * @return string A "previous" link or $disabledTitle text if the link is disabled.
198:  */
199:     function prev($title = '<< Previous', $options = array(), $disabledTitle = null, $disabledOptions = array()) {
200:         return $this->__pagingLink('Prev', $title, $options, $disabledTitle, $disabledOptions);
201:     }
202: /**
203:  * Generates a "next" link for a set of paged records
204:  *
205:  * Options:
206:  *
207:  * - `tag` The tag wrapping tag you want to use, defaults to 'span'
208:  * - `escape` Whether you want the contents html entity encoded, defaults to true
209:  * - `model` The model to use, defaults to PaginatorHelper::defaultModel()
210:  *
211:  * @param string $title Title for the link. Defaults to 'Next >>'.
212:  * @param mixed $options Options for pagination link. See above for list of keys.
213:  * @param string $disabledTitle Title when the link is disabled.
214:  * @param mixed $disabledOptions Options for the disabled pagination link. See above for list of keys.
215:  * @return string A "next" link or or $disabledTitle text if the link is disabled.
216:  */
217:     function next($title = 'Next >>', $options = array(), $disabledTitle = null, $disabledOptions = array()) {
218:         return $this->__pagingLink('Next', $title, $options, $disabledTitle, $disabledOptions);
219:     }
220: /**
221:  * Generates a sorting link. Sets named parameters for the sort and direction.  Handles
222:  * direction switching automatically.
223:  *
224:  * Options:
225:  *
226:  * - `escape` Whether you want the contents html entity encoded, defaults to true
227:  * - `model` The model to use, defaults to PaginatorHelper::defaultModel()
228:  *
229:  * @param string $title Title for the link.
230:  * @param string $key The name of the key that the recordset should be sorted.  If $key is null
231:  *   $title will be used for the key, and a title will be generated by inflection.
232:  * @param array $options Options for sorting link. See above for list of keys.
233:  * @return string A link sorting default by 'asc'. If the resultset is sorted 'asc' by the specified
234:  *  key the returned link will sort by 'desc'.
235:  */
236:     function sort($title, $key = null, $options = array()) {
237:         $options = array_merge(array('url' => array(), 'model' => null), $options);
238:         $url = $options['url'];
239:         unset($options['url']);
240: 
241:         if (empty($key)) {
242:             $key = $title;
243:             $title = __(Inflector::humanize(preg_replace('/_id$/', '', $title)), true);
244:         }
245:         $dir = 'asc';
246:         $sortKey = $this->sortKey($options['model']);
247:         $isSorted = ($sortKey === $key || $sortKey === $this->defaultModel() . '.' . $key);
248: 
249:         if ($isSorted && $this->sortDir($options['model']) === 'asc') {
250:             $dir = 'desc';
251:         }
252: 
253:         if (is_array($title) && array_key_exists($dir, $title)) {
254:             $title = $title[$dir];
255:         }
256: 
257:         $url = array_merge(array('sort' => $key, 'direction' => $dir), $url, array('order' => null));
258:         return $this->link($title, $url, $options);
259:     }
260: /**
261:  * Generates a plain or Ajax link with pagination parameters
262:  *
263:  * Options
264:  *
265:  * - `update` The Id of the DOM element you wish to update.  Creates Ajax enabled links
266:  *    with the AjaxHelper.
267:  * - `escape` Whether you want the contents html entity encoded, defaults to true
268:  * - `model` The model to use, defaults to PaginatorHelper::defaultModel()
269:  *
270:  * @param string $title Title for the link.
271:  * @param mixed $url Url for the action. See Router::url()
272:  * @param array $options Options for the link. See #options for list of keys.
273:  * @return string A link with pagination parameters.
274:  */
275:     function link($title, $url = array(), $options = array()) {
276:         $options = array_merge(array('model' => null, 'escape' => true), $options);
277:         $model = $options['model'];
278:         unset($options['model']);
279: 
280:         if (!empty($this->options)) {
281:             $options = array_merge($this->options, $options);
282:         }
283:         if (isset($options['url'])) {
284:             $url = array_merge((array)$options['url'], (array)$url);
285:             unset($options['url']);
286:         }
287:         $url = $this->url($url, true, $model);
288: 
289:         $obj = isset($options['update']) ? 'Ajax' : 'Html';
290:         $url = array_merge(array('page' => $this->current($model)), $url);
291:         $url = array_merge(Set::filter($url, true), array_intersect_key($url, array('plugin'=>true)));
292:         return $this->{$obj}->link($title, $url, $options);
293:     }
294: /**
295:  * Merges passed URL options with current pagination state to generate a pagination URL.
296:  *
297:  * @param array $options Pagination/URL options array
298:  * @param boolean $asArray Return the url as an array, or a URI string 
299:  * @param string $model Which model to paginate on
300:  * @return mixed By default, returns a full pagination URL string for use in non-standard contexts (i.e. JavaScript)
301:  */
302:     function url($options = array(), $asArray = false, $model = null) {
303:         $paging = $this->params($model);
304:         $url = array_merge(array_filter(Set::diff(array_merge($paging['defaults'], $paging['options']), $paging['defaults'])), $options);
305: 
306:         if (isset($url['order'])) {
307:             $sort = $direction = null;
308:             if (is_array($url['order'])) {
309:                 list($sort, $direction) = array($this->sortKey($model, $url), current($url['order']));
310:             }
311:             unset($url['order']);
312:             $url = array_merge($url, compact('sort', 'direction'));
313:         }
314: 
315:         if ($asArray) {
316:             return $url;
317:         }
318:         return parent::url($url);
319:     }
320: /**
321:  * Protected method for generating prev/next links
322:  *
323:  */
324:     function __pagingLink($which, $title = null, $options = array(), $disabledTitle = null, $disabledOptions = array()) {
325:         $check = 'has' . $which;
326:         $_defaults = array('url' => array(), 'step' => 1, 'escape' => true, 'model' => null, 'tag' => 'div');
327:         $options = array_merge($_defaults, (array)$options);
328:         $paging = $this->params($options['model']);
329: 
330:         if (!$this->{$check}($options['model']) && (!empty($disabledTitle) || !empty($disabledOptions))) {
331:             if (!empty($disabledTitle) && $disabledTitle !== true) {
332:                 $title = $disabledTitle;
333:             }
334:             $options = array_merge($_defaults, (array)$disabledOptions);
335:         } elseif (!$this->{$check}($options['model'])) {
336:             return null;
337:         }
338: 
339:         foreach (array_keys($_defaults) as $key) {
340:             ${$key} = $options[$key];
341:             unset($options[$key]);
342:         }
343:         $url = array_merge(array('page' => $paging['page'] + ($which == 'Prev' ? $step * -1 : $step)), $url);
344: 
345:         if ($this->{$check}($model)) {
346:             return $this->link($title, $url, array_merge($options, array('escape' => $escape)));
347:         } else {
348:             return $this->Html->tag($tag, $title, $options, $escape);
349:         }
350:     }
351: /**
352:  * Returns true if the given result set is not at the first page
353:  *
354:  * @param string $model Optional model name. Uses the default if none is specified.
355:  * @return boolean True if the result set is not at the first page.
356:  */
357:     function hasPrev($model = null) {
358:         return $this->__hasPage($model, 'prev');
359:     }
360: /**
361:  * Returns true if the given result set is not at the last page
362:  *
363:  * @param string $model Optional model name.  Uses the default if none is specified.
364:  * @return boolean True if the result set is not at the last page.
365:  */
366:     function hasNext($model = null) {
367:         return $this->__hasPage($model, 'next');
368:     }
369: /**
370:  * Returns true if the given result set has the page number given by $page
371:  *
372:  * @param string $model Optional model name.  Uses the default if none is specified.
373:  * @param int $page The page number - if not set defaults to 1.
374:  * @return boolean True if the given result set has the specified page number.
375:  */
376:     function hasPage($model = null, $page = 1) {
377:         if (is_numeric($model)) {
378:             $page = $model;
379:             $model = null;
380:         }
381:         $paging = $this->params($model);
382:         return $page <= $paging['pageCount'];
383:     }
384: /**
385:  * Protected method
386:  *
387:  */
388:     function __hasPage($model, $page) {
389:         $params = $this->params($model);
390:         if (!empty($params)) {
391:             if ($params["{$page}Page"] == true) {
392:                 return true;
393:             }
394:         }
395:         return false;
396:     }
397: /**
398:  * Gets the default model of the paged sets
399:  *
400:  * @return string Model name or null if the pagination isn't initialized.
401:  */
402:     function defaultModel() {
403:         if ($this->__defaultModel != null) {
404:             return $this->__defaultModel;
405:         }
406:         if (empty($this->params['paging'])) {
407:             return null;
408:         }
409:         list($this->__defaultModel) = array_keys($this->params['paging']);
410:         return $this->__defaultModel;
411:     }
412: /**
413:  * Returns a counter string for the paged result set
414:  *
415:  * Options
416:  *
417:  * - `model` The model to use, defaults to PaginatorHelper::defaultModel();
418:  * - `format` The format string you want to use, defaults to 'pages' Which generates output like '1 of 5'
419:  *    set to 'range' to generate output like '1 - 3 of 13'.  Can also be set to a custom string, containing 
420:  *    the following placeholders `%page%`, `%pages%`, `%current%`, `%count%`, `%start%`, `%end%` and any
421:  *    custom content you would like.
422:  * - `separator` The separator string to use, default to ' of '
423:  *
424:  * @param mixed $options Options for the counter string. See #options for list of keys.
425:  * @return string Counter string.
426:  */
427:     function counter($options = array()) {
428:         if (is_string($options)) {
429:             $options = array('format' => $options);
430:         }
431: 
432:         $options = array_merge(
433:             array(
434:                 'model' => $this->defaultModel(),
435:                 'format' => 'pages',
436:                 'separator' => __(' of ', true)
437:             ),
438:         $options);
439: 
440:         $paging = $this->params($options['model']);
441:         if ($paging['pageCount'] == 0) {
442:             $paging['pageCount'] = 1;
443:         }
444:         $start = 0;
445:         if ($paging['count'] >= 1) {
446:             $start = (($paging['page'] - 1) * $paging['options']['limit']) + 1;
447:         }
448:         $end = $start + $paging['options']['limit'] - 1;
449:         if ($paging['count'] < $end) {
450:             $end = $paging['count'];
451:         }
452: 
453:         switch ($options['format']) {
454:             case 'range':
455:                 if (!is_array($options['separator'])) {
456:                     $options['separator'] = array(' - ', $options['separator']);
457:                 }
458:                 $out = $start . $options['separator'][0] . $end . $options['separator'][1] . $paging['count'];
459:             break;
460:             case 'pages':
461:                 $out = $paging['page'] . $options['separator'] . $paging['pageCount'];
462:             break;
463:             default:
464:                 $replace = array(
465:                     '%page%' => $paging['page'],
466:                     '%pages%' => $paging['pageCount'],
467:                     '%current%' => $paging['current'],
468:                     '%count%' => $paging['count'],
469:                     '%start%' => $start,
470:                     '%end%' => $end
471:                 );
472:                 $out = str_replace(array_keys($replace), array_values($replace), $options['format']);
473:             break;
474:         }
475:         return $this->output($out);
476:     }
477: /**
478:  * Returns a set of numbers for the paged result set
479:  * uses a modulus to decide how many numbers to show on each side of the current page (default: 8)
480:  *
481:  * Options
482:  *
483:  * - `before` Content to be inserted before the numbers
484:  * - `after` Content to be inserted after the numbers
485:  * - `model` Model to create numbers for, defaults to PaginatorHelper::defaultModel()
486:  * - `modulus` how many numbers to include on either side of the current page, defaults to 8.
487:  * - `separator` Separator content defaults to ' | '
488:  * - `tag` The tag to wrap links in, defaults to 'span'
489:  * - `first` Whether you want first links generated, set to an integer to define the number of 'first' 
490:  *    links to generate
491:  * - `last` Whether you want last links generated, set to an integer to define the number of 'last' 
492:  *    links to generate
493:  *
494:  * @param mixed $options Options for the numbers, (before, after, model, modulus, separator)
495:  * @return string numbers string.
496:  */
497:     function numbers($options = array()) {
498:         if ($options === true) {
499:             $options = array(
500:                 'before' => ' | ', 'after' => ' | ',
501:                 'first' => 'first', 'last' => 'last',
502:             );
503:         }
504: 
505:         $options = array_merge(
506:             array(
507:                 'tag' => 'span',
508:                 'before'=> null, 'after'=> null,
509:                 'model' => $this->defaultModel(),
510:                 'modulus' => '8', 'separator' => ' | ',
511:                 'first' => null, 'last' => null,
512:             ),
513:         (array)$options);
514: 
515:         $params = array_merge(array('page'=> 1), (array)$this->params($options['model']));
516:         unset($options['model']);
517: 
518:         if ($params['pageCount'] <= 1) {
519:             return false;
520:         }
521: 
522:         extract($options);
523:         unset($options['tag'], $options['before'], $options['after'], $options['model'],
524:             $options['modulus'], $options['separator'], $options['first'], $options['last']);
525: 
526:         $out = '';
527: 
528:         if ($modulus && $params['pageCount'] > $modulus) {
529:             $half = intval($modulus / 2);
530:             $end = $params['page'] + $half;
531: 
532:             if ($end > $params['pageCount']) {
533:                 $end = $params['pageCount'];
534:             }
535:             $start = $params['page'] - ($modulus - ($end - $params['page']));
536:             if ($start <= 1) {
537:                 $start = 1;
538:                 $end = $params['page'] + ($modulus  - $params['page']) + 1;
539:             }
540: 
541:             if ($first && $start > 1) {
542:                 $offset = ($start <= (int)$first) ? $start - 1 : $first;
543:                 if ($offset < $start - 1) {
544:                     $out .= $this->first($offset, array('tag' => $tag, 'separator' => $separator));
545:                 } else {
546:                     $out .= $this->first($offset, array('tag' => $tag, 'after' => $separator, 'separator' => $separator));
547:                 }
548:             }
549: 
550:             $out .= $before;
551: 
552:             for ($i = $start; $i < $params['page']; $i++) {
553:                 $out .= $this->Html->tag($tag, $this->link($i, array('page' => $i), $options)) . $separator;
554:             }
555: 
556:             $out .= $this->Html->tag($tag, $params['page'], array('class' => 'current'));
557:             if ($i != $params['pageCount']) {
558:                 $out .= $separator;
559:             }
560: 
561:             $start = $params['page'] + 1;
562:             for ($i = $start; $i < $end; $i++) {
563:                 $out .= $this->Html->tag($tag, $this->link($i, array('page' => $i), $options)). $separator;
564:             }
565: 
566:             if ($end != $params['page']) {
567:                 $out .= $this->Html->tag($tag, $this->link($i, array('page' => $end), $options));
568:             }
569: 
570:             $out .= $after;
571: 
572:             if ($last && $end < $params['pageCount']) {
573:                 $offset = ($params['pageCount'] < $end + (int)$last) ? $params['pageCount'] - $end : $last;
574:                 if ($offset <= $last && $params['pageCount'] - $end > $offset) {
575:                     $out .= $this->last($offset, array('tag' => $tag, 'separator' => $separator));
576:                 } else {
577:                     $out .= $this->last($offset, array('tag' => $tag, 'before' => $separator, 'separator' => $separator));
578:                 }
579:             }
580: 
581:         } else {
582:             $out .= $before;
583: 
584:             for ($i = 1; $i <= $params['pageCount']; $i++) {
585:                 if ($i == $params['page']) {
586:                     $out .= $this->Html->tag($tag, $i, array('class' => 'current'));
587:                 } else {
588:                     $out .= $this->Html->tag($tag, $this->link($i, array('page' => $i), $options));
589:                 }
590:                 if ($i != $params['pageCount']) {
591:                     $out .= $separator;
592:                 }
593:             }
594: 
595:             $out .= $after;
596:         }
597: 
598:         return $this->output($out);
599:     }
600: /**
601:  * Returns a first or set of numbers for the first pages
602:  *
603:  * Options:
604:  *
605:  * - `tag` The tag wrapping tag you want to use, defaults to 'span'
606:  * - `before` Content to insert before the link/tag
607:  * - `model` The model to use defaults to PaginatorHelper::defaultModel()
608:  * - `separator` Content between the generated links, defaults to ' | '
609:  *
610:  * @param mixed $first if string use as label for the link, if numeric print page numbers
611:  * @param mixed $options
612:  * @return string numbers string.
613:  */
614:     function first($first = '<< first', $options = array()) {
615:         $options = array_merge(
616:             array(
617:                 'tag' => 'span',
618:                 'after'=> null,
619:                 'model' => $this->defaultModel(),
620:                 'separator' => ' | ',
621:             ),
622:         (array)$options);
623: 
624:         $params = array_merge(array('page'=> 1), (array)$this->params($options['model']));
625:         unset($options['model']);
626: 
627:         if ($params['pageCount'] <= 1) {
628:             return false;
629:         }
630:         extract($options);
631:         unset($options['tag'], $options['after'], $options['model'], $options['separator']);
632: 
633:         $out = '';
634: 
635:         if (is_int($first) && $params['page'] > $first) {
636:             if ($after === null) {
637:                 $after = '...';
638:             }
639:             for ($i = 1; $i <= $first; $i++) {
640:                 $out .= $this->Html->tag($tag, $this->link($i, array('page' => $i), $options));
641:                 if ($i != $first) {
642:                     $out .= $separator;
643:                 }
644:             }
645:             $out .= $after;
646:         } elseif ($params['page'] > 1) {
647:             $out = $this->Html->tag($tag, $this->link($first, array('page' => 1), $options)) . $after;
648:         }
649:         return $out;
650:     }
651: /**
652:  * Returns a last or set of numbers for the last pages
653:  *
654:  * Options:
655:  *
656:  * - `tag` The tag wrapping tag you want to use, defaults to 'span'
657:  * - `before` Content to insert before the link/tag
658:  * - `model` The model to use defaults to PaginatorHelper::defaultModel()
659:  * - `separator` Content between the generated links, defaults to ' | '
660:  *
661:  * @param mixed $last if string use as label for the link, if numeric print page numbers
662:  * @param mixed $options Array of options
663:  * @return string numbers string.
664:  */
665:     function last($last = 'last >>', $options = array()) {
666:         $options = array_merge(
667:             array(
668:                 'tag' => 'span',
669:                 'before'=> null,
670:                 'model' => $this->defaultModel(),
671:                 'separator' => ' | ',
672:             ),
673:         (array)$options);
674: 
675:         $params = array_merge(array('page'=> 1), (array)$this->params($options['model']));
676:         unset($options['model']);
677: 
678:         if ($params['pageCount'] <= 1) {
679:             return false;
680:         }
681: 
682:         extract($options);
683:         unset($options['tag'], $options['before'], $options['model'], $options['separator']);
684: 
685:         $out = '';
686:         $lower = $params['pageCount'] - $last + 1;
687: 
688:         if (is_int($last) && $params['page'] < $lower) {
689:             if ($before === null) {
690:                 $before = '...';
691:             }
692:             for ($i = $lower; $i <= $params['pageCount']; $i++) {
693:                 $out .= $this->Html->tag($tag, $this->link($i, array('page' => $i), $options));
694:                 if ($i != $params['pageCount']) {
695:                     $out .= $separator;
696:                 }
697:             }
698:             $out = $before . $out;
699:         } elseif ($params['page'] < $params['pageCount']) {
700:             $out = $before . $this->Html->tag($tag, $this->link($last, array('page' => $params['pageCount']), $options));
701:         }
702:         return $out;
703:     }
704: }
705: ?>
OpenHub
Rackspace
Rackspace
  • Business Solutions
  • Showcase
  • Documentation
  • Book
  • API
  • Videos
  • Reporting Security Issues
  • Privacy Policy
  • Logos & Trademarks
  • Community
  • Get Involved
  • Issues (GitHub)
  • Bakery
  • Featured Resources
  • Training
  • Meetups
  • My CakePHP
  • CakeFest
  • Newsletter
  • Linkedin
  • YouTube
  • Facebook
  • Twitter
  • Mastodon
  • Help & Support
  • Forum
  • Stack Overflow
  • Slack
  • Paid Support

Generated using CakePHP API Docs