<?php

/**
 *--------------------------------------
 * 管理日志
 *--------------------------------------
 * @project		: uwa
 * @author		: cblee
 * @created		: 2013-11-22
 * @copyright	: (c)2013 AsThis
 *--------------------------------------
 */

class AdminLogCtrlr extends ManageCtrlr
{
	public function list_log()
	{
		/* 筛选状态 */
		$alStatus = ARequest::get('al_status') ? ARequest::get('al_status') : '';
		if('f' == $alStatus)
		{
			$where['__ADMIN_LOG__.al_status'] = array('EQ', 0);
		}
		elseif('s' == $alStatus)
		{
			$where['__ADMIN_LOG__.al_status'] = array('EQ', 1);
		}

		/* 列表排序 */
		$orderBy = 'al_time';
		$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('AdminLog')->where($where)->count();
		$p = new APage($rowsNum, $pageSize, Url::U('admin_log/list_log?f_status='.$fStatus
			.'&order_by='.$orderBy
			.'&order_turn='.$orderTurn
			.'&'.C('VAR.PAGE').'=_page_'));
		$this->assign('PAGE_LIST', $p->show());
		$limit = $p->get_limit();

		/* 日志列表 */
		$_ALL = M('AdminLog')->get_logList($where, $order, $limit);
		$this->assign('_ALL', $_ALL);

		$this->display();
	}

	public function download_log()
	{
		/* 筛选状态 */
		$alStatus = ARequest::get('al_status') ? ARequest::get('al_status') : '';
		if('f' == $alStatus)
		{
			$where['__ADMIN_LOG__.al_status'] = array('EQ', 0);
		}
		elseif('s' == $alStatus)
		{
			$where['__ADMIN_LOG__.al_status'] = array('EQ', 1);
		}

		/* 列表排序 */
		$orderBy = 'al_time';
		$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('AdminLog')->where($where)->count();
		$p = new APage($rowsNum, $pageSize, Url::U('admin_log/list_log?f_status='.$fStatus
			.'&order_by='.$orderBy
			.'&order_turn='.$orderTurn
			.'&'.C('VAR.PAGE').'=_page_'));
		$this->assign('PAGE_LIST', $p->show());
		$limit = $p->get_limit();

		/* 日志列表 */
		$_ALL = M('AdminLog')->get_logList($where, $order, $limit);

		M('AdminLog')->add_log(ASession::get('m_userid'), L('DOWNLOAD').L('ADMIN_LOG'));

		if(!empty($_ALL))
		{
			header('Cache-Control: no-cache, must-revalidate');
			header("Content-type:application/vnd.ms-excel");
			header("Content-Disposition:filename=log_".date('YmdHi',time()).".xls"); 

			$this->assign('_ALL', $_ALL);
			$data = $this->te->fetch();
			echo($data);
		}
		else
		{
			M('AdminLog')->add_log(ASession::get('m_userid'), L('DOWNLOAD').L('ADMIN_LOG').': '.L('EMPTY'), 0);
			$this->error(L('EMPTY'), Url::U('admin_log/list_log'));
		}
	}

	public function delete_old_log()
	{
		$where = array();
		$where['al_time'] = array('LT', time() - 86400*30);
		$result = M('AdminLog')->where($where)->delete();
		if(false === $result)
		{
			M('AdminLog')->add_log(ASession::get('m_userid'), L('DELETE_OLD_LOG'), 0);
			$this->error(L('DELETE_FAILED'), Url::U('admin_log/list_log'));
		}
		M('AdminLog')->add_log(ASession::get('m_userid'), L('DELETE_OLD_LOG'));
		$this->success(L('DELETE_SUCCESS'), Url::U('admin_log/list_log'));
	}

}

?>