<?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_block_category($params, $content, $smarty, &$repeat)
{ 
  	if(!isset($params['item']) || $params['item']==""){
		if(!isset($params['name']) || $params['name']==""){
			$params['name'] = "item";
		}
	}else{
		$params['name'] = $params['item'];
	}
	$dataindex = md5(__FUNCTION__ . md5(serialize($params)));
	if(!isset($smarty->block_data)){$smarty->block_data=array();}
	if(!isset($smarty->block_data[$dataindex])){
		$page = getgpc("page");
		$wheres = "";
		if(isset($params['limit']) && $params['limit']!=""){		
			if($params['limit']!=="false"){			
				$limit = $params['limit'];
				if(settype($limit,"integer")){
					$limit = $limit;
				}else{
					$limit = 10;
				}
			}else{
				$limit =false;
			}
		}else{
			$limit = 10;
		}
		$fields = '';
		$pageSize = $limit;
		$smarty->assign("limit",$limit);
		$limit = "limit $limit";
		$page = getgpc("page");
		if(!$page || !settype($page,"integer") || $params['pager']!="true"){$page=1;}
		$offset = ($page-1)*$pageSize;
		$limit = "limit $offset,$pageSize";
		if($page && settype($page,"integer")){
			$offset = ($page-1)*$pageSize;
			$limit = "limit $offset,$pageSize";
		}
		if($params['limit']=="false"){
			$limit = "";
		}
		$fields="";
		if(isset($params['parentId']) && $params['parentId']!==""){
			$parentId = $params['parentId'];
			if(!settype($parentId,"integer")){$parentId=0;}
		}else{
			$parentId = false;
		}
		if($parentId!==false){
			$wheres = " and parentId in ('".$parentId."')";
		}
		if(isset($params['thumb'])){
			$fields="thumb";
			if($params['thumb']=="true"){
				$wheres .= " and thumbId<>0";
			}else if($params['thumb']=="false"){
				$wheres .= " and thumbId=0";
			}
		}
		if(isset($params['mainNav'])){
			$mainNav=$params['mainNav'];
			if($mainNav && is_numeric($mainNav)){$wheres.=' and `mainNav`='.$mainNav;}
		}
		$key=$params['key'];
		if($key){ $wheres .= " and name like '%".$key."%'"; }
		if(isset($params['id']) && $params['id']!=""){
			$parentId = $params['id'];
			if(settype($parentId,"integer")){
				$wheres .= " and id='$parentId'";
			}
		}
		$sqlOrderBy = "";
		if(isset($params['orderBy']) && $params['orderBy']!=""){
			$orderbys = $params['orderBy'];
			$orderbyArr = explode(",",$orderbys);
			$orderbys = "";
			foreach($orderbyArr as $val){
				$orderArr = explode(" ",$val);
				if($orderArr[0] && isset($orderArr[1]) && in_array($orderArr[1],array("asc","desc"))){
					if($orderArr[0]!=""){
						if($sqlOrderBy==""){
							$sqlOrderBy .= $orderArr[0]." ".$orderArr[1];
						}else{
							$sqlOrderBy .= ",".$orderArr[0]." ".$orderArr[1];
						}
					}
				}
			}
		}else{
			$sqlOrderBy = " id asc";
		}

		if(defined('SITE_ID') && $GLOBALS['siteConf']['privateData']){$wheres .= " and (siteId='".SITE_ID."' or siteId=0)";}

		$allCount = $GLOBALS['db']->getOne("select count(id) from qk_category where iStatus=1 $wheres");
		
		$smarty->assign("allCount",$allCount);
		$res = $GLOBALS['db']->getAll("select id,name,parentId,seoUrl,sortId,posttime,template,mainNav from qk_category where iStatus=1 $wheres order by $sqlOrderBy $limit");
		$i = 0;
		load_class("category");
		$cm = new CategoryModel();
		foreach($res as $k=>$v){
			$res[$i] = $cm->getInfoExt($v,'id,name,parentId,url,subNum');
			$res[$i]['index'] = $i;
			$res[$i]['allCount'] = $allCount;
			$i++;
		}
		$smarty->block_data[$dataindex]=$res;
	}
	if(is_array($smarty->block_data[$dataindex]) && $smarty->block_data[$dataindex]){
		$item = array_shift($smarty->block_data[$dataindex]);
		$smarty->assign($params['name'], $item);
		if(count($smarty->block_data[$dataindex])==0){
			$smarty->block_data[$dataindex] = false;
		}
		$repeat= true;
	}else{
		unset($smarty->block_data[$dataindex]);
		$repeat = false;
	}
	echo $content;
}

?>