<?php
 /**
 *      [XCECP] (C) COLNET Inc.
 *      This is NOT a freeware, use is subject to license terms
 */
if(!defined("IN_QKWEB")){exit("Access Denied");}
class CategoryAction{
	function index(){
		global $siteConf;
		$id = getgpc("id");
		if(!$id || !is_numeric($id)){
		$sm = new SeoModel();
		$pageInfo = $sm->pageInfo(9);
		}else{
			load_class("category");
			$cm = new CategoryModel();
			$info = $cm->getInfo($id);
			if($info){
				$pageInfo['title'] = $info['name'];
			}
		}
		if($pageInfo['title']){
			$pageInfo['title'] .= " ";
		}
		$pageInfo['title'] .= $siteConf['siteName'];
		assign("pageInfo",$pageInfo);
		display();
	}

	function data(){
		$inajax=getgpc('inajax','P');
		$act=getgpc('act','P');
		if($inajax=='1'){
			$ret=array('success'=>false,'message'=>'非法访问');
			if($act=='getChild'){
				$id=getgpc('id','P');
				if($id && is_numeric($id)){
					load_class('category');
					$cm = new CategoryModel();
					$sql = "select * from qk_category where parentId='$id'";
					$rows=$GLOBALS['db']->getAll($sql);
					if($rows && is_array($rows) && count($rows)>0){
						$data=array();
						$i=0;
						foreach($rows as $row){
							$data[$i]['id']=$row['id'];
							$data[$i]['name']=$row['name'];
							$data[$i]['mainNav']=$row['mainNav'];
							$data[$i]['parentInfo']=$GLOBALS['db']->getRow("select id,name from qk_category where id='".$row['parentId']."'");
							$data[$i]['seoUrl']=$row['seoUrl'];
							$data[$i]['template']=$row['template']?$row['template']:"";
							$data[$i]['sortId']=$row['sortId'];
							$data[$i]['datetime']=date('Y-m-d',$row['posttime']);
							$data[$i]['grade']=$cm->getCatGrade($row['id']);
							$nbsp="";
							for($foo=1;$foo<$data[$i]['grade']*4;$foo++){$nbsp.='&nbsp;';}
							$data[$i]['nbsp']=$nbsp;
							$data[$i]['subNum'] = $GLOBALS['db']->getOne("select count(id) from qk_category where parentId='".$row['id']."'");

							$i++;
						}
						$ret=array('success'=>true,'message'=>'Success','data'=>$data);
					}
				}
			}
			echo json_encode($ret);exit();
		}
	}

	function view(){
		$id=getgpc('id');
		if(!$id || !is_numeric($id)){Msg::show('商品分类不正确');}
		load_class('category');
		$cm = new CategoryModel();
		$catInfo=$cm->getInfo($id);
		if(!$catInfo){Msg::show('商品分类不正确');}
		$url = $_SERVER['REQUEST_URI'];
		$pageUrl = $GLOBALS['baseUrl'].'category/'.$catInfo['id'].'.html';
		if($temp=urlAfter($pageUrl,'key',getgpc('key'))){$pageUrl=$temp;}
		if($temp=urlAfter($pageUrl,'sort',getgpc('sort'))){$pageUrl=$temp;}
		if($temp=urlAfter($pageUrl,'filter',getgpc('filter'))){$pageUrl=$temp;}
		$catId = $id;
		if($catId && settype($catId,"integer")){
			$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++; }
			}
			$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'] = $GLOBALS['db']->getRow("select * from qk_goods_attr where id='".$item."'");
				if($filters[$i]['type']){
					$filters[$i]['item'] = $db->getAll($sql);
					$i++;
				}else{
					continue;
				}
			}
			$i=0;
			foreach($filters as $item){
				$filter = $item['item'];	//每一种筛选方式
				$j=0;
					foreach($filter as $attr){//每一种筛选方式中的筛选值
						$k=0;$filterUrl = "";$filterAttrTemp = $filterValue;//筛选方式组合参数数量
						foreach($filterAttr as $v){
							if($v==$item['type']['id']){//“全部”链接
								$filterAttrTemp[$k] = 0;$l = 0;$filterUrl = "";
								foreach($filterAttrTemp as $val){ $filterUrl .= $val;$l++;if($l<count($filterAttrTemp)){ $filterUrl.="-"; } }
							}
							$k++;
						}
					//	if($filterUrl){$filterUrl="filter/".$filterUrl;}
						
						$filterAll[0]['id'] = 0;
						$filterAll[0]['attrId'] = $attr['attrId'];
						$filterAll[0]['value'] = '全部';
						$filterAll[0]['url'] = urlAfter($pageUrl,'filter',$filterUrl);
						break;
					}
					foreach($filter as $attr){//每一种筛选方式中的筛选值
						$k=0;$filterUrl = "";$filterAttrTemp = $filterValue;//筛选方式组合参数数量
						foreach($filterAttr as $v){
							if($v==$item['type']['id']){
								$extId = $attr['id'];$filterAttrTemp[$k] = $extId;$l = 0;$filterUrl = "";
								foreach($filterAttrTemp as $val){ $filterUrl .= $val;$l++;if($l<count($filterAttrTemp)){ $filterUrl.="-"; } }
							}
							$k++;
						}
						//if($filterUrl){$filterUrl="filter/".$filterUrl;}
						$filters[$i]['item'][$j]['url'] = urlAfter($pageUrl,'filter',$filterUrl);
						$j++;
					}
				$filters[$i]['item'] = array_merge($filterAll,$filters[$i]['item']);
				$i++;
			}
			assign("filters",$filters);
			}
			assign("catId",$catId);
		}

		$sortUrl['sales'] = urlAfter($pageUrl,'sort','sales');
		$sortUrl['sales'] = urlAfter($pageUrl,'sort','sales');
		$sortUrl['price'] = urlAfter($pageUrl,'sort','price');
		$sortUrl['time'] = urlAfter($pageUrl,'sort','time');
		$pageInfo=array(
			'title'=>$catInfo['name'].'商品列表'.' - '.$GLOBALS['siteConf']['siteName'],
			'keywords'=>$catInfo['name'].','.$catInfo['name'].'推荐'.','.$catInfo['name'].'商品列表',
			'description'=>$catInfo['name'].'商品列表'
		);
		assign("sortUrl",$sortUrl);
		assign('catId',$id);
		assign('catInfo',$catInfo);
		assign('pageInfo',$pageInfo);
		display('category/view.html');
	}
}
?>