| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244 |
- <?php
- /**
- * Klasa obsługująca podział listy na strony.
- */
- class PageNav {
- var $total;
- var $page;
- var $pages;
- var $limit;// int - limit
- var $limitstart;// int - offset
- var $limits;
- var $max2show;
- var $req_method;// str - Request method GET or POST
- var $req_args;// array - Request args
- var $page_nr_arg_name;// str - Request arg name for page number
- var $_post_uniq_id = null;
- var $_post_form_rendered = false;
- /**
- * @param $total - ilosc elementow
- * @param $limitstart - początkowy indeks z listy elementów
- * @param $limit - limit elementów na strone
- * @param $limits - tablica ilosci elementów na stronie
- */
- public function __construct($total, $limitstart, $limit = 10, $limits = null) {
- if (is_array($limits)) {
- $this->limits = $limits;
- }
- else {
- $this->limits = array(10);
- }
- $this->limit = ($limit > 0) ? $limit : $this->limits[0];// ilość produktów na stornie
- $this->limitstart = ($limitstart > 0) ? (floor($limitstart / $this->limit) * $this->limit) : 0;
- $this->total = $total;
- $this->page = intval($this->limitstart / $this->limit) + 1;
- $this->pages = (int)ceil($this->total / $this->limit);
- $this->max2show = 10;
- $this->req_method = 'GET';
- $this->req_args = array();
- $this->page_nr_arg_name = 'nr';
- }
- public function set_request_args($args) {
- $this->req_args = $args;
- }
- public function set_request_arg_page_nr_name($nr_name) {
- $this->page_nr_arg_name = $nr_name;
- }
- public function set_request_method_post() {
- $this->req_method = 'POST';
- $this->_post_uniq_id = uniqid('PageNav_');
- }
- public function set_request_method_get() {
- $this->req_method = 'GET';
- }
- /**
- * Generate page nr link.
- */
- public function get_page_link($nr) {
- $params = array();
- foreach ($this->req_args as $k => $v) {
- $params [$k] = $v;
- }
- $params[$this->page_nr_arg_name] = ($nr - 1) * $this->limit;
- $params_out = array();
- foreach ($params as $k => $v) {
- $params_out[] = $k . "=" . $v;
- }
- return "?" . implode("&", $params_out);
- }
- function render_post_form() {
- $out = '';
- if (!$this->_post_form_rendered) {
- $out .= '<script type="text/javascript">' . "
- function {$this->_post_uniq_id}_frm(nr){
- var frm=document.getElementById('{$this->_post_uniq_id}');
- if (undefined !== frm['{$this->page_nr_arg_name}']) {
- frm['{$this->page_nr_arg_name}'].value=nr;
- frm.submit();
- }
- return false;
- }
- " . '</script>';
- $out .= '<form action="" method="post" id="' . $this->_post_uniq_id . '" style="display:none;">';
- $out .= '<input type="text" name="' . $this->page_nr_arg_name . '" value="" />';
- foreach ($this->req_args as $k => $v) {
- $out .= '<input type="text" name="' . $k . '" value="' . $v . '" />';
- }
- $out .= '</form>';
-
- $this->_post_form_rendered = true;
- }
- return $out;
- }
- /**
- * Render navigation links.
- */
- public function render($type = 'table') {
- $out = '';
- if ($this->req_method == 'POST') {
- $out .= $this->render_post_form();
- }
- $out .= '<div class="page-nav">';
- $out .= '<table cellspacing="0" cellpadding="0" style="width:100%;"><tr><td>';
- if (0) {//TODO: count($this->limits)) {
- $out .= '<div class="limit">';
- $out .= ' Pokaż: ';
- $out .= ' <select id="limit" name="limit" class="inputbox" onchange="javascript: document.listaForm.submit();" size="1">';
- foreach ($this->limits as $val) {
- $out .= ' <option '.(($this->limit == $val) ? ' selected="selected" ' : '').' value="'.$val.'">'.$val.'</option>';
- }
- $out .= ' </select>';
- $out .= '</div>';//class="limit"
- }//if (0)
- $out .= '</td><td>';
- $out .= '<div class="strony">';
- if ($this->total > $this->limit) {# nie pokazuje listy stron jak jest tylko jedna
- /*
- * _1 2 3 4 5 6 7 8 ... 59 60
- * 1 _2 3 4 5 6 7 8 ... 59 60
- * 1 2 _3 4 5 6 7 8 ... 59 60
- * 1 2 3 _4 5 6 7 8 ... 59 60
- * 1 2 3 4 _5 6 7 8 ... 59 60
- * 1 2 3 4 5 _6 7 8 ... 59 60
- * 1 2 ... 5 6 _7 8 9 ... 59 60
- * 1 2 ... 6 7 _8 9 10 ... 59 60
- * ...
- * 1 2 ... 52 53 _54 55 56 ... 59 60
- * 1 2 ... 53 54 _55 56 57 58 59 60
- * 1 2 ... 53 54 55 _56 57 58 59 60
- * 1 2 ... 53 54 55 56 _57 58 59 60
- * 1 2 ... 53 54 55 56 57 _58 59 60
- */
- if ($this->page == 1) {
- $out .= '<span title="Pierwsza"> << </span>';
- $out .= '<span title="Poprzednia"> < </span>';
- }
- else {
- $out .= $this->link('<span> << </span>', 1, array('title'=>"Pierwsza"));
- $out .= $this->link('<span> < </span>', $this->page - 1, array('title'=>"Poprzednia"));
- }
- if ($this->pages > $this->max2show) {
- $out .= $this->show_page(1);
- $out .= $this->show_page(2);
- if ($this->page <= 6) {//show [0:8] ... [-2]
- for ($i = 3; $i <= 8; $i++) {
- $out .= $this->show_page($i);
- }
- $out .= ' ... ';
- }
- else if ($this->page > $this->pages - 6) {//show [0:2] ... [-8]
- $out .= ' ... ';
- for ($i = $this->pages - 7; $i <= $this->pages - 2; $i++) {
- $out .= $this->show_page($i);
- }
- }
- else {//show [0:2] ... [$i-2:$i+2] ... [-2]
- $out .= ' ... ';
- for ($i = $this->page - 2; $i <= $this->page + 2; $i++) {
- $out .= $this->show_page($i);
- }
- $out .= ' ... ';
- }
- $out .= $this->show_page($this->pages - 1);
- $out .= $this->show_page($this->pages);
- }
- else {//print all nr
- for ($i = 1; $i <= $this->pages; $i++) {
- $out .= ' '.$this->show_page($i).' ';
- }
- }
- if ($this->page == $this->pages) {
- $out .= '<span title="Następna"> > </span>';
- $out .= '<span title="Ostatnia"> >> </span>';
- }
- else {
- $out .= $this->link('<span> > </span>', $this->page + 1, array('title'=>"Następna"));
- $out .= $this->link('<span> >> </span>', $this->pages, array('title'=>"Ostatnia"));
- }
- if ($type != 'links_only') {
- $out .= '<br />';
- }
- }
- if ($type != 'links_only') {
- $out .= '<span class="strona">strona '.$this->page.' z '.$this->pages.'</span>';
- $out .= ' (' . $this->total . ' rekordów)';
- }
- $out .= '</div>';//.strony
- $out .= '</td></tr></table>';
- $out .= '</div>';//.page-nav
- return $out;
- }
- public function show_page($nr) {
- if ($nr > 0 && $nr <= $this->total) {
- if ($nr == $this->page) {
- return '<span title="'.$nr.'" class="current">'.$nr.'</span>';
- }
- else {
- return $this->link('<span>'.$nr.'</span>', $nr, array('title'=>"Strona {$nr}"));
- }
- }
- return '';
- }
- public function link($label, $nr, $params = array()) {
- $out = '';
- $link_out = $this->get_page_link($nr);
- $attrs = array();
- if ('' != ($title = V::get('title', '', $params))) {
- $attrs[] = 'title="' . $title . '"';
- }
- if ('' != ($class = V::get('class', '', $params))) {
- $attrs[] = 'class="' . $class . '"';
- }
- if ($this->req_method == 'POST') {
- $offset = ($nr - 1) * $this->limit;
- $attrs[] = 'onclick="' . "return {$this->_post_uniq_id}_frm({$offset})" . '"';
- }
- $attrs_out = (!empty($attrs))? ' ' . implode(' ', $attrs) : '';
- $out = '<a href="' . $link_out . '"' . $attrs_out . '>' . $label . '</a>';
- return $out;
- }
- }
|