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

/*
	slideshow.php, a recipe for PmWiki
	Adam Overton, August 2009
	based on code by Patrick R. Michaud and Stirling Westrup
	from the ?refresh=nn scripts at http://pmwiki.org/wiki/Cookbook/AutomaticPageRefresh,
	
	. . .
	
	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.
	http://www.gnu.org/copyleft/gpl.html
	
	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.
	
	. . .

	slideshowRefresh.php creates a "slideshow" by using the 'Refresh' meta tag, coupled with pagelist in order to automatically load one-page-after-another on a wiki, at an interval specified by the visitor. It uses the 'slideshow' action and the following urlfmt as it's foundation:

	?action=slideshow&wait=nn
	
	Appending this to your URL will advance the browser to the next page within the current group every nn-seconds. The underlying code is using the 'Refresh' meta-tag, but it's basically redirecting to another page each time, rather than remaining on the same page. (This recipe is based on the code for Cookbook/AutomaticPageRefresh.)
	
	The recipe also accepts the additional query '&anchor=myAnchor', which will return the viewer to #myAnchor.
	
	Finally, several pagelist parameters are available:  &group=group&name=name&order=random&list=normal
	
	An example URL:
	     http://mySite.com/Main/Test?action=slideshow&wait=30&order=random&group=Main
	     &name=-Blam&list=normal&anchor=midPage
	
	... this example will randomly move through the pages in the group Main, meanwhile avoiding the page Blam, refreshing every 30 seconds, and always looking for and landing on the #midPage anchor.

*/

$RecipeInfo['slideshowRefresh']['Version'] = '2009-09-03';

$HandleActions['slideshow'] = 'slideshow_func';
$HandleAuth['slideshow'] = 'read';              # authorization level $auth

function slideshow_func($pagename, $auth) {
	global $group, $name, $HTMLHeaderFmt;
	
		# determine duration of refresh
		if (@$_GET['wait']) {
			$refresh = $_GET['wait'];
			$refreshQ = "&wait=$refresh"; # add to URL
		} else { HandleBrowse($pagename); }
		# determine an anchorname
		if (@$_GET['anchor']) {
			$anchor = "#".$_GET['anchor'];	# get the specified anchorname
			$anchorQ = "&anchor=".$_GET['anchor'];	# make sure to call the same anchor on the next page
		}
		# use pagelist to determine the next page to visit
		if (@$_GET['group']) { 
			$grp = $_GET['group'];
			$grpQ = "&group=$grp"; # add 'group' to URL
		} else { $grp = $group; }
		
		if (@$_GET['name']) { 
			$nm = $_GET['name']; 
			$nmQ = "&name=$nm"; # add 'name' to URL
		} else { $nm = ''; }
		
		if (@$_GET['list']) { 
			$lst = $_GET['list']; 
			$lstQ = "&list=$lst"; # add 'list' to URL
		} else { $lst = 'normal'; }

		if (@$_GET['order']) {
			$order = $_GET['order'];
			$orderQ = "&order=$order"; # add 'order' to URL
		} else { $order = 'name'; }

		# i= is the current index determining the current page; don't set it if 'order=random'
		if (@$_GET['i'] && @$_GET['order']!='random') { $i = $_GET['i']; }
		else { $i = 0; }

		$nextPageArray = nextPage_func($pagename,$grp,$nm,$lst,$order,$i);
		$nextPage = $nextPageArray[0]; # get nextPage

		if (@$_GET['order']!='random') {
			$i++; # increment for next time
			if ($i >= $nextPageArray[1]) $i = 0;  # reset counter
			$iQ = "&i=$i"; # add 'i' to URL
		}
		# change $nextPage from Group.Name to Group/Name
		$nextPage = str_replace('.','/',$nextPage);

		# Pagelist Query = group + name + list + order + i
		$pagelistQ = "$grpQ$nmQ$lstQ$orderQ$iQ";		
		# refreshQuery + pagelistQuery + anchorQuery + #anchor
		$fullQuery = "?action=slideshow$refreshQ$pagelistQ$anchorQ$anchor";
		
		$HTMLHeaderFmt['slideshow'] =
		"<meta http-equiv='Refresh' Content='$refresh; URL=".PageVar($pagename,'$ScriptUrl')."/$nextPage$fullQuery' />";

	//echo str_replace('<','',$HTMLHeaderFmt['slideshow']); break;
	HandleBrowse($pagename);	# display the page specified in the url (e.g. MyGroup.MyPage)
}


function nextPage_func($pagename,$grp,$nm='',$lst='normal',$order='name',$i='0') {
	# args for pagelist
	$opt = array(
		'name' => $nm
		,'list' => $lst
		,'group' => $grp
		,'order' => $order
	);
	# creates an array of all pages
	# note: this produces keys that are all out of whack - therefore, need to retrieve item from array_values()...
	$nextPageArray = array_values(MakePageList($pagename, $opt, 0));
	
/*	$j=0;
	echo "$i<br />";
	foreach(array_values($nextPageArray) as $key => $vv) { 
		if ($j==$i) { echo "<b>$key => $vv</b><br />"; }
		else { echo "$key => $vv<br />"; }
		$j++;
	}
*/

	$nextPage = $nextPageArray[$i]; # normal - choose i'th element from the current array
	//echo "<b>$nextPage</b>";
	# determine no. of pages
	$count = count($nextPageArray);
	return array($nextPage, $count);
}