<?php
 /**
 *      [QKSHOP] (C) QIKEWEB Inc.
 *      This is NOT a freeware, use is subject to license terms
 */

class IndustryModel extends BaseModel{

	function addCategory($params){
		$act=$params['act'];
		if($act=="add"){
			$code = $params['code'];
			$name = $params['name'];
			$parentId = $params['parentId'];
			$_SESSION['industryParentId'] = $parentId;
			$sortId = $params['sortId'];
			if(settype($sortId,"integer")===false){$sortId=50;}
			if(!$parentId || !settype($parentId,"integer")){$parentId=0;}
			
			$nameArr = explode("\r\n",$name);
			$i=0;
			foreach($nameArr as $item){
				$name = $item;
				if($name){
					if($act=="add"){
						$sql = "insert into qk_industry set ";
					}else if($act=="edit"){
						$sql = "update qk_industry set ";
					}
					$sql .= " name='$name',parentId='$parentId',sortId='$sortId',code='$code'";
					if($act=="add"){
						$sql.=",posttime=UNIX_TIMESTAMP()";
					}else if($act=="edit"){
						$sql .= " where id='".$id."'";
					}
					if($this->db->query($sql)){
						$i++;
					};
				}
			}
			if($i>0 || $i==count($nameArr)){
				unset($i);
				return true;
			}
			unset($i);
		}
		return false;

	}
	function getCategory($catId=0,$page=0,$pageSize=0){
		global $db;
		$tmp = array();
		$catInfo = $db->getRow("select code,name,parentId,sortId,id,posttime from qk_industry where iStatus=1 and id='".$catId."' order by sortId 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'],"code"=>$catInfo['code']);
		}
		$sql = "select * from qk_industry where parentId=".$catId." and iStatus=1 order by sortId asc";
		$res = $db->getAll($sql);
		if($res){
			foreach($res as $item){
				$tmp = array_merge($tmp,$this->getCategory($item['id']));
			}
		}
		return $tmp;
	}


	function getCatGrade($catId){
		global $db;
		$grade = 0;
		$cats = $catId;
		$sql = "select parentId from qk_industry where id='".$catId."'";
		$res = $db->getRow($sql);
			if($res && $res['parentId']>0){
				$grade++;
				$grade += $this->getCatGrade($res['parentId']);
			}
		return $grade;
	}

	function getInfo($id){
			$sql = "select * from qk_industry where id='".$id."'";
			$info = $this->db->getRow($sql);
			if($info){
				$info['nameLength'] = strlen($v['name']);
				$info['url'] = $GLOBALS['baseUrl'].str_replace(" ","-",$info['name'])."-s/".$info['id'].".htm";
			}else{
				$info = false;
			}
			return $info;
	}
	function ajaxColEdit($param){
				$col = $param['col'];
				$val = $param['val'];
				$id = $param['id'];
				if($col && settype($id,"integer")){
					$sql = "update qk_industry set `$col`='$val' where id='".$id."'";
					if($this->db->query($sql)){
						return '{"success":true}';
						}
				}
			return '{"success":false}';
	}

}
?>