trunk /sally/core/lib/sly/Util/Pager.php

Language PHP Lines 322
MD5 Hash 3192635344e48afaa13ee452249bfe08 Estimated Cost $5,117 (why?)
Repository https://bitbucket.org/SallyCMS/trunk View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
<?php
/*
 * Copyright (c) 2012, webvariants GbR, http://www.webvariants.de
 *
 * This file is released under the terms of the MIT license. You can find the
 * complete text in the attached LICENSE file or online at:
 *
 * http://www.opensource.org/licenses/mit-license.php
 */

/**
 * @ingroup util
 */
class sly_Util_Pager {
	const FIRST_ACTIVE   = -1;  ///< int
	const FIRST_INACTIVE = -2;  ///< int
	const PREV_ACTIVE    = -3;  ///< int
	const PREV_INACTIVE  = -4;  ///< int
	const NEXT_ACTIVE    = -5;  ///< int
	const NEXT_INACTIVE  = -6;  ///< int
	const LAST_ACTIVE    = -7;  ///< int
	const LAST_INACTIVE  = -8;  ///< int
	const ELLIPSIS_LEFT  = -9;  ///< int
	const ELLIPSIS_RIGHT = -10; ///< int

	protected $currentPage;     ///< int
	protected $totalElements;   ///< int
	protected $perPage;         ///< int
	protected $maxLinks;        ///< int
	protected $linksLeftRight;  ///< int
	protected $linksOnEnd;      ///< int

	private $pages; ///< int

	/**
	 * @param int $currentPage
	 * @param int $totalElements
	 * @param int $perPage
	 * @param int $maxLinks
	 * @param int $linksLeftRight
	 * @param int $linksOnEnds
	 */
	public function __construct($currentPage, $totalElements, $perPage = 10, $maxLinks = 10, $linksLeftRight = 2, $linksOnEnds = 2) {
		$this->currentPage    = abs((int) $currentPage);
		$this->totalElements  = abs((int) $totalElements);
		$this->perPage        = abs((int) $perPage);
		$this->maxLinks       = abs((int) $maxLinks);
		$this->linksLeftRight = abs((int) $linksLeftRight);
		$this->linksOnEnd     = abs((int) $linksOnEnds);

		$this->pages = ceil($this->totalElements / $this->perPage);

		if ($this->currentPage > $this->pages-1) $this->currentPage = $this->pages - 1;
		if ($this->currentPage < 0) $this->currentPage = 0;
		if ($this->maxLinks < 5) $this->maxLinks = 5;
	}

	/**
	 * @return array
	 */
	public function getPaginationData() {
		$result = array();

		if ($this->currentPage > 0) {
			$result[] = self::FIRST_ACTIVE;
			$result[] = self::PREV_ACTIVE;
		}
		else {
			$result[] = self::FIRST_INACTIVE;
			$result[] = self::PREV_INACTIVE;
		}

		if ($this->pages <= $this->maxLinks) {
			for ($i = 0; $i < $this->pages; ++$i) $result[] = $i;
			if ($this->pages == 0) $result[] = 0;
		}
		else {
			// Links am Anfang

			$result[] = 0;
			for ($i = 0; $i < $this->linksOnEnd; ++$i) $result[] = $i+1;

			// Links um die aktuelle Seite herum

			$begin = $this->currentPage - $this->linksLeftRight;
			$end   = $this->currentPage + $this->linksLeftRight;

			if ($begin-1 > $this->linksOnEnd) $result[] = self::ELLIPSIS_LEFT;

			for ($i = $begin; $i <= $end; ++$i) {
				if ($i > 0 && $i < $this->pages) $result[] = $i;
			}

			if ($end < ($this->pages - $this->linksOnEnd - 2)) $result[] = self::ELLIPSIS_RIGHT;

			// Links am Ende

			for ($i = $this->linksOnEnd; $i > 0; --$i) $result[] = $this->pages - $i - 1;
			$result[] = $this->pages - 1;

			// Doppelte entfernen

			$result = array_unique($result);
		}

		if ($this->currentPage < $this->pages - 1) {
			$result[] = self::NEXT_ACTIVE;
			$result[] = self::LAST_ACTIVE;
		}
		else {
			$result[] = self::NEXT_INACTIVE;
			$result[] = self::LAST_INACTIVE;
		}

		return $result;
	}

	/**
	 * @param  array  $getParams
	 * @param  string $pageParamName
	 * @param  string $filename
	 * @param  array  $specialSymbols
	 * @return array
	 */
	public function getRawLinks($getParams = array(), $pageParamName = 'p', $filename = 'index.php', $specialSymbols = array()) {
		// Standardwerte f?r die Spezialsymbole

		if (!isset($specialSymbols['first_active']))   $specialSymbols['first_active']   = '|&laquo;';
		if (!isset($specialSymbols['first_inactive'])) $specialSymbols['first_inactive'] = '|&laquo;';
		if (!isset($specialSymbols['prev_active']))    $specialSymbols['prev_active']    = '&laquo;';
		if (!isset($specialSymbols['prev_inactive']))  $specialSymbols['prev_inactive']  = '&laquo;';
		if (!isset($specialSymbols['next_active']))    $specialSymbols['next_active']    = '&raquo;';
		if (!isset($specialSymbols['next_inactive']))  $specialSymbols['next_inactive']  = '&raquo;';
		if (!isset($specialSymbols['last_active']))    $specialSymbols['last_active']    = '&raquo;|';
		if (!isset($specialSymbols['last_inactive']))  $specialSymbols['last_inactive']  = '&raquo;|';
		if (!isset($specialSymbols['ellipsis']))       $specialSymbols['ellipsis']       = '&hellip;';

		// Auf geht's!

		$data = $this->getPaginationData();
		if (isset($getParams[$pageParamName])) unset($getParams[$pageParamName]);
		$links = array();

		foreach ($data as $pageCode) {
			$url     = '';
			$text    = '';
			$attribs = array();

			switch ($pageCode) {
				case self::FIRST_ACTIVE:
					$url     = $this->getURL($filename, $getParams, $pageParamName, 0);
					$text    = $specialSymbols['first_active'];
					$attribs = array('class' => 'first');
					break;

				case self::FIRST_INACTIVE:
					$url     = '';
					$text    = $specialSymbols['first_inactive'];
					$attribs = array('class' => 'first');
					break;

				case self::PREV_ACTIVE:
					$url     = $this->getURL($filename, $getParams, $pageParamName, $this->currentPage-1);
					$text    = $specialSymbols['prev_active'];
					$attribs = array('class' => 'prev');
					break;

				case self::PREV_INACTIVE:
					$url     = '';
					$text    = $specialSymbols['prev_inactive'];
					$attribs = array('class' => 'prev');
					break;

				case self::NEXT_ACTIVE:
					$url     = $this->getURL($filename, $getParams, $pageParamName, $this->currentPage+1);
					$text    = $specialSymbols['next_active'];
					$attribs = array('class' => 'next');
					break;

				case self::NEXT_INACTIVE:
					$url     = '';
					$text    = $specialSymbols['next_inactive'];
					$attribs = array('class' => 'next');
					break;

				case self::LAST_ACTIVE:
					$url     = $this->getURL($filename, $getParams, $pageParamName, $this->pages - 1);
					$text    = $specialSymbols['last_active'];
					$attribs = array('class' => 'last');
					break;

				case self::LAST_INACTIVE:
					$url     = '';
					$text    = $specialSymbols['last_inactive'];
					$attribs = array('class' => 'last');
					break;

				case self::ELLIPSIS_LEFT:
				case self::ELLIPSIS_RIGHT:
					$url     = '';
					$text    = $specialSymbols['ellipsis'];
					$attribs = array('class' => 'ellipsis');
					break;

				case $this->currentPage:
					$url     = '';
					$text    = $pageCode + 1;
					$attribs = array('class' => 'active page'.($this->currentPage + 1));
					break;

				default:
					$url       = $this->getURL($filename, $getParams, $pageParamName, $pageCode);
					$text      = $pageCode + 1;
					$direction = $pageCode < $this->currentPage ? 'before' : 'after';
					$attribs   = array('class' => 'normal page'.($pageCode+1).' '.$direction);
					break;
			}

			if (mb_strlen($text) > 0) {
				$links[] = array(
					'url'        => $url,
					'text'       => $text,
					'attributes' => $attribs
				);
			}
		}

		return $links;
	}

	/**
	 * @param  array  $getParams
	 * @param  string $pageParamName
	 * @param  string $filename
	 * @param  array  $specialSymbols
	 * @return string
	 */
	public function getHTMLString($getParams = array(), $pageParamName = 'p', $filename = 'index.php', $specialSymbols = array()) {
		$links = $this->getRawLinks($getParams, $pageParamName, $filename, $specialSymbols);

		foreach ($links as $idx => $data) {
			if (!empty($data['url'])) {
				$data['attributes']['href'] = $data['url'];
			}

			foreach ($data['attributes'] as $name => $value) {
				$data['attributes'][$name] = $name.'="'.$value.'"';
			}

			$attributes  = implode(' ', $data['attributes']);
			$attributes  = empty($attributes) ? '' : " $attributes";
			$tagName     = empty($data['url']) ? 'span' : 'a';
			$links[$idx] = '<'.$tagName.$attributes.'>'.$data['text'].'</'.$tagName.'>';
		}

		return "\n".implode("\n", $links)."\n";
	}

	/**
	 * @param  string $tag
	 * @param  array  $getParams
	 * @param  string $pageParamName
	 * @param  string $filename
	 * @param  array  $specialSymbols
	 * @return string
	 */
	public function getHTMLList($tag = 'ul', $getParams = array(), $pageParamName = 'p', $filename = 'index.php', $specialSymbols = array()) {
		$links  = $this->getRawLinks($getParams, $pageParamName, $filename, $specialSymbols);
		$result = "\n<$tag class=\"pager\">";

		foreach ($links as $idx => $data) {
			foreach ($data['attributes'] as $name => $value) {
				if ($name == 'class' && $value == 'first') unset($data['attributes'][$name]);
				else $data['attributes'][$name] = $name.'="'.$value.'"';
			}

			$attributes = implode(' ', $data['attributes']);
			$attributes = empty($attributes) ? '' : " $attributes";
			$link       = empty($data['url']) ? '<span>'.$data['text'].'</span>' : '<a href="'.$data['url'].'">'.$data['text'].'</a>';
			$result    .= "\n<li$attributes>$link</li>";
		}

		$result .= "\n</$tag>\n";
		return $result;
	}

	/**
	 * @return array
	 */
	public function getCurrentElements() {
		$elements = array();
		$base     = $this->currentPage * $this->perPage;
		for ($i = 0; $i < $this->perPage; ++$i) {
			$elements[] = $base + $i;
		}
		return $elements;
	}

	/**
	 * @param  string $filename
	 * @param  array  $getParams
	 * @param  string $pageParamName
	 * @param  int    $page
	 * @return string
	 */
	protected function getURL($filename, $getParams = array(), $pageParamName = 'p', $page = 0) {
		$link = $filename;
		if ($page > 0) $getParams[$pageParamName] = $page;
		$getString = http_build_query($getParams, '', '&amp;');
		if (!empty($getString)) $link .= '?'.$getString;
		return $link;
	}

	/**
	 * @param  int $code
	 * @return boolean
	 */
	public static function isEllipsis($code) {
		return $code == self::ELLIPSIS_LEFT || $code == self::ELLIPSIS_RIGHT;
	}
}
Back to Top