<?php if (!defined('PmWiki')) exit();

/* breakpagelist.php for PmWiki 2, to break a long pagelist display into several subpages, 
   and display links to all subpages as a series of page numbers.
   Copyright 2009 Hans Bracker
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published
   by the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   Usage: 
   Create first pagelist for determining page count and displaying navigation links: 
   (:pagelist .... fmt=bplnavlinks :)
   Create second pagelist as the pagelist with formatted output of items:
   (:pagelist ..... count={$BPLRange} :)
   Both pagelists should have same listing parameters (to get same pagelist results),
   apart from the difference in fmt= and count= parameters,
   but you can use in the fmt=bplnavlinks pagelist optional parameters 
   bplpre= and bplsuf= for setting a different format for the nav links, 
   using standard inline wiki markup.
*/

$RecipeInfo['BreakPageList']['Version'] = '2009-02-13';

SDVA($BPLConfig, array(
	'count' => 10000,     //initial count #, must be higher than expected max number of pages in list
	'perpageitems' => 20, //set number of pagelist items to be displayed per page
	'urlargkey' => 'p',   //set key used for url parameter
	'words' => array(
				'res' => 'Results',
				'pg'  => 'Page',
				'of'  => 'of',
				'go'  => 'Go to page',
				'nxt' => 'Next',
				'prv' => 'Previous',
	),
));	

# Page variable {$BPLRange} for (:pagelist .... count={$BPLRange} :) to set range of pages
$FmtPV['$BPLRange'] = 'BreakPageList($pagename, "range")';

# fmt=bplnavlinks for pagelist  to generate $BPLPageCount and create nav links
# by calling BreakPageList() 
SDVA($FPLFormatOpt['bplnavlinks'], array('fn' => 'BPLNavLinks'));
function BPLNavLinks($pagename, &$matches, $opt) {
	global $BPLConfig;
	$matches = array_values(MakePageList($pagename, $opt, 0));
	$BPLConfig['count'] = count($matches);
	return BreakPageList($pagename, 'links', $opt);
} //}}} 
  
# dual use function:
# calculate 'range' for (:pagelist ..... count={$BPLRange} :), 
# generate navigation links to page chunks for (:pagelist .... fmt=bplnavlinks:)
function BreakPageList($pagename, $opt, $args='') {
	global $BPLConfig;
	$res = $BPLConfig['count'];
	$ppi = $BPLConfig['perpageitems'];
	$key = $BPLConfig['urlargkey'];
	$wds = $BPLConfig['words'];
	$wdres = ($args['bplresults']) ? $args['bplresults'] : $wds['res'];
	if($ppi==0) $ppi=1; //prevent div by zero error
	$pmax = ceil(intval($res)/$ppi);
	if ($pmax<=0) $pmax=1;
	$p = @$_REQUEST[$key];
	if ($p<1) $p=1;
	if ($p>$pmax) $p=$pmax;
	$from = ($p-1)*$ppi+1;
	$to = $p*$ppi;
	$range = $from."..".$to;
	switch($opt) {
		case 'range': return $range;
		case 'links': 
			if (isset($args['bplpre'])) $nav = $args['bplpre'];
			elseif ($pmax > 1) 
				$nav = "$res $wdres &ndash; {$wds['pg']} $p {$wds['of']} $pmax &ndash; {$wds['go']}";
			else $nav = "$res $wdres &ndash; {$wds['pg']}";
			$prev = $p-1;
			if ($prev>0) $nav .= " [[$pagename?$key=$prev|{$wds['prv']}]]";
			for($i=1; $i<=$pmax; $i++) {
				if ($i==$p) $nav .= " $p";
				elseif (($i==$p-3 && $i>1) OR ($i==$p+3 && $i<$pmax)) $nav .= " ...";
				elseif ($i==$p-1 || $i==$p+1 || $i==$p-2 || $i==$p+2 || $i==1 || $i==$pmax)
					$nav .= " [[$pagename?$key=$i|$i]]";
			}
			$next = $p+1;
			if ($next<=$pmax) $nav .= " [[$pagename?$key=$next|{$wds['nxt']}]]";	
			$nav .= " ".@$args['bplsuf'];
			return $nav;
	}
} //}}}