<?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_goodsfilter($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(($catId = $params['catId']) && is_numeric($catId)){
			load_class("category");
			$cm = new CategoryModel();
			$catInfo = $cm->getInfo($catId);
			$pFilter = getgpc("filter");
			$filterAttr = $catInfo['filter'];
			$filterValue = explode("-",$pFilter);
			$filter = array();
			if($filterAttr && is_array($filterAttr) && count($filterAttr)>0){
			if(count($filterAttr) == count($filterValue)){
				$i=0;
				foreach($filterAttr as $attr){
					$filter[$attr] = $filterValue[$i];
					$i++;
				}
			}
			assign("filter",$filter);
			if(!$pFilter){
				$filterValue = $filterAttr;
				$i=0;
				foreach($filterValue as $v){ $filterValue[$i] = 0; $i++; }
				//print_r($filterValue);
			}
			global $db;
			global $baseUrl;
			$filters = array();
			$i = 0;
			foreach($filterAttr as $item){
				$sql = "select * from qk_goods_attr_ext where attrId='".$item."' order by sortId asc,id asc";
				$filters[$i]['type'] = $db->getRow("select * from qk_goods_attr where id='".$item."'");
				$filters[$i]['item'] = $db->getAll($sql);
				$i++;
			}
			//print_r($filterAttr);
			$i=0;
			//print_r($filterAttr);//Array ( [0] => 6 [1] => 5 [2] => 3 [3] => 4 ) exit;
			//print_r($filterValue);//Array ( [0] => 6 [1] => 5 [2] => 3 [3] => 4 ) exit;
			//print_r($filters);
			//组合筛选URL
			foreach($filters as $item){
				$filter = $item['item'];	//每一种筛选方式
				$j=0;
				foreach($filter as $attr){//每一种筛选方式中的筛选值
					$filterUrl = "";
					$k=0;
					$filterAttrTemp = $filterValue;	//筛选方式组合参数数量
					foreach($filterAttr as $v){
						//echo $v."-".$item['type']['id'].",";
						//echo $k."-".$item['item'][$k]['id'].",";
						if($v==$item['type']['id']){
							//$extId=$item['item'][$k]['id'];
							$extId = $attr['id'];
							$filterAttrTemp[$k] = $extId;
							//$filterUrl.=$extId;
							$l = 0;
							$filterUrl = "";
							foreach($filterAttrTemp as $val){
								$filterUrl .= $val;
								$l++;
								if($l<count($filterAttrTemp)){
									$filterUrl.="-";
								}
							}
						}
						$k++;
					}
					if($filterUrl){$filterUrl="filter/".$filterUrl;}
					if($sort){$order="sort/".$sort;}
					$filters[$i]['item'][$j]['url'] = $baseUrl."goods/list/catId/$catId/$order".$filterUrl;
					$filters[$i]['index'] = $i;
					$j++;
				}
				$i++;
			}
			}
		}
			//assign("catId",$catId);
		//	assign("catInfo",$catInfo); 
		//print_r($filters);
		$res = $filters;
		$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;
}

?>