<?php
/**
 * 五十九秒网络购物cms 首页数据调取实体类
 * ============================================================================
 * 版权所有 2009-2012 五十九秒网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.59miao.com；
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: longzhang
 * 
*/
if (!defined('IN_59MIAO'))
{
	exit('Access Defined');
}
class HomenavModel extends Model {
	public function __construct() {
		parent::__construct();
		$this->_fields = array('id','category','tao_category','name','state','info','sort','sid');
		$this->_tables = array(DB_FREFIX.'homenav');
		$this->_check = new NavCheck();
		list(
				$this->_R['id'],
				$this->_R['sid'],
				$this->_R['name']
		) = $this->getRequest()->getParam(array(
				isset($_GET['id']) ? $_GET['id'] : null,
				isset($_GET['sid']) ? $_GET['sid'] : 0,
				isset($_POST['name']) ? $_POST['name'] : null
		));
	}
	
	public function findAll() {
		return parent::select(array('id','category','tao_category','name','state','info','sort','sid'), 
											array('where'=>array("sid='{$this->_R['sid']}'"), 'limit'=>$this->_limit, 'order'=>'sort ASC'));
	}
	//获取所有的父类导航
	public function findAllHomeNav(){
		return parent::select(array('id','sid','category','name'), 
											array('where'=>array("sid='{$this->_R['sid']}'"), 'order'=>'sort ASC'));
	}
	//获取所有的子类导航
	public function findAllSonNav($id){
		$_where=array("sid='{$id}'");
		return parent::select(array('id','sid','category','name'), 
											array('where'=>$_where, 'order'=>'sort ASC'));
	}
	public function findOne() {
		if (isset($_GET['sid'])) {
			return parent::select(array('id','category','tao_category','name','state','info','sort','sid'),
											array('where'=>array("id='{$this->_R['sid']}'"), 'limit'=>'1'));
		}
		$_where = array("id='{$this->_R['id']}'");
		if (!$this->_check->oneCheck($this, $_where)) $this->_check->error();
		return parent::select(array('id','category','tao_category','name','state','info','sort','sid'),
											array('where'=>$_where, 'limit'=>'1'));
	}
	
	public function total() {
		return parent::total(array('where'=>array("sid='{$this->_R['sid']}'")));
	}
	
	public function add() {
		$_where = array("name='{$this->_R['name']}'");
		if (!$this->_check->addCheck($this, $_where)) $this->_check->error();
		$_addData = $this->getRequest()->add($this->_fields);
		if(!is_numeric($_addData['sort'])){
			$_addData['sort']=10;
		}		
		return parent::add($_addData);
	}
	
	public function update() {
		$_where = array("id='{$this->_R['id']}'");
		if (!$this->_check->oneCheck($this, $_where)) $this->_check->error();
		if (!$this->_check->updateCheck($this)) $this->_check->error();
		$_updateData = $this->getRequest()->update($this->_fields);
		if(!is_numeric($_updateData['sort'])){
			$_addData['sort']=10;
		}	
		return parent::update($_where, $_updateData);
	}
	
	public function delete() {
		$_where = array("id='{$this->_R['id']}'");
		return parent::delete($_where);
	}
	
	public function sort() {
		foreach ($_POST['sort'] as $_key=>$_value) {
			if (!is_numeric($_value)) continue;
			$_setField = array('sort'=>$_value);
			$_where = array("id='$_key'");
			parent::update($_where, $_setField);
		}
		return true;
	}
	
	public function isName() {
		$_where = array("name='{$this->_R['name']}'");
		$this->_check->ajax($this, $_where);
	}
}
?>