>
------------------------------------------------------------------------------
this classes provides base functions to manage pages navigation
- constructor pages_navigation($url)
- (public) function set_nb_items($nbitems)
- (public) function get_nb_items()
- (public) function set_nb_items_per_page($nbitems)
- (public) function get_nb_items_per_page()
- (public) function get_nb_pages()
- (public) function set_current_page($page)
- (public) function get_current_page()
- (public) function set_base_url($url)
- (public) function get_base_url()
- (public) function set_options($var)
- (public) function get_options()
- (public) function make_navigation()
- (public) function make_navigation_function()
- (private) function calc_nb_pages()
---------------------------------------------------------------------- */
class pages_navigation
{
protected $nbitems;
protected $nbitemsperpages;
protected $nbpages;
protected $currentpage;
protected $baseurl;
protected $pagevarurl;
protected $options;
public function pages_navigation()
{
$this->nbitems=0;
$this->nbitemsperpages=0;
$this->nbpages=0;
$this->currentpage=0;
$this->baseurl='';
$this->pagevarurl='';
$this->options=array(
'prev_next' => true,
'first_last' => true,
'display_all' => true,
'number_displayed' => 2, //number of page displayed before and after current page
'text_prev' => "<",
'text_first' => "<<",
'text_next' => ">",
'text_last' => ">>",
);
}
/*
define value for total number of items
*/
public function set_nb_items($nbitems)
{
if($nbitems!=$this->nbitems)
{
$this->nbitems=$nbitems;
$this->calc_nb_pages();
}
return($nbitems);
}
public function get_nb_items()
{
return($nbitems);
}
/*
define value for number of items displayed per pages
*/
public function set_nb_items_per_page($nbitems)
{
if(($nbitems!=$this->nbitemsperpages)&&($nbitems>0))
{
$this->nbitemsperpages=$nbitems;
$this->calc_nb_pages();
}
return($this->nbitemsperpages);
}
public function get_nb_items_per_page()
{
return($this->nbitemsperpages);
}
/*
return numbers of pages
*/
public function get_nb_pages()
{
return($this->nbpages);
}
/*
define the current page number
*/
public function set_current_page($page)
{
if(($page!=$this->currentpage)&&($page<=$this->nbpages)&&($page>0))
{
$this->currentpage=$page;
}
return($this->currentpage);
}
/*
returns the current page number
*/
public function get_current_page()
{
return($this->currentpage);
}
/*
define the value for url
ex: "http://mysite.com/admin.php?var1=xxx&var2=xxx"
*/
public function set_base_url($url)
{
if($url!=$this->baseurl)
{
$this->baseurl=$url;
}
return($this->baseurl);
}
public function get_base_url()
{
return($this->baseurl);
}
/*
define the value for variables's name
ex: url = "http://mysite.com/admin.php?var1=xxx&var2=xxx"
pagevar = "pagenumber"
url made is "http://mysite.com/admin.php?var1=xxx&var2=xxx&pagenumber=xxx"
*/
public function set_pagevar_url($var)
{
if($var!=$this->pagevarurl)
{
$this->pagevarurl=$var;
}
return($this->pagevarurl);
}
public function get_pagevar_url()
{
return($this->pagevarurl);
}
/*
define the navigation bar options
*/
public function set_options($var)
{
if(is_array($var))
{
foreach($this->options as $key=>$val)
{
if(isset($var[$key]))
{
$this->options[$key]=$var[$key];
}
}
}
return($this->options);
}
public function get_options()
{
return($this->options);
}
/*
returns an html formatted string
*/
public function make_navigation($functionname='')
{
$text='';
if(($this->options['display_all'])||($this->options['number_displayed']>=$this->nbpages))
{
for($i=1;$i<=$this->nbpages;$i++)
{
if($i!=$this->currentpage)
{
if($functionname=='')
{
$text.=''.$i.' ';
}
else
{
$text.=''.$i.' ';
}
}
else
{
$text.=$i.' ';
}
}
}
else
{
for($i=$this->currentpage-$this->options['number_displayed'];$i<=$this->currentpage+$this->options['number_displayed'];$i++)
{
if(($i>0)&&($i<=$this->nbpages))
{
if($i!=$this->currentpage)
{
if($functionname=='')
{
$text.=''.$i.' ';
}
else
{
$text.=''.$i.' ';
}
}
else
{
$text.=$i.' ';
}
}
}
if($this->currentpage-$this->options['number_displayed']>0)
{
$text=' ... '.$text;
}
if($this->currentpage+$this->options['number_displayed']<$this->nbpages)
{
$text.=' ... ';
}
}
if($this->options['prev_next'])
{
$prevp='';
$nextp='';
if($this->currentpage>1)
{
if($functionname=='')
{
$prevp=' '.$this->options['text_prev'].' ';
}
else
{
$prevp=' '.$this->options['text_prev'].' ';
}
}
if($this->currentpage<$this->nbpages)
{
if($functionname=='')
{
$nextp=' '.$this->options['text_next'].' ';
}
else
{
$nextp=' '.$this->options['text_next'].' ';
}
}
$text=$prevp.$text.$nextp;
}
if($this->options['first_last'])
{
$firstp='';
$lastp='';
if($this->currentpage>1)
{
if($functionname=='')
{
$firstp=' '.$this->options['text_first'].' ';
}
else
{
$firstp=' '.$this->options['text_first'].' ';
}
}
if($this->currentpage<$this->nbpages)
{
if($functionname=='')
{
$lastp=' '.$this->options['text_last'].' ';
}
else
{
$lastp=' '.$this->options['text_last'].' ';
}
}
$text=$firstp.$text.$lastp;
}
return($text);
}
/*
calculate the number of pages...
*/
public function calc_nb_pages()
{
if($this->nbitemsperpages>0)
{
$this->nbpages=ceil($this->nbitems/$this->nbitemsperpages);
}
}
} //class
?>