<?php
/*
评论管理程序 
*/
class CommentsModel extends DBModel {
	/*
	评论发布
	*/
	public function Add($POST){
		//print_r($POST);
		IF (empty($POST['username'])){
			$this->error = _('评论人不能为空！');
			return false;
		}
		if (empty($POST['content'])){
			$this->error = _('请填写评论内容!');
			return false;
		}
		$POST['addtime'] = time();
		$this->_DB->insert(VODCMS_COMMENT, $POST);
		$sql = 'UPDATE '.VODCMS_MOVIE.' set review=review+1 WHERE movid='.(int)$POST['movid'];
		$this->_DB->exec($sql);
		return $this->_DB->lastinsertid();
	}
	/*获取指定条件的评论内容*/
	public function getAll($where = null){
		$select = $this->_DB->select();
		$select->from(VODCMS_COMMENT);
		$select->where($where);
		$select->where('locked=0');
		$select->order('id DESC');
		$sql = $select->toString();
		$total = $this->_DB->getCount($sql);
		$this->_Page->set($total, 10);
		$this->printPage = $this->_Page->printBody();
		return $this->_DB->fetAll($sql.$this->_Page->limit());
	}
	/*后台获取指定条件的评论内容*/
	public function getList($where = null){
		$select = $this->_DB->select();
		$select->from(VODCMS_COMMENT);
		$select->where($where);
		$select->order('id DESC');
		$sql = $select->toString();
		$total = $this->_DB->getCount($sql);
		$this->_Page->set($total, 10);
		$this->printPage = $this->_Page->printpage();
		return $this->_DB->fetAll($sql.$this->_Page->limit());
	}
	/*删除一行评论同时影片评论数减一*/
	public function delete($id){
		if (is_array($id)){
			$array = $id;
		}else{
			$array = array($id);
		}
		foreach ( $array as $id){
			$row = $this->getRow($id);
			$movid = (int)$row['movid'];
			$sql = 'UPDATE '.VODCMS_MOVIE.' set review=review-1 WHERE movid='.$movid;
			$this->_DB->exec($sql);
			$this->_DB->delete(VODCMS_COMMENT, array('id'=>(int)$id));
		}
		return true;
	}
	/*获取一行记录*/
	public function getRow($id){
		$select = $this->_DB->select();
		$select->from(VODCMS_COMMENT);
		$select->where(array('id'=>$id));
		$sql = $select->toString();
		return $this->_DB->fetRow($sql);
	}
	public function locked($id){
		if (is_array($id)){
			$where = implode(',', array_map('intval', $id));
		}else{
			$where = $id;
		}
		return $this->_DB->update(VODCMS_COMMENT, array('locked'=>0), 'id in ('.$where.')');
	}
	
}?>