<?php


function smarty_function_db_fetchpage( $params, &$smarty )
{
	extract( $params );
	$table = !empty( $table ) ? trim( $table ) : "";
	$items = !empty( $items ) ? trim( $items ) : "";
	$condition = !empty( $condition ) ? trim( $condition ) : "";
	$orderby = !empty( $orderby ) ? trim( $orderby ) : "";
	$page = !empty( $page ) ? intval( $page ) : 0;
	$pagesize = !empty( $pagesize ) ? intval( $pagesize ) : 20;
	$varname = !empty( $varname ) ? trim( $varname ) : "";
	$necessaryParams = array( "table", "items", "pagesize", "varname" );
	$paramCount = count( $necessaryParams );
	$i = 0;
	for ( ;	$i < $paramCount;	++$i	)
	{
		if ( !isset( $necessaryParams[$i] ) )
		{
			$smarty->trigger_error( "db_FetchPage: missing '".$necessaryParams[$i]."' parameter" );
		}
		else
		{
			$$necessaryParams[$i] = trim( $$necessaryParams[$i] );
		}
	}
	$sqlCondition = "";
	if ( !empty( $condition ) )
	{
		$sqlCondition = "WHERE (".$condition.")";
	}
	$sqlOrderby = "";
	if ( !empty( $orderby ) )
	{
		$sqlOrderby = "ORDER BY ".$orderby;
	}
	global $db;
	global $gDb;
	$table = $gDb['prefix'].$table;
	$sql = "SELECT ".$items." FROM {$table} {$sqlCondition} {$sqlOrderby}";
	$arr = $db->fetchpage( $sql, $page, $pagesize );
	$smarty->assign( $varname, $arr );
	unset( $arr );
}

?>
