Overview

Packages

  • Onion::Controllers
  • Onion::Core
  • Onion::UI
  • Onion::Utils

Classes

  • Form
  • Grid
  • Html
  • IconsGrid
  • Paginator
  • Template
  • Widget
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: 
  3: /**
  4:  * Onion Framework - Stránkovač
  5:  *
  6:  * Copyright (c) 2011 Jano Gašpar (http://webstranky.net)
  7:  *
  8:  * @author     Jano Gašpar
  9:  * @copyright  Copyright (c) 2011 Jano Gašpar
 10:  * @package   Onion::UI
 11:  **/
 12: class Paginator
 13: {
 14:     private $app;
 15: 
 16: 
 17:     public $current_page = 1;
 18: 
 19: 
 20:     public $pages;
 21: 
 22: 
 23:     public $limit = array();
 24: 
 25: 
 26:     public $links = 9;
 27: 
 28: 
 29:     public $side_links = 3;
 30: 
 31: 
 32:     public $separator = '&nbsp;';
 33: 
 34: 
 35:     public $groups_separator = '&nbsp;&hellip;&nbsp;';
 36: 
 37: 
 38:     public $uri_parameter_name = NULL;
 39: 
 40: 
 41:     public $add_current_uri_parameters = TRUE;
 42: 
 43: 
 44:     public $add_uri_parameters = array();
 45: 
 46: 
 47:     public $anchor;
 48: 
 49: 
 50:     public $route_name = NULL;
 51: 
 52: 
 53:     public $full_paginator = FALSE;
 54: 
 55: 
 56:     public function __construct($app, $current_page, $total_items, $items_on_page)
 57:     {
 58:         $this->app = $app;
 59: 
 60:         $pages = ceil($total_items / $items_on_page);
 61: 
 62:         if ($current_page < 1) {
 63:             $current_page = 1;
 64:         }
 65: 
 66:         if ($pages < 1) {
 67:             $pages = 1;
 68:         }
 69: 
 70:         if ($current_page > $pages) {
 71:             $current_page = $pages;
 72:         }
 73: 
 74:         $this->limit[0] = ($current_page - 1) * $items_on_page;
 75:         $this->limit[1] = $items_on_page;
 76: 
 77:         $this->current_page = $current_page;
 78:         $this->pages = $pages;
 79:         return $this;
 80:     }
 81: 
 82: 
 83:     public function __tostring() {
 84:         return $this->render();
 85:     }
 86: 
 87: 
 88:     public function render($full_paginator = FALSE)
 89:     {
 90:         if ($full_paginator == TRUE
 91:             OR $this->full_paginator == TRUE
 92:             OR $this->pages < ($this->links * 2)) {
 93: 
 94:             return $this->full_paginator();
 95:         }
 96: 
 97:         return $this->normal_paginator();
 98:     }
 99: 
100: 
101:     private function normal_Paginator()
102:     {
103:         if (($this->links % 2) == 0) {
104:             $this->links--;
105:         }
106: 
107:         // kontrola aktuálnej stránky či je v povolenom rozsahu
108:         if ($this->current_page < 1) {
109:             $this->current_page = 1;
110:         }
111: 
112:         if ($this->current_page > $this->pages) {
113:             $this->current_page = $this->pages;
114:         }
115: 
116:         // fragment = je počet stránok pred a za aktuálnou stránkou v strednej skupine
117:         // 1 2 3 ... 7  8  9 10 [11] 12 13 14 15 ... 45 46 47
118:         //           <-------->      <--------->
119:         $fragment = floor($this->links / 2);
120: 
121:         // ľavá hranica strednej skupiny
122:         $leftBorder = $this->current_page - $fragment;
123:         if ($leftBorder < 0) {
124:             $leftBorder = 0;
125:         }
126: 
127:         // pravá hranica strednej skupiny
128:         $rightBorder = $this->current_page + $fragment;
129:         if ($rightBorder > $this->pages) {
130:             $rightBorder = $this->pages;
131:         }
132: 
133:         $links = array();
134: 
135:         // prvá skupina
136:         $group = array();
137:         $group_length = $this->side_links;
138:         if ($this->current_page < $this->links) {
139:             $group_length = $this->links;
140:         }
141:         for ($page = 1; $page <= $group_length; $page++) {
142:             $group[] = $this->link($page);
143:         }
144: 
145:         $links[] = implode($this->separator, $group);
146: 
147:         // prostredná skupina
148:         if ($this->current_page >= $this->links
149:             AND $this->current_page <= ($this->pages - $this->links + 1)) {
150: 
151:             $group = array();
152:             $page = $this->current_page - $fragment;
153:             $end = $page + $this->links - 1;
154:             for ($page; $page <= $end; $page++) {
155:                 $group[] = $this->link($page);
156:             }
157: 
158:             $links[] = implode($this->separator, $group);
159:         }
160: 
161:         // posledná skupina
162:         $group = array();
163:         $page = $this->pages - $this->side_links;
164:         if ($this->current_page > ($this->pages - $this->links + 1)) {
165:             $page = $this->pages - $this->links;
166:         }
167:         $page++;
168:         for ($page; $page <= $this->pages; $page++) {
169:             $group[] = $this->link($page);
170:         }
171: 
172:         $links[] = implode($this->separator, $group);
173: 
174:         return implode($this->groups_separator, $links);
175:     }
176: 
177: 
178:     private function full_paginator()
179:     {
180:         $links = array();
181:         for ($page = 1; $page <= $this->pages; $page++) {
182:             $links[] = $this->link($page);
183:         }
184: 
185:         return implode($this->separator, $links);
186:     }
187: 
188: 
189:     private function link($page)
190:     {
191:         if ($this->current_page == $page) {
192:             $link = Html::element('strong');
193: 
194:         } else {
195:             $link = Html::element('a');
196:         }
197:         $link->set_text($page);
198: 
199:         $uri_parameters = array();
200:         if (empty($this->add_current_uri_parameters) === FALSE) {
201:             if (is_array($this->add_current_uri_parameters) === FALSE) {
202:                 $uri_parameters = (array) $this->app->request->get;
203: 
204:             } else {
205:                 foreach ($this->add_current_uri_parameters as $uri_parameter) {
206:                     if (isset($this->app->request->get[$uri_parameter]) == TRUE) {
207:                         $uri_parameters[$uri_parameter] = $this->app->request->get[$uri_parameter];
208:                     }
209:                 }
210:             }
211:         }
212: 
213:         $uri_parameters = array_merge($uri_parameters, $this->add_uri_parameters);
214: 
215:         if ($this->uri_parameter_name !== NULL) {
216:             $uri_parameters[$this->uri_parameter_name] = $page;
217:             $url = $this->app->create_uri($this->route_name, array(), $uri_parameters);
218: 
219:         } else {
220:             $url = $this->app->create_uri($this->route_name, array('page' => $page), $uri_parameters);
221:         }
222: 
223:         if (empty($this->anchor) == FALSE) {
224:             $url .= '#'.$this->anchor;
225:         }
226: 
227:         if ($this->current_page <> $page) {
228:             $link->href($url);
229:         }
230: 
231:         return $link->render();
232:     }
233: }
Onion API documentation generated by ApiGen.
Generated using the TokenReflection library.