1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
|
<?php
require_once dirname(__FILE__) . '/Widget.php';
/**
* The Horde_UI_Pager:: provides links to individual pages.
*
* $Horde: framework/UI/UI/Pager.php,v 1.7.10.8 2006/06/20 09:08:26 jan Exp $
*
* Copyright 2004-2006 Joel Vandal <jvandal@infoteck.qc.ca>
*
* See the enclosed file COPYING for license information (LGPL). If you
* did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
*
* @author Ben Chavet <ben@chavet.net>
* @author Joel Vandal <jvandal@infoteck.qc.ca>
* @author Chuck Hagenbuch <chuck@horde.org>
* @since Horde_UI 0.0.1
* @package Horde_UI
*/
class Horde_UI_Pager extends Horde_UI_Widget {
function Horde_UI_Pager($name, &$vars, $config)
{
if (!isset($config['page_limit'])) {
$config['page_limit'] = 10;
}
if (!isset($config['perpage'])) {
$config['perpage'] = 100;
}
parent::Horde_UI_Widget($name, $vars, $config);
}
/**
* Render the pager.
*
* @return string HTML code containing a centered table with the pager
* links.
*/
function render()
{
global $prefs, $registry, $conf;
$num = $this->_config['num'];
$url = $this->_config['url'];
$page_limit = $this->_config['page_limit'];
$perpage = $this->_config['perpage'];
$current_page = $this->_vars->get($this->_name);
// Figure out how many pages there will be.
$pages = ($num / $perpage);
if (is_integer($pages)) {
$pages--;
}
$pages = (int)$pages;
// Return nothing if there is only one page.
if ($pages == 0 || $num == 0) {
return '';
}
$html = '<div class="pager">';
// Create the '<< Prev' link if we are not on the first page.
$link = Util::addParameter($url, $this->_name, $current_page - 1);
$link = $this->_addPreserved($link);
if ($current_page > 0) {
$html .= Horde::link(Horde::applicationUrl($link), '', 'prev') . htmlspecialchars(_("<Previous")) . '</a>';
}
// Figure out the top & bottom display limits.
$bottom = max(0, $current_page - ($page_limit / 2) + 1);
$top = $bottom + $page_limit - 1;
if ($top - 1 > $pages) {
$bottom -= ($top - 1) - $pages;
$top = $pages + 1;
}
// Create bottom '[x-y]' link if necessary.
$link = $this->_addPreserved(Util::addParameter($url, $this->_name, $bottom - 1));
if ($bottom > 0) {
$html .= ' ' . Horde::link(Horde::applicationUrl($link), '', 'prevRange') . '[' . ($bottom == 1 ? $bottom : '1-' . $bottom) . ']</a>';
}
// Create links to individual pages between limits.
for ($i = $bottom; $i <= $top && $i <= $pages; ++$i) {
if ($i == $current_page) {
$html .= ' <strong>(' . ($i + 1) . ')</strong>';
} elseif ($i >= 0 && $i <= $pages) {
$link = $this->_addPreserved(Util::addParameter($url, $this->_name, $i));
$html .= ' ' . Horde::link(Horde::applicationUrl($link)) . ($i + 1) . '</a>';
}
}
// Create top '[x-y]' link if necessary.
if ($top < $pages) {
$link = $this->_addPreserved(Util::addParameter($url, $this->_name, $top + 1));
$html .= ' ' . Horde::link(Horde::applicationUrl($link), '', 'nextRange') . '[' .
($top + 2 == $pages + 1 ? $pages + 1 : ($top + 2) . '-' . ($pages + 1)) . ']</a>';
}
// Create the 'Next>>' link if we are not on the last page.
if ($current_page < $pages) {
$link = $this->_addPreserved(Util::addParameter($url, $this->_name, $current_page + 1));
$html .= ' ' . Horde::link(Horde::applicationUrl($link), '', 'next') . htmlspecialchars(_("Next>")) . '</a>';
}
return $html . '</div>';
}
}
|