aboutsummaryrefslogtreecommitdiff
path: root/lib/Minz/Paginator.php
blob: 72eaafec19826323753e30a58f6335676e060dff (plain)
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
<?php
/**
 * MINZ - Copyright 2011 Marien Fressinaud
 * Sous licence AGPL3 <http://www.gnu.org/licenses/>
*/

/**
 * La classe Paginator permet de gérer la pagination de l'application facilement
 */
class Minz_Paginator {
	/**
	 * $items tableau des éléments à afficher/gérer
	 */
	private $items = array ();

	/**
	 * $nbItemsPerPage le nombre d'éléments par page
	 */
	private $nbItemsPerPage = 10;

	/**
	 * $currentPage page actuelle à gérer
	 */
	private $currentPage = 1;

	/**
	 * $nbPage le nombre de pages de pagination
	 */
	private $nbPage = 1;

	/**
	 * $nbItems le nombre d'éléments
	 */
	private $nbItems = 0;

	/**
	 * Constructeur
	 * @param $items les éléments à gérer
	 */
	public function __construct ($items) {
		$this->_items ($items);
		$this->_nbItems (count ($this->items (true)));
		$this->_nbItemsPerPage ($this->nbItemsPerPage);
		$this->_currentPage ($this->currentPage);
	}

	/**
	 * Permet d'afficher la pagination
	 * @param $view nom du fichier de vue situé dans /app/views/helpers/
	 * @param int $getteur variable de type $_GET[] permettant de retrouver la page
	 */
	public function render ($view, $getteur) {
		$view = APP_PATH . '/views/helpers/'.$view;

		if (file_exists ($view)) {
			include ($view);
		}
	}

	/**
	 * Permet de retrouver la page d'un élément donné
	 * @param $item l'élément à retrouver
	 * @return float|false la page à laquelle se trouve l’élément, false si non trouvé
	 */
	public function pageByItem ($item) {
		$i = 0;

		do {
			if ($item == $this->items[$i]) {
				return ceil(($i + 1) / $this->nbItemsPerPage);
			}
			$i++;
		} while ($i < $this->nbItems());

		return false;
	}

	/**
	 * Permet de retrouver la position d'un élément donné (à partir de 0)
	 * @param $item l'élément à retrouver
	 * @return float|false la position à laquelle se trouve l’élément, false si non trouvé
	 */
	public function positionByItem ($item) {
		$i = 0;

		do {
			if ($item == $this->items[$i]) {
				return $i;
			}
			$i++;
		} while ($i < $this->nbItems ());

		return false;
	}

	/**
	 * Permet de récupérer un item par sa position
	 * @param $pos la position de l'élément
	 * @return mixed item situé à $pos (dernier item si $pos<0, 1er si $pos>=count($items))
	 */
	public function itemByPosition ($pos) {
		if ($pos < 0) {
			$pos = $this->nbItems () - 1;
		}
		if ($pos >= count($this->items)) {
			$pos = 0;
		}

		return $this->items[$pos];
	}

	/**
	 * GETTEURS
	 */
	/**
	 * @param $all si à true, retourne tous les éléments sans prendre en compte la pagination
	 */
	public function items ($all = false) {
		$array = array ();
		$nbItems = $this->nbItems ();

		if ($nbItems <= $this->nbItemsPerPage || $all) {
			$array = $this->items;
		} else {
			$begin = ($this->currentPage - 1) * $this->nbItemsPerPage;
			$counter = 0;
			$i = 0;

			foreach ($this->items as $key => $item) {
				if ($i >= $begin) {
					$array[$key] = $item;
					$counter++;
				}
				if ($counter >= $this->nbItemsPerPage) {
					break;
				}
				$i++;
			}
		}

		return $array;
	}
	public function nbItemsPerPage () {
		return $this->nbItemsPerPage;
	}
	public function currentPage () {
		return $this->currentPage;
	}
	public function nbPage () {
		return $this->nbPage;
	}
	public function nbItems () {
		return $this->nbItems;
	}

	/**
	 * SETTEURS
	 */
	public function _items ($items) {
		if (is_array ($items)) {
			$this->items = $items;
		}

		$this->_nbPage ();
	}
	public function _nbItemsPerPage ($nbItemsPerPage) {
		if ($nbItemsPerPage > $this->nbItems ()) {
			$nbItemsPerPage = $this->nbItems ();
		}
		if ($nbItemsPerPage < 0) {
			$nbItemsPerPage = 0;
		}

		$this->nbItemsPerPage = $nbItemsPerPage;
		$this->_nbPage ();
	}
	public function _currentPage ($page) {
		if($page < 1 || ($page > $this->nbPage && $this->nbPage > 0)) {
			throw new Minz_CurrentPagePaginationException($page);
		}

		$this->currentPage = $page;
	}
	private function _nbPage () {
		if ($this->nbItemsPerPage > 0) {
			$this->nbPage = ceil ($this->nbItems () / $this->nbItemsPerPage);
		}
	}
	public function _nbItems ($value) {
		$this->nbItems = $value;
	}
}