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

class BrandModel extends BaseModel{
	function del($id){
		if($id && settype($id,"integer")){
			$sql = "delete from qk_brand where id='$id'";
			if($this->db->query($sql)){
				$url = $GLOBALS['baseUrl']."brand/view/id/".$id;$this->db->query("delete from qk_seo_url where url='$url'");
				return true;
			}
		}
		return false;
	}
	function add($params){
		return $this->query($params);
	}
	function edit($params){
		return $this->query($params);
	}
	function query($params){
		global $baseUrl;
			$act = $params['act'];
			$id = $params['id'];
			$name = $params['name'];
			$catId = $params['catId'];
			$sortId = $params['sortId'];
			$thumbId = $params['thumbId'];
			$website = $params['website'];
			$intro = $params['intro'];
			$seoUrl = $params['seoUrl'];
			$templateView = $params['templateView'];
			$content = $params['content'];
			if($seoUrl){if(substr($seoUrl,0,1)!="/"){$seoUrl = $baseUrl.$seoUrl;}}
			if(settype($thumbId,"integer")===false){$thumbId=0;}
			if(settype($catId,"integer")===false){$catId=0;}
			if(settype($sortId,"integer")===false){$sortId=50;}
			
			if($name){
				if($act=="edit"){
					$sql = "update qk_brand set ";
					$sqlEnd = " where id='".$id."'";
				}else if($act=="add"){
					$sql = "select id from qk_brand where name='$name'";
					if($this->db->getOne($sql)){
						$this->msg='品牌名重复';
						return false;
					}
					$sql = "insert into qk_brand set ";
					$sqlEnd = ",posttime=UNIX_TIMESTAMP()";
				}
				$sql .= " name='$name',sortId='$sortId',catId='$catId',thumbId='$thumbId',website='$website',intro='$intro'";
				$sql .= ",templateView='$templateView',content='$content'";
				$sql .= $sqlEnd;
				if($this->db->query($sql)){
							$this->msg='品牌修改成功';
							if($act=="add"){$this->msg='添加品牌成功';$id = $this->db->insert_id();}$url = $GLOBALS['baseUrl']."brand/view/id/".$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_brand set seoUrl='$seoUrl' where id='$id'");
							}}
					if($act=="add"){return $id;}
				}
			}else{
				$this->msg = '品牌名不能为空';return false;
			}
		return false;
	}

	function getInfo($id){
		load_class("attachment");
		$am = new AttachmentModel();
		$sql = "select * from qk_brand where id='$id'";
		$info = $this->db->getRow($sql);
		if(!$info){return false;}
		$info['thumbnail']['url'] = $am->getUrl($info['thumbId']);
		$info['logo']['url'] = $am->getUrl($info['logoId']);
		if(!$info['logo']['url']){$info['logo']['url']=$info['thumbnail']['url'];}
		return $info;
	}
}
?>