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

class SeoModel extends BaseModel{
	public $msg='';
	function pageInfo($id){
		global $siteConf;
		if(!$id || !settype($id,"integer")){return false;}
		$wheres='';if(defined('SITE_ID')  && !$GLOBALS['siteConf']['share']){$wheres .= " and (siteId='".SITE_ID."' or siteId=0)";}
		$sql = "select * from qk_seo_page where id='$id' $wheres";
		$info = $this->db->getRow($sql);
		if(!$info){
			$info['title'] = $GLOBALS['siteConf']['title'];
			$info['keywords'] = $GLOBALS['siteConf']['keywords'];
			$info['description'] = $GLOBALS['siteConf']['description'];
		}
		return $info;
	}
	function delSeoPage($id){
		$wheres='';if(defined('SITE_ID') && !$GLOBALS['siteConf']['share']){$wheres .= " and (siteId='".SITE_ID."' or siteId=0)";}
			$sql = "delete from qk_seo_page where id='$id' $wheres limit 1";
			if($this->db->query($sql)){
				$this->msg = '删除成功';
				return true;
			}
			return false;
	}
	function getSeoPageInfo($id){
		if($id && settype($id,"integer")){
			$sql = "select * from qk_seo_page where id='$id'";
			if($res = $this->db->getRow($sql)){
				return $res;
			}
		}
		return false;
	}
	function setSeoPageInfo($param){
		$id = $param['id'];
		$name = $param['name'];
		if($name && $id && settype($id,"integer")){
			$title = $param['title'];
			$keywords = $param['keywords'];
			$description = $param['description'];
			$sql="update qk_seo_page set name='$name',title='$title',keywords='$keywords',description='$description' where id='$id'";
			if($this->db->query($sql)){
				$this->msg = '修改成功';
				return true;
			}
		}else{
			$this->msg = '页面名称不能为空';
		}
		return false;
	}

	function addSeoPage($param){
		$name = $param['name'];
		if($name){
			$title = $param['title'];
			$keywords = $param['keywords'];
			$description = $param['description'];
			$siteId="";if(defined('SITE_ID')){$siteId .= " ,siteId='".SITE_ID."'";}
			$sql="insert into qk_seo_page set name='$name',title='$title',keywords='$keywords',description='$description',posttime=UNIX_TIMESTAMP()".$siteId;
			if($this->db->query($sql)){
				$this->msg = '添加成功';
				return true;
			}
		}else{
			$this->msg = '页面名称不能为空';
		}
		return false;
	}

	function delUrl($id){if($id && is_numeric($id)){if($this->db->query("delete from `qk_seo_url` where id='$id'")){return true;}}return false;}
	
	function seoUrl($url,$seoUrl,$act='add'){
		if(!$url){$this->msg='原URL为空';return false;}
		if(!$seoUrl){$this->msg='自定义URL为空';return false;}
		if($act=='add'){
				$sql = "select * from qk_seo_url where url='$url'";$seoUrlInfo=$this->db->getRow($sql);
				if($seoUrlInfo){$this->msg='原URL已存在';return false;}
				$sql = "select * from qk_seo_url where seoUrl='$seoUrl' or seoUrl='$seoUrl/'";$seoUrlInfo=$this->db->getRow($sql);// and url<>'$url'
				if($seoUrlInfo){$this->msg='自定义URL已存在';return false;}	//且指向到不同原URL
				$sql = "insert into qk_seo_url set url='$url',seoUrl='$seoUrl',posttime=UNIX_TIMESTAMP()";
				$this->db->query($sql);return true;
		}else if($act=='edit'){
				$sql = "select * from qk_seo_url where (seoUrl='$seoUrl' or seoUrl='$seoUrl/') and url<>'$url'";$seoUrlInfo=$this->db->getRow($sql);
				if($seoUrlInfo){
					$exists=true;
					if(preg_match("/article\/view\/id\/(\d+)$/",$seoUrlInfo['url'],$data)){
						if(isset($data[1]) && $data[1] && is_numeric($data[1])){
							$exists = $this->db->getOne("select id from qk_article where id='".$data[1]."'");
							if(!$exists){
								$this->db->query("delete from qk_seo_url where url='".$seoUrlInfo['url']."'");
							}
						}
					}
					if($exists){
						$this->msg='自定义URL已存在';return false;
					}
				}
				$sql = "select * from qk_seo_url where url='$url'";$urlInfo=$this->db->getRow($sql);
				if(!$urlInfo){	 //$this->msg='原URL不存在';return false;
					$sql = "insert qk_seo_url set seoUrl='$seoUrl',url='$url',posttime=UNIX_TIMESTAMP()";
				}else{
					if($urlInfo['seoUrl']==$seoUrl){return true;}
					$sql = "update qk_seo_url set seoUrl='$seoUrl' where id='".$urlInfo['id']."'";
				}
				$this->db->query($sql);return true;
		}
		$this->msg='操作参数错误';
		return false;
	}
}
?>