<?php

/**
 *--------------------------------------
 * 会员模型管理
 *--------------------------------------
 * @project		: uwa
 * @author		: cblee
 * @created		: 2012-10-08
 * @copyright	: (c)2012 AsThis
 *--------------------------------------
 */

class MemberModelCtrlr extends ManageCtrlr
{
	public function list_model()
	{
		$_MML = M('MemberModel')->get_modelList(false);
		$this->assign('_MML', $_MML);
		$this->display();
	}

	/* 添加模型 */
	public function add_model()
	{
		$this->display();
	}
	public function add_model_do()
	{
		$data = ARequest::get();
		$result = M('MemberModel')->add_model($data);
		if(!empty($result['error']))
		{
			M('AdminLog')->add_log(ASession::get('m_userid'), L('ADD').L('MEMBER_MODEL').': '.$result['error'], 0);
			$this->error($result['error'], Url::U('member_model/list_model'));
		}
		M('AdminLog')->add_log(ASession::get('m_userid'), L('ADD').L('MEMBER_MODEL').': MEMBER_MODEL_ID['.$result['data'].']');
		$this->success(L('ADD_SUCCESS'), Url::U('member_model/list_model'));
	}

	/* 编辑模型 */
	public function edit_model()
	{
		$memberModelId = ARequest::get('member_model_id');
		$_MMI = M('MemberModel')->get_modelInfo($memberModelId);
		if(empty($_MMI))
		{
			$this->error(L('ITEM_NOT_EXIST'), Url::U('member_model/list_model'));
		}

		$this->assign('_MMI', $_MMI);
		$this->display();
	}
	public function edit_model_do()
	{
		$data = ARequest::get();
		$result = M('MemberModel')->edit_model($data);
		if(!empty($result['error']))
		{
			M('AdminLog')->add_log(ASession::get('m_userid'), L('EDIT').L('MEMBER_MODEL').': MEMBER_MODEL_ID['.$data['member_model_id'].']', 0);
			$this->error($result['error'], Url::U('member_model/list_model'));
		}
		M('AdminLog')->add_log(ASession::get('m_userid'), L('EDIT').L('MEMBER_MODEL').': MEMBER_MODEL_ID['.$data['member_model_id'].']');
		$this->success(L('EDIT_SUCCESS'), Url::U('member_model/list_model'));
	}

	/* 执行删除模型 */
	public function delete_model_do()
	{
		$memberModelId = ARequest::get('member_model_id');
		$result = M('MemberModel')->delete_model($memberModelId);
		if(!empty($result['error']))
		{
			M('AdminLog')->add_log(ASession::get('m_userid'), L('DELETE').L('MEMBER_MODEL').': MEMBER_MODEL_ID['.$memberModelId.']'.$result['error'], 0);
			$this->error($result['error'], Url::U('member_model/list_model'));
		}

		M('AdminLog')->add_log(ASession::get('m_userid'), L('DELETE').L('MEMBER_MODEL').': MEMBER_MODEL_ID['.$memberModelId.']');
		$this->success(L('DELETE_SUCCESS'), Url::U('member_model/list_model'));
	}

	/* 切换模型状态 */
	public function toggle_model_status_do()
	{
		$data['member_model_id'] = ARequest::get('member_model_id');
		$data['mm_status'] = ARequest::get('mm_status');
		if(false === M('MemberModel')->update($data))
		{
			M('AdminLog')->add_log(ASession::get('m_userid'), L('EDIT').L('MEMBER_MODEL').': MEMBER_MODEL_ID['.$data['member_model_id'].']', 0);
			$this->error(L('TOGGLE_FAILED'), Url::U('member_model/list_model'));
		}
		M('AdminLog')->add_log(ASession::get('m_userid'), L('EDIT').L('MEMBER_MODEL').': MEMBER_MODEL_ID['.$data['member_model_id'].']');
		$this->success(L('TOGGLE_SUCCESS'), Url::U('member_model/list_model'));
	}

	/* 添加模型字段 */
	public function add_model_field()
	{
		$memberModelId = ARequest::get('member_model_id');
		$_MMI = M('MemberModel')->get_modelInfo($memberModelId);
		if(empty($_MMI))
		{
			$this->error(L('ITEM_NOT_EXIST'), Url::U('member_model/edit_model?member_model_id='.$memberModelId));
		}

		$_FT = load('fieldtype#comm');
		$this->assign('_FT', $_FT);

		$this->assign('_MMI', $_MMI);

		$this->display();
	}
	public function add_model_field_do()
	{
		$memberModelId = ARequest::get('member_model_id');
		$field[ARequest::get('f_name')] = ARequest::get();
		$result = M('MemberModel')->add_modelField($memberModelId, $field);
		if(!empty($result['error']))
		{
			M('AdminLog')->add_log(ASession::get('m_userid'), L('ADD').L('MEMBER_MODEL_FIELD').': MEMBER_MODEL_ID['.$memberModelId.'], FIELD['.ARequest::get('f_name').'] '.$result['error'], 0);
			$this->error($result['error'], Url::U('member_model/edit_model?member_model_id='.$memberModelId));
		}

		M('AdminLog')->add_log(ASession::get('m_userid'), L('ADD').L('MEMBER_MODEL_FIELD').': MEMBER_MODEL_ID['.$memberModelId.'], FIELD['.ARequest::get('f_name').']');
		$this->success(L('ADD_SUCCESS'), Url::U('member_model/edit_model?member_model_id='.$memberModelId));
	}

	/* 编辑模型字段 */
	public function edit_model_field()
	{
		$memberModelId = ARequest::get('member_model_id');
		$fName = ARequest::get('f_name');

		$_MMFI = M('MemberModel')->get_modelFieldInfo($memberModelId, $fName);
		if(empty($_MMFI))
		{
			$this->error(L('ITEM_NOT_EXIST'), Url::U('member_model/edit_model?member_model_id='.$memberModelId));
		}

		$_FT = load('fieldtype#comm');
		$this->assign('_FT', $_FT);

		$_MMFI['member_model_id'] = $memberModelId;
		$_MMFI['f_name'] = $fName;
		$this->assign('_MMFI', $_MMFI);

		$this->display();
	}
	public function edit_model_field_do()
	{
		$memberModelId = ARequest::get('member_model_id');
		$field[ARequest::get('f_name')] = ARequest::get();

		$result = M('MemberModel')->edit_modelField($memberModelId, $field);
		if(!empty($result['error']))
		{
			M('AdminLog')->add_log(ASession::get('m_userid'), L('EDIT').L('MEMBER_MODEL_FIELD').': MEMBER_MODEL_ID['.$memberModelId.'], FIELD['.ARequest::get('f_name').'] '.$result['error'], 0);
			$this->error($result['error'], Url::U('member_model/edit_model?member_model_id='.$memberModelId));
		}

		M('AdminLog')->add_log(ASession::get('m_userid'), L('EDIT').L('MEMBER_MODEL_FIELD').': MEMBER_MODEL_ID['.$memberModelId.'], FIELD['.ARequest::get('f_name').']');
		$this->success(L('EDIT_SUCCESS'), Url::U('member_model/edit_model?member_model_id='.$memberModelId));
	}

	/* 执行删除模型字段 */
	public function delete_model_field_do()
	{
		$memberModelId = ARequest::get('member_model_id');
		$fName = ARequest::get('f_name');
		$result = M('MemberModel')->delete_modelField($memberModelId, $fName);
		if(!empty($result['error']))
		{
			M('AdminLog')->add_log(ASession::get('m_userid'), L('DELETE').L('MEMBER_MODEL_FIELD').': MEMBER_MODEL_ID['.$memberModelId.'], FIELD['.ARequest::get('f_name').'] '.$result['error'], 0);
			$this->error($result['error'], Url::U('member_model/edit_model?member_model_id='.$memberModelId));
		}

		/* 记录日志 */
		M('AdminLog')->add_log(ASession::get('m_userid'), L('DELETE').L('MEMBER_MODEL_FIELD').': MEMBER_MODEL_ID['.$memberModelId.'], FIELD['.ARequest::get('f_name').']');
		$this->success(L('DELETE_SUCCESS'), Url::U('member_model/edit_model?member_model_id='.$memberModelId));
	}

	/* 导出模型 */
	public function export_model()
	{
		M('AdminLog')->add_log(ASession::get('m_userid'), L('EXPORT').L('MEMBER_MODEL'));

		$memberModelId = ARequest::get('member_model_id');
		$_MMI = M('MemberModel')->where('member_model_id = \''.$memberModelId.'\'')->find();
		unset($_MMI['member_model_id']);

		$_MMI['model_config'] = ACrypt::encrypt(serialize($_MMI));

		$this->assign('_MMI', $_MMI);
		$this->display();
	}

	/* 导入模型 */
	public function import_model()
	{
		$this->display();
	}
	public function import_model_do()
	{
		$modelConfig = ARequest::get('model_config');
		$result = M('MemberModel')->import_model($modelConfig);
		if(!empty($result['error']))
		{
			M('AdminLog')->add_log(ASession::get('m_userid'), L('IMPORT').L('MEMBER_MODEL').': '.$result['error'], 0);
			$this->error($result['error'], Url::U('member_model/list_model'));
		}
		M('AdminLog')->add_log(ASession::get('m_userid'), L('IMPORT').L('MEMBER_MODEL'));
		$this->success(L('IMPORT_SUCCESS'), Url::U('member_model/list_model'));
	}
}

?>