<?php

/**
 *--------------------------------------
 * 档案模型管理
 *--------------------------------------
 * @project		: uwa
 * @author		: cblee
 * @created		: 2012-09-28
 * @copyright	: (c)2012 AsThis
 *--------------------------------------
 */

class ArchiveModelCtrlr extends ManageCtrlr
{
	public function list_model()
	{
		$_AML = M('ArchiveModel')->get_modelList(false);
		$this->assign('_AML', $_AML);
		$this->display();
	}

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

		M('AdminLog')->add_log(ASession::get('m_userid'), L('ADD').L('ARCHIVE_MODEL').': ARCHIVE_MODEL_ID['.$result['data'].']');
		$this->success(L('ADD_SUCCESS'), Url::U('archive_model/list_model'));
	}

	/* 编辑模型 */
	public function edit_model()
	{
		$archiveModelId = ARequest::get('archive_model_id');
		$_AMI = M('ArchiveModel')->get_modelInfo($archiveModelId);
		if(empty($_AMI))
		{
			$this->error(L('ITEM_NOT_EXIST'), Url::U('archive_model/list_model'));
		}

		$this->assign('_AMI', $_AMI);
		$this->display();
	}
	public function edit_model_do()
	{
		$data = ARequest::get();
		$result = M('ArchiveModel')->edit_model($data);
		if(!empty($result['error']))
		{
			M('AdminLog')->add_log(ASession::get('m_userid'), L('EDIT').L('ARCHIVE_MODEL').': ARCHIVE_MODEL_ID['.$data['archive_model_id'].']'.$result['error'], 0);
			$this->error($result['error'], Url::U('archive_model/list_model'));
		}
		M('AdminLog')->add_log(ASession::get('m_userid'), L('EDIT').L('ARCHIVE_MODEL').': ARCHIVE_MODEL_ID['.$data['archive_model_id'].']');
		$this->success(L('EDIT_SUCCESS'), Url::U('archive_model/list_model'));
	}

	/* 执行删除模型 */
	public function delete_model_do()
	{
		$archiveModelId = ARequest::get('archive_model_id');
		$result = M('ArchiveModel')->delete_model($archiveModelId);
		if(!empty($result['error']))
		{
			M('AdminLog')->add_log(ASession::get('m_userid'), L('DELETE').L('ARCHIVE_MODEL').': ARCHIVE_MODEL_ID['.$archiveModelId.']'.$result['error'], 0);
			$this->error($result['error'], Url::U('archive_model/list_model'));
		}

		M('AdminLog')->add_log(ASession::get('m_userid'), L('DELETE').L('ARCHIVE_MODEL').': ARCHIVE_MODEL_ID['.$archiveModelId.']');
		$this->success(L('DELETE_SUCCESS'), Url::U('archive_model/list_model'));
	}

	/* 切换模型状态 */
	public function toggle_model_status_do()
	{
		$data['archive_model_id'] = ARequest::get('archive_model_id');
		$data['am_status'] = ARequest::get('am_status');
		if(false === M('ArchiveModel')->update($data))
		{
			M('AdminLog')->add_log(ASession::get('m_userid'), L('EDIT').L('ARCHIVE_MODEL').': ARCHIVE_MODEL_ID['.$data['archive_model_id'].']', 0);
			$this->error(L('TOGGLE_FAILED'), Url::U('archive_model/list_model'));
		}
		F('~ami/~am_'.$data['archive_model_id'], null); /* 删除模型缓存 */
		F('~aml', null);

		M('AdminLog')->add_log(ASession::get('m_userid'), L('EDIT').L('ARCHIVE_MODEL').': ARCHIVE_MODEL_ID['.$data['archive_model_id'].']');
		$this->success(L('TOGGLE_SUCCESS'), Url::U('archive_model/list_model'));
	}

	/* 添加模型字段 */
	public function add_model_field()
	{
		$archiveModelId = ARequest::get('archive_model_id');
		$_AMI = M('ArchiveModel')->get_modelInfo($archiveModelId);
		if(empty($_AMI))
		{
			$this->error(L('ITEM_NOT_EXIST'), Url::U('archive_model/edit_model?archive_model_id='.$archiveModelId));
		}

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

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

		$this->display();
	}
	public function add_model_field_do()
	{
		$archiveModelId = ARequest::get('archive_model_id');
		$field[ARequest::get('f_name')] = ARequest::get();
		$result = M('ArchiveModel')->add_modelField($archiveModelId, $field);
		if(!empty($result['error']))
		{
			M('AdminLog')->add_log(ASession::get('m_userid'), L('ADD').L('ARCHIVE_MODEL_FIELD').': ARCHIVE_MODEL_ID['.$archiveModelId.'], FIELD['.ARequest::get('f_name').'] '.$result['error'], 0);
			$this->error($result['error'], Url::U('archive_model/edit_model?archive_model_id='.$archiveModelId));
		}

		M('AdminLog')->add_log(ASession::get('m_userid'), L('ADD').L('ARCHIVE_MODEL_FIELD').': ARCHIVE_MODEL_ID['.$archiveModelId.'], FIELD['.ARequest::get('f_name').']');
		$this->success(L('ADD_SUCCESS'), Url::U('archive_model/edit_model?archive_model_id='.$archiveModelId));
	}

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

		$_AMFI = M('ArchiveModel')->get_modelFieldInfo($archiveModelId, $fName);
		if(empty($_AMFI))
		{
			$this->error(L('ITEM_NOT_EXIST'), Url::U('archive_model/edit_model?archive_model_id='.$archiveModelId));
		}

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

		$_AMFI['archive_model_id'] = $archiveModelId;
		$_AMFI['f_name'] = $fName;
		$this->assign('_AMFI', $_AMFI);

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

		$result = M('ArchiveModel')->edit_modelField($archiveModelId, $field);
		if(!empty($result['error']))
		{
			M('AdminLog')->add_log(ASession::get('m_userid'), L('EDIT').L('ARCHIVE_MODEL_FIELD').': ARCHIVE_MODEL_ID['.$archiveModelId.'] , FIELD['.ARequest::get('f_name').']'.$result['error'], 0);
			$this->error($result['error'], Url::U('archive_model/edit_model?archive_model_id='.$archiveModelId));
		}

		M('AdminLog')->add_log(ASession::get('m_userid'), L('EDIT').L('ARCHIVE_MODEL_FIELD').': ARCHIVE_MODEL_ID['.$archiveModelId.'], FIELD['.ARequest::get('f_name').']');
		$this->success(L('EDIT_SUCCESS'), Url::U('archive_model/edit_model?archive_model_id='.$archiveModelId));
	}

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

		M('AdminLog')->add_log(ASession::get('m_userid'), L('DELETE').L('ARCHIVE_MODEL_FIELD').': ARCHIVE_MODEL_ID['.$archiveModelId.'], FIELD['.ARequest::get('f_name').']');
		$this->success(L('DELETE_SUCCESS'), Url::U('archive_model/edit_model?archive_model_id='.$archiveModelId));
	}

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

		$archiveModelId = ARequest::get('archive_model_id');
		$_AMI = M('ArchiveModel')->where('archive_model_id = \''.$archiveModelId.'\'')->find();
		unset($_AMI['archive_model_id']);

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

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

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

?>