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

class LinksModel extends BaseModel{
	public $msg = '';
	function add($params){return $this->query($params);}
	function edit($params){return $this->query($params);}

	function query($params){
		$act = $params['act'];
		$id = $params['id'];
		$userId = $_SESSION['adminId'];
		$catId = $params['catId'];
		$name = $params['name'];
		$title = $params['title'];
		$url = $params['url'];
		$iStatus = $params['iStatus'];
		$sortId = $params['sortId'];
		$thumbId = $params['thumbId'];
		if($iStatus!=1){$iStatus=0;}
		if(!settype($catId,"integer")){$catId==0;}
		if(!settype($sortId,"integer")){$iStatus==50;}
		if(!settype($thumbId,"integer")){$thumbId==0;}
		if($act=="add"){
			$sql = "insert into qk_links set";
			$sqlEnd = ",posttime=UNIX_TIMESTAMP()";
		}else if($act=="edit" && $id && settype($id,"integer")){
			$sql = "update qk_links set";
			$sqlEnd = " where id='$id'";
		}
		if($sql){
			$sql .= " catId='$catId',name='$name',title='$title',url='$url',iStatus='$iStatus',sortId='$sortId',userId='$userId',thumbId='$thumbId'";
			$sql .= $sqlEnd;
			//echo $sql;exit;
			if($this->db->query($sql)){
				return true;
			}
		}
		return false;

	}

	function getInfo($id){
		$linkInfo = false;
		if($id && settype($id,"integer")){
			load_class("attachment");
			$am = new AttachmentModel();
			$sql = "select * from qk_links where id='$id'";
			$linkInfo = $this->db->getRow($sql);
			$linkInfo['userInfo']['loginName'] = $this->db->getOne("select loginName from qk_user where id='".$linkInfo['userId']."'");
			$linkInfo['catInfo'] = $this->db->getRow("select id,name,parentId,userId from qk_links_cat where id='".$linkInfo['catId']."'");
			$linkInfo['thumbnail']['url'] = $am->getUrl($linkInfo['thumbId']);
		}
		return $linkInfo;
	}
	function getCatInfo($id){
		$linkInfo = false;
		if($id && settype($id,"integer")){
			$sql = "select * from qk_links_cat where id='$id'";
			$linkInfo = $this->db->getRow($sql);
		}
		return $linkInfo;
	}
	function addCat($params){return $this->queryCat($params);}
	function editCat($params){return $this->queryCat($params);}
	function queryCat($params){
		$act = $params['act'];
		$id = $params['id'];
		$userId = $_SESSION['adminId'];
		$name = $params['name'];
		if($act=="add"){
			$sql = "insert into qk_links_cat set";
			$sqlEnd = ",userId='$userId',posttime=UNIX_TIMESTAMP()";
		}else if($act=="edit" && $id && settype($id,"integer")){
			$sql = "update qk_links_cat set";
			$sqlEnd = " where id='$id'";
		}
		if($sql){
			$sql .= " name='$name'";
			$sql .= $sqlEnd;
			if($this->db->query($sql)){return true;}
		}
		return false;

	}

	function del($id){if($id && settype($id,"integer")){$sql = "delete from qk_links where id='$id'";$this->db->query($sql);return true;}return false;}
	function delCat($id){if($id && settype($id,"integer")){$sql = "delete from qk_links_cat where id='$id'";$this->db->query($sql);return true;}return false;}

	function ajaxEdit($params){
		$id = $params['id'];
		$values = "";
		foreach($params as $key=>$val){
			if($key!="id"){
				$values .= " `$key`='$val',";
			}
		}
		if($values){
			$values = substr($values,0,strlen($values)-1);
		}
		$sql = "update qk_links set $values where id='$id'";
		$this->db->query($sql);
		$this->msg = '删除成功';
		return true;
	}
}
?>