<?php
/*
* @package LabCMS
* @copyright (c) 2006 Alexander Levin aka Loki (loki_angel@mail.ru)
* @support http://labcms.ru
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*/

class admin_settings_main extends ModuleTools
{
	public function __construct()
	{
		if (!access('admin')) return LabCMS::i()->error403();
	}
	
	public function core()
	{
		$core_cfg=$this->_getCoreCfg();
		
		if ($_SERVER['REQUEST_METHOD']=='POST') {
			//config::cleanModule('core');
			foreach ($this->all_settings as $key=>$conf_info) {
				config::save('core.'.$key, $_POST['core'][$key]);
			}
			LabCMS::reset_cache('objects');
			refresh();
		}
		
		self::_setTpl('core');
		self::_assign('core_cfg', $this->_getCoreCfg());
	}
	
	public function modules()
	{
		require_once LAB_PATH_MODULES.'/admin_groups/functions.php';
		$module_conf=getUserGroupConf(-1);
		self::_assign('modules', $module_conf);

		if ($_SERVER['REQUEST_METHOD']=='POST') {
			updateUserGroupConf(-1, $module_conf, $_POST);
			LabCMS::reset_cache('all');
			refresh('', _url(false, 'modules'));
		}
		self::_setTpl('access');
	}
	
	protected function _getCoreCfg()
	{
		//получаем настройки движка
		$config=config::getCoreConfig();
		$xml = simplexml_load_file(LAB_PATH_KERNEL.'/install/config.xml');
		$core_cfg=array();
		include_once LAB_PATH_KERNEL.'/functions/config.func.php';
		//$xml=get_object_vars($xml);

		$i=0;
		$this->all_settings=array();
		foreach ($xml->configs as $key=>$group) {
			$core_cfg[$i]['name']=(string)$group->attributes()->name;
			foreach ($group as $cfg) {
				$cfg=get_object_vars($cfg);
				if (!isset($config[$cfg['name']])) $val=$cfg['default'];
				else $val=$config[$cfg['name']];
				$core_cfg[$i]['config'][$cfg['name']]=array(
					'val'=>$val,
					'title'=>$cfg['title'],
					'describe'=>$cfg['describe'],
					'type'=>$cfg['type'],
					'default'=>$cfg['default']
				);
				
				if (is_object($cfg['default'])) $cfg['default']='';
				
				$this->all_settings[$cfg['name']]=array('type'=>$cfg['type'], 'default'=>$cfg['default']);
				
				if (!empty($cfg['values'])) {
					$tmp=get_object_vars($cfg['values']);
					$core_cfg[$i]['values'][$cfg['name']]=array_combine($tmp['value'], $tmp['value']);
				} elseif (!empty($cfg['callback'])) {
					//тут вызываем функцию возвращающую возможные значения
					$core_cfg[$i]['values'][$cfg['name']]=$cfg['callback']();
				}
			}
			$i++;
		}
		return $core_cfg;
	}
}