<?php
class article_cateModel extends Model
{
    

	public function get_cats() {
        $id_arr = $this->where(array('status'=>1))->select();
        $array = array();
        foreach ($id_arr as $val) {
            $array[$val['id']] = $val;
        }
        return $array;
    }
    

    public function gradesql($sqlname){
		$config_file = CONF_PATH . 'db.php';
        $config = require $config_file;
		$sqlgradename = $config['DB_PREFIX'].$sqlname;
		$tagsql = M($sqlname);
		if($tagsql->getDbError()){
$sql = "CREATE TABLE `".$sqlgradename."` (
  `vid` int(11) unsigned NOT NULL,  
  `info` text NOT NULL,
  PRIMARY KEY (`vid`),
  UNIQUE KEY `vid` (`vid`)  
) COLLATE='utf8_general_ci' ENGINE=MyISAM;";        
        $result = $this->execute($sql);
        if($result ===false){
           return 0;	
        }else{
           return 1;	
        }
        }else{
           return 9999;	
		}
	}
    public function delsql($sqlname){
		$config_file = CONF_PATH . 'db.php';
        $config = require $config_file;
		$sqlgradename = $config['DB_PREFIX'].$sqlname;
		$tagsql = M($sqlname);
		if(!$tagsql->getDbError()){
$sql = "DROP TABLE `".$sqlgradename."`;";        
        $result = $this->execute($sql);
        if($result ===false){
           return 0;	
        }else{
           return 1;	
        }
        }else{
           return 9999;	
		}
	}
    public function xiufusql($sqlname){
		$config_file = CONF_PATH . 'db.php';
        $config = require $config_file;
		$sqlgradename = $config['DB_PREFIX'].$sqlname;
		$tagsql = M($sqlname);
		if(!$tagsql->getDbError()){
$sql = "REPAIR TABLE `".$sqlgradename."`;";        
        $result = $this->execute($sql);
        if($result ===false){
           return 0;	
        }else{
           return 1;	
        }
        }else{
           return 9999;	
		}
	}
    /**
     * 检测分类是否存在
     * 
     * @param string $name
     * @param int $pid
     * @param int $id
     * @return bool 
     */
    public function name_exists($name,  $id=0) {
        $where = "name='" . $name . "' AND  id<>'" . $id . "'";
        $result = $this->where($where)->count('id');
        if ($result) {
            return true;
        } else {
            return false;
        }
    }
	
	/**
     * 检测分类拼音是否存在
     * 
     * @param string $ename
     * @param int $pid
     * @param int $id
     * @return bool 
     */
    public function ename_exists($ename,  $id=0) {
        $where = "ename='" . $ename . "' AND  id<>'" . $id . "'";
        $result = $this->where($where)->count('id');
        if ($result) {
            return true;
        } else {
            return false;
        }
    }
    public function get_name($id) {
	    $map=array('id'=>$id);
		$name = $this->where($map)->getField('name');
        return $name;
    }
    /**
     * 写入缓存
     */
    public function cate_cache() {
        $artcate_list = array();
        $cate_data = $this->field('id,name,ename')->where('status=1')->order('ordid')->select();
        foreach ($cate_data as $val) {            
                $artcate_list['p'][$val['id']] = $val;            
        }
        F('artcate_list', $artcate_list);
        return $artcate_list;
    }

    /**
     * 更新则删除缓存
     */
    protected function _before_write(&$data) {
        F('artcate_list', NULL);
		$this->gradesql('article_'.$data['id']);
    } 
	
	/**
     * 新增则删除缓存
     */
	protected function _after_insert(&$data) {
        F('artcate_list', NULL);
		$this->gradesql('article_'.$data['id']);
    } 
	
    /**
     * 删除也删除缓存
     */
    protected function _after_delete($data, $options) {
        F('artcate_list', NULL);		
		$this->delsql('article_'.$data['id']);	
		D('article')->where(array('cate_id'=>$data['id']))->delete();
		$this->xiufusql('article');
		$this->xiufusql('article_cate');
    }
}