<?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_eventgoods($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']!=""){		
			if($params['limit']!=="false"){			
				$limit = $params['limit'];
				if(settype($limit,"integer")){
					$limit = $limit;
				}else{
					$limit = 10;
				}
			}else{
				$limit =false;
			}
		}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($page && settype($page,"integer")){
			$offset = ($page-1)*$pageSize;
			$limit = "limit $offset,$pageSize";
		}
		if($params['limit']=="false"){
			$limit = "";
		}

		if(isset($params['catId']) && $params['catId']!="" && settype($params['catId'],"integer")){
			$catId = $params['catId'];
		}else{
			$catId = false;
		}
		if($catId){
			$wheres = " and catId in (select id from qk_category where id='".$catId."' or parentId='".$catId."')";
		}

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

		if(isset($params['thumb'])){
			if($params['thumb']=="true"){
				$wheres .= " and thumbId<>0";
			}else if($params['thumb']=="false"){
				$wheres .= " and thumbId=0";
			}
		}
		if(isset($params['logo'])){
			if($params['logo']=="true"){
				$wheres .= " and logoId<>0";
			}else if($params['logo']=="false"){
				$wheres .= " and logoId=0";
			}
		}
		
		$allCount = $GLOBALS['db']->getOne("select count(id) from qk_event_goods where 1=1 $wheres");
		
		$smarty->assign("allCount",$allCount);
		$res = $GLOBALS['db']->getAll("select * from qk_event_goods where 1=1 $wheres order by sortId asc,id desc $limit");
		
		$i = 0;
		load_class("attachment");
		$am = new AttachmentModel();
		load_class("goods");
		$gm = new GoodsModel();
		foreach($res as $k=>$v){
			$res[$i]['thumbnail']['url'] = $am->getUrl($v['thumbId']);
			$res[$i]['logo']['url'] = $am->getUrl($v['logo']);
			$res[$i]['eventName'] = $GLOBALS['db']->getOne("select name from qk_event where id='".$v['eventId']."'");
			$res[$i]['goodsName'] = $GLOBALS['db']->getOne("select name from qk_goods where id='".$v['goodsId']."'");
			$res[$i]['goodsInfo'] = $gm->getInfo($v['goodsId']);
			$res[$i]['index'] = $i;
			$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;
}

?>