'page', // название GET переменной хранящей значение страницы 'showAnyway' => false, // показывать навигацию даже если в ней только одна страница 'showPages' => 0 // кол-во страниц которые будут показаны в ряд ЕСЛИ "0" будут выведены все страницы ); $options['showPages'] = max($options['showPages'], 0); $this->total = max((int)$total/*-$this->limit*/, 0); $this->limit = max((int)$limit, 0); if($this->total < 1 || $this->limit < 1) return null; if($this->limit > $this->total) $this->limit = $total; $maxpage = ceil($this->total/$this->limit); $page = (int)@$_REQUEST[$options['limitstartVar']]; $page = max($page, 1); $page = min($page, $maxpage); if($this->limit == $this->total) { $this->limitstart = 0; } else { $this->limitstart = max($page*$this->limit-$this->limit, 0); } //vd($this->limitstart); //if($this->limitstart >= $this->total) // $this->limitstart = $this->total-1; //vd($this->limitstart); // вырубаемся если у нас только одна страница if(!$options['showAnyway'] && $this->total == $this->limit) return false; $prevpage = $page - 1; $prevpage = max($prevpage, 1); $nextpage = $page + 1; $nextpage = min($nextpage, $maxpage); ////////////////// $items = array(); if($this->limitstart == 0) { $items['start'] = $this->_renderItem('item_unactive', array('key' => 'start', 'text'=>'« '.$this->getText('pagination_start'), 'current' => false) ); $items['prev'] = $this->_renderItem('item_unactive', array('key' => 'prev', 'text'=>'< '.$this->getText('pagination_previous'), 'current' => false) ); } else { $items['start'] = $this->_renderItem('item_active', array('key' => 'start', 'text'=>'« '.$this->getText('pagination_start'), 'current' => false, 'link' => $this->setLink($options['limitstartVar'], 1)) ); $items['prev'] = $this->_renderItem('item_active', array('key' => 'prev', 'text'=>'< '.$this->getText('pagination_previous'), 'current' => false, 'link' => $this->setLink($options['limitstartVar'], $prevpage)) ); } $items['pages'] = array(); $start = $options['showPages']? max($page - (int)(($options['showPages']-1)/2), 1) : 1; $end = $options['showPages']? min($page + (int)($options['showPages']/2), $maxpage) : $maxpage; for($i = $start; $i <= $end; $i+=1) { if($i == $page) { $items['pages'][] = $this->_renderItem('item_unactive', array('key' => 'page', 'text'=>$i, 'current' => true) ); } else { $items['pages'][] = $this->_renderItem('item_active', array('key' => 'page', 'text'=>$i, 'current' => false, 'link' => $this->setLink($options['limitstartVar'], $i)) ); } } if($page >= $maxpage) { $items['end'] = $this->_renderItem('item_unactive', array('key' => 'end', 'text'=>$this->getText('pagination_end').' »', 'current'=>false) ); $items['next'] = $this->_renderItem('item_unactive', array('key' => 'next', 'text'=>$this->getText('pagination_next').' >', 'current'=>false) ); } else { $items['end'] = $this->_renderItem('item_active', array('key' => 'end', 'text'=>$this->getText('pagination_end').' »', 'current' => false, 'link' => $this->setLink($options['limitstartVar'], $maxpage)) ); $items['next'] = $this->_renderItem('item_active', array('key' => 'next', 'text'=>$this->getText('pagination_next').' >', 'current' => false, 'link' => $this->setLink($options['limitstartVar'], $nextpage)) ); } $this->_html = $this->_renderItem('items_render', $items); } function getText($varName) { return $varName; } function setLink($var, $val) { $q = $_SERVER['QUERY_STRING']; parse_str($q, $q); $q[$var] = $val; $ru = $_SERVER['REQUEST_URI']; $ru = preg_replace('#\?.*$#s', '', $ru); $ru .= '?'.http_build_query($q); return $ru; } function _renderItem($itemName, $param = array()) { if(function_exists('pagination_'.$itemName)) { return call_user_func('pagination_'.$itemName, $param); } return call_user_func(array($this, '_'.$itemName), $param); } function _item_active($item) { $html = ''.$item['text'].''; return $html; } function _item_unactive($item) { $html = ''.$item['text'].''; return $html; } function _items_render($items) { $html = ''; return $html; } // вернуть хтмл код function getHtml() { return $this->_html; } // вернуть стартовый лимит function getLimitstart() { return $this->limitstart; } } ?>