<?php
// 评论模型
class CommentModel extends Model {
	public $commentModule = array();
	protected function _initialize() {
		$this -> commentModule = Cache :: read('comment_module');
	}
	/**
	 * 获取列表（后台）
	 *
	 * @access public
	 * @param mix $page 当前分页数字
	 * @param intval $pageSize 分页大小
	 * @param mix $where 条件
	 * @param mix $order 排序,字符串或数组
	 * @return array
	 */
	public function getAdminList($module, $page = '', $pageSize = 0, $where = array(), $order = 'default') {
		$pageSize or $pageSize = 30;
		$this -> limit($pageSize);
		if (R('field')) {
			$order = R('order')?strtoupper(R('order')):'DESC';
			$order = 'A.' . R('field') . ' ' . $order;
		} elseif ($order == 'default') $order = 'A.cid DESC';
		if ($order) $this -> order($order);
		if (is_numeric($page)) {
			$page = max($page, 1);
			$this -> page = $page;
			$this -> page($page);
		}
		if (empty($module['id'])) show_error(L('MODULE_IS_NOT_EXIST'));
		if (empty($where)) $where = array('A.module' => $module['id']);
		else {
			if (!is_array($where)) $where = array($where);
			$where['A.module'] = $module['id'];
		}
		$this -> where($where);
		return $this -> field(array('A.*', 'B.title')) -> table('comment A') -> join("$module[name] B", 'A.id=B.id') -> select();
	}
	public function save() {
		$info = $_POST['info'];
		$info['cid'] = isset($info['cid'])?intval($info['cid']):0;
		if (!$info['cid']) {
			unset($info['cid']);
			$info['userid'] = get_cookie('userid');
			$info['username'] = get_cookie('username');
			$info['userip'] = IP;
			$info['addtime'] = TIME;
		}
		// 内容有效性
		$info['content'] = trim($info['content']);
		if (empty($info['content'])) {
			$this -> error = L('CONTENT_EMPTY');
			return false;
		}
		// 模块有效性
		if ($info['module']) {
			$info['module'] = get_module($info['module'], 'id');
		}
		$attachmentObj = M('attachment', '', 'comment');
		if (R('saveremote')) $info['content'] = $attachmentObj -> remote($info['content']);
		// 获取已上传附件
		$attachments = $attachmentObj -> getAttachments($info['content']);
		$info['passed'] = isset($info['passed'])?intval($info['passed']):0;
		$this -> create($info);
		if (!isset($info['cid'])) {
			$rs = $this -> add();
			$info['cid'] = $rs;
		} else {
			$rs = $this -> update();
		}
		if ($rs) {
			$attachmentObj -> setContentId($attachments, $info['cid']);
			$attachmentObj -> check($attachments, $info['cid']);
		}
		isset($this -> commentModule[$info['module']]) or $this -> cache();
		return $rs;
	}
	// 删除
	public function del() {
		$selids = R('selids');
		if (!empty($selids)) {
			$where = strstr($selids, ',')?"cid IN ($selids)":"cid=$selids";
			if ($this -> where($where) -> delete()) $rs = true;
			else {
				$this -> error = L('OPERATE_FAIL');
				$rs = false;
			}
		} else {
			$this -> error = L('NOPARAM');
			$rs = false;
		}
		return $rs;
	}
	public function cache() {
		$data = $this -> field('module') -> group('module') -> order('module') -> select();
		$module = array();
		if (is_array($data)) {
			foreach($data as $tmp) {
				$id = $tmp['module'];
				if ($id) {
					$name = get_module($id, 'name');
					$module[$id] = array('id' => $id, 'name' => $name, 'title' => get_module($id, 'title'), 'extrcomment' => C('extrcomment', 0, $name)?1:0);
				}
			}
		}
		empty($module) or Cache :: write('comment_module', $module);
	}
}
