source: extensions/grum_plugins_classes-2/pages_navigation.class.inc.php @ 14974

Last change on this file since 14974 was 3395, checked in by grum, 15 years ago

Add plugin Grum Plugins Class-2

  • Property svn:executable set to *
File size: 7.8 KB
Line 
1<?php
2
3/* -----------------------------------------------------------------------------
4  class name: pages_navigation
5  class version: 1.1
6  date: 2009-04-26
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 set_options($var)
25    - (public) function get_options()
26    - (public) function make_navigation()
27    - (public) function make_navigation_function()
28    - (private) function calc_nb_pages()
29   ---------------------------------------------------------------------- */
30class pages_navigation
31{
32  protected $nbitems;
33  protected $nbitemsperpages;
34  protected $nbpages;
35  protected $currentpage;
36  protected $baseurl;
37  protected $pagevarurl;
38  protected $options;
39
40  public function pages_navigation()
41  {
42    $this->nbitems=0;
43    $this->nbitemsperpages=0;
44    $this->nbpages=0;
45    $this->currentpage=0;
46    $this->baseurl='';
47    $this->pagevarurl='';
48    $this->options=array(
49      'prev_next' => true,
50      'first_last' => true,
51      'display_all' => true,
52      'number_displayed' => 2, //number of page displayed before and after current page
53      'text_prev' => "&lt;",
54      'text_first' => "&lt;&lt;",
55      'text_next' => "&gt;",
56      'text_last' => "&gt;&gt;",
57    );
58  }
59
60  /*
61    define value for total number of items
62  */
63  public function set_nb_items($nbitems)
64  {
65    if($nbitems!=$this->nbitems)
66    {
67      $this->nbitems=$nbitems;
68      $this->calc_nb_pages();
69    }
70    return($nbitems);
71  }
72
73  public function get_nb_items()
74  {
75    return($nbitems);
76  }
77
78  /*
79    define value for number of items displayed per pages
80  */
81  public function set_nb_items_per_page($nbitems)
82  {
83    if(($nbitems!=$this->nbitemsperpages)&&($nbitems>0))
84    {
85      $this->nbitemsperpages=$nbitems;
86      $this->calc_nb_pages();
87    }
88    return($this->nbitemsperpages);
89  }
90
91  public function get_nb_items_per_page()
92  {
93    return($this->nbitemsperpages);
94  }
95
96  /*
97    return numbers of pages
98  */
99  public function get_nb_pages()
100  {
101    return($this->nbpages);
102  }
103
104  /*
105    define the current page number
106  */
107  public function set_current_page($page)
108  {
109    if(($page!=$this->currentpage)&&($page<=$this->nbpages)&&($page>0))
110    {
111      $this->currentpage=$page;
112    }
113    return($this->currentpage);
114  }
115
116  /*
117    returns the current page number
118  */
119  public function get_current_page()
120  {
121    return($this->currentpage);
122  }
123
124  /*
125    define the value for url
126    ex: "http://mysite.com/admin.php?var1=xxx&var2=xxx"
127  */
128  public function set_base_url($url)
129  {
130    if($url!=$this->baseurl)
131    {
132      $this->baseurl=$url;
133    }
134    return($this->baseurl);
135  }
136
137  public function get_base_url()
138  {
139    return($this->baseurl);
140  }
141
142  /*
143    define the value for variables's name
144    ex: url = "http://mysite.com/admin.php?var1=xxx&var2=xxx"
145        pagevar = "pagenumber"
146    url made is "http://mysite.com/admin.php?var1=xxx&var2=xxx&pagenumber=xxx"
147  */
148  public function set_pagevar_url($var)
149  {
150    if($var!=$this->pagevarurl)
151    {
152      $this->pagevarurl=$var;
153    }
154    return($this->pagevarurl);
155  }
156
157  public function get_pagevar_url()
158  {
159    return($this->pagevarurl);
160  }
161
162  /*
163    define the navigation bar options
164  */
165  public function set_options($var)
166  {
167    if(is_array($var))
168    {
169      foreach($this->options as $key=>$val)
170      {
171        if(isset($var[$key]))
172        {
173          $this->options[$key]=$var[$key];
174        }
175      }
176    }
177    return($this->options);
178  }
179
180  public function get_options()
181  {
182    return($this->options);
183  }
184
185
186  /*
187    returns an html formatted string
188  */
189  public function make_navigation($functionname='')
190  {
191    $text='';
192    if(($this->options['display_all'])||($this->options['number_displayed']>=$this->nbpages))
193    {
194      for($i=1;$i<=$this->nbpages;$i++)
195      {
196        if($i!=$this->currentpage)
197        {
198          if($functionname=='')
199          {
200            $text.='<a href="'.$this->baseurl.'&'.$this->pagevarurl.'='.$i.'">'.$i.'</a>&nbsp;';
201          }
202          else
203          {
204            $text.='<a style="cursor:pointer;" onclick="'.$functionname.'('.$i.');">'.$i.'</a>&nbsp;';
205          }
206        }
207        else
208        {
209          $text.=$i.'&nbsp;';
210        }
211      }
212    }
213    else
214    {
215      for($i=$this->currentpage-$this->options['number_displayed'];$i<=$this->currentpage+$this->options['number_displayed'];$i++)
216      {
217        if(($i>0)&&($i<=$this->nbpages))
218        {
219          if($i!=$this->currentpage)
220          {
221            if($functionname=='')
222            {
223              $text.='<a href="'.$this->baseurl.'&'.$this->pagevarurl.'='.$i.'">'.$i.'</a>&nbsp;';
224            }
225            else
226            {
227              $text.='<a style="cursor:pointer;" onclick="'.$functionname.'('.$i.');">'.$i.'</a>&nbsp;';
228            }
229          }
230          else
231          {
232            $text.=$i.'&nbsp;';
233          }
234        }
235      }
236      if($this->currentpage-$this->options['number_displayed']>0)
237      {
238        $text='&nbsp;...&nbsp;'.$text;
239      }
240      if($this->currentpage+$this->options['number_displayed']<$this->nbpages)
241      {
242        $text.='&nbsp;...&nbsp;';
243      }
244    }
245
246    if($this->options['prev_next'])
247    {
248      $prevp='';
249      $nextp='';
250      if($this->currentpage>1)
251      {
252        if($functionname=='')
253        {
254          $prevp='<a href="'.$this->baseurl.'&'.$this->pagevarurl.'='.($this->currentpage-1).'">&nbsp;'.$this->options['text_prev'].'&nbsp;</a>';
255        }
256        else
257        {
258          $prevp='<a style="cursor:pointer;" onclick="'.$functionname.'('.($this->currentpage-1).');">&nbsp;'.$this->options['text_prev'].'&nbsp;</a>';
259        }
260      }
261      if($this->currentpage<$this->nbpages)
262      {
263        if($functionname=='')
264        {
265          $nextp='<a href="'.$this->baseurl.'&'.$this->pagevarurl.'='.($this->currentpage+1).'">&nbsp;'.$this->options['text_next'].'&nbsp;</a>';
266        }
267        else
268        {
269          $nextp='<a style="cursor:pointer;" onclick="'.$functionname.'('.($this->currentpage+1).');">&nbsp;'.$this->options['text_next'].'&nbsp;</a>';
270        }
271      }
272
273      $text=$prevp.$text.$nextp;
274    }
275
276    if($this->options['first_last'])
277    {
278      $firstp='';
279      $lastp='';
280      if($this->currentpage>1)
281      {
282        if($functionname=='')
283        {
284          $firstp='<a href="'.$this->baseurl.'&'.$this->pagevarurl.'=1">&nbsp;'.$this->options['text_first'].'&nbsp;</a>';
285        }
286        else
287        {
288          $firstp='<a style="cursor:pointer;" onclick="'.$functionname.'(1);">&nbsp;'.$this->options['text_first'].'&nbsp;</a>';
289        }
290      }
291      if($this->currentpage<$this->nbpages)
292      {
293        if($functionname=='')
294        {
295          $lastp='<a href="'.$this->baseurl.'&'.$this->pagevarurl.'='.$this->nbpages.'">&nbsp;'.$this->options['text_last'].'&nbsp;</a>';
296        }
297        else
298        {
299          $lastp='<a style="cursor:pointer;" onclick="'.$functionname.'('.$this->nbpages.');">&nbsp;'.$this->options['text_last'].'&nbsp;</a>';
300        }
301      }
302
303      $text=$firstp.$text.$lastp;
304    }
305
306    return($text);
307  }
308
309
310  /*
311    calculate the number of pages...
312  */
313  public function calc_nb_pages()
314  {
315    if($this->nbitemsperpages>0)
316    {
317      $this->nbpages=ceil($this->nbitems/$this->nbitemsperpages);
318    }
319  }
320
321} //class
322
323?>
Note: See TracBrowser for help on using the repository browser.