<?php
 /**
 *      [XCECP] (C) COLNET Inc.
 *      This is NOT a freeware, use is subject to license terms
 */
if(!defined("IN_QKWEB")){exit("Access Denied");}
class CategoryModel extends BaseModel{
	public $msg = '';
	function getInfo($id,$fields='*'){
		global $baseUrl;
			load_class("attachment");
			$am = new AttachmentModel();
			load_class("brand");
			$bm = new BrandModel();
			$fieldsArr = array();$tempArr = array();
			if($fields!='*'){
				$fieldsArr = explode(',',$fields);//print_r($fieldsArr);
				foreach($fieldsArr as $val){ if(in_array($val,array('id','name','parentId'))){ $tempArr[]=$val; } }
				$fields=implode(',',$tempArr);
			}
			$sql = "select $fields from qk_category where id='".$id."'";//echo $sql;exit;
			$info = $this->db->getRow($sql);
			if($info){
				if($fields=='*' || in_array('brand',$fieldsArr)){$info['brandArr'] = explode(",",$info['brandIds']);foreach($info['brandArr'] as $item){$info['brand'][] = $bm->getInfo($item);}}
				if($fields=='*' || in_array('hotkey',$fieldsArr)){if($info['hotkey']){$info['hotkeyArr'] = explode(",",$info['hotkey']);}}
				if($fields=='*' || in_array('logo',$fieldsArr)){$info['logo']['url'] = $am->getUrl($info['logoId']);}
				if($fields=='*' || in_array('banner',$fieldsArr)){$info['banner']['url'] = $am->getUrl($info['bannerId']);}
				$info['nameLength'] = strlen($v['name']);
				$info['url'] = $GLOBALS['baseUrl']."goods/list/catId/".$info['id'];
				$info['url'] = $GLOBALS['baseUrl'].'category/'.$info['id'].'.html';
				if($info['seoUrl']){$info['url'] = $info['seoUrl']; }
				if($fields=='*' || in_array('subNum',$fieldsArr))$info['subNum'] = $this->db->getOne("select count(id) from qk_category where parentId='".$info['id']."'");
				if($fields=='*' || in_array('filter',$fieldsArr))$info['filter'] = unserialize($info['filter']);
			}else{
				$info = false;
			}
			return $info;
	}
	function getInfoExt($catInfo,$fields='*'){
				$catInfo['url'] = $GLOBALS['baseUrl'].'category-'.$catInfo['id'].'.html';
				if($catInfo['seoUrl']){$catInfo['url'] = $catInfo['seoUrl']; }
			if($catInfo){
				$fieldsArr = array();$tempArr = array();
				if($fields!='*'){
					$fieldsArr = explode(',',$fields);//print_r($fieldsArr);
					foreach($fieldsArr as $val){ if(in_array($val,array('id','name','parentId'))){ $tempArr[]=$val; } }
					$fields=implode(',',$tempArr);
				}
				if($fields=='*' || in_array('brand',$fieldsArr)){$catInfo['brandArr'] = explode(",",$catInfo['brandIds']);foreach($catInfo['brandArr'] as $item){$catInfo['brand'][] = $bm->getcatInfo($item);}}
				if($fields=='*' || in_array('banner',$fieldsArr)){$catInfo['banner']['url'] = $am->getUrl($catInfo['bannerId']);}
				$catInfo['nameLength'] = strlen($v['name']);
				$catInfo['url'] = $GLOBALS['baseUrl']."goods/list/catId/".$catInfo['id'];
				$catInfo['url'] = $GLOBALS['baseUrl'].'category/'.$catInfo['id'].'.html';
				if($catInfo['seoUrl']){$catInfo['url'] = $catInfo['seoUrl']; }
				if($fields=='*' || in_array('subNum',$fieldsArr))$catInfo['subNum'] = $this->db->getOne("select count(id) from qk_category where parentId='".$catInfo['id']."'");
			}else{
				$info = false;
			}
		return $catInfo;
	}
	function getCatGrade($catId){
		global $db;
		$grade = 0;
		$cats = $catId;
		$sql = "select parentId from qk_category where id='".$catId."'";
		$res = $db->getRow($sql);
			if($res && $res['parentId']>0){
				$grade++;
				$grade += $this->getCatGrade($res['parentId']);
			}
		return $grade;
	}

	function getParentCats($catId){
		global $db;
		$cats[] = $catId;
		$res = $db->getOne("select parentId from qk_category where id='".$catId."'");
		if($res){
			$cats =array_merge($cats,$this->getParentCats($res));
		}
		return $cats;
	}

	function getList($catId=0){
		global $db;
		$tmp = array();
		$wheres="";if(defined('SITE_ID') && $GLOBALS['siteConf']['privateData']){$wheres .= " and siteId='".SITE_ID."'";}
		$catInfo = $db->getRow("select name,parentId,sortId,id,posttime,seoUrl,template from qk_category where id='".$catId."' $wheres order by sortId asc,id asc");
		if(!$catInfo['name'] && $catId!=0){return;}
		if($catId!==0){
				$grade=$this->getCatGrade($catId);
		$tmp[] = array("id"=>$catId,"name"=>$catInfo['name'],"parentId"=>$catInfo['parentId'],"grade"=>$grade,"sortId"=>$catInfo['sortId'],"id"=>$catInfo['id'],"posttime"=>$catInfo['posttime'],"seoUrl"=>$catInfo['seoUrl'],"template"=>$catInfo['template']);
		}
		$sql = "select * from qk_category where parentId=".$catId." and iStatus=1 $wheres order by sortId asc,id asc";
		$res = $db->getAll($sql);
		if($res){
			foreach($res as $item){
				$tmp = array_merge($tmp,$this->getList($item['id']));
			}
		}
		return $tmp;
	}

	function del($id){
		if($id && settype($id,"integer")){
			$sql = "delete from qk_category where id='$id'";
			if($this->db->query($sql)){
				$url = $GLOBALS['baseUrl']."goods/list/catId/".$id;$this->db->query("delete from qk_seo_url where url='$url'");
				return true;
			}
		}
		return false;
	}
	function add($params){ $params['act']='add'; return $this->query($params);}
	function edit($params){ $params['act']='edit'; return $this->query($params);}
	function query($params){
		global $baseUrl;
			$act = $params['act'];
			$name = $params['name'];
			$parentId = $params['parentId'];
			$sortId = $params['sortId'];
			$logoId = $params['logoId'];
			$bannerId = $params['bannerId'];
			$brandId = $params["brandId"];
			$hotkey = $params["hotkey"];
			$title = $params["title"];
			$keywords = $params["keywords"];
			$description = $params["description"];
			$seoUrl = $params["seoUrl"];
			$typeId = $params["typeId"];
			$template = $params['template'];
			if(is_array($brandId)){$brandIds = implode(",",$brandId);}
			$id = $params['id'];
			if($name==''){ $this->msg='分类名不能为空';return false; }
			if(!$sortId || !settype($sortId,"integer")){$sortId=50;}
			if(!$parentId || !settype($parentId,"integer")){$parentId=0;}
			if($brandId && is_array($brandId) && count($brandId)>0){
				$brandIds = implode(",",$brandId);
			}
			if($act=="add"){
				$nameArr = explode("\r\n",$name);
				$i=0;
				foreach($nameArr as $item){
					$name = $item;
					if($name){
						if($this->db->getOne("select id from qk_category where name='$name' and parentId='$parentId'")){
							$this->msg = '同父类下产品分类名 '.$name.' 重复'; return false;
						}
						$filter='';
						if($typeId){
							$sql = "select id from qk_goods_attr where typeId='$typeId'";
							$attrArr = $this->db->getAll($sql);$attrs=array();
							foreach($attrArr as $attr){
								$attrs[]=$attr['id'];
							}
							$filter = serialize($attrs);
							unset($attrs);
						}
						$siteId="";if(defined('SITE_ID')){$siteId = SITE_ID;}
						$sql = "insert into qk_category set ";
						$sql .= " name='$name',parentId='$parentId',sortId='$sortId',logoId='$logoId',bannerId='$bannerId',typeId='$typeId',siteId='$siteId'";
						$sql .= ",brandIds='$brandIds',hotkey='$hotkey',title='$title',keywords='$keywords',description='$description',filter='$filter',template='$template'";
						$sql.=",posttime=UNIX_TIMESTAMP()";
						if($this->db->query($sql)){
							$catId = $this->db->insert_id();
							$this->msg='分类修改成功';
							if($act=="add"){$this->msg='添加分类成功';$id = $this->db->insert_id();}$url = $GLOBALS['baseUrl']."goods/list/catId/".$catId;
							if($seoUrl){load_class('seo');$sm=new SeoModel();if(!$sm->seoUrl($url,$seoUrl,$act)){$this->msg.= ', '.$sm->msg;}else{
								$this->db->query("update qk_category set seoUrl='$seoUrl' where id='$catId'");
							}}
							$i++;
						};
					}
				}
				if($i>0 || $i==count($nameArr)){
					unset($i);
					return $catId;
					//return true;
				}
				unset($i);
			}else if($act=='edit'){
				if($name){
					$catInfo=$this->db->getRow("select * from qk_category where id='".$id."'");
					if(!$catInfo){$this->msg='该分类不存在或已被删除';return false;}
						$filter='';
						if($typeId){
							$sql = "select id from qk_goods_attr where typeId='$typeId'";
							$attrArr = $this->db->getAll($sql);$attrs=array();
							foreach($attrArr as $attr){
								$attrs[]=$attr['id'];
							}
							$filter = serialize($attrs);
							unset($attrs);
						}
					$sql = "update qk_category set ";
					$sql .= " name='$name',parentId='$parentId',sortId='$sortId',logoId='$logoId',bannerId='$bannerId',typeId='$typeId'";
					$sql .= ",brandIds='$brandIds',hotkey='$hotkey',title='$title',keywords='$keywords',description='$description',filter='$filter',template='$template'";
					$sql .= " where id='".$id."'";
					if($this->db->query($sql)){
						$this->msg='分类修改成功';
						if($act=="add"){$this->msg='添加分类成功';$id = $this->db->insert_id();}$url = $GLOBALS['baseUrl']."goods/list/catId/".$id;
						if($seoUrl){load_class('seo');$sm=new SeoModel();if(!$sm->seoUrl($url,$seoUrl,$act)){$this->msg.= ', '.$sm->msg;}else{
							$this->db->query("update qk_category set seoUrl='$seoUrl' where id='$id'");
						}}else{$this->db->query("update `qk_category` set seoUrl='' where id='$id'");$this->db->query("delete from `qk_seo_url` where `seoUrl`='".$catInfo['seoUrl']."' and url='".$url."'");}

						return true;
					}
				}
			}
		return false;
	}

	function ajaxColEdit($param){
				$col = $param['col'];
				$val = $param['val'];
				$id = $param['id'];
				if($col && settype($id,"integer")){
					$sql = "update qk_category set `$col`='$val' where id='".$id."'";
					if($this->db->query($sql)){
						return true;
						}
				}
			return false;
	}
	function flushattr(){
		$sql = "select * from qk_category where filter<>'' order by id desc";
		$cats = $this->db->getAll($sql);
		foreach($cats as $param){
			$param['act']='edit';
			$this->edit($param);
		}
		return true;
	}
}
?>