1 | <?php |
---|
2 | |
---|
3 | /* ----------------------------------------------------------------------------- |
---|
4 | class name: pages_navigation |
---|
5 | class version: 1.0 |
---|
6 | date: 2007-11-17 |
---|
7 | ------------------------------------------------------------------------------ |
---|
8 | author: grum at grum.dnsalias.com |
---|
9 | << May the Little SpaceFrog be with you >> |
---|
10 | ------------------------------------------------------------------------------ |
---|
11 | |
---|
12 | this classes provides base functions to manage pages navigation |
---|
13 | |
---|
14 | - constructor pages_navigation($url) |
---|
15 | - (public) function set_nb_items($nbitems) |
---|
16 | - (public) function get_nb_items() |
---|
17 | - (public) function set_nb_items_per_page($nbitems) |
---|
18 | - (public) function get_nb_items_per_page() |
---|
19 | - (public) function get_nb_pages() |
---|
20 | - (public) function set_current_page($page) |
---|
21 | - (public) function get_current_page() |
---|
22 | - (public) function set_base_url($url) |
---|
23 | - (public) function get_base_url() |
---|
24 | - (public) function make_navigation() |
---|
25 | - (public) function make_navigation_function() |
---|
26 | - (private) function calc_nb_pages() |
---|
27 | ---------------------------------------------------------------------- */ |
---|
28 | class pages_navigation |
---|
29 | { |
---|
30 | var $nbitems; |
---|
31 | var $nbitemsperpages; |
---|
32 | var $nbpages; |
---|
33 | var $currentpage; |
---|
34 | var $baseurl; |
---|
35 | var $pagevarurl; |
---|
36 | var $options; |
---|
37 | |
---|
38 | function pages_navigation() |
---|
39 | { |
---|
40 | $this->nbitems=0; |
---|
41 | $this->nbitemsperpages=0; |
---|
42 | $this->nbpages=0; |
---|
43 | $this->currentpage=0; |
---|
44 | $this->baseurl=''; |
---|
45 | $this->pagevarurl=''; |
---|
46 | $this->options=array( |
---|
47 | 'prev_next' => true, |
---|
48 | 'first_last' => true, |
---|
49 | 'display_all' => true, |
---|
50 | 'number_displayed' => 2 //number of page displayed before and after current page |
---|
51 | ); |
---|
52 | } |
---|
53 | |
---|
54 | /* |
---|
55 | define value for total number of items |
---|
56 | */ |
---|
57 | function set_nb_items($nbitems) |
---|
58 | { |
---|
59 | if($nbitems!=$this->nbitems) |
---|
60 | { |
---|
61 | $this->nbitems=$nbitems; |
---|
62 | $this->calc_nb_pages(); |
---|
63 | } |
---|
64 | return($nbitems); |
---|
65 | } |
---|
66 | |
---|
67 | function get_nb_items() |
---|
68 | { |
---|
69 | return($nbitems); |
---|
70 | } |
---|
71 | |
---|
72 | /* |
---|
73 | define value for number of items displayed per pages |
---|
74 | */ |
---|
75 | function set_nb_items_per_page($nbitems) |
---|
76 | { |
---|
77 | if(($nbitems!=$this->nbitemsperpages)&&($nbitems>0)) |
---|
78 | { |
---|
79 | $this->nbitemsperpages=$nbitems; |
---|
80 | $this->calc_nb_pages(); |
---|
81 | } |
---|
82 | return($this->nbitemsperpages); |
---|
83 | } |
---|
84 | |
---|
85 | function get_nb_items_per_page() |
---|
86 | { |
---|
87 | return($this->nbitemsperpages); |
---|
88 | } |
---|
89 | |
---|
90 | /* |
---|
91 | return numbers of pages |
---|
92 | */ |
---|
93 | function get_nb_pages() |
---|
94 | { |
---|
95 | return($this->nbpages); |
---|
96 | } |
---|
97 | |
---|
98 | /* |
---|
99 | define the current page number |
---|
100 | */ |
---|
101 | function set_current_page($page) |
---|
102 | { |
---|
103 | if(($page!=$this->currentpage)&&($page<=$this->nbpages)&&($page>0)) |
---|
104 | { |
---|
105 | $this->currentpage=$page; |
---|
106 | } |
---|
107 | return($this->currentpage); |
---|
108 | } |
---|
109 | |
---|
110 | /* |
---|
111 | returns the current page number |
---|
112 | */ |
---|
113 | function get_current_page() |
---|
114 | { |
---|
115 | return($this->currentpage); |
---|
116 | } |
---|
117 | |
---|
118 | /* |
---|
119 | define the value for url |
---|
120 | ex: "http://mysite.com/admin.php?var1=xxx&var2=xxx" |
---|
121 | */ |
---|
122 | function set_base_url($url) |
---|
123 | { |
---|
124 | if($url!=$this->baseurl) |
---|
125 | { |
---|
126 | $this->baseurl=$url; |
---|
127 | } |
---|
128 | return($this->baseurl); |
---|
129 | } |
---|
130 | |
---|
131 | function get_base_url() |
---|
132 | { |
---|
133 | return($this->baseurl); |
---|
134 | } |
---|
135 | |
---|
136 | /* |
---|
137 | define the value for variables's name |
---|
138 | ex: url = "http://mysite.com/admin.php?var1=xxx&var2=xxx" |
---|
139 | pagevar = "pagenumber" |
---|
140 | url made is "http://mysite.com/admin.php?var1=xxx&var2=xxx&pagenumber=xxx" |
---|
141 | */ |
---|
142 | function set_pagevar_url($var) |
---|
143 | { |
---|
144 | if($var!=$this->pagevarurl) |
---|
145 | { |
---|
146 | $this->pagevarurl=$var; |
---|
147 | } |
---|
148 | return($this->pagevarurl); |
---|
149 | } |
---|
150 | |
---|
151 | function get_pagevar_url() |
---|
152 | { |
---|
153 | return($this->pagevarurl); |
---|
154 | } |
---|
155 | |
---|
156 | |
---|
157 | /* |
---|
158 | returns an html formatted string |
---|
159 | */ |
---|
160 | function make_navigation($functionname='') |
---|
161 | { |
---|
162 | $text=''; |
---|
163 | if(($this->options['display_all'])||($this->options['number_displayed']>=$this->nbpages)) |
---|
164 | { |
---|
165 | for($i=1;$i<=$this->nbpages;$i++) |
---|
166 | { |
---|
167 | if($i!=$this->currentpage) |
---|
168 | { |
---|
169 | if($functionname=='') |
---|
170 | { |
---|
171 | $text.='<a href="'.$this->baseurl.'&'.$this->pagevarurl.'='.$i.'">'.$i.'</a> '; |
---|
172 | } |
---|
173 | else |
---|
174 | { |
---|
175 | $text.='<a style="cursor:pointer;" onclick="'.$functionname.'('.$i.');">'.$i.'</a> '; |
---|
176 | } |
---|
177 | } |
---|
178 | else |
---|
179 | { |
---|
180 | $text.=$i.' '; |
---|
181 | } |
---|
182 | } |
---|
183 | } |
---|
184 | else |
---|
185 | { |
---|
186 | for($i=$this->currentpage-$this->options['number_displayed'];$i<=$this->currentpage+$this->options['number_displayed'];$i++) |
---|
187 | { |
---|
188 | if(($i>0)&&($i<=$this->nbpages)) |
---|
189 | { |
---|
190 | if($i!=$this->currentpage) |
---|
191 | { |
---|
192 | if($functionname=='') |
---|
193 | { |
---|
194 | $text.='<a href="'.$this->baseurl.'&'.$this->pagevarurl.'='.$i.'">'.$i.'</a> '; |
---|
195 | } |
---|
196 | else |
---|
197 | { |
---|
198 | $text.='<a style="cursor:pointer;" onclick="'.$functionname.'('.$i.');">'.$i.'</a> '; |
---|
199 | } |
---|
200 | } |
---|
201 | else |
---|
202 | { |
---|
203 | $text.=$i.' '; |
---|
204 | } |
---|
205 | } |
---|
206 | } |
---|
207 | if($this->currentpage-$this->options['number_displayed']>0) |
---|
208 | { |
---|
209 | $text=' ... '.$text; |
---|
210 | } |
---|
211 | if($this->currentpage+$this->options['number_displayed']<$this->nbpages) |
---|
212 | { |
---|
213 | $text.=' ... '; |
---|
214 | } |
---|
215 | } |
---|
216 | |
---|
217 | if($this->options['prev_next']) |
---|
218 | { |
---|
219 | $prevp=''; |
---|
220 | $nextp=''; |
---|
221 | if($this->currentpage>1) |
---|
222 | { |
---|
223 | if($functionname=='') |
---|
224 | { |
---|
225 | $prevp='<a href="'.$this->baseurl.'&'.$this->pagevarurl.'='.($this->currentpage-1).'"> Prev </a>'; |
---|
226 | } |
---|
227 | else |
---|
228 | { |
---|
229 | $prevp='<a style="cursor:pointer;" onclick="'.$functionname.'('.($this->currentpage-1).');"> Prev </a>'; |
---|
230 | } |
---|
231 | } |
---|
232 | if($this->currentpage<$this->nbpages) |
---|
233 | { |
---|
234 | if($functionname=='') |
---|
235 | { |
---|
236 | $nextp='<a href="'.$this->baseurl.'&'.$this->pagevarurl.'='.($this->currentpage+1).'"> Next </a>'; |
---|
237 | } |
---|
238 | else |
---|
239 | { |
---|
240 | $nextp='<a style="cursor:pointer;" onclick="'.$functionname.'('.($this->currentpage+1).');"> Next </a>'; |
---|
241 | } |
---|
242 | } |
---|
243 | |
---|
244 | $text=$prevp.$text.$nextp; |
---|
245 | } |
---|
246 | |
---|
247 | if($this->options['first_last']) |
---|
248 | { |
---|
249 | $firstp=''; |
---|
250 | $lastp=''; |
---|
251 | if($this->currentpage>1) |
---|
252 | { |
---|
253 | if($functionname=='') |
---|
254 | { |
---|
255 | $firstp='<a href="'.$this->baseurl.'&'.$this->pagevarurl.'=1"> First </a>'; |
---|
256 | } |
---|
257 | else |
---|
258 | { |
---|
259 | $firstp='<a style="cursor:pointer;" onclick="'.$functionname.'(1);"> First </a>'; |
---|
260 | } |
---|
261 | } |
---|
262 | if($this->currentpage<$this->nbpages) |
---|
263 | { |
---|
264 | if($functionname=='') |
---|
265 | { |
---|
266 | $lastp='<a href="'.$this->baseurl.'&'.$this->pagevarurl.'='.$this->nbpages.'"> Last </a>'; |
---|
267 | } |
---|
268 | else |
---|
269 | { |
---|
270 | $lastp='<a style="cursor:pointer;" onclick="'.$functionname.'('.$this->nbpages.');"> Last </a>'; |
---|
271 | } |
---|
272 | } |
---|
273 | |
---|
274 | $text=$firstp.$text.$lastp; |
---|
275 | } |
---|
276 | |
---|
277 | return($text); |
---|
278 | } |
---|
279 | |
---|
280 | |
---|
281 | /* |
---|
282 | calculate the number of pages... |
---|
283 | */ |
---|
284 | function calc_nb_pages() |
---|
285 | { |
---|
286 | if($this->nbitemsperpages>0) |
---|
287 | { |
---|
288 | $this->nbpages=ceil($this->nbitems/$this->nbitemsperpages); |
---|
289 | } |
---|
290 | } |
---|
291 | |
---|
292 | } //class |
---|
293 | |
---|
294 | ?> |
---|