XOOPS RMCommon Utilities  2.1.8.91RC
 All Classes Namespaces Files Functions Variables
pagenav.php
Go to the documentation of this file.
1 <?php
2 // $Id: pagenav.php 949 2012-04-14 04:18:10Z i.bitcero $
3 // --------------------------------------------------------------
4 // Red México Common Utilities
5 // A framework for Red México Modules
6 // Author: Eduardo Cortés <i.bitcero@gmail.com>
7 // Email: i.bitcero@gmail.com
8 // License: GPL 2.0
9 // --------------------------------------------------------------
10 
11 class RMPageNav
12 {
13  private $total_results;
14  private $items_page;
15  private $current_page;
16  private $steps = 5;
17  private $url = '';
18  private $showing = '';
19  private $displayed = false;
20  private $rendered = '';
21  private $template = '';
22  private $start = 0;
23 
33  $this->total_results = $total_results;
34  $this->items_page = $items_page;
35  $this->current_page = $current_page;
36  $this->steps = $steps;
37  RMTemplate::get()->add_style('pagenav.css', 'rmcommon');
38  }
39 
48  public function target_url(){
49  $num = func_num_args();
50  if ($num>0){
51  $url = func_get_arg(0);
52  $this->url = $url;
53  } else {
54  return $this->url;
55  }
56  }
57 
64  public function steps(){
65  $num = func_num_args();
66  if ($num>0){
67  $steps = func_get_arg(0);
68  $this->steps = $steps;
69  } else {
70  return $this->steps;
71  }
72  }
73 
77  public function get_showing(){
78  return $this->showing;
79  }
80 
84  public function start(){
85  return $this->start;
86  }
87 
92  public function set_template($path){
93  $this->template = $path;
94  }
95 
103  public function render($caption, $showing=0){
104 
105  // If we have the content of render then return it
106  if ($this->displayed){
107  return $this->rendered;
108  }
109 
110  $total_pages = ceil($this->total_results / $this->items_page);
111  $current_page = $this->current_page > $total_pages ? $total_pages : $this->current_page;
115  $steps = $this->steps();
116  $url = $this->url;
117 
118  if ($current_page==1){
119  $first_element = 1;
120  } else{
121  $first_element = $total_results > 0 ? (($current_page * $items_page) - ($items_page)) + 1 : 0;
122  }
123 
124  $last_element = $current_page*$items_page;
125  $last_element = $last_element>$total_results ? $total_results : $last_element;
126  $this->showing = sprintf(__('Showing <strong>%u</strong> to <strong>%u</strong> of <strong>%u</strong>.','rmcommon'), $first_element, $last_element, $total_results);
127 
128  if ($total_pages<=1) return;
129 
130  if($showing) $showing_legend = $this->showing;
131 
132  ob_start();
133 
134  $start = 1;
135 
136  if ($current_page>$steps-1){
137  $start = $current_page-floor($steps/2);
138  }
139  //echo "$start>($total_pages - $steps) ? ($total_pages - $steps)+1 : $start;"; die();
140  $start = $start>=($total_pages - $steps) && $start!=1 ? ($total_pages - $steps)+1 : $start;
141  $start = $start<1 ? 1 : $start;
142  $this->start = $start;
143 
144  $end = $start+($steps-1);
145  $end = $end > $total_pages ? $total_pages : $end;
146 
147  if($this->template!='' && is_file($this->template)){
148  include $this->template;
149  } else {
150  include RMTemplate::get()->get_template('navigation_pages.php', 'module', 'rmcommon');
151  }
152 
153  $this->displayed = true;
154 
155  $this->rendered = ob_get_clean();
156  return $this->rendered;
157 
158  }
159 
163  public function display($caption = true, $showing = 0){
164  echo $this->render($caption, $showing);
165  }
166 
167 }