source: extensions/GrumPluginClasses/classes/GPCPagesNavigation.class.inc.php @ 5550

Last change on this file since 5550 was 5550, checked in by grum, 14 years ago

Release 3.0.0 : the plugin has been completely rewritten

  • Property svn:executable set to *
File size: 8.3 KB
Line 
1<?php
2
3/* -----------------------------------------------------------------------------
4  class name     : GPCPagesNavigation
5  class version  : 2.0.0
6  plugin version : 3.0.0
7  date           : 2010-03-30
8  ------------------------------------------------------------------------------
9  author: grum at piwigo.org
10  << May the Little SpaceFrog be with you >>
11  ------------------------------------------------------------------------------
12
13  :: HISTORY
14
15| release | date       |
16| 2.0.0   | 2010/03/30 | * Update class & function names
17|         |            |
18|         |            |
19|         |            |
20|         |            |
21|         |            |
22|         |            |
23
24  ------------------------------------------------------------------------------
25
26   this classes provides base functions to manage pages navigation
27
28    - constructor GPCPagesNavigation()
29    - (public) function setNbItems($nbitems)
30    - (public) function getNbItems()
31    - (public) function setNbItemsPerPage($nbitems)
32    - (public) function getNbItemsPerPage()
33    - (public) function getNbPages()
34    - (public) function setCurrentPage($page)
35    - (public) function getCurrentPage()
36    - (public) function setBaseUrl($url)
37    - (public) function getBaseUrl()
38    - (public) function setOptions($var)
39    - (public) function getOptions()
40    - (public) function makeNavigation()
41    - (private) function calcNbPages()
42   ---------------------------------------------------------------------- */
43class GPCPagesNavigation
44{
45  protected $nbitems;
46  protected $nbitemsperpages;
47  protected $nbpages;
48  protected $currentpage;
49  protected $baseurl;
50  protected $pagevarurl;
51  protected $options;
52
53  public function __construct()
54  {
55    $this->nbitems=0;
56    $this->nbitemsperpages=0;
57    $this->nbpages=0;
58    $this->currentpage=0;
59    $this->baseurl='';
60    $this->pagevarurl='';
61    $this->options=array(
62      'prev_next' => true,
63      'first_last' => true,
64      'display_all' => true,
65      'number_displayed' => 2, //number of page displayed before and after current page
66      'text_prev' => "&lt;",
67      'text_first' => "&lt;&lt;",
68      'text_next' => "&gt;",
69      'text_last' => "&gt;&gt;",
70    );
71  }
72
73  public function __destruct()
74  {
75    unset($this->nbitems);
76    unset($this->nbitemsperpages);
77    unset($this->nbpages);
78    unset($this->currentpage);
79    unset($this->baseurl);
80    unset($this->pagevarurl);
81    unset($this->options);
82  }
83
84  /*
85    define value for total number of items
86  */
87  public function setNbItems($nbitems)
88  {
89    if($nbitems!=$this->nbitems)
90    {
91      $this->nbitems=$nbitems;
92      $this->calcNbPages();
93    }
94    return($nbitems);
95  }
96
97  public function getNbItems()
98  {
99    return($nbitems);
100  }
101
102  /*
103    define value for number of items displayed per pages
104  */
105  public function setNbItemsPerPage($nbitems)
106  {
107    if(($nbitems!=$this->nbitemsperpages)&&($nbitems>0))
108    {
109      $this->nbitemsperpages=$nbitems;
110      $this->calcNbPages();
111    }
112    return($this->nbitemsperpages);
113  }
114
115  public function getNbItemsPerPage()
116  {
117    return($this->nbitemsperpages);
118  }
119
120  /*
121    return numbers of pages
122  */
123  public function getNbPages()
124  {
125    return($this->nbpages);
126  }
127
128  /*
129    define the current page number
130  */
131  public function setCurrentPage($page)
132  {
133    if(($page!=$this->currentpage)&&($page<=$this->nbpages)&&($page>0))
134    {
135      $this->currentpage=$page;
136    }
137    return($this->currentpage);
138  }
139
140  /*
141    returns the current page number
142  */
143  public function getCurrentPage()
144  {
145    return($this->currentpage);
146  }
147
148  /*
149    define the value for url
150    ex: "http://mysite.com/admin.php?var1=xxx&var2=xxx"
151  */
152  public function setBaseUrl($url)
153  {
154    if($url!=$this->baseurl)
155    {
156      $this->baseurl=$url;
157    }
158    return($this->baseurl);
159  }
160
161  public function getBaseUrl()
162  {
163    return($this->baseurl);
164  }
165
166  /*
167    define the value for variables's name
168    ex: url = "http://mysite.com/admin.php?var1=xxx&var2=xxx"
169        pagevar = "pagenumber"
170    url made is "http://mysite.com/admin.php?var1=xxx&var2=xxx&pagenumber=xxx"
171  */
172  public function setPageVarUrl($var)
173  {
174    if($var!=$this->pagevarurl)
175    {
176      $this->pagevarurl=$var;
177    }
178    return($this->pagevarurl);
179  }
180
181  public function getPageVarUrl()
182  {
183    return($this->pagevarurl);
184  }
185
186  /*
187    define the navigation bar options
188  */
189  public function setOptions($var)
190  {
191    if(is_array($var))
192    {
193      foreach($this->options as $key=>$val)
194      {
195        if(isset($var[$key]))
196        {
197          $this->options[$key]=$var[$key];
198        }
199      }
200    }
201    return($this->options);
202  }
203
204  public function getOptions()
205  {
206    return($this->options);
207  }
208
209
210  /*
211    returns an html formatted string
212  */
213  public function makeNavigation($functionname='')
214  {
215    $text='';
216    if(($this->options['display_all'])||($this->options['number_displayed']>=$this->nbpages))
217    {
218      for($i=1;$i<=$this->nbpages;$i++)
219      {
220        if($i!=$this->currentpage)
221        {
222          if($functionname=='')
223          {
224            $text.='<a href="'.$this->baseurl.'&'.$this->pagevarurl.'='.$i.'">'.$i.'</a>&nbsp;';
225          }
226          else
227          {
228            $text.='<a style="cursor:pointer;" onclick="'.$functionname.'('.$i.');">'.$i.'</a>&nbsp;';
229          }
230        }
231        else
232        {
233          $text.=$i.'&nbsp;';
234        }
235      }
236    }
237    else
238    {
239      for($i=$this->currentpage-$this->options['number_displayed'];$i<=$this->currentpage+$this->options['number_displayed'];$i++)
240      {
241        if(($i>0)&&($i<=$this->nbpages))
242        {
243          if($i!=$this->currentpage)
244          {
245            if($functionname=='')
246            {
247              $text.='<a href="'.$this->baseurl.'&'.$this->pagevarurl.'='.$i.'">'.$i.'</a>&nbsp;';
248            }
249            else
250            {
251              $text.='<a style="cursor:pointer;" onclick="'.$functionname.'('.$i.');">'.$i.'</a>&nbsp;';
252            }
253          }
254          else
255          {
256            $text.=$i.'&nbsp;';
257          }
258        }
259      }
260      if($this->currentpage-$this->options['number_displayed']>0)
261      {
262        $text='&nbsp;...&nbsp;'.$text;
263      }
264      if($this->currentpage+$this->options['number_displayed']<$this->nbpages)
265      {
266        $text.='&nbsp;...&nbsp;';
267      }
268    }
269
270    if($this->options['prev_next'])
271    {
272      $prevp='';
273      $nextp='';
274      if($this->currentpage>1)
275      {
276        if($functionname=='')
277        {
278          $prevp='<a href="'.$this->baseurl.'&'.$this->pagevarurl.'='.($this->currentpage-1).'">&nbsp;'.$this->options['text_prev'].'&nbsp;</a>';
279        }
280        else
281        {
282          $prevp='<a style="cursor:pointer;" onclick="'.$functionname.'('.($this->currentpage-1).');">&nbsp;'.$this->options['text_prev'].'&nbsp;</a>';
283        }
284      }
285      if($this->currentpage<$this->nbpages)
286      {
287        if($functionname=='')
288        {
289          $nextp='<a href="'.$this->baseurl.'&'.$this->pagevarurl.'='.($this->currentpage+1).'">&nbsp;'.$this->options['text_next'].'&nbsp;</a>';
290        }
291        else
292        {
293          $nextp='<a style="cursor:pointer;" onclick="'.$functionname.'('.($this->currentpage+1).');">&nbsp;'.$this->options['text_next'].'&nbsp;</a>';
294        }
295      }
296
297      $text=$prevp.$text.$nextp;
298    }
299
300    if($this->options['first_last'])
301    {
302      $firstp='';
303      $lastp='';
304      if($this->currentpage>1)
305      {
306        if($functionname=='')
307        {
308          $firstp='<a href="'.$this->baseurl.'&'.$this->pagevarurl.'=1">&nbsp;'.$this->options['text_first'].'&nbsp;</a>';
309        }
310        else
311        {
312          $firstp='<a style="cursor:pointer;" onclick="'.$functionname.'(1);">&nbsp;'.$this->options['text_first'].'&nbsp;</a>';
313        }
314      }
315      if($this->currentpage<$this->nbpages)
316      {
317        if($functionname=='')
318        {
319          $lastp='<a href="'.$this->baseurl.'&'.$this->pagevarurl.'='.$this->nbpages.'">&nbsp;'.$this->options['text_last'].'&nbsp;</a>';
320        }
321        else
322        {
323          $lastp='<a style="cursor:pointer;" onclick="'.$functionname.'('.$this->nbpages.');">&nbsp;'.$this->options['text_last'].'&nbsp;</a>';
324        }
325      }
326
327      $text=$firstp.$text.$lastp;
328    }
329
330    return($text);
331  }
332
333
334  /*
335    calculate the number of pages...
336  */
337  public function calcNbPages()
338  {
339    if($this->nbitemsperpages>0)
340    {
341      $this->nbpages=ceil($this->nbitems/$this->nbitemsperpages);
342    }
343  }
344
345} //class
346
347?>
Note: See TracBrowser for help on using the repository browser.