<?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_post($params, $content, $smarty, &$repeat)
{ 
	
  	if(!isset($params['name']) || $params['name']==""){
		$params['name'] = "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']!="" && settype($params['limit'],"integer")){
			$limit = $params['limit'];
		}else{
			$limit = 10;
		}
		$pageSize = $limit;
		$smarty->assign("limit",$limit);
		$limit = "limit $limit";
		$page = getgpc("page");
		if(!$page || !settype($page,"integer")){$page=1;}
		$offset = ($page-1)*$pageSize;
		$limit = "limit $offset,$pageSize";

		if(isset($params['catId']) && $params['catId']!="" && settype($params['catId'],"integer")){
			$catId = $params['catId'];
		}else{
			$catId = false;
		}
		if($catId){
			$wheres .= " and catId='".$catId."'";
		}

		if(isset($params['articleId']) && $params['articleId']!="" && settype($params['articleId'],"integer")){
			$articleId = $params['articleId'];
		}else{
			$articleId = false;
		}
		if($articleId){
			$wheres .= " and articleId='".$articleId."'";
		}

		if(isset($params['goodsId']) && $params['goodsId']!="" && settype($params['goodsId'],"integer")){
			$goodsId = $params['goodsId'];
		}else{
			$goodsId = false;
		}
		if($goodsId){
			$wheres .= " and goodsId='".$goodsId."'";
		}

		if(isset($params['iStatus']) && $params['iStatus']!="" && settype($params['iStatus'],"integer")){
			$iStatus = $params['iStatus'];
		}else{
			$iStatus = false;
		}
		if($iStatus){
			$wheres .= " and iStatus='".$iStatus."'";
		}
	
		if(isset($params['parentId']) && settype($params['parentId'],"integer") && $params['parentId']!==""){
			$parentId = $params['parentId'];
		}else{
			$parentId = false;
		}
		if($parentId!==false){
			$wheres .= " and parentId='".$parentId."'";
		}

		if(isset($params['order']) && $params['order']!=""){
			$order = $params['order'];
		}else{
			$order = "id asc";
		}
		
		if(defined('SITE_ID')){$wheres .= " and (siteId='".SITE_ID."' or siteId=0)";}

		$allCount = $GLOBALS['db']->getOne("select count(id) from qk_post where 1=1 $wheres");

		if(!isset($params['pager']) || $params['pager']!="false"){
			$smarty->assign("allCount",$allCount);
		}

		if($page && settype($page,"integer")){
			$offset = ($page-1)*$pageSize;
			$limit = "limit $offset,$pageSize";
		}

		$res = $GLOBALS['db']->getAll("select * from qk_post where 1=1 $wheres order by $order $limit");
		
		load_class("user");$um=new UserModel();
		$i = 0;
		foreach($res as $k=>$v){
			$userInfo = $um->getInfo($v['userId']);
			$res[$i]['userInfo'] = $userInfo;
			$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;
}

?>