<?php
namespace Bitweaver\Plugins;

/*** Handle jQuery plugin naming conflict between jQuery UI and Bootstrap ***/
/**
 * Smarty {pagination} function plugin
 * @package Smarty
 * @subpackage plugins
 * @link https://www.bitweaver.org/wiki/function_pagination function_pagination
 */

/**
 * Smarty {pagination} function plugin
 *
 * Type:     function<br>
 * Name:     pagination<br>
 * Input:<br>
 *           - <attribute>=<value>  (optional) - pass in any attributes and they will be added to the pagination string<br>
 * Output:   url of the form: $_SERVER[SCRIPT_NAME]?attribute1=value1&attribute2=value2
 */
function smarty_function_pagination( $params, &$gSmartyTemplate ) {
	global $gBitSmarty;

	$pgnUrl = $gBitSmarty->getTemplateVars('returnURL');
	if ( isset( $params['url'] ) ) {
		   $pgnUrl = $params['url'];
		unset( $params['url'] );
	}
	if( empty( $pgnUrl ) ) {
		$pgnUrl = $_SERVER['SCRIPT_NAME'];
	}

	$gBitSmarty->assign( 'pgnUrl', $pgnUrl );

	$pgnVars = '';
	$pgnHidden = [];
	foreach( $params as $form_param => $form_val ) {
		if ( !is_array($form_val)) {
			$pgnVars .= "&amp;$form_param=$form_val";
			$pgnHidden[$form_param] = $form_val;
		}
	}

	$gBitSmarty->assign( 'pgnVars', $pgnVars );
	$gBitSmarty->assign( 'pgnHidden', $pgnHidden );
	return $gBitSmarty->fetch('bitpackage:kernel/pagination.tpl');
}
