<?php


function smarty_function_db_fetchpagecount( $params, &$smarty )
{
	extract( $params );
	$necessaryParams = array( "table", "pagesize", "varname" );
	$paramCount = count( $necessaryParams );
	$i = 0;
	for ( ;	$i < $paramCount;	++$i	)
	{
		if ( !isset( $necessaryParams[$i] ) )
		{
			$smarty->trigger_error( "fetchpagecount: missing '".$necessaryParams[$i]."' parameter" );
		}
		else
		{
			$$necessaryParams[$i] = trim( $$necessaryParams[$i] );
		}
	}
	$table = !empty( $table ) ? trim( $table ) : "";
	$item = !empty( $item ) ? trim( $item ) : "f_id";
	$condition = !empty( $condition ) ? trim( $condition ) : "";
	$pagesize = !empty( $pagesize ) ? trim( $pagesize ) : "20";
	$varname = !empty( $varname ) ? trim( $varname ) : "";
	if ( empty( $table ) )
	{
		$smarty->trigger_error( "fetchpagecount: invalid 'table' parameter" );
	}
	else
	{
		$sqlCondition = "";
		$condition = trim( $condition );
		if ( !empty( $condition ) )
		{
			$sqlCondition = "WHERE (".$condition.")";
		}
		global $db;
		global $gDb;
		$table = $gDb['prefix'].$table;
		$sql = "SELECT COUNT(".$item.") AS _count FROM {$table} {$sqlCondition}";
		$arr = $db->fetchrow( $sql );
		$count = $arr['_count'];
		$smarty->assign( $varname, ceil( $arr['_count'] / $pagesize ) );
		unset( $arr );
	}
}

?>
