Uff, das ist komplizierter, als es zunächst aussah. Bei der "pagination" wird so viel auf- und abgerundet, dass ich es erstaunlich finde, dass da nicht mehr schief geht.
Mal sehen, ob ich mich am Wochenende da mal genauer durchwuseln kann. Für heute gebe ich auf.
[php]class pagination
{
var $requester;
var $parms;
var $start;
// constructor
function pagination($requester='', $parms='', $varname='')
{
$this->requester = $requester;
$this->parms = empty($parms) ? array() : $parms;
$this->varname = empty($varname) ? 'start' : $varname;
}
function append_sid($url, $non_html_amp = false)
{
global $SID;
if ( !empty($SID) && !preg_match('#sid=#', $url) )
{
$url .= ( ( strpos($url, '?') != false ) ? ( ( $non_html_amp ) ? '&' : '&' ) : '?' ) . $SID;
}
return $url;
}
function get_forks($total_items, $item_per_page, $current_item)
{
global $config;
// get the number of pages
$total_pages = ceil($total_items / $item_per_page);
if ( $total_pages == 1 )
{
return '';
}
// limits:
// - scope_min & scope_max are the limits for the number of pages displayed
// - the percentage is applied to the total items to get the scope
$scope_min = empty($config->data['pagination_min']) ? 5 : intval($config->data['pagination_min']); // 2 on sides + current page
$scope_max = empty($config->data['pagination_max']) ? 11 : intval($config->data['pagination_max']); // 5 on sides + current page
$scope_percent = empty($config->data['pagination_percent']) ? 10 : intval($config->data['pagination_percent']); // 10 %
// center on the current page : $scope is half the number of page around the current page ($middle)
$scope = ceil((min(max(intval($total_items * $scope_percent / 100), $scope_max), $scope_min) - 1) / 2);
$middle = floor($current_item / $item_per_page);
// get forks limits
$left_end = min($scope, $total_pages-1);
$middle_start = max($middle-$scope, $scope, 0);
$middle_end = min( $middle + $scope, $total_pages-$scope );
$right_start = max( $total_pages-$scope-1, 0 );
// middle get over edges
$is_left = $middle_start > $left_end+$scope;
$is_right = $middle_end+$scope < $right_start;
if ( !$is_left )
{
$middle_start = 0;
}
if ( !$is_right )
{
$middle_end = $total_pages-1;
}
// store forks
$forks = array();
if ( $is_left )
{
$forks[] = array(0, $left_end);
}
$forks[] = array($middle_start, $middle_end);
if ( $is_right )
{
$forks[] = array($right_start, $total_pages-1);
}
return $forks;
}
function display($tpl_switch, $total_items, $item_per_page, $current_item, $page_1=true, $item_name_count='', $item_total_count=0)
{
global $template, $config, $user;
if ( empty($item_per_page) )
{
$item_per_page = 50;
}
$pages = $this->get_forks($total_items, $item_per_page, $current_item);
$current_page = floor($current_item / $item_per_page);
$total_pages = ceil($total_items / $item_per_page);
$res = '';
$n_count = !empty($item_total_count) ? $item_total_count : $total_items;
$l_count = ($n_count == 1) ? $item_name_count . '_1' : $item_name_count;
if ( ($total_pages > 1) || (($total_pages == 1) && $page_1) )
{
$template->assign_block_vars($tpl_switch, array(
'START_FIELD' => $this->varname,
'START' => $current_page * $item_per_page,
'U_PREVIOUS' => append_sid($config->url($this->requester, $this->parms + array($this->varname => (($current_page > 0) ? ($current_page-1) * $item_per_page : 0)))),
'L_PREVIOUS' => $user->lang('Previous'),
'I_PREVIOUS' => $user->img('left_arrow'),
'PREVIOUS' => ($current_page > 0) ? ($current_page-1) * $item_per_page : 0,
'U_NEXT' => append_sid($config->url($this->requester, $this->parms + array($this->varname => (($current_page+1 < $total_pages) ? ($current_page+1) * $item_per_page : ($total_pages-1) * $item_per_page)))),
'L_NEXT' => $user->lang('Next'),
'I_NEXT' => $user->img('right_arrow'),
'NEXT' => ($current_page+1 < $total_pages) ? ($current_page+1) * $item_per_page : ($total_pages-1) * $item_per_page,
'L_GOTO' => $user->lang('Goto_page'),
'I_GOTO' => $user->img('icon_gotopost'),
'L_PAGE_OF' => sprintf($user->lang('Page_of'), $current_page+1, $total_pages),
'L_COUNT' => sprintf($user->lang($l_count), $n_count),
));
$template->set_switch($tpl_switch . '.previous', $current_page > 0);
$template->set_switch($tpl_switch . '.next', ($current_page+1) < $total_pages);
$template->set_switch($tpl_switch . '.unique', ($total_pages <= 1) );
// dump the forks
if ( $total_pages > 1 )
{
$first = true;
$count_pages = count($pages);
for ( $j = 0; $j < $count_pages; $j++ )
{
if ( !$first )
{
// send "...,"
$template->set_switch($tpl_switch . '.page_number');
$template->set_switch($tpl_switch . '.page_number.number', false);
}
for ( $k = $pages[$j][0]; $k <= $pages[$j][1]; $k++ )
{
$template->assign_block_vars($tpl_switch . '.page_number', array(
'U_PAGE' => append_sid($config->url($this->requester, $this->parms + array($this->varname => $k * $item_per_page))),
'PAGE' => ($k + 1),
'START' => ($k * $item_per_page),
'L_SEP' => ($j == $count_pages - 1) && ($k == $pages[$j][1]) ? '' : ', ',
));
$last = ($j == $count_pages - 1) && ($k == $pages[$j][1]);
$template->set_switch($tpl_switch . '.page_number.number');
$template->set_switch($tpl_switch . '.page_number.number.current', ($k == $current_page));
$first = false;
}
}
}
}
}
}[/php]
_________________ Praxis ist, wenn alles klappt aber keiner weiß warum. Theorie ist, wenn man weiß wie es geht, aber nichts klappt. Wir haben beides erfolgreich vereinigt: Bei uns klappt nichts und keiner weiß warum!
Neues Video: Marios freier Tag in Second Life
|