paginator.php

来自「Cake Framwork , Excellent」· PHP 代码 · 共 622 行 · 第 1/2 页

PHP
622
字号
			return $this->link($title, $url, array_merge($options, array('escape' => $escape)));		} else {			return $this->Html->tag($tag, $title, $options, $escape);		}	}/** * Returns true if the given result set is not at the first page * * @param string $model Optional model name.  Uses the default if none is specified. * @return boolean True if the result set is not at the first page. */	function hasPrev($model = null) {		return $this->__hasPage($model, 'prev');	}/** * Returns true if the given result set is not at the last page * * @param string $model Optional model name.  Uses the default if none is specified. * @return boolean True if the result set is not at the last page. */	function hasNext($model = null) {		return $this->__hasPage($model, 'next');	}/** * Returns true if the given result set has the page number given by $page * * @param  string $model Optional model name.  Uses the default if none is specified. * @param  int $page The page number - if not set defaults to 1. * @return boolean True if the given result set has the specified page number. */	function hasPage($model = null, $page = 1) {		if (is_numeric($model)) {			$page = $model;			$model = null;		}		$paging = $this->params($model);		return $page <= $paging['pageCount'];	}/** * Protected method * */	function __hasPage($model, $page) {		$params = $this->params($model);		if (!empty($params)) {			if ($params["{$page}Page"] == true) {				return true;			}		}		return false;	}/** * Gets the default model of the paged sets * * @return string Model name or null if the pagination isn't initialized. */	function defaultModel() {		if ($this->__defaultModel != null) {			return $this->__defaultModel;		}		if (empty($this->params['paging'])) {			return null;		}		list($this->__defaultModel) = array_keys($this->params['paging']);		return $this->__defaultModel;	}/** * Returns a counter string for the paged result set * * @param  mixed $options Options for the counter string. See #options for list of keys. * @return string Counter string. */	function counter($options = array()) {		if (is_string($options)) {			$options = array('format' => $options);		}		$options = array_merge(			array(				'model' => $this->defaultModel(),				'format' => 'pages',				'separator' => ' of '			),		$options);		$paging = $this->params($options['model']);		$paging['pageCount'] = ife($paging['pageCount'] == 0, 1, $paging['pageCount']);		$start = ife($paging['count'] >= 1, ($paging['page'] - 1) * ($paging['options']['limit']) + 1, '0');		$end = ife(($paging['count'] < ($start + $paging['options']['limit'] - 1)), $paging['count'], ($start + $paging['options']['limit'] - 1));		switch ($options['format']) {			case 'range':				if (!is_array($options['separator'])) {					$options['separator'] = array(' - ', $options['separator']);				}				$out = $start . $options['separator'][0] . $end . $options['separator'][1] . $paging['count'];			break;			case 'pages':				$out = $paging['page'] . $options['separator'] . $paging['pageCount'];			break;			default:				$replace = array(					'%page%' => $paging['page'],					'%pages%' => $paging['pageCount'],					'%current%' => $paging['current'],					'%count%' => $paging['count'],					'%start%' => $start,					'%end%' => $end				);				$out = str_replace(array_keys($replace), array_values($replace), $options['format']);			break;		}		return $this->output($out);	}/** * Returns a set of numbers for the paged result set * uses a modulus to decide how many numbers to show on each side of the current page (default: 8) * * @param  mixed $options Options for the numbers, (before, after, model, modulus, separator) * @return string numbers string. */	function numbers($options = array()) {		if ($options === true) {			$options = array(						'before' => ' | ', 'after' => ' | ',						'first' => 'first', 'last' => 'last',						);		}		$options = array_merge(			array(				'before'=> null, 'after'=> null,				'model' => $this->defaultModel(),				'modulus' => '8', 'separator' => ' | ',				'first' => null, 'last' => null,			),		(array)$options);		$params = array_merge(array('page'=> 1), (array)$this->params($options['model']));		unset($options['model']);		if ($params['pageCount'] <= 1) {			return false;		}		extract($options);		unset($options['before'], $options['after'], $options['model'], $options['modulus'], $options['separator'], $options['first'], $options['last']);		$out = '';		if ($modulus && $params['pageCount'] > $modulus) {			$half = intval($modulus / 2);			$end = $params['page'] + $half;			if ($end > $params['pageCount']) {				$end = $params['pageCount'];			}			$start = $params['page'] - ($modulus - ($end - $params['page']));			if ($start <= 1) {				$start = 1;				$end = $params['page'] + ($modulus  - $params['page']) + 1;			}			if ($first && $start > (int)$first) {				if ($start == $first + 1) {					$out .= $this->first($first, array('after' => $separator));				} else {					$out .= $this->first($first);				}			}			$out .= $before;			for ($i = $start; $i < $params['page']; $i++) {				$out .= '<span>' . $this->link($i, array('page' => $i), $options) . '</span>' . $separator;			}			$out .= '<span class="current">' . $params['page'] . '</span>';			if ($i != $params['pageCount']) {				$out .= $separator;			}			$start = $params['page'] + 1;			for ($i = $start; $i < $end; $i++) {				$out .= '<span>' .$this->link($i, array('page' => $i), $options) . '</span>'. $separator;			}			if ($end != $params['page']) {				$out .= '<span>' .$this->link($i, array('page' => $end), $options) . '</span>';			}			$out .= $after;			if ($last && $end <= $params['pageCount'] - (int)$last) {				if ($end + 1 == $params['pageCount']) {					$out .= $this->last($last, array('before' => $separator));				} else {					$out .= $this->last($last);				}			}		} else {			$out .= $before;			for ($i = 1; $i <= $params['pageCount']; $i++) {				if ($i == $params['page']) {					$out .= '<span class="current">' . $i . '</span>';				} else {					$out .= '<span>' .$this->link($i, array('page' => $i), $options) . '</span>';				}				if ($i != $params['pageCount']) {					$out .= $separator;				}			}			$out .= $after;		}		return $this->output($out);	}/** * Returns a first or set of numbers for the first pages * * @param  mixed $first if string use as label for the link, if numeric print page numbers * @param  mixed $options * @return string numbers string. */	function first($first = '<< first', $options = array()) {		$options = array_merge(			array(				'after'=> null,				'model' => $this->defaultModel(),				'separator' => ' | ',			),		(array)$options);		$params = array_merge(array('page'=> 1), (array)$this->params($options['model']));		unset($options['model']);		if ($params['pageCount'] <= 1) {			return false;		}		extract($options);		unset($options['after'], $options['model'], $options['separator']);		$out = '';		if (is_int($first) && $params['page'] > $first) {			if ($after === null) {				$after = '...';			}			for ($i = 1; $i <= $first; $i++) {				$out .= '<span>' . $this->link($i, array('page' => $i), $options) . '</span>';				if ($i != $first) {					$out .= $separator;				}			}			$out .= $after;		} elseif ($params['page'] > 1) {			$out = '<span>' . $this->link($first, array('page' => 1), $options) . '</span>' . $after;		}		return $out;	}/** * Returns a last or set of numbers for the last pages * * @param  mixed $last if string use as label for the link, if numeric print page numbers * @param  mixed $options * @return string numbers string. */	function last($last = 'last >>', $options = array()) {		$options = array_merge(			array(				'before'=> null,				'model' => $this->defaultModel(),				'separator' => ' | ',			),		(array)$options);		$params = array_merge(array('page'=> 1), (array)$this->params($options['model']));		unset($options['model']);		if ($params['pageCount'] <= 1) {			return false;		}		extract($options);		unset($options['before'], $options['model'], $options['separator']);		$out = '';		$lower = $params['pageCount'] - $last + 1;		if (is_int($last) && $params['page'] < $lower) {			if ($before === null) {				$before = '...';			}			for ($i = $lower; $i <= $params['pageCount']; $i++) {				$out .= '<span>' . $this->link($i, array('page' => $i), $options) . '</span>';				if ($i != $params['pageCount']) {					$out .= $separator;				}			}			$out = $before . $out;		} elseif ($params['page'] < $params['pageCount']) {			$out = $before . '<span>' . $this->link($last, array('page' => $params['pageCount']), $options) . '</span>';		}		return $out;	}}?>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?