<?php

/**
 *--------------------------------------
 * 选项
 *--------------------------------------
 * @project		: uwa
 * @author		: cblee
 * @created		: 2012-10-15
 * @copyright	: (c)2012 AsThis
 *--------------------------------------
 */

class OptionCtrlr extends ManageCtrlr
{
	public function edit_option()
	{
		$_LANG = list_dir(LANG_PATH);
		$this->assign('_LANG', $_LANG);

		$_TPL = list_dir(TPL_PATH);
		$this->assign('_TPL', $_TPL);

		/* 系统参数 */
		$_O = M('Option')->get_option(array('site', 'core', 'member', 'upload', 'image', 'interaction'));
		$this->assign('_O', $_O);

		/* 自定义参数 */
		$_CO = M('Option')->where('`o_type` = 1')->select();
		$this->assign('_CO', $_CO);

		/* 控制器列表 rewrite用 */
		$_CL = get_ctrlrList(LIB_CTRLR_PATH.D_S.'Home');
		$this->assign('_CL', $_CL); //todo

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

		$this->display();
	}
	public function edit_option_do()
	{
		$data = ARequest::get();

		$cfgFile = CFG_PATH.D_S.'comm.php';

		$_C = include($cfgFile);
		/* 保存语言设置 */
		$_C['LANG']['NAME'] = $data['site']['language'];
		/* 保存模板设置 */
		$_C['TE']['TPL_THEME'] = $data['site']['theme'];
		$_C['TE']['TPL_PROTECTION'] = $data['site']['tpl_protection'];
		/* HTML */
		$_C['HTML']['DIR'] = $data['core']['html_path'];
		/* 保存cookie设置 */
		$_C['COOKIE']['PREFIX'] = $data['core']['cookie_prefix'];
		$_C['COOKIE']['EXPIRE'] = $data['core']['cookie_expire'];
		$_C['COOKIE']['KEY'] = $data['core']['cookie_key'];
		/* 保存调试开关 */
		$_C['DEBUG']['SWITCH'] = $data['core']['debug_switch'];
		$_C['DEBUG']['STAT'] = $data['core']['debug_stat'];
		$_C['DEBUG']['PAGE_TRACE'] = $data['core']['debug_page_trace'];
		/* 是否伪静态 */
		if(1 == $data['core']['rewrite_switch'])
		{
			$_C['URL']['TYPE'] = 3;
		}
		else
		{
			$_C['URL']['TYPE'] = 1;
		}

		$content = var_export($_C, true);
		$content = "<?php\r\nreturn {$content};\r\n?>";
		if(!@file_put_contents($cfgFile, $content))
		{
			M('AdminLog')->add_log(ASession::get('m_userid'), L('EDIT').L('OPTION').': '.L('SAVE_CFG_FILE_FAILED'), 0);
			$this->error(L('SAVE_CFG_FILE_FAILED'), Url::U('option/edit_option'));
		}

		if(file_exists(RUNTIME_PATH.D_S.'~runtime.php'))
		{
			@unlink(realpath(RUNTIME_PATH.D_S.'~runtime.php'));
		}

		if(!M('Option')->save_option($data))
		{
			M('AdminLog')->add_log(ASession::get('m_userid'), L('EDIT').L('OPTION'), 0);
			$this->error(L('EDIT_FAILED'), Url::U('option/edit_option'));
		}
		M('AdminLog')->add_log(ASession::get('m_userid'), L('EDIT').L('OPTION'));
		$this->success(L('EDIT_SUCCESS'), Url::U('option/edit_option'));
	}

	public function add_custom_option_do()
	{
		$data = ARequest::get();

		if('bool' == $data['o_value_type'])
		{
			$data['o_value'] = strtoupper($data['o_value']);
			if('Y' != $data['o_value'] && 'N' != $data['o_value'])
			{
				M('AdminLog')->add_log(ASession::get('m_userid'), L('ADD').L('CUSTOM_OPTION').': OPTION_TITLE['.$data['o_title'].']', 0);
				$this->error(L('ADD_FAILED').': '.L('PARAMS_ERROR'), Url::U('option/edit_option'));
			}
		}

		$data['o_type'] = 1;
		$result = M('Option')->insert($data);

		if(false === $result)
		{
			M('AdminLog')->add_log(ASession::get('m_userid'), L('ADD').L('CUSTOM_OPTION').': '.$result['error'], 0);
			$this->error(L('ADD_FAILED'), Url::U('option/edit_option'));
		}

		M('AdminLog')->add_log(ASession::get('m_userid'), L('ADD').L('CUSTOM_OPTION').': OPTION_TITLE['.$data['o_title'].']');
		$this->success(L('ADD_SUCCESS'), Url::U('option/edit_option'));
	}

}

?>