summaryrefslogtreecommitdiffstats
path: root/inc/pagination.inc.php
blob: 65785a364939961efd08cdedbc4d792ea308e297 (plain) (blame)
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
<?php

/**
 * TODO: Why does this class exist?
 * There's already the Paginate class which works more efficient by using the LIMIT statement
 * for the query, and has more options. Consider refactoring the places where this class is
 * used (see syslog or eventlog for usage examples), then get rid of this one.
 */
class Pagination
{
	private $items;
	private $page;
	private $maxItems;

	public function __construct($par1, $par2)
	{
		$this->items = $par1;
		$this->page = $par2;

		$this->maxItems = 5;
	}

	public function getPagination()
	{
		$ret = array();
		$n = ceil(count($this->items) / $this->maxItems);
		for ($i = 1; $i <= $n; $i++) {
			$class = ($i == $this->page) ? 'active' : '';
			$ret[] = array(
				'class' => $class,
				'page' => $i
			);
		}
		return $ret;
	}

	public function getItems()
	{
		$ret = array();
		$first = ($this->page - 1) * $this->maxItems;
		for ($i = 0; $i < $this->maxItems; $i++) {
			if ($first + $i < count($this->items))
				$ret[] = $this->items[$first + $i];
		}
		return $ret;
	}
}