<?php
 /**
 *      [XCECP] (C) COLNET Inc.
 *      This is NOT a freeware, use is subject to license terms
 */
if(!defined("IN_QKWEB")){exit("Access Denied");}
function smarty_function_pager($params, $template)
{	
	global $_G;
    $allCount = (isset($params['allCount']) && settype($params['allCount'],"integer"))?$params['allCount']:0;
	$prevnext = (isset($params['prevnext']) && ($params['prevnext']=='false' || $params['prevnext']=='true'))?$params['prevnext']:'';
    $dispatcher = (isset($params['dispatcher']) && $params['dispatcher']=='false')?false:true;
	if(!$allCount){
		$allCount = $template->getTemplateVars("allCount");
	}
	$type = $params['type'];
	$url = str_replace("index.php","",$_SERVER["PHP_SELF"]);
	if(isset($params['url']) && $params['url']){$url = $params['url'];}
	if(isset($params['type']) && $type!='category' && $type!='goods'){
		if($type=="goods"){
			$url = "/goods/list/";
			if(MODEL_NAME=='Admin'){
				$url = "/qkadmin/goods/act/list/";
			}
			if($type=="goods"){
				$iStatus = $_REQUEST['iStatus'];
				if($iStatus=='1' || $iStatus=='0'){
					$url.="iStatus/$iStatus/";
				}
				if($brand = $_GET['brand']){
					$url.="brand/$brand/";
				}
				if($sort = $_GET['sort']){
					$url.="sort/$sort/";
				}
				if($key = $template->getTemplateVars("key")){
					$url.="key/".urlencode($key)."/";
				}
				$p = $_REQUEST['p'];if(!$p){$p = $_REQUEST['promotion'];}
				if($p=='1' || $p=='0'){
					$url.="p/$p/";
				}
			}
		}else if($type=="article"){
			$url = "/article/list/";
				if($cat = getgpc("cat")){
					$url.="cat/$cat/";
				}
			$catId = $template->getTemplateVars("catId");
			if($catId){
					$url.="catId/$catId/";
			}
		}else if($type=="domain"){
			$url = "/qkadmin/domain/";
			if($keyTypeId = getgpc("keyTypeId")){
				$url.="keyTypeId/$keyTypeId/";
			}
			if($day = getgpc("day")){
				$url.="day/$day/";
			}
		}else if($type == "product"){
			$url="/qkadmin/product/";
		}else if($type == "hosting"){
			$url="/qkadmin/hosting/";
		}else if($type=="finance"){
			$url = "/qkadmin/finance/";
		
			if($userId = getgpc("userId")){
				$url.="userId/$userId/";
			}
		}
		if($iStatus = getgpc("iStatus")){
			$url.="iStatus/".urlencode($iStatus)."/";
		}
		if($catId = getgpc("catId")){
			$url.="catId/".urlencode($catId)."/";
		}
		if($key = getgpc("key")){
			$url.="key/".urlencode($key)."/";
		}
		if($apiId = getgpc("apiId")){
			$url.="apiId/".urlencode($apiId)."/";
		}
		if($order = getgpc('order','GP')){
					$url.="order/$order/";
		}
		$url .= "page/";

	}else{
		if(!$dispatcher){
			$role=$params['role'];
			if($type=='category'){
				$url=$GLOBALS['baseUrl'].'category/'.$template->getTemplateVars('catId').'.html?';
				//$sort=trim(getgpc('sort'));if($sort && in_array($sort,array('sales','price','time'))){$url.='sort='.$sort;}
			}else if($type=="goods"){
				$url=$GLOBALS['baseUrl'].'goods/';
				if($role=='admin'){$url=$GLOBALS['adminUrl'].'goods/';}
				if($temp=urlAfter($url,'catId',getgpc('catId'))){$url=$temp;}
			}
			if($temp=urlAfter($url,'sort',getgpc('sort'))){$url=$temp;}
			if($temp=urlAfter($url,'filter',getgpc('filter'))){$url=$temp;}
			if($temp=urlAfter($url,'key',getgpc('key'))){$url=$temp;}
			$endstr = substr($url,-1,1);
			if(strpos($url,'&')===false && strpos($url,'?')===false){
				$url .= '?page=';
			}else if(strpos($url,'?')>-1){
				if(substr($url,-1,1)!='&' && substr($url,-1,1)!='?'){
					$url .= '&page=';
				}else{
					$url .= 'page=';
				}
			}
		}
	}
	$limit = $template->getTemplateVars("limit");
	$cat = $template->getTemplateVars("cat");
	if($cat){
		$cat = $cat."/";
	}
	$page = getgpc("page");
	if(!$page || settype($page,"integer")!=true){$page=1;}
	
	if($limit){
		$pageSize = $limit;
		$pageCount = ceil($allCount/$limit);
		//$retval = "共 $allCount 条记录，共 $pageCount 页 当前第 $page 页<br>";
		$retval = "";
		if($page!=1 && $prevnext!='false'){
			if($dispatcher){
				$retval .= "<a class='page-start' href=\"$url".$cat."1\">".$_G['lang']['pager']['first']."</a>";			
			}else{
				$retval .= "<a class='page-start' href=\"{$url}1\">".$_G['lang']['pager']['first']."</a>";
			}
		}else{
			$retval .= "<span class='prev-disabled page-start'>".$_G['lang']['pager']['first']."</span>";
		}

		if(isset($params['number']) && $params['number'] && $params['number']=="false"){
			$number=false;
		}else{
			$number=true;
		}

		if($prevnext!='false'){
			if($page>1){
				$retval .= " <a class='page-start'' href=\"$url".$cat.($page-1)."\">".$_G['lang']['pager']['prev']."</a>";
			}else{
				$retval .= " <span class='prev-disabled page-start'><i class=\"fa fa-chevron-left\"></i> ".$_G['lang']['pager']['prev']."</span>";
			}
		}
	
		if($pageCount && $number){
			$max = $pageCount;
			if($pageCount>9){$max=9;}else{$max=$pageCount;}
			$thisPage = $page;
			if($thisPage<6){$thisPage=1;}else{$thisPage=$page-4;}
			for($i=0;$i<$max;$i++){
				if($thisPage==$page){
					$retval .= "<span class=\"page-cur\">".($thisPage)."</span>";
				}else{
					$retval .= "<a href=\"$url".$cat.($thisPage)."\">".($thisPage)."</a>";
				}
				$thisPage++;
				if($thisPage>$pageCount){break;}
			}
		}
		if($prevnext!='false'){
			if($page<$pageCount){
				if($dispatcher){
					$retval .= " <a href=\"$url".$cat.($page+1)."\" class='page-next'><i class=\"fa fa-chevron-right\"></i> ".$_G['lang']['pager']['next']."</a>";
				}else{
					$retval .= " <a href=\"{$url}".($page+1)."\" class='page-next'><i class=\"fa fa-chevron-right\"></i> ".$_G['lang']['pager']['next']."</a>";
				}
			}else{
				$retval .= " <span class='next-disabled  page-start'>".$_G['lang']['pager']['next']."</span>";
			}
			if($page!=$pageCount && $allCount){
				if($dispatcher){
					$retval .= " <a href=\"$url".$cat."$pageCount\" class='page-end'>".$_G['lang']['pager']['last']."</a>";
				}else{
					$retval .= " <a href=\"{$url}$pageCount\" class='page-end'>".$_G['lang']['pager']['last']."</a>";
				}
			}else{
				$retval .= " <span class='next-disabled  page-end'>".$_G['lang']['pager']['last']."</span>";
			}
		}
		
		if($pageCount==1 || !$allCount){
			$retval = "";
		}
	}else{
		$retval = "";
	}

    return $retval;
}

?>