<?php

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

class MemberCtrlr extends ManageCtrlr
{
	public function list_member()
	{
		/* 模型列表 */
		$_MML = M('MemberModel')->get_modelList(1);
		$this->assign('_MML', $_MML);

		/* 等级列表 */
		$_MLL = M('MemberLevel')->get_levelList();
		$this->assign('_MLL', $_MLL);

		$where = array();
		/* 筛选模型 */
		$memberModelId = ARequest::get('member_model_id') ? ARequest::get('member_model_id') : 0;
		if($memberModelId > 0)
		{
			$where['__MEMBER__.member_model_id'] = array('EQ', $memberModelId);
		}

		/* 筛选等级 */
		$memberLevelId = ARequest::get('member_level_id') ? ARequest::get('member_level_id') : 0;
		if($memberLevelId > 0)
		{
			$where['__MEMBER__.member_level_id'] = array('EQ', $memberLevelId);
		}

		/* 筛选状态 */
		$mStatus = ARequest::get('m_status') ? ARequest::get('m_status') : '';
		if('n' == $mStatus)
		{
			$where['__MEMBER__.m_status'] = array('EQ', 0);
		}
		elseif('p' == $mStatus)
		{
			$where['__MEMBER__.m_status'] = array('EQ', 1);
		}
		elseif('d' == $mStatus)
		{
			$where['__MEMBER__.m_status'] = array('EQ', 2);
		}

		/* 筛选昵称 */
		$mUsername = ARequest::get('m_username');
		if(!empty($mUsername))
		{
			$where['__MEMBER__.m_username'] = array('LIKE', '%'.$mUsername.'%');
		}

		/* 列表排序 */
		$orderBy = ARequest::get('order_by') ? ARequest::get('order_by') : 'member_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('Member')->where($where)->count();
		$p = new APage($rowsNum, $pageSize, Url::U('member/list_member?member_model_id='.$memberModelId
			.'&member_level_id='.$memberLevelId
			.'&m_status='.$mStatus
			.'&m_username='.$mUsername
			.'&order_by='.$orderBy
			.'&order_turn='.$orderTurn
			.'&'.C('VAR.PAGE').'=_page_'));
		$this->assign('PAGE_LIST', $p->show());
		$limit = $p->get_limit();

		/* 档案列表 */
		$_ML = M('Member')->join('__MEMBER_MODEL__ AS mm ON mm.member_model_id = __MEMBER__.member_model_id')
			->join('__MEMBER_LEVEL__ AS ml ON ml.member_level_id = __MEMBER__.member_level_id')
			->where($where)->order($order)->limit($limit)->select();
		$this->assign('_ML', $_ML);

		$this->display();
	}

	/* 添加会员 */
	public function add_member()
	{
		$memberModelId = ARequest::get('member_model_id');
		$_MMI = M('MemberModel')->get_modelInfo($memberModelId);
		if(empty($_MMI))
		{
			$this->error(L('ITEM_NOT_EXIST'), Url::U('member/list_member'));
		}
		if(0 == $_MMI['mm_status'])
		{
			$this->error(L('MODEL_IS_NOT_ACTIVE'), Url::U('member/list_member'));
		}
		$this->assign('_MI', $_MMI);

		/* 等级列表 */
		$_MLL = M('MemberLevel')->get_levelList();
		if(empty($_MLL))
		{
			$this->error(L('ADD_LEVEL_FIRST'), Url::U('member_level/add_level'));
		}
		$this->assign('_MLL', $_MLL);

		/* 附加表信息 */
		$_FI = '';
		load('field#func');
		foreach($_MMI['mm_field'] as $tag => $params)
		{
			if(1 == $params['f_is_auto'])
			{
				$_FI .= get_formItem($tag, $params);
			}
		}
		$this->assign('_FI', $_FI);

		/* 积分类型列表 */
		$_MCTL = M('MemberCreditType')->get_creditTypeList();
		$this->assign('_MCTL', $_MCTL);

		$this->display('admin/'.$_MMI['mm_tpl_add']);
	}
	public function add_member_do()
	{
		$data = ARequest::get();
		$data['m_userid'] = strtolower($data['m_userid']); /* userid is lowercase */
		$data['m_email'] = strtolower($data['m_email']); /* email is lowercase */
		$data['m_password'] = md5($data['m_userid'].md5($data['m_password']));
		$data['m_experience'] = !empty($data['m_experience']) ? $data['m_experience'] : 0;
		$data['m_points'] = !empty($data['m_points']) ? $data['m_points'] : 0;
		$data['m_reg_time'] = !empty($data['m_reg_time']) ? strtotime($data['m_reg_time']) : time();
		$data['m_reg_ip'] = !empty($data['m_reg_ip']) ? $data['m_reg_ip'] : AServer::get_ip();
		$data['m_login_time'] = !empty($data['m_login_time']) ? strtotime($data['m_login_time']) : time();
		$data['m_login_ip'] = !empty($data['m_login_ip']) ? $data['m_login_ip'] : AServer::get_ip();

		$result = M('Member')->add_member($data);
		if(!empty($result['error']))
		{
			M('AdminLog')->add_log(ASession::get('m_userid'), L('ADD').L('MEMBER').': '.$result['error'], 0);
			$this->error($result['error'], Url::U('member/list_member'));
		}

		/* 更新上传文件 */
		M('Upload')->update_upload($result['data'], ASession::get('member_id'));

		M('AdminLog')->add_log(ASession::get('m_userid'), L('ADD').L('MEMBER').': MEMBER_ID['.$result['data'].']');
		$this->success(L('ADD_SUCCESS'), Url::U('member/list_member'));
	}

	/* 编辑会员 */
	public function edit_member()
	{
		$memberId = ARequest::get('member_id');
		$_MI = M('Member')->get_memberInfo($memberId);
		if(empty($_MI))
		{
			$this->error(L('ITEM_NOT_EXIST'), Url::U('member/list_member'));
		}
		$this->assign('_MI', $_MI);

		/* 等级列表 */
		$_MLL = M('MemberLevel')->get_levelList();
		if(empty($_MLL))
		{
			$this->error(L('ADD_LEVEL_FIRST'), Url::U('member_level/add_level'));
		}
		$this->assign('_MLL', $_MLL);

		/* 附加表信息 */
		$_FI = '';
		load('field#func');
		foreach($_MI['mm_field'] as $tag => $params)
		{
			if(1 == $params['f_is_auto'])
			{
				$_FI .= get_formItem($tag, $params, $_MI);
			}
		}
		$this->assign('_FI', $_FI);

		/* 积分类型列表 */
		$_MCTL = M('MemberCreditType')->get_creditTypeList();
		$this->assign('_MCTL', $_MCTL);

		$this->display('admin/'.$_MI['mm_tpl_edit']);
	}
	public function edit_member_do()
	{
		$data = ARequest::get();
		$data['m_email'] = strtolower($data['m_email']); /* email is lowercase */
		if(empty($data['m_password']))
		{
			unset($data['m_password']);
		}
		else
		{
			$data['m_password'] = md5($data['m_userid'].md5($data['m_password']));
		}

		$result = M('Member')->edit_member($data);
		if(!empty($result['error']))
		{
			M('AdminLog')->add_log(ASession::get('m_userid'), L('EDIT').L('MEMBER').': MEMBER_ID['.$data['member_id'].']'.$result['error'], 0);
			$this->error($result['error'], Url::U('member/list_member'));
		}

		/* 更新上传文件 */
		M('Upload')->update_upload($data['member_id'], ASession::get('member_id'));

		M('AdminLog')->add_log(ASession::get('m_userid'), L('EDIT').L('MEMBER').': MEMBER_ID['.$data['member_id'].']');
		$this->success(L('EDIT_SUCCESS'), Url::U('member/list_member'));
	}

	/* 删除会员 */
	public function delete_member_do()
	{
		$memberId = ARequest::get('member_id');
		$_L_ID = is_array($memberId) ? implode(',', $memberId) : $memberId; /* 日志记录ID */

		if(is_array($memberId))
		{
			foreach($memberId as $memberId)
			{
				$result = M('Member')->delete_member($memberId);
				if(!empty($result['error']))
				{
					M('AdminLog')->add_log(ASession::get('m_userid'), L('DELETE').L('MEMBER').': MEMBER_ID['.$memberId.']'.$result['error'], 0);
					$this->error($result['error'], Url::U('member/list_member'));
				}
			}
		}
		else
		{
			$result = M('Member')->delete_member($memberId);
			if(!empty($result['error']))
			{
				M('AdminLog')->add_log(ASession::get('m_userid'), L('DELETE').L('MEMBER').': MEMBER_ID['.$memberId.']'.$result['error'], 0);
				$this->error($result['error'], Url::U('member/list_member'));
			}
		}

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

	/* 编辑自己信息 */
	public function edit_my_info()
	{
		$memberId = ASession::get('member_id');
		$_MI = M('Member')->get_memberInfo($memberId);
		if(empty($_MI))
		{
			$this->error(L('ITEM_NOT_EXIST'), Url::U('member/list_member'));
		}
		$this->assign('_MI', $_MI);

		/* 等级列表 */
		$_MLL = M('MemberLevel')->get_levelList();
		if(empty($_MLL))
		{
			$this->error(L('ADD_LEVEL_FIRST'), Url::U('member_level/add_level'));
		}
		$this->assign('_MLL', $_MLL);

		/* 积分类型列表 */
		$_MCTL = M('MemberCreditType')->get_creditTypeList();
		$this->assign('_MCTL', $_MCTL);

		/* 附加表信息 */
		$_FI = '';
		load('field#func');
		foreach($_MI['mm_field'] as $tag => $params)
		{
			if(1 == $params['f_is_auto'])
			{
				$_FI .= get_formItem($tag, $params, $_MI);
			}
		}
		$this->assign('_FI', $_FI);

		$this->display();
	}
	public function edit_my_info_do()
	{
		$data = ARequest::get();
		$data['m_email'] = strtolower($data['m_email']); /* email is lowercase */
		if(empty($data['m_password']))
		{
			unset($data['m_password']);
		}
		else
		{
			$data['m_password'] = md5($data['m_userid'].md5($data['m_password']));
		}

		$result = M('Member')->edit_member($data, true, false);
		if(!empty($result['error']))
		{
			M('AdminLog')->add_log(ASession::get('m_userid'), L('EDIT_MY_INFO').': '.$result['error'], 0);
			$this->error($result['error'], Url::U('index/show_system_info'));
		}
		M('AdminLog')->add_log(ASession::get('m_userid'), L('EDIT_MY_INFO'));
		$this->success(L('EDIT_SUCCESS'), Url::U('index/show_system_info'));
	}

	/* 审核会员 */
	public function pass_member_do()
	{
		$memberId = ARequest::get('member_id');
		$_L_ID = is_array($memberId) ? implode(',', $memberId) : $memberId; /* 日志记录ID */

		if(is_array($memberId))
		{
			foreach($memberId as $memberId)
			{
				$result = M('Member')->pass_member($memberId);
				if(!empty($result['error']))
				{
					M('AdminLog')->add_log(ASession::get('m_userid'), L('PASS').L('MEMBER').': MEMBER_ID['.$memberId.']'.$result['error'], 0);
					$this->error($result['error'], Url::U('member/list_member'));
				}
			}
		}
		else
		{
			$result = M('Member')->pass_member($memberId);
			if(!empty($result['error']))
			{
				M('AdminLog')->add_log(ASession::get('m_userid'), L('PASS').L('MEMBER').': MEMBER_ID['.$memberId.']'.$result['error'], 0);
				$this->error($result['error'], Url::U('member/list_member'));
			}
		}

		/* 记录日志 */
		M('AdminLog')->add_log(ASession::get('m_userid'), L('PASS').L('MEMBER').': MEMBER_ID['.$_L_ID.']');
		$this->success(L('PASS_SUCCESS'), Url::U('member/list_member'));
	}

	/* 禁止会员 */
	public function forbidden_member_do()
	{
		$memberId = ARequest::get('member_id');
		$_L_ID = is_array($memberId) ? implode(',', $memberId) : $memberId; /* 日志记录ID */

		if(is_array($memberId))
		{
			foreach($memberId as $memberId)
			{
				$result = M('Member')->forbidden_member($memberId);
				if(!empty($result['error']))
				{
					M('AdminLog')->add_log(ASession::get('m_userid'), L('FORBIDDEN').L('MEMBER').': MEMBER_ID['.$memberId.']'.$result['error'], 0);
					$this->error($result['error'], Url::U('member/list_member'));
				}
			}
		}
		else
		{
			$result = M('Member')->forbidden_member($memberId);
			if(!empty($result['error']))
			{
				M('AdminLog')->add_log(ASession::get('m_userid'), L('FORBIDDEN').L('MEMBER').': MEMBER_ID['.$memberId.']'.$result['error'], 0);
				$this->error($result['error'], Url::U('member/list_member'));
			}
		}

		/* 记录日志 */
		M('AdminLog')->add_log(ASession::get('m_userid'), L('FORBIDDEN').L('MEMBER').': MEMBER_ID['.$_L_ID.']');
		$this->success(L('FORBIDDEN_SUCCESS'), Url::U('member/list_member'));
	}

}


?>