<?php

#
#	Plug-in for CMS - CMS Made Simple
#	Made in 2010 by Stabbquadd (stabbquadd@gmail.com)
#	This project's homepage is : http://dev.cmsmadesimple.org/projects/randomize
#
#	To install this plug-in, simply drop this file into the plugins directory of your CMS Made Simple installation
#
#	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.
#
#	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 at  http://www.gnu.org/licenses/gpl.html
#

function smarty_cms_function_randomize($params, &$smarty) {

	$file		=isset($params['file']) ? $params['file']:false;
	$nbr		=isset($params['nbr']) ? $params['nbr']:1;
	$delimiter	=isset($params['delimiter']) ? $params['delimiter']:"";

	$fp = fopen($file, "r");
	$contents = fread($fp, filesize($file));
	$text = explode("\n",$contents);
	fclose($fp);
	$display="";
	if(sizeof($text)<$nbr) $nbr=sizeof($text);
	for ($i=1;$i<=$nbr;$i++){
		srand((double)microtime()*1000000);
		$index = (rand(1, sizeof($text)) - 1);
		$display .= $text[$index].$delimiter;
		unset($text[$index]);
		$text=array_values($text);
	}
	
	$delen		= strlen($delimiter);
	if ( $delen > 0 ) {
		$display = substr($display, 0, $delen);
	}
	
	return $display;

}

function smarty_cms_help_function_randomize() {
	?>
	<h3>What does this do?</h3>
	<p>It prints a random line in the given text file.</p>
	<h3>How do I use it?</h3>
	<p>You need to insert this tag : <code>{randomize file="path"}</code></p>
	<h3>What parameters does it take?</h3>
	<ul>
		<li><em>file</em> - The path to the text file you need to randomize</li>
		<li><em>nbr</em> (optional) - The number of randomized lines to display</li>
		<li><em>delimiter</em> (optional) - What you want to put between two randomized lines (you may want something like a <pre><br /></pre>)</li>
	</ul>
	</p>
	<?php
}

function smarty_cms_about_function_randomize() {
	?>
	<p>Author: Verticea&lt;webmaster@verticea.fr&gt;</p>
	<p>Version: 0.1</p>
	<p>
	Change History:<br/>
	None
	</p>
	<?php
}
?>
