<?php

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

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

class tj_topics_comments extends tj_model {

	var $table = 'dbpre_group_topics_comments';
    var $key = 'commentid';

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

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

	function init_field() {
		$this->add_field('commentid,referid,topicid,uid,content,addtime');
		$this->add_field_fun('commentid,referid,topicid,uid,addtime', 'intval');
		$this->add_field_fun('content', '_HTML');
	}

	function read_all($type=null,$lstart,$listnum) {
        $this->db->from($this->table);
		$this->db->order_by('addtime','ASC');
		$this->db->limit($lstart,$listnum);
		if($type) $this->db->where('topicid',$type);
        return $this->db->fetch_all_assoc();
    }
	
	function read_one($commentid) {
        $this->db->from($this->table);
        $this->db->where('commentid', $commentid);
        return $this->db->once_fetch_assoc();
    }
	
	
	
    function save(& $post, $commentid=NULL, $cache=TRUE) {
        $this->check_post($post);
		
        if(!$commentid) {
			global $_CO;
            $post['uid'] = $_CO['uid'];
            $post['addtime'] = $this->global['timestamp'];
        }
        return parent::save($post, $commentid, $cache, FALSE);
    }

    function update($post) {
       
    }
	
	function delcomment($commentid,$type=null) {
		
		if($type) {
			 $this->db->where('topicid', $type);  
			
		}else{
			
			 $this->db->where('commentid', $commentid);  
		}
       
		
		$this->db->from($this->table);     

        $this->db->delete();
		
    }
	
	//Refer二级循环
	function recomment($referid){
		$strComment = $this->read_one($referid);
		$strComment['user'] = G_class('user')->read($strComment['uid']);
		$strComment['content'] = _HTML($strComment['content']);
		
		return $strComment;
	}

    function check_post($post,$is_edit=false) {
		if(!$post['topicid']) Note('回复话题不能为空！');
        if(!$post['content']) Note('回复内容不能为空！');
    }
}
?>