<?php

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

class MemberCtrlr extends IndexCtrlr
{
	protected $_o_m;

	public function __construct()
	{
		parent::__construct();

		/* 会员开关检查 */
		$this->_o_m = M('Option')->get_option('member');
		if(!$this->_o_m['switch'])
		{
			$this->error(L('MEMBER_CENTER_IS_OFF'), __APP__);
		}

		/* 检查cookie是否有效 */
		if(ASession::get('member_id'))
		{
			if(!ACookie::get('member_id'))
			{
				$_MI = M('Member')->field('m_userid,m_username')->where('member_id=\''.ASession::get('member_id').'\'')->find();
				ACookie::set('member_id', ASession::get('member_id'));
				ACookie::set('m_userid', $_MI['m_userid']);
				ACookie::set('m_username', $_MI['m_username']);
			}
		}
		elseif(ACookie::get('member_id'))
		{
			$_MI = M('Member')->field('m_userid,m_status')->where('member_id=\''.ACookie::get('member_id').'\'')->find();
			if($_MI['m_userid'] == ACookie::get('m_userid'))
			{
				ASession::set('member_id', ACookie::get('member_id'));
				ASession::set('m_status', $_MI['m_status']);
				ASession::set('ml_rank', M('Member')->get_mlRank(ACookie::get('member_id')));
			}
			else
			{
				ASession::clear();
				ACookie::clear();
			}
		}

		/* 登录检查*/
		$commActn = array('register', 'register_do', 'login', 'login_do', 'logout_do', 'register_check');
		if(!ASession::get('member_id') and !in_array(ACTN_NAME, $commActn))
		{
			redirect(Url::U('home@member/login'));
			exit();
		}

		/* 会员状态检查 */
		$extraActn = array('add_archive', 'add_archive_do', 'edit_archive', 'edit_archive_do');
		if(1 != ASession::get('m_status') and in_array(ACTN_NAME, $extraActn))
		{
			$this->error(L('PERMISSION_LIMIT'), Url::U('home@member/index'));
		}

		/* 档案模型列表 */
		$_AML = M('ArchiveModel')->get_modelList(1, true);
		$this->assign('_AML', $_AML);

	}

	public function index()
	{
		/* 会员信息 */
		$_MI = M('Member')->get_memberInfo(ASession::get('member_id'));
		$this->assign('_MI', $_MI);

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

		/* 档案列表 */
		$_AL = M('Archive')->get_archiveList('member_id= \''.ASession::get('member_id').'\'', '`a_edit_time` DESC', 5);
		$this->assign('_AL', $_AL);

		$this->display();
	}

	public function register()
	{
		/* 会员注册开关检查 */
		if(!$this->_o_m['register'])
		{
			$this->error(L('MEMBER_REGISTER_IS_OFF'), __APP__);
		}

		$memberModelId = ARequest::get('member_model_id');
		if(0 < $memberModelId)
		{
			$_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);

			/* 附加表信息 */
			$_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);

			$this->display('home/'.$_MMI['mm_tpl_add_member']);
		}
		else
		{
			/* 模型列表 */
			$_MML = M('MemberModel')->get_modelList(1);
			$this->assign('_L', $_MML);

			$this->display('home/member/register_guide');
		}
	}
	public function register_do()
	{
		/* 会员注册开关检查 */
		if(!$this->_o_m['register'])
		{
			$this->error(L('MEMBER_REGISTER_IS_OFF'), __APP__);
		}

		check_interaction('register');

		/* 检查模型ID */
		$_MMI = M('MemberModel')->get_modelInfo(ARequest::get('member_model_id'));
		if(empty($_MMI))
		{
			$this->error(L('ITEM_NOT_EXIST'), AServer::get_preUrl());
		}
		if(0 == $_MMI['mm_status'])
		{
			$this->error(L('MODEL_IS_NOT_ACTIVE'), AServer::get_preUrl());
		}

		/* 检查数据password */
		if(strlen(ARequest::get('m_password')) < $this->_o_m['password_min_length'])
		{
			$this->error(L('PASSWORD_TOO_SHORT'), AServer::get_preUrl());
		}
		elseif(ARequest::get('m_password') != ARequest::get('m_password_repeat'))
		{
			$this->error(L('PASSWORD_NOT_MATCH'), AServer::get_preUrl());
		}

		$data = array();
		/* 检查数据userid, username, email */
		if(!AFilter::is_userid(ARequest::get('m_userid')))
		{
			$this->error(L('USERID_FORMAT_ERROR'), AServer::get_preUrl());
		}
		elseif(strlen(ARequest::get('m_userid')) < $this->_o_m['userid_min_length'])
		{
			$this->error(L('USERID_TOO_SHORT'), AServer::get_preUrl());
		}
		elseif(!M('Member')->check_userid(ARequest::get('m_userid')))
		{
			$this->error(L('USERID_HAS_BEEN_USED'), AServer::get_preUrl());
		}
		if('' == ARequest::get('m_username') or !AFilter::is_username(ARequest::get('m_username')))
		{
			$this->error(L('USERNAME_FORMAT_ERROR'), AServer::get_preUrl());
		}
		if(!AFilter::is_email(ARequest::get('m_email')))
		{
			$this->error(L('EMAIL_FORMAT_ERROR'), AServer::get_preUrl());
		}
		elseif(!M('Member')->check_email(ARequest::get('m_email')))
		{
			$this->error(L('EMAIL_HAS_BEEN_USED'), AServer::get_preUrl());
		}

		$data['member_model_id'] = intval(ARequest::get('member_model_id'));
		$data['m_userid'] = strtolower(ARequest::get('m_userid'));
		$data['m_username'] = ARequest::get('m_username');
		$data['m_email'] = strtolower(ARequest::get('m_email'));
		$data['m_password'] = md5($data['m_userid'].md5(ARequest::get('m_password')));

		$data['m_experience'] = 0;
		$data['m_points'] = 0;
		$data['m_reg_time'] = time();
		$data['m_reg_ip'] = AServer::get_ip();
		$data['m_login_time'] = $data['m_reg_time'];
		$data['m_login_ip'] = $data['m_reg_ip'];
		$data['member_level_id'] = 1;
		if(1 != $this->_o_m['pass_type'])
		{
			$data['m_status'] = 1;
		}
		else
		{
			$data['m_status'] = 0;
		}
		$result = M('Member')->add_member($data);

		if(!empty($result['error']))
		{
			$this->error(L('ERROR_UNKONW'), Url::U('home@member/register'));
		}
		ASession::set('member_id', $result['data']);
		ASession::set('ml_rank', M('Member')->get_mlRank($result['data']));
		ASession::set('m_status', $data['m_status']);
		ACookie::set('member_id', $result['data']);
		ACookie::set('m_userid', $data['m_userid']);
		ACookie::set('m_username', $data['m_username']);

		$this->success(L('REGISTER_SUCCESS'), Url::U('home@member/index'));
	}

	public function login()
	{
		$this->display();
	}
	public function login_do()
	{
		check_interaction('login');

		$where = array();
		if(!AFilter::is_userid(ARequest::get('m_userid')))
		{
			$this->error(L('VERIFY_FAILED'), AServer::get_preUrl());
		}
		$where['m_userid'] = array('EQ', ARequest::get('m_userid'));
		$where['m_password'] = array('EQ', md5(ARequest::get('m_userid').md5(ARequest::get('m_password'))));

		$_MI = M('Member')->where($where)->find();
		if(empty($_MI))
		{
			$this->error(L('VERIFY_FAILED'), AServer::get_preUrl());
		}

		$data = array();
		$data['member_id'] = $_MI['member_id'];
		$data['m_login_time'] = time();
		$data['m_login_ip'] = AServer::get_ip();
		/* 更新等级 */
		$_ML = M('MemberLevel')->where('`member_level_id` = '.$_MI['member_level_id'])->find();
		if($_ML['ml_type'])
		{
			$mlId = M('MemberLevel')->get_levelId($_MI['m_experience']);
			if($mlId != $_MI['member_level_id'])
			{
				$data['member_level_id'] = $mlId;
			}
			M('Member')->update($data);
		}
		/* 更新积分 */
		if(date('Ymd', time()) > date('Ymd', $_MI['m_login_time']))
		{
			M('Member')->update_credit($data['member_id'], 'login');
		}

		/*设置*/
		$expireTime = intval(ARequest::get('expire_time'));
		ASession::set('member_id', $_MI['member_id']);
		ASession::set('m_status', $_MI['m_status']);
		ASession::set('ml_rank', M('Member')->get_mlRank($_MI['member_id']));
		ACookie::set('member_id', $_MI['member_id'], null, null, $expireTime);
		ACookie::set('m_userid', $_MI['m_userid'], null, null, $expireTime);
		ACookie::set('m_username', $_MI['m_username'], null, null, $expireTime);

		$this->success(L('LOGIN_SUCCESS'), Url::U('home@member/index'));
	}

	public function logout_do()
	{
		ASession::clear();
		ACookie::clear();
		$this->success(L('LOGOUT_SUCCESS'), AServer::get_preUrl());
	}

	public function register_check()
	{
		if('m_userid' == ARequest::get('type'))
		{
			if(!AFilter::is_userid(ARequest::get('m_userid')))
			{
				exit('<span class="fc_r">'.L('USERID_FORMAT_ERROR').'</span>');
			}
			if(!M('Member')->check_userid(ARequest::get('m_userid')))
			{
				exit('<span class="fc_r">'.L('USERID_HAS_BEEN_USED').'</span>');
			}
			exit('<span class="fc_g">'.L('USERID_IS_AVAILABLE').'</span>');
		}
		elseif('m_email' == ARequest::get('type'))
		{
			if(!AFilter::is_email(ARequest::get('m_email')))
			{
				exit('<span class="fc_r">'.L('EMAIL_FORMAT_ERROR').'</span>');
			}
			if(!M('Member')->check_email(ARequest::get('m_email')))
			{
				exit('<span class="fc_r">'.L('EMAIL_HAS_BEEN_USED').'</span>');
			}
			exit('<span class="fc_g">'.L('EMAIL_IS_AVAILABLE').'</span>');
		}
		elseif('m_username' == ARequest::get('type'))
		{
			if(!AFilter::is_username(ARequest::get('m_username')))
			{
				exit('<span class="fc_r">'.L('USERNAME_FORMAT_ERROR').'</span>');
			}
			if(!M('Report')->report_check(ARequest::get('m_username')))
			{
				exit('<span class="fc_r">'.L('USERNAME_HAS_BEEN_USED').'</span>');
			}
			exit('<span class="fc_g">'.L('USENAME_IS_AVAILABLE').'</span>');
		}
		exit();
	}

	public function list_archive()
	{
		$where = array();
		$where['__ARCHIVE__.member_id'] = array('EQ', ASession::get('member_id'));

		/* 获取模型信息 */
		$archiveModelId = intval(ARequest::get('archive_model_id'));
		$_AMI = M('ArchiveModel')->get_modelInfo($archiveModelId);
		if(empty($_AMI) or !$_AMI['am_status'])
		{
			$this->error(L('MODEL_IS_NOT_ACTIVE'), Url::U('home@member/index'));
		}
		$this->assign('_AMI', $_AMI);

		/* 筛选频道 */
		$_ACL = M('ArchiveChannel')->get_memberChannelList($archiveModelId, ASession::get('ml_rank'));
		$act = new ATree($_ACL, array('archive_channel_id', 'ac_parent_id', 'ac_sub_channel'));
		$_aclId = implode(',', $act->get_leafid());
		$where['__ARCHIVE__.archive_channel_id'] = array('IN', $_aclId);

		/* 筛选状态 */
		$aStatus = in_array(ARequest::get('a_status'), array('n', 'p', 'r')) ? ARequest::get('a_status') : '';
		if('n' == $aStatus)
		{
			$where['__ARCHIVE__.a_status'] = array('EQ', 0);
		}
		elseif('p' == $aStatus)
		{
			$where['__ARCHIVE__.a_status'] = array('EQ', 1);
		}
		elseif('r' == $aStatus)
		{
			$where['__ARCHIVE__.a_status'] = array('EQ', 2);
		}

		$order = "`a_edit_time` DESC";

		/* 获取分页信息 */
		$rowsNum = M('Archive')->where($where)->count();
		$p = new APage($rowsNum, 10, Url::U('home@member/list_archive?archive_model_id='.$archiveModelId.'&a_status='.$aStatus.'&'.C('VAR.PAGE').'=_page_'));
		$this->assign('PAGE_LIST', $p->show());
		$limit = $p->get_limit();

		/* 档案列表 */
		$_AL = M('Archive')->get_archiveList($where, $order, $limit);
		$this->assign('_AL', $_AL);

		$this->display('home/'.$_AMI['am_tpl_list_member']);
	}

	public function add_archive()
	{
		/* 获取模型信息 */
		$archiveModelId = intval(ARequest::get('archive_model_id'));
		$_AMI = M('ArchiveModel')->get_modelInfo($archiveModelId);
		if(empty($_AMI) or !$_AMI['am_status'])
		{
			$this->error(L('MODEL_IS_NOT_ACTIVE'), Url::U('home@member/index'));
		}
		$this->assign('_AI', $_AMI);

		/* 获取频道列表 */
		$_ACL = M('ArchiveChannel')->get_memberChannelList($archiveModelId, ASession::get('ml_rank'));
		if(empty($_ACL))
		{
			$this->error(L('NO_CHANNEL_AVAILABLE'), Url::U('home@member/index'));
		}
		$act = new ATree($_ACL, array('archive_channel_id', 'ac_parent_id', 'ac_sub_channel'));
		$_ACLStr = $act->get_leafStr(0, "<option value='\$archive_channel_id'>\$spacer\$ac_name</option>\r\n");
		$this->assign('_ACLStr', $_ACLStr);

		$_FI = '';
		load('field#func');
		foreach($_AMI['am_field'] as $tag => $params)
		{
			if(1 == $params['f_is_auto'])
			{
				$_FI .= get_formItem($tag, $params, null, true);
			}
		}
		$this->assign('_FI', $_FI);

		$this->display('home/'.$_AMI['am_tpl_add_member']);
	}
	public function add_archive_do()
	{
		check_interaction();

		$data = array();
		$data['archive_channel_id'] = intval(ARequest::get('archive_channel_id'));
		/* 检查权限 */
		$_aclId = explode(',', M('ArchiveChannel')->get_memberChannelList(0, ASession::get('ml_rank'), true));
		if(!in_array($data['archive_channel_id'], $_aclId))
		{
			$this->error(L('PERMISSION_LIMIT'), Url::U('home@member/index'));
		}

		/* 上传缩略图 */
		if('' != $_FILES['a_thumb_uploader']['name'])
		{
			/* 是否水印 */
			$watermark = false;
			/* 返回类型 normal, editor */
			$returnType = 'normal';
			/* 上传类型, 如archive, flink, ad */
			$uploadType = 'archive';

			/* 上传文件 */
			$result = M('Upload')->upload_file('a_thumb_uploader', 'image', true, false, 'normal', 'archive');
			if(empty($result['error']))
			{
				$data['a_thumb'] = $result['data'];
			}
		}

		$data['a_title'] = AFilter::plain_text(ARequest::get('a_title'), 32);
		$data['a_keywords'] = AFilter::plain_text(ARequest::get('a_keywords'), 96);
		$data['a_description'] = AFilter::text(ARequest::get('a_description'), 200);
		$data['a_cost_points'] = intval(ARequest::get('a_cost_points'));
		$data['member_id'] = ASession::get('member_id');
		$data['m_username'] = ACookie::get('m_username');
		$data['a_add_time'] = time();
		$data['a_edit_time'] = $data['a_add_time'];
		$data['a_add_ip'] = AServer::get_ip();
		$data['a_edit_ip'] = $data['a_add_ip'];

		$_pass_switch = M('ArchiveChannel')->field('ac_pass_switch')->find($data['archive_channel_id']);
		if(0 == $_pass_switch['ac_pass_switch'])
		{
			$data['a_status'] = 1;
		}
		else
		{
			$data['a_status'] = 0;
		}

		$_o = M('Option')->get_option(array('core', 'interaction'));
		$_aci = M('ArchiveChannel')->where('archive_channel_id=\''.$data['archive_channel_id'].'\'')->find();
		if($_o['interaction']['review_switch'] and $_aci['ac_review_switch'])
		{
			$data['a_review_switch'] = 1;
		}
		else
		{
			$data['a_review_switch'] = 0;
		}
		if($_o['core']['html_switch'] and $_aci['ac_is_html'])
		{
			$data['a_is_html'] = 1;
		}
		else
		{
			$data['a_is_html'] = 0;
		}

		/* 插入主表 */
		$result = M('Archive')->add_archive($data);
		if(!empty($result['error']))
		{
			$this->error(L('PUBLISH_FAILED'), Url::U('home@member/index'));
		}

		/* 插入附加表 */
		$data['archive_id'] = $result['data'];
		$data = array_merge(ARequest::get(), $data);
		$result = M('Archive')->add_archive_addon($data);
		if(!empty($result['error']))
		{
			$this->error(L('PUBLISH_FAILED'), Url::U('home@member/index'));
		}

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

		/* 更新会员积分 */
		if($data['a_status'])
		{
			M('Member')->update_credit(ASession::get('member_id'), 'publish');
		}

		$this->success(L('PUBLISH_SUCCESS'), Url::U('home@member/index'));
	}

	public function edit_archive()
	{
		$_AI = M('Archive')->get_archiveInfo(intval(ARequest::get('archive_id')));
		if(empty($_AI) or ASession::get('member_id') != $_AI['member_id'])
		{
			$this->error(L('ITEM_NOT_EXIST'), Url::U('home@member/index'));
		}
		$this->assign('_AI', $_AI);

		/* 获取频道列表 */
		$_ACL = M('ArchiveChannel')->get_memberChannelList($_AI['archive_model_id'], ASession::get('ml_rank'));
		if(empty($_ACL))
		{
			$this->error(L('NO_CHANNEL_AVAILABLE'), Url::U('home@member/index'));
		}
		$act = new ATree($_ACL, array('archive_channel_id', 'ac_parent_id', 'ac_sub_channel'));
		$_ACLStr = $act->get_leafStr(0, "<option value='\$archive_channel_id'>\$spacer\$ac_name</option>\r\n", $_AI['archive_channel_id'], "<option value='\$archive_channel_id' selected='selected'>\$spacer\$ac_name</option>\r\n");
		$this->assign('_ACLStr', $_ACLStr);

		$_FI = '';
		load('field#func');
		foreach($_AI['am_field'] as $tag => $params)
		{
			if(1 == $params['f_is_auto'])
			{
				$_FI .= get_formItem($tag, $params, $_AI, true);
			}
		}
		$this->assign('_FI', $_FI);

		$this->display('home/'.$_AI['am_tpl_edit_member']);
	}
	public function edit_archive_do()
	{
		check_interaction();

		$data = array();
		/* 检查权限 */
		$data['archive_id'] = intval(ARequest::get('archive_id'));
		$_AI = M('Archive')
			->where('archive_id = \''.$data['archive_id'].'\' AND member_id=\''.ASession::get('member_id').'\'')
			->field('archive_channel_id,a_status,a_thumb')->find();
		if(empty($_AI))
		{
			$this->error(L('ITEM_NOT_EXIST'), Url::U('home@member/index'));
		}
		$data['archive_channel_id'] = intval(ARequest::get('archive_channel_id'));

		$_ACL = M('ArchiveChannel')->get_memberChannelList(0, ASession::get('ml_rank'));
		$act = new ATree($_ACL, array('archive_channel_id', 'ac_parent_id', 'ac_sub_channel'));
		$_aclId = $act->get_leafid();
		if(!in_array($data['archive_channel_id'], $_aclId))
		{
			$this->error(L('PERMISSION_LIMIT'), Url::U('home@member/index'));
		}

		/* 上传缩略图 */
		if('' != $_FILES['a_thumb_uploader']['name'])
		{
			/* 是否水印 */
			$watermark = false;
			/* 返回类型 normal, editor */
			$returnType = 'normal';
			/* 上传类型, 如archive, flink, ad */
			$uploadType = 'archive';

			/* 上传文件 */
			$result = M('Upload')->upload_file('a_thumb_uploader', 'image', true, false, 'normal', 'archive');
			if(empty($result['error']))
			{
				$data['a_thumb'] = $result['data'];
			}

			/* 删除原缩略图 */
			if(!empty($_AI['a_thumb']))
			{
				M('Upload')->where('u_src=\''.$_AI['a_thumb'].'\'')->delete();
				@unlink(realpath($_SERVER['DOCUMENT_ROOT'].$_AI['a_thumb']));
			}
		}

		$data['a_title'] = AFilter::plain_text(ARequest::get('a_title'), 32);
		$data['a_keywords'] = AFilter::plain_text(ARequest::get('a_keywords'), 96);
		$data['a_description'] = AFilter::text(ARequest::get('a_description'), 500);
		$data['a_cost_points'] = intval(ARequest::get('a_cost_points'));
		$data['member_id'] = ASession::get('member_id');
		$data['m_username'] = ACookie::get('m_username');
		$data['a_edit_time'] = time();
		$data['a_edit_ip'] = AServer::get_ip();

		$_pass_switch = M('ArchiveChannel')->field('ac_pass_switch')->find($data['archive_channel_id']);
		if(0 == $_pass_switch['ac_pass_switch'])
		{
			$data['a_status'] = 1;
		}
		else
		{
			$data['a_status'] = 0;
		}

		/* 编辑主表 */
		$result = M('Archive')->edit_archive($data);
		if(!empty($result['error']))
		{
			$this->error(L('EDIT_FAILED'), Url::U('home@member/index'));
		}

		/* 编辑附加表 */
		$data = array_merge(ARequest::get(), $data);
		$result = M('Archive')->edit_archive_addon($data);
		if(!empty($result['error']))
		{
			$this->error(L('EDIT_FAILED'), Url::U('home@member/index'));
		}

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

		$this->success(L('EDIT_SUCCESS'), Url::U('home@member/index'));
	}

	public function list_notify()
	{
		$where = array();
		$where['mn_m_id'] = array('IN', '-1,'.ASession::get('member_id'));
		/* 筛选状态 */
		$mnStatus = in_array(ARequest::get('mn_status'), array('u', 'r')) ? ARequest::get('mn_status') : '';
		if('u' == $mnStatus)
		{
			$where['mn_status'] = array('EQ', 0);
		}
		elseif('r' == $mnStatus)
		{
			$where['mn_status'] = array('EQ', 1);
		}

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

		/* 获取分页信息 */
		$_GET[C('VAR.PAGE')] = intval(ARequest::get(C('VAR.PAGE'))) ? intval(ARequest::get(C('VAR.PAGE'))) : 1;
		$rowsNum = M('MemberNotify')->where($where)->count();
		$p = new APage($rowsNum, 10, Url::U('home@member/list_notify?mn_status='.$mnStatus.'&'.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 read_notify()
	{
		$where = array();
		$where['member_notify_id'] = array('EQ', intval(ARequest::get('member_notify_id')));
		$where['mn_m_id'] = array('EQ', ASession::get('member_id'));
		M('MemberNotify')->where($where)->set_field('mn_status', 1);
		exit();
	}

	public function delete_notify_do()
	{
		$_o_i = M('Option')->get_option('interaction');
		/* 操作间隔太短 */
		if(!I('interaction', $_o_i['feedback_interval']))
		{
			$this->error(L('_TRY_LATER_'), AServer::get_preUrl());
		}

		$where = array();
		$where['mn_m_id'] = array('EQ', ASession::get('member_id'));
		$where['member_notify_id'] = array('EQ', intval(ARequest::get('member_notify_id')));

		if(false === M('MemberNotify')->where($where)->delete())
		{
			$this->error(L('DELETE_NOTIFY_FAILED'), AServer::get_preUrl());
		}

		I('interaction');
		$this->success(L('DELETE_NOTIFY_SUCCESS'), Url::U('home@member/list_notify'));
	}

	public function edit_info_base()
	{
		/* 会员信息 */
		$_MI = M('Member')->get_memberInfo(ASession::get('member_id'));
		$this->assign('_MI', $_MI);

		$this->display();
	}
	public function edit_info_base_do()
	{
		check_interaction();

		/* 检查会员 */
		$where = array();
		$where['member_id'] = array('EQ', ASession::get('member_id'));
		$where['m_userid'] = array('EQ', ARequest::get('m_userid'));
		$where['m_password'] = array('EQ', md5(ARequest::get('m_userid').md5(ARequest::get('m_password'))));
		$_MI = M('Member')->field('m_email')->where($where)->find();
		if(empty($_MI))
		{
			$this->error(L('VERIFY_FAILED'), AServer::get_preUrl());
		}
		/* 检查username */
		if('' == ARequest::get('m_username') or !AFilter::is_username(ARequest::get('m_username')))
		{
			$this->error(L('USERNAME_FORMAT_ERROR'), AServer::get_preUrl());
		}
		/* 检查email */
		if(!AFilter::is_email(ARequest::get('m_email')))
		{
			$this->error(L('EMAIL_FORMAT_ERROR'), AServer::get_preUrl());
		}
		if((strtolower(ARequest::get('m_email')) != strtolower($_MI['m_email']))
			and !M('Member')->check_email(ARequest::get('m_email')))
		{
			$this->error(L('EMAIL_HAS_BEEN_USED'), AServer::get_preUrl());
		}
		/* 检查new password */
		if((0 < strlen(ARequest::get('m_new_password')))
			and (strlen(ARequest::get('m_new_password')) < $this->_o_m['password_min_length']))
		{
			$this->error(L('PASSWORD_TOO_SHORT'), AServer::get_preUrl());
		}
		elseif(ARequest::get('m_new_password') != ARequest::get('m_new_password_repeat'))
		{
			$this->error(L('PASSWORD_NOT_MATCH'), AServer::get_preUrl());
		}
		$data = array();
		$data['member_id'] = ASession::get('member_id');
		$data['m_username'] = ARequest::get('m_username');
		$data['m_email'] = strtolower(ARequest::get('m_email'));
		if(strlen(ARequest::get('m_new_password')) >= $this->_o_m['password_min_length'])
		{
			$data['m_password'] = md5(ARequest::get('m_userid').md5(ARequest::get('m_new_password')));
		}

		if(false === M('Member')->update($data))
		{
			$this->error(L('EDIT_FAILED'), AServer::get_preUrl());
		}
		ACookie::set('m_username', $data['m_username']);
		$this->success(L('EDIT_SUCCESS'), AServer::get_preUrl());
	}

	public function edit_info_addon()
	{
		$_MI = M('Member')->get_memberInfo(ASession::get('member_id'));
		/* 附加表信息 */
		$_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('home/'.$_MI['mm_tpl_edit_member']);
	}
	public function edit_info_addon_do()
	{
		check_interaction();

		$memberModelId = M('Member')->where('member_id = \''.ASession::get('member_id').'\'')->get_field('member_model_id');
		$_MMI = M('MemberModel')->get_modelInfo($memberModelId);

		$data = array();
		$data['member_id'] = ASession::get('member_id');
		/* 处理附加表字段 */
		load('field#func');
		foreach($_MMI['mm_field'] as $tag => $params)
		{
			$data[$tag] = get_fieldValue($tag, $params, ARequest::get());
		}

		if(false === M(parse_name($_MMI['mm_addon_table'], 1))->update($data))
		{
			$this->error(L('EDIT_FAILED'), AServer::get_preUrl());
		}

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

		$this->success(L('EDIT_SUCCESS'), AServer::get_preUrl());
	}

	public function list_upload()
	{
		$where = array();
		$where['member_id'] = array('EQ', ASession::get('member_id'));
		/* 筛选文件名 */
		$uFilename = AFilter::keyword(ARequest::get('u_filename'));
		if(MAGIC_QUOTES_GPC)
		{
			$uFilename = stripslashes($uFilename);
		}
		$uFilename = str_replace('\'', '', $uFilename);
		$uFilename = str_replace('"', '', $uFilename);
		$uFilename = preg_replace('/\s+/', ' ', $uFilename);
		if(!empty($uFilename))
		{
			$where['u_filename'] = array('LIKE', '%'.$uFilename.'%');
		}

		/* 列表排序 */
		$orderBy = in_array(ARequest::get('order_by'), array('upload_id', 'u_filename', 'u_type', 'u_size', 'u_add_time')) ? ARequest::get('order_by') : 'upload_id';
		$orderTurn = in_array(ARequest::get('order_turn'), array('desc', 'asc')) ? ARequest::get('order_turn') : 'desc';
		$order = "`{$orderBy}` {$orderTurn}";

		/* 获取分页信息 */
		$_GET[C('VAR.PAGE')] = intval(ARequest::get(C('VAR.PAGE'))) ? intval(ARequest::get(C('VAR.PAGE'))) : 1;
		$rowsNum = M('Upload')->where($where)->count();
		$p = new APage($rowsNum, 10, Url::U('home@member/list_upload?u_filename='.$uFilename
			.'&order_by='.$orderBy
			.'&order_turn='.$orderTurn
			.'&'.C('VAR.PAGE').'=_page_'));
		$this->assign('PAGE_LIST', $p->show());
		$limit = $p->get_limit();

		/* 档案列表 */
		$_UL = M('Upload')->get_uploadList($where, $order, $limit);
		$this->assign('_UL', $_UL);

		$this->display();
	}

	public function credit_exchange()
	{
		/* 会员信息 */
		$_MI = M('Member')->get_memberInfo(ASession::get('member_id'));
		$this->assign('_MI', $_MI);

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

		$this->display();
	}

	public function credit_exchange_do()
	{
		check_interaction();
		$type = ARequest::get('type');
		if(!AFilter::is_word(ARequest::get('mct_alias')))
		{
			$this->error(L('ERROR_UNKNOWN'), AServer::get_preUrl());
		}
		$mtcAlias = ARequest::get('mct_alias');
		$amount = intval(ARequest::get('amount'));
		$_c_old = M('MemberCredit')->where('member_id = \''.ASession::get('member_id').'\'')->get_field($mtcAlias);
		$_p_old = M('Member')->where('member_id = \''.ASession::get('member_id').'\'')->get_field('m_points');
		$ratio = intval(M('MemberCreditType')->where('mct_alias = \''.$mtcAlias.'\'')->get_field('mct_ratio'));
		if('ctp' == $type)
		{
			if($amount > $_c_old)
			{
				$this->error(L('CREDIT_NOT_ENOUGH'), AServer::get_preUrl());
			}
			$_c_change = $amount;
			$_p_change = $amount * $ratio;
			/* 减少积分 */
			M('MemberCredit')->where('member_id = \''.ASession::get('member_id').'\'')->field_dec($mtcAlias, '', $_c_change);
			/* 增加点券 */
			M('Member')->where('member_id = \''.ASession::get('member_id').'\'')->field_inc('m_points', '', $_p_change);
		}
		elseif('ptc' == $type)
		{
			if($amount > $_p_old)
			{
				$this->error(L('POINTS_NOT_ENOUGH'), AServer::get_preUrl());
			}
			$_c_change = floor($amount / $ratio);
			$_p_change = $amount - $amount % $ratio;
			/* 减少点券 */
			M('Member')->where('member_id = \''.ASession::get('member_id').'\'')->field_dec('m_points', '', $_p_change);
			/* 增加积分 */
			M('MemberCredit')->where('member_id = \''.ASession::get('member_id').'\'')->field_inc($mtcAlias, '', $_c_change);
		}

		$this->success(L('EXCHANGE_SUCCESS'), AServer::get_preUrl());
	}

	public function buy_archive_do()
	{
		$_o_i = M('Option')->get_option('interaction');
		/* 操作间隔太短 */
		if(!I('buy', $_o_i['feedback_interval']))
		{
			$this->error(L('_TRY_LATER_'), AServer::get_preUrl());
		}

		$archiveId = intval(ARequest::get('archive_id'));
		$_AI = M('Archive')->where('archive_id = \''.$archiveId.'\'')
			->field('a_title,a_cost_points,member_id')->find();
		if(empty($_AI))
		{
			$this->error(L('ITEM_NOT_EXSIT'), AServer::get_preUrl());
		}

		$_MOI = M('MemberOrder')->where('member_id =\''.ASession::get('member_id').'\' and mo_product_type=\'archive\' and mo_product_name =\'ARCHIVE'.$archiveId.'\'')->find();
		if(!empty($_MOI))
		{
			$this->error(L('PAIED'), AServer::get_preUrl());
		}

		$_mp = M('Member')->where('member_id = \''.ASession::get('member_id').'\'')->get_field('m_points');
		if($_mp < $_AI['a_cost_points'])
		{
			$this->error(L('POINTS_NOT_ENOUGH'), AServer::get_preUrl());
		}
		M('Member')->where('member_id = \''.ASession::get('member_id').'\'')->field_dec('m_points', '', $_AI['a_cost_points']);
		M('Member')->where('member_id = \''.$_AI['member_id'].'\'')->field_inc('m_points', '', $_AI['a_cost_points']);

		$data = array();
		$data['member_id'] = ASession::get('member_id');
		$data['mo_product_type'] = 'archive';
		$data['mo_product_name'] = 'ARCHIVE'.$archiveId;
		$data['mo_points'] = $_AI['a_cost_points'];
		$data['mo_status'] = 1;
		$data['mo_add_time'] = time();
		$data['mo_add_ip'] = AServer::get_ip();
		if(false === M('MemberOrder')->insert($data))
		{
			$this->error(L('BUY_FAILED'), AServer::get_preUrl());
		}

		/* 添加到收藏 */
		$_MFI = M('MemberFavorite')->where('member_id =\''.ASession::get('member_id').'\' and archive_id =\''.$archiveId.'\'')->find();
		if(empty($_MFI))
		{
			$data = array();
			$data['member_id'] = ASession::get('member_id');
			$data['archive_id'] = $archiveId;
			$data['mf_title'] = $_AI['a_title'];
			$data['mf_url'] = $_AI['a_url'];
			$data['mf_add_time'] = time();
			M('MemberFavorite')->insert($data);
		}

		I('buy');
		$this->success(L('BUY_SUCCESS'), AServer::get_preUrl());
	}

	public function add_favorite_do()
	{
		$_o_i = M('Option')->get_option('interaction');
		/* 操作间隔太短 */
		if(!I('interaction', $_o_i['feedback_interval']))
		{
			$this->error(L('_TRY_LATER_'), AServer::get_preUrl());
		}

		$archiveId = intval(ARequest::get('archive_id'));
		$_AI = M('Archive')->where('archive_id = \''.$archiveId.'\'')
			->field('a_title,a_url')->find();
		if(empty($_AI))
		{
			$this->error(L('ITEM_NOT_EXSIT'), AServer::get_preUrl());
		}
		$_MFI = M('MemberFavorite')->where('member_id =\''.ASession::get('member_id').'\' and archive_id =\''.$archiveId.'\'')->find();
		if(!empty($_MFI))
		{
			$this->error(L('FAVORITE_EXSIT'), Url::U('home@member/list_favorite'));
		}
		$data = array();
		$data['member_id'] = ASession::get('member_id');
		$data['archive_id'] = $archiveId;
		$data['mf_title'] = $_AI['a_title'];
		$data['mf_url'] = $_AI['a_url'];
		$data['mf_add_time'] = time();
		if(false === M('MemberFavorite')->insert($data))
		{
			$this->error(L('ADD_FAVORITE_FAILED'), AServer::get_preUrl());
		}

		I('interaction');
		$this->success(L('ADD_FAVORITE_SUCCESS'), Url::U('home@member/list_favorite'));
	}

	public function list_favorite()
	{
		$where = array();
		$where['member_id'] = array('EQ', ASession::get('member_id'));
		/* 筛选文件名 */
		$mfTitle = AFilter::plain_text(ARequest::get('mf_title'), 32);
		if(MAGIC_QUOTES_GPC)
		{
			$mfTitle = stripslashes($mfTitle);
		}
		$mfTitle = str_replace('\'', '', $mfTitle);
		$mfTitle = str_replace('"', '', $mfTitle);
		$mfTitle = preg_replace('/\s+/', ' ', $mfTitle);
		if(!empty($mfTitle))
		{
			$where['mf_title'] = array('LIKE', '%'.$mfTitle.'%');
		}

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

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

		/* 收藏列表 */
		$_MFL = M('MemberFavorite')->get_favoriteList($where, $order, $limit);
		$this->assign('_MFL', $_MFL);

		$this->display();
	}

	public function delete_favorite_do()
	{
		$_o_i = M('Option')->get_option('interaction');
		/* 操作间隔太短 */
		if(!I('interaction', $_o_i['feedback_interval']))
		{
			$this->error(L('_TRY_LATER_'), AServer::get_preUrl());
		}

		$where = array();
		$where['member_id'] = array('EQ', ASession::get('member_id'));
		$where['member_favorite_id'] = array('EQ', intval(ARequest::get('member_favorite_id')));

		if(false === M('MemberFavorite')->where($where)->delete())
		{
			$this->error(L('DELETE_FAVORITE_FAILED'), AServer::get_preUrl());
		}

		I('interaction');
		$this->success(L('DELETE_FAVORITE_SUCCESS'), Url::U('home@member/list_favorite'));
	}

}

?>