<?php
/**
 *      [XCECP] (C) COLNET Inc.
 *      This is NOT a freeware, use is subject to license terms
 */
if(!defined("IN_QKWEB")){exit("Access Denied");}
class GoodsAction{
	function view(){
		global $db;
		load_class("goods");
		$gm = new GoodsModel();
		load_class("category");
		$cm = new CategoryModel();
		$userId = $_SESSION['userId'];
		$inajax = getgpc("inajax","P");
		$act = getgpc("act","P");
		$articleId = getgpc("articleId");
		if($inajax==1){
			if($act=="post" && $articleId && settype($articleId,"integer")){
				$content = getgpc("content","P");
				if($content && strlen($content)>=5 && strlen($content)<=500){
					BaseModel::checkLogin();
					$sql = "insert into qk_post set userId='$userId',content='$content',articleId='$articleId',posttime=UNIX_TIMESTAMP()";
					$sql .= "";
					if($db->query($sql)){
						exit('{"success":true,"message":"发表成功，谢谢！","type":3,"data":{"loginName":"'.$_SESSION['loginName'].'","posttime":"'.date("Y-m-d H:i:s").'"}}');
					}
				}else{
					exit('{"success":false,"message":"评论内容不符合规则","type":2}');
				}
			}
			exit();
		}

		$id = getgpc("id");
		$mega = getgpc("mega");
		$sku = getgpc("sku");
		if(($id && settype($id,"integer")!=false) || ($mega && $sku)){
			if($id){
				$param=$id;
			}else if($mega && $sku){
				$param = array("mega"=>str_replace("-"," ",$mega),"sku"=>$sku);
			}
			$goodsInfo = $gm->getInfo($param);
			if(!$goodsInfo){Msg::show('商品不存在或已被删除');}
			$catId = $goodsInfo['catId'];
			$cats= $cm->getParentCats($catId);
			$i=0;
			$page_addr=$goodsInfo['name'];
			foreach($cats as $item){
					$link = '<a href="/goods/cat/'.$item.'">'.getCatName($item).'</a>&nbsp;&gt;&nbsp;';
				if($i+1<count($cats)){
					$page_addr =  $link. $page_addr;
				}else{
					$page_addr = "<span>".$page_addr."</span>";
					$page_addr = "<strong>".$link."</strong>".$page_addr;
				}
				$i++;
			}
			unset($i);
			assign("preview",$preview);
			assign("goodsInfo",$goodsInfo);
			assign("catInfo",$cm->getInfo($catId));
			assign("topParentId",$topParentId);
			assign("page_addr",$page_addr);
			$pageInfo['title'] = $goodsInfo['title']?$goodsInfo['title']:$goodsInfo['name'];
			$pageInfo['title'] .=" - ".$GLOBALS['siteConf']['siteName'];
			$pageInfo['keywords'] = $goodsInfo['keywords'];
			$pageInfo['description'] = $goodsInfo['description'];
			assign("pageInfo",$pageInfo);
			display("goods/view");
		}
	}
	function index(){
		global $siteConf,$db,$baseUrl;
		$url = $_SERVER['REQUEST_URI'];
		$key = getgpc("key","P");
		if(!$key){
			$key = getgpc("key","G");
			if($key){if(strpos($_SERVER['SERVER_SOFTWARE'],"Microsoft")){$key = mb_convert_encoding($key,"UTF-8","GBK");}}
		}
		assign("key",$key);
		$pageUrl = $GLOBALS['baseUrl'].'goods/';
		if($temp=urlAfter($pageUrl,'key',getgpc('key'))){$pageUrl=$temp;}
		if($temp=urlAfter($pageUrl,'catId',getgpc('catId'))){$pageUrl=$temp;}
		if($temp=urlAfter($pageUrl,'sort',getgpc('sort'))){$pageUrl=$temp;}
		$catId = getgpc("catId");
		if($catId && settype($catId,"integer")){
			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++; }
			}
			$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."'");
				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);
			assign("catInfo",$catInfo); 
		}
		if($temp=urlAfter($pageUrl,'filter',getgpc('filter'))){$pageUrl=$temp;}
		$sortUrl['sales'] = urlAfter($pageUrl,'sort','sales');
		$sortUrl['price'] = urlAfter($pageUrl,'sort','price');
		$sortUrl['time'] = urlAfter($pageUrl,'sort','time');
		assign("sortUrl",$sortUrl);

		$sm = new SeoModel();
		$pageInfo = $sm->pageInfo(4);
		$title = $catInfo['title']?$catInfo['title']:($catInfo['name']?$catInfo['name']:$pageInfo['title']);
		$pageInfo['title'] = ($title?$title." - ":"").$siteConf['siteName'];
		$pageInfo['keywords'] = $catInfo['keywords']?$catInfo['keywords']:($catInfo['name']?$catInfo['name']:$pageInfo['title']);
		$pageInfo['description'] = $catInfo['description']?$catInfo['description']:($catInfo['name']?$catInfo['name']:$pageInfo['title']);

		assign("pageInfo",$pageInfo);
		$template = "";
		if($catInfo['template']){$template=$catInfo['template'];}
		$buyWay = getgpc("buyWay");
		if($buyWay==2){
			$template = "goods/score";
		}
		display($template);
	}

	function go(){
		$id = getgpc("id");
		if(!$id){
			$id = $_GET['PATH_INFO'][2];
		}
		if($id && is_numeric($id)){
			load_class("goods");
			$gm = new GoodsModel();
			$goodsInfo = $gm->getInfo($id);
			if($goodsInfo && $goodsInfo['clickUrl']){
				echo '<script>location.href="'.$goodsInfo['clickUrl'].'"</script>';
			}else{
				Msg::show('不存在该商品');
			}
		}
	}
}
?>