| [ PHPXref.com ] | [ Generated: Sun Jul 20 20:29:46 2008 ] | [ Swat 0.1.8 ] |
| [ Index ] [ Variables ] [ Functions ] [ Classes ] [ Constants ] [ Statistics ] | ||
[Summary view] [Print] [Text view]
1 <?php 2 3 require_once 'Swat/SwatControl.php'; 4 require_once 'Swat/SwatHtmlTag.php'; 5 require_once 'Swat/SwatString.php'; 6 7 /** 8 * A widget to allow navigation between paginated data 9 * 10 * @package Swat 11 * @copyright 2004-2006 silverorange 12 * @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1 13 */ 14 class SwatPagination extends SwatControl 15 { 16 // {{{ public properties 17 18 /** 19 * Link 20 * 21 * The initial link used when building links. If null, links will 22 * begin with '?'. 23 * 24 * @var string 25 */ 26 public $link = null; 27 28 /** 29 * HTTP GET vars to clobber 30 * 31 * An array of GET variable names to unset before rebuilding a new link. 32 * 33 * @var array 34 */ 35 public $unset_get_vars = array(); 36 37 /** 38 * Current page 39 * 40 * The number of the current page. The value is zero based. 41 * 42 * @var integer 43 */ 44 public $current_page = 0; 45 46 /** 47 * Page size 48 * 49 * The number of records that are displayed per page. 50 * 51 * @var integer 52 */ 53 public $page_size = 20; 54 55 /** 56 * Total records 57 * 58 * The total number of records that are available for display. 59 * 60 * @var integer 61 */ 62 public $total_records = 0; 63 64 /** 65 * Current record 66 * 67 * The record that is currently being displayed first on the page. 68 * 69 * @var integer 70 */ 71 public $current_record = 0; 72 73 // }}} 74 // {{{ protected properties 75 76 /** 77 * The next page to display 78 * 79 * The value is zero based. 80 * 81 * @var integer 82 */ 83 protected $next_page = 0; 84 85 /** 86 * The previous page to display 87 * 88 * The value is zero based. 89 * 90 * @var integer 91 */ 92 protected $prev_page = 0; 93 94 /** 95 * The total number of pages in the database 96 * 97 * @var integer 98 */ 99 protected $total_pages = 0; 100 101 // }}} 102 // {{{ public function __construct() 103 104 /** 105 * Creates a new pagination widget 106 * 107 * Enforces that a unique id is set. 108 * 109 * @param string $id a non-visible unique id for this widget. 110 * 111 * @see SwatWidget::__construct() 112 */ 113 public function __construct($id = null) 114 { 115 parent::__construct($id); 116 117 $this->requires_id = true; 118 119 $this->addStyleSheet('swat/styles/swat-pagination.css'); 120 } 121 122 // }}} 123 // {{{ public function getResultsMessage() 124 125 /** 126 * Get Results Message 127 * 128 * Takes the current state of a {@link SwatPagination} widget and 129 * outputs a human readable summary of what is currently shown. 130 * 131 * @param $unit string Type of unit being returned (default 'record') 132 * @param $unit string Plural type of unit being returned (default 133 * 'records') 134 * 135 * @return string Results message 136 */ 137 public function getResultsMessage($unit = null, $unit_plural = null) 138 { 139 if ($unit === null) 140 $unit = Swat::_('record'); 141 142 if ($unit_plural === null) 143 $unit_plural = Swat::_('records'); 144 145 146 if ($this->total_records == 0) 147 return sprintf(Swat::_('No %s.'), $unit_plural); 148 149 elseif ($this->total_records == 1) 150 return sprintf(Swat::_('One %s.'), $unit); 151 152 else 153 return sprintf(Swat::_('%s %s, displaying %s to %s'), 154 SwatString::numberFormat($this->total_records), 155 $unit_plural, 156 SwatString::numberFormat($this->current_record + 1), 157 SwatString::numberFormat(min($this->current_record + 158 $this->page_size, $this->total_records))); 159 } 160 161 // }}} 162 // {{{ public function display() 163 164 /** 165 * Displays this pagination widget 166 */ 167 public function display() 168 { 169 $this->calculatePages(); 170 171 if ($this->total_pages > 1) { 172 173 $this->unset_get_vars[] = $this->id; 174 175 $div = new SwatHtmlTag('div'); 176 $div->class = 'swat-pagination'; 177 $div->open(); 178 179 $this->displayPosition(); 180 $this->displayPrev(); 181 $this->displayPages(); 182 $this->displayNext(); 183 184 $div->close(); 185 186 } 187 } 188 189 // }}} 190 // {{{ public function process() 191 192 /** 193 * Processes this pagination widget 194 * 195 * Sets the current_page and current_record properties. 196 */ 197 public function process() 198 { 199 if (array_key_exists($this->id, $_GET)) 200 $this->current_page = $_GET[$this->id]; 201 202 $this->current_record = $this->current_page * $this->page_size; 203 } 204 205 // }}} 206 // {{{ protected function displayPrev() 207 208 /** 209 * Displays the previous page link 210 */ 211 protected function displayPrev() 212 { 213 if ($this->prev_page != -1) { 214 $link = $this->getLink(); 215 216 $anchor = new SwatHtmlTag('a'); 217 $anchor->href = sprintf($link, (string) $this->prev_page); 218 $anchor->setContent(sprintf(Swat::_('%s Previous'), '«')); 219 $anchor->class = 'nextprev'; 220 $anchor->display(); 221 } else { 222 $span = new SwatHtmlTag('span'); 223 $span->class = 'nextprev'; 224 $span->setContent(sprintf(Swat::_('%s Previous'), '«')); 225 $span->display(); 226 } 227 } 228 229 // }}} 230 // {{{ protected function displayPosition() 231 232 /** 233 * Displays the current page position 234 * 235 * i.e. "1 of 3" 236 */ 237 protected function displayPosition() 238 { 239 $div = new SwatHtmlTag('div'); 240 $div->class = 'position'; 241 242 $div->setContent(sprintf(Swat::_('Page %d of %d'), 243 $this->current_page + 1, $this->total_pages)); 244 245 $div->display(); 246 } 247 248 // }}} 249 // {{{ protected function displayNext() 250 251 /** 252 * Displays the next page link 253 */ 254 protected function displayNext() 255 { 256 if ($this->next_page != -1) { 257 $link = $this->getLink(); 258 259 $anchor = new SwatHtmlTag('a'); 260 $anchor->href = sprintf($link, (string) $this->next_page); 261 $anchor->setContent(sprintf(Swat::_('Next %s'), '»')); 262 $anchor->class = 'nextprev'; 263 $anchor->display(); 264 } else { 265 $span = new SwatHtmlTag('span'); 266 $span->class = 'nextprev'; 267 $span->setContent(sprintf(Swat::_('Next %s'), '»')); 268 $span->display(); 269 } 270 } 271 272 // }}} 273 // {{{ protected function displayPages() 274 275 /** 276 * Displays a smart list of pages 277 */ 278 protected function displayPages() 279 { 280 $j = -1; 281 282 $link = $this->getLink(); 283 284 $anchor = new SwatHtmlTag('a'); 285 $span = new SwatHtmlTag('span'); 286 $current = new SwatHtmlTag('span'); 287 $current->class = 'current'; 288 289 for ($i = 0; $i < $this->total_pages; $i++) { 290 $display = false; 291 292 if ($this->current_page < 7 && $i < 10) { 293 // Current page is in the first 6, show the first 10 pages 294 $display = true; 295 296 } elseif ($this->current_page >= $this->total_pages - 7 && 297 $i >= $this->total_pages - 10) { 298 299 // Current page is in the last 6, show the last 10 pages 300 $display = true; 301 302 } elseif ($i <= 1 || $i >= $this->total_pages -2 || 303 abs($this->current_page - $i) <= 3) { 304 305 // Always show the first 2, last 2, and middle 6 pages 306 $display = true; 307 } 308 309 if ($display) { 310 if ($j + 1 != $i) { 311 // ellipses 312 $span->setContent('…'); 313 $span->display(); 314 } 315 316 if ($i == $this->current_page) { 317 $current->setContent((string)($i + 1)); 318 $current->display(); 319 } else { 320 $anchor->href = sprintf($link, (string)$i); 321 $anchor->title = 322 sprintf(Swat::_('Go to page %d'), ($i + 1)); 323 324 $anchor->setContent((string)($i + 1)); 325 $anchor->display(); 326 } 327 328 $j = $i; 329 } 330 } 331 } 332 333 // }}} 334 // {{{ private function getLink() 335 336 /** 337 * Gets the base link for all page links 338 * 339 * This removes all unwanted elements from the get variables and adds 340 * all the wanted ones back into an acceptable url string. 341 * 342 * @return string the base link for all pages with cleaned get variables. 343 */ 344 private function getLink() 345 { 346 //$vars = array_diff_key($_GET, array_flip($this->unset_get_vars)); 347 $vars = $_GET; 348 349 foreach($vars as $name => $value) 350 if (in_array($name, $this->unset_get_vars)) 351 unset($vars[$name]); 352 353 if ($this->link === null) 354 $link = '?'; 355 else 356 $link = $this->link.'?'; 357 358 foreach($vars as $name => $value) 359 $link .= $name.'='.urlencode($value).'&'; 360 361 $link.= urlencode($this->id).'=%s'; 362 363 return $link; 364 } 365 366 // }}} 367 // {{{ private function calculatePages() 368 369 /** 370 * Calculates page totals 371 * 372 * Sets the internal total_pages, next_page and prev_page properties. 373 */ 374 private function calculatePages() 375 { 376 $this->total_pages = ceil($this->total_records / $this->page_size); 377 378 if (($this->total_pages <= 1) || 379 ($this->total_pages - 1 == $this->current_page)) 380 $this->next_page = -1; 381 else 382 $this->next_page = $this->current_page + 1; 383 384 if ($this->current_page > 0) 385 $this->prev_page = $this->current_page - 1; 386 else 387 $this->prev_page = -1; 388 } 389 390 // }}} 391 } 392 393 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| [ Powered by PHPXref - Served by Debian GNU/Linux ] |