<?php

/**
 *--------------------------------------
 * 留言
 *--------------------------------------
 * @project		: uwa
 * @author		: cblee
 * @created		: 2012-10-26
 * @copyright	: (c)2012 AsThis
 *--------------------------------------
 */

class GuestbookCtrlr extends IndexCtrlr
{
	public function list_guestbook()
	{
		$_o = M('Option')->get_option('interaction/guestbook_switch');
		if(!$_o)
		{
			$this->error(L('GUESTBOOK_IS_OFF'), __APP__);
		}

		$where = array();
		/* 筛选状态 */
		$where['__GUESTBOOK__.g_status'] = array('EQ', 1);

		/* 列表排序 */
		$order = "`g_add_time` DESC";

		/* 获取分页信息 */
		$_GET[C('VAR.PAGE')] = intval(ARequest::get(C('VAR.PAGE'))) ? intval(ARequest::get(C('VAR.PAGE'))) : 1;
		$rowsNum = M('Guestbook')->where($where)->count();
		$p = new APage($rowsNum, 10, Url::U('home@guestbook/list_guestbook?'.C('VAR.PAGE').'=_page_'));
		$this->assign('PAGE_LIST', $p->show());
		$limit = $p->get_limit();

		/* 留言列表 */
		$_GL = M('Guestbook')->get_guestbookList($where, $order, $limit);
		$this->assign('_L', $_GL);

		/* 页面位置 */
		$this->assign('POSITION', '<a href="'.__APP__.'">'.L('HOME').'</a> &raquo; '.L('GUESTBOOK'));

		$this->display('home/list_guestbook');
	}

	public function add_guestbook_do()
	{
		check_interaction('feedback');

		$data = array();
		$data['g_author'] = AFilter::text(ARequest::get('g_author'), 96);
		$data['g_content'] = AFilter::text(ARequest::get('g_content'), 500);
		$data['g_add_time'] = time();
		$data['g_add_ip'] = AServer::get_ip();
		$data['member_id'] = ASession::get('member_id');

		/* 内容过滤有问题 或者需审核 */
		$_o_i = M('Option')->get_option('interaction');
		if($_o_i['auto_report'] and !M('Report')->report_check(array($data['g_author'], $data['g_content'])))
		{
			$data['g_status'] = 0;
			$report = true;
		}
		elseif($_o_i['feedback_check'])
		{
			$data['g_status'] = 0;
		}
		else
		{
			$data['g_status'] = 1;
		}

		$result = M('Guestbook')->add_guestbook($data);

		/* 需报告 */
		if($report)
		{
			$_t_data['r_item_type'] = 'guestbook';
			$_t_data['r_item_id'] = M('Guestbook')->get_lastInsID();
			$_t_data['r_info'] = 'filter';
			$_t_data['r_add_time'] = time();
			$_t_data['r_add_ip'] = AServer::get_ip();
			$_t_data['r_status'] = 0;
			M('Report')->insert($_t_data);
		}

		if(!empty($result['error']))
		{
			$this->error($result['error'], Url::U('home@guestbook/list_guestbook'));
		}
		$this->success(L('ADD_SUCCESS'), Url::U('home@guestbook/list_guestbook'));
	}

}

?>