<?php

/**
 *--------------------------------------
 * 会员信息
 *--------------------------------------
 * @project		: uwa
 * @author		: cblee
 * @created		: 2012-10-13
 * @copyright	: (c)2012 AsThis
 *--------------------------------------
 */

class MemberNotifyCtrlr extends ManageCtrlr
{
	public function list_notify()
	{
		$where = array();
		/* 筛选状态 */
		$mnStatus = ARequest::get('mn_status') ? ARequest::get('mn_status') : '';
		if('u' == $mnStatus)
		{
			$where['mn_status'] = array('EQ', 0);
		}
		elseif('r' == $mnStatus)
		{
			$where['mn_status'] = array('EQ', 1);
		}

		/* 筛选标题 */
		$mnTitle = ARequest::get('mn_title');
		if(!empty($mnTitle))
		{
			$where['mn_title'] = array('LIKE', '%'.$mnTitle.'%');
		}

		/* 筛选内容 */
		$mnContent = ARequest::get('mn_content');
		if(!empty($mnContent))
		{
			$where['mn_content'] = array('LIKE', '%'.$mnContent.'%');
		}

		/* 筛选管理员 */
		$mnAdminUserId = ARequest::get('mn_admin_userid') ? ARequest::get('mn_admin_userid') : '';
		if(!empty($mnAdminUserId))
		{
			$where['mn_admin_userid'] = array('EQ', $mnAdminUserId);
		}

		/* 筛选会员 */
		$mnMId = ARequest::get('mn_m_id') ? ARequest::get('mn_m_id') : 0;
		if($mnMId  > 0)
		{
			$where['mn_m_id'] = array('EQ', $mnMId);
		}

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

		/* 获取分页信息 */
		$pageSize = (ARequest::get('page_size') > 0 ? ARequest::get('page_size') : 10);
		$_GET[C('VAR.PAGE')] = ARequest::get(C('VAR.PAGE')) ? ARequest::get(C('VAR.PAGE')) : 1;
		$rowsNum = M('MemberNotify')->where($where)->count();
		$p = new APage($rowsNum, $pageSize, Url::U('member_notify/list_notify?mn_admin_userid'.$mnAdminUserId
			.'&mn_m_id='.$mnMId
			.'&mn_status='.$mnStatus
			.'&mn_title='.$mnTitle
			.'&mn_content='.$mnContent
			.'&order_by='.$orderBy
			.'&order_turn='.$orderTurn
			.'&'.C('VAR.PAGE').'=_page_'));
		$this->assign('PAGE_LIST', $p->show());
		$limit = $p->get_limit();

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

		$this->display();
	}

	public function add_notify_do()
	{
		$data = ARequest::get();
		$data['mn_admin_userid'] = ASession::get('m_userid');
		$data['mn_send_time'] = time();
		$mMId = explode(',', $data['mn_m_id']);
		$_L_ID = array(); /* 日志记录ID */
		foreach($mMId as $mMId)
		{
			$data['mn_m_id'] = $mMId;
			$result = M('MemberNotify')->add_notify($data);
			if(!empty($result['error']))
			{
				M('AdminLog')->add_log(ASession::get('m_userid'), L('ADD').L('MEMBER_NOTIFY').': '.$result['error'], 0);
				$this->error($result['error'], Url::U('member_notify/list_notify'));
			}
			$_L_ID[] = $result['data'];
		}

		M('AdminLog')->add_log(ASession::get('m_userid'), L('ADD').L('MEMBER_NOTIFY').': MEMBER_NOTIFY_ID['.implode(',', $_L_ID).']');
		$this->success(L('ADD_SUCCESS'), Url::U('member_notify/list_notify'));
	}

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

		if(is_array($memberNotifyId))
		{
			$memberNotifyId = implode(',', $memberNotifyId);
		}
		$result = M('MemberNotify')->delete_notify($memberNotifyId);
		if(!empty($result['error']))
		{
			M('AdminLog')->add_log(ASession::get('m_userid'), L('DELETE').L('MEMBER_NOTIFY').': MEMBER_NOTIFY_ID['.$memberNotifyId.']'.$result['error'], 0);
			$this->error($result['error'], Url::U('member_notify/list_notify'));
		}

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


?>