PageNav.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <?php
  2. /**
  3. * Klasa obsługująca podział listy na strony.
  4. */
  5. class PageNav {
  6. var $total;
  7. var $page;
  8. var $pages;
  9. var $limit;// int - limit
  10. var $limitstart;// int - offset
  11. var $limits;
  12. var $max2show;
  13. var $req_method;// str - Request method GET or POST
  14. var $req_args;// array - Request args
  15. var $page_nr_arg_name;// str - Request arg name for page number
  16. var $_post_uniq_id = null;
  17. var $_post_form_rendered = false;
  18. /**
  19. * @param $total - ilosc elementow
  20. * @param $limitstart - początkowy indeks z listy elementów
  21. * @param $limit - limit elementów na strone
  22. * @param $limits - tablica ilosci elementów na stronie
  23. */
  24. public function __construct($total, $limitstart, $limit = 10, $limits = null) {
  25. if (is_array($limits)) {
  26. $this->limits = $limits;
  27. }
  28. else {
  29. $this->limits = array(10);
  30. }
  31. $this->limit = ($limit > 0) ? $limit : $this->limits[0];// ilość produktów na stornie
  32. $this->limitstart = ($limitstart > 0) ? (floor($limitstart / $this->limit) * $this->limit) : 0;
  33. $this->total = $total;
  34. $this->page = intval($this->limitstart / $this->limit) + 1;
  35. $this->pages = (int)ceil($this->total / $this->limit);
  36. $this->max2show = 10;
  37. $this->req_method = 'GET';
  38. $this->req_args = array();
  39. $this->page_nr_arg_name = 'nr';
  40. }
  41. public function set_request_args($args) {
  42. $this->req_args = $args;
  43. }
  44. public function set_request_arg_page_nr_name($nr_name) {
  45. $this->page_nr_arg_name = $nr_name;
  46. }
  47. public function set_request_method_post() {
  48. $this->req_method = 'POST';
  49. $this->_post_uniq_id = uniqid('PageNav_');
  50. }
  51. public function set_request_method_get() {
  52. $this->req_method = 'GET';
  53. }
  54. /**
  55. * Generate page nr link.
  56. */
  57. public function get_page_link($nr) {
  58. $params = array();
  59. foreach ($this->req_args as $k => $v) {
  60. $params [$k] = $v;
  61. }
  62. $params[$this->page_nr_arg_name] = ($nr - 1) * $this->limit;
  63. $params_out = array();
  64. foreach ($params as $k => $v) {
  65. $params_out[] = $k . "=" . $v;
  66. }
  67. return "?" . implode("&", $params_out);
  68. }
  69. function render_post_form() {
  70. $out = '';
  71. if (!$this->_post_form_rendered) {
  72. $out .= '<script type="text/javascript">' . "
  73. function {$this->_post_uniq_id}_frm(nr){
  74. var frm=document.getElementById('{$this->_post_uniq_id}');
  75. if (undefined !== frm['{$this->page_nr_arg_name}']) {
  76. frm['{$this->page_nr_arg_name}'].value=nr;
  77. frm.submit();
  78. }
  79. return false;
  80. }
  81. " . '</script>';
  82. $out .= '<form action="" method="post" id="' . $this->_post_uniq_id . '" style="display:none;">';
  83. $out .= '<input type="text" name="' . $this->page_nr_arg_name . '" value="" />';
  84. foreach ($this->req_args as $k => $v) {
  85. $out .= '<input type="text" name="' . $k . '" value="' . $v . '" />';
  86. }
  87. $out .= '</form>';
  88. $this->_post_form_rendered = true;
  89. }
  90. return $out;
  91. }
  92. /**
  93. * Render navigation links.
  94. */
  95. public function render($type = 'table') {
  96. $out = '';
  97. if ($this->req_method == 'POST') {
  98. $out .= $this->render_post_form();
  99. }
  100. $out .= '<div class="page-nav">';
  101. $out .= '<table cellspacing="0" cellpadding="0" style="width:100%;"><tr><td>';
  102. if (0) {//TODO: count($this->limits)) {
  103. $out .= '<div class="limit">';
  104. $out .= ' Pokaż: ';
  105. $out .= ' <select id="limit" name="limit" class="inputbox" onchange="javascript: document.listaForm.submit();" size="1">';
  106. foreach ($this->limits as $val) {
  107. $out .= ' <option '.(($this->limit == $val) ? ' selected="selected" ' : '').' value="'.$val.'">'.$val.'</option>';
  108. }
  109. $out .= ' </select>';
  110. $out .= '</div>';//class="limit"
  111. }//if (0)
  112. $out .= '</td><td>';
  113. $out .= '<div class="strony">';
  114. if ($this->total > $this->limit) {# nie pokazuje listy stron jak jest tylko jedna
  115. /*
  116. * _1 2 3 4 5 6 7 8 ... 59 60
  117. * 1 _2 3 4 5 6 7 8 ... 59 60
  118. * 1 2 _3 4 5 6 7 8 ... 59 60
  119. * 1 2 3 _4 5 6 7 8 ... 59 60
  120. * 1 2 3 4 _5 6 7 8 ... 59 60
  121. * 1 2 3 4 5 _6 7 8 ... 59 60
  122. * 1 2 ... 5 6 _7 8 9 ... 59 60
  123. * 1 2 ... 6 7 _8 9 10 ... 59 60
  124. * ...
  125. * 1 2 ... 52 53 _54 55 56 ... 59 60
  126. * 1 2 ... 53 54 _55 56 57 58 59 60
  127. * 1 2 ... 53 54 55 _56 57 58 59 60
  128. * 1 2 ... 53 54 55 56 _57 58 59 60
  129. * 1 2 ... 53 54 55 56 57 _58 59 60
  130. */
  131. if ($this->page == 1) {
  132. $out .= '<span title="Pierwsza"> &lt;&lt; </span>';
  133. $out .= '<span title="Poprzednia"> &lt; </span>';
  134. }
  135. else {
  136. $out .= $this->link('<span> &lt;&lt; </span>', 1, array('title'=>"Pierwsza"));
  137. $out .= $this->link('<span> &lt; </span>', $this->page - 1, array('title'=>"Poprzednia"));
  138. }
  139. if ($this->pages > $this->max2show) {
  140. $out .= $this->show_page(1);
  141. $out .= $this->show_page(2);
  142. if ($this->page <= 6) {//show [0:8] ... [-2]
  143. for ($i = 3; $i <= 8; $i++) {
  144. $out .= $this->show_page($i);
  145. }
  146. $out .= ' ... ';
  147. }
  148. else if ($this->page > $this->pages - 6) {//show [0:2] ... [-8]
  149. $out .= ' ... ';
  150. for ($i = $this->pages - 7; $i <= $this->pages - 2; $i++) {
  151. $out .= $this->show_page($i);
  152. }
  153. }
  154. else {//show [0:2] ... [$i-2:$i+2] ... [-2]
  155. $out .= ' ... ';
  156. for ($i = $this->page - 2; $i <= $this->page + 2; $i++) {
  157. $out .= $this->show_page($i);
  158. }
  159. $out .= ' ... ';
  160. }
  161. $out .= $this->show_page($this->pages - 1);
  162. $out .= $this->show_page($this->pages);
  163. }
  164. else {//print all nr
  165. for ($i = 1; $i <= $this->pages; $i++) {
  166. $out .= ' '.$this->show_page($i).' ';
  167. }
  168. }
  169. if ($this->page == $this->pages) {
  170. $out .= '<span title="Następna"> &gt; </span>';
  171. $out .= '<span title="Ostatnia"> &gt;&gt; </span>';
  172. }
  173. else {
  174. $out .= $this->link('<span> &gt; </span>', $this->page + 1, array('title'=>"Następna"));
  175. $out .= $this->link('<span> &gt;&gt; </span>', $this->pages, array('title'=>"Ostatnia"));
  176. }
  177. if ($type != 'links_only') {
  178. $out .= '<br />';
  179. }
  180. }
  181. if ($type != 'links_only') {
  182. $out .= '<span class="strona">strona '.$this->page.' z '.$this->pages.'</span>';
  183. $out .= ' (' . $this->total . ' rekordów)';
  184. }
  185. $out .= '</div>';//.strony
  186. $out .= '</td></tr></table>';
  187. $out .= '</div>';//.page-nav
  188. return $out;
  189. }
  190. public function show_page($nr) {
  191. if ($nr > 0 && $nr <= $this->total) {
  192. if ($nr == $this->page) {
  193. return '<span title="'.$nr.'" class="current">'.$nr.'</span>';
  194. }
  195. else {
  196. return $this->link('<span>'.$nr.'</span>', $nr, array('title'=>"Strona {$nr}"));
  197. }
  198. }
  199. return '';
  200. }
  201. public function link($label, $nr, $params = array()) {
  202. $out = '';
  203. $link_out = $this->get_page_link($nr);
  204. $attrs = array();
  205. if ('' != ($title = V::get('title', '', $params))) {
  206. $attrs[] = 'title="' . $title . '"';
  207. }
  208. if ('' != ($class = V::get('class', '', $params))) {
  209. $attrs[] = 'class="' . $class . '"';
  210. }
  211. if ($this->req_method == 'POST') {
  212. $offset = ($nr - 1) * $this->limit;
  213. $attrs[] = 'onclick="' . "return {$this->_post_uniq_id}_frm({$offset})" . '"';
  214. }
  215. $attrs_out = (!empty($attrs))? ' ' . implode(' ', $attrs) : '';
  216. $out = '<a href="' . $link_out . '"' . $attrs_out . '>' . $label . '</a>';
  217. return $out;
  218. }
  219. }