<?php

/**
 *--------------------------------------
 * 举报
 *--------------------------------------
 * @project		: uwa
 * @author		: cblee
 * @created		: 2012-10-17
 * @copyright	: (c)2012 AsThis
 *--------------------------------------
 */

class ReportCtrlr extends ManageCtrlr
{
	public function list_report()
	{
		$where = array();
		/* 筛选类型*/
		$rItemType = ARequest::get('r_item_type') ? ARequest::get('r_status') : '';
		if(!empty($rItemType))
		{
			$where['r_item_type'] = array('EQ', $rItemType);
		}

		/* 筛选状态 */
		$rStatus = ARequest::get('r_status') ? ARequest::get('r_status') : '';
		if('n' == $rStatus)
		{
			$where['r_status'] = array('EQ', 0);
		}
		elseif('d' == $rStatus)
		{
			$where['r_status'] = array('EQ', 1);
		}

		/* 筛选关键词 */
		$rInfo = ARequest::get('r_info');
		if(!empty($rInfo))
		{
			$where['r_info'] = array('LIKE', '%'.$rInfo.'%');
		}

		/* 列表排序 */
		$orderBy = ARequest::get('order_by') ? ARequest::get('order_by') : 'report_id';
		$orderTurn = ARequest::get('order_turn') ? ARequest::get('order_turn') : 'desc';
		$order = "`{$orderBy}` {$orderTurn}";

		/* 获取分页信息 */
		$pageSize = (ARequest::get('page_size') > 0 ? ARequest::get('page_size') : 20);
		$_GET[C('VAR.PAGE')] = ARequest::get(C('VAR.PAGE')) ? ARequest::get(C('VAR.PAGE')) : 1;
		$rowsNum = M('Report')->where($where)->count();
		$p = new APage($rowsNum, $pageSize, Url::U('reportw/list_report?r_item_type='.$rItemType
			.'&r_status='.$rStatus
			.'&r_info='.$rInfo
			.'&order_by='.$orderBy
			.'&order_turn='.$orderTurn
			.'&'.C('VAR.PAGE').'=_page_'));
		$this->assign('PAGE_LIST', $p->show());
		$limit = $p->get_limit();

		/* 档案列表 */
		$_RL = M('Report')->where($where)->order($order)->limit($limit)->select();
		$this->assign('_RL', $_RL);

		$this->display();
	}

	public function toggle_report_status_do()
	{
		$data['report_id'] = ARequest::get('report_id');
		$data['r_status'] = ARequest::get('r_status');
		if(false === M('Report')->update($data))
		{
			M('AdminLog')->add_log(ASession::get('m_userid'), L('EDIT').L('REPORT').': REPORT_ID['.$data['report_id'].']', 0);
			$this->error(L('TOGGLE_FAILED'), Url::U('report/list_report'));
		}
		M('AdminLog')->add_log(ASession::get('m_userid'), L('EDIT').L('REPORT').': REPORT_ID['.$data['report_id'].']');
		$this->success(L('TOGGLE_SUCCESS'), Url::U('report/list_report'));
	}

	public function deal_report_do()
	{
		$reportId = ARequest::get('report_id');
		$_L_ID = is_array($reportId) ? implode(',', $reportId) : $reportId; /* 日志记录ID */

		if(is_array($reportId))
		{
			foreach($reportId as $reportId)
			{
				$result = M('Report')->deal_report($reportId);
				if(!empty($result['error']))
				{
					M('AdminLog')->add_log(ASession::get('m_userid'), L('DEAL').L('REPORT').': REPORT_ID['.$reportId.']'.$result['error'], 0);
					$this->error($result['error'], Url::U('report/list_report'));
				}
			}
		}
		else
		{
			$result = M('Report')->deal_report($reportId);
			if(!empty($result['error']))
			{
				M('AdminLog')->add_log(ASession::get('m_userid'), L('DEAL').L('REPORT').': REPORT_ID['.$reportId.']'.$result['error'], 0);
				$this->error($result['error'], Url::U('report/list_report'));
			}
		}

		/* 记录日志 */
		M('AdminLog')->add_log(ASession::get('m_userid'), L('DEAL').L('REPORT').': REPORT_ID['.$_L_ID.']');
		$this->success(L('DEAL_SUCCESS'), Url::U('report/list_report'));
	}

	public function delete_report_do()
	{
		$reportId = ARequest::get('report_id');
		$_L_ID = is_array($reportId) ? implode(',', $reportId) : $reportId; /* 日志记录ID */

		if(is_array($reportId))
		{
			foreach($reportId as $reportId)
			{
				$result = M('Report')->delete_report($reportId);
				if(!empty($result['error']))
				{
					M('AdminLog')->add_log(ASession::get('m_userid'), L('DELETE').L('REPORT').': REPORT_ID['.$reportId.']'.$result['error'], 0);
					$this->error($result['error'], Url::U('report/list_report'));
				}
			}
		}
		else
		{
			$result = M('Report')->delete_report($reportId);
			if(!empty($result['error']))
			{
				M('AdminLog')->add_log(ASession::get('m_userid'), L('DELETE').L('REPORT').': REPORT_ID['.$reportId.']'.$result['error'], 0);
				$this->error($result['error'], Url::U('report/list_report'));
			}
		}

		/* 记录日志 */
		M('AdminLog')->add_log(ASession::get('m_userid'), L('DELETE').L('REPORT').': REPORT_ID['.$_L_ID.']');
		$this->success(L('DELETE_SUCCESS'), Url::U('report/list_report'));
	}


}

?>