<?php

/**
 *      【橙创家校通v4.0】 (C)2010-2012 Tuntron Inc.
 *      
 *    	 http://www.tuntron.com   QQ：1121251729
 *
 *      &公告基类文件&
 */

if (!defined('IS_RUN')) die('Forbidden');

class tj_ann extends tj_model {

	var $table = 'dbpre_ann';
    var $key = 'id';

	function __construct() {
		parent::__construct();
		$this->init_field();
	}

    function msm_announcement() {
        $this->__construct();
    }

	function init_field() {
		$this->add_field('title,bid,available,content');
		$this->add_field_fun('bid,available', 'intval');
        $this->add_field_fun('title', '_T');
        $this->add_field_fun('content', '_HTML');
	}

	function read_all($type=null) {
        $this->db->from($this->table);
        if($type) {
			if(strpos($type,'/')){
			$type=explode("/",'1/'.$type);
			}
            $this->db->where('bid', $type);
        }
		$this->db->where('available', '1');
		$this->db->order_by('dateline','DESC');//必须为大写DESC或ASC
        $this->db->select('id,title,bid,content,author,pageview,dateline,available');
        return $this->db->fetch_all_assoc();
    }
	
	function read_one($id) {
        $this->db->from($this->table);
        $this->db->where('id', $id);
        $this->db->select('id,title,bid,content,author,pageview,dateline,available');
        return $this->db->once_fetch_assoc();
    }
	
	
	
    function save(& $post, $id=NULL, $cache=TRUE) {
        $this->check_post($post);
		
        if(!$id) {
			global $_CO;
            $post['author'] = $_CO['username'];
            $post['dateline'] = $this->global['timestamp'];
        }
        return parent::save($post, $id, $cache, FALSE);
    }

    function update($post) {
        if(!is_array($post)) Note('global_op_unselect');
        foreach($post as $id => $val) {
            $val['available'] = (int) $val['available'];
            $this->db->from($this->table);
            $this->db->set($val);
            $this->db->where('id',$id);
            $this->db->update();
        }
    }
	
	function delann($id) {
		
        $this->db->where('id', $id);  
		
		$this->db->from($this->table);     

        $this->db->delete();
		
        
    }

    function check_post($post,$is_edit=false) {
        if(!$post['title']) Note('公告标题不能为空！');
        if(!$post['content']) Note('公告内容不能为空！');
    }
}
?>