<?php
// ÆÀÂÛ¿ØÖÆÆ÷
class CommentAction extends Action {
	protected function _initialize() {
		$this -> obj = M('comment');
	}
	public function defaults() {
		$m = strtolower(R('m'));
		$module = get_module($m);
		$id = intval(R('id'));
		$data = M($m) -> where(array('passed' => 1, 'recycle' => 0, 'id' => $id)) -> find();
		if (empty($data)) $this -> showMsg(L('ACCESS_IS_NOT_EXIST'));
		$data['realtitle'] = $data['title'];
		$data['title'] = $module['title'] . ' ' . $data['title'] . ' ' . L('COMMENT');
		$this -> assign('extract', $data);
		$page = max(1, intval(R('page')));
		$pageSize = 20;
		$where = array('module' => $module['id'], 'id' => $id, 'passed' => 1);
		$this -> assign('commentList', $this -> obj -> getList($page, $pageSize, $where));
		$this -> assign('pages', $this -> obj -> pages());
		$this -> assign('totle', $this -> obj -> totle);
		$this -> display('comment');
	}
	public function getList() {
		$pageSize = intval(R('num'));
		$pageSize or $pageSize = 5;
		$id = intval(R('id'));
		$m = strtolower(R('m'));
		$module = get_module($m);
		$where = array('module' => $module['id'], 'id' => $id, 'passed' => 1);
		$totle = $this -> obj -> where($where) -> count();
		$this -> assign('commentList', $this -> obj -> getList('', $pageSize, $where));
		$data = $this -> fetch('comment_list');
		$response = array($totle, iconv(C('APP_CHARSET', 1), 'utf-8', $data));
		exit(json_encode($response));
	}
	public function add() {
		if ($this -> obj -> save()) {
			$response = array(1, iconv(C('APP_CHARSET', 1), 'utf-8', L('OPERATE_SUCCESS')));
		} else {
			$response = array(0, iconv(C('APP_CHARSET', 1), 'utf-8', $this -> obj -> error));
		}
		exit(json_encode($response));
	}
}
