<?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
*/
	switch (true)
	{
		case($_GET['action']=="install" && access("admin")):
			include dirname(__FILE__)."/install.php";
		break;
		case($_GET['action']=="edit" && access("admin")):
			include dirname(__FILE__)."/edit.php";
		break;
		case($_GET['action']=="delete" && access("admin")):
			include dirname(__FILE__)."/delete.php";
		break;
		case($_GET['action']=="reinstall" && access("admin")):
			include dirname(__FILE__)."/reinstall.php";
		break;
		case($_GET['action']=="toggle" && access("admin")):
			include dirname(__FILE__)."/toggle.php";
		break;
		case($_GET['action']=="info" && access("admin")):
			include dirname(__FILE__)."/info.php";
		break;
		case(access("admin")):
			include dirname(__FILE__)."/list.php";
		break;
		default:
			LabCMS::i()->error403();
	}

/**
 * Выборка модулей и проверка на подключение
 *
 * @param none
 * @return array
 */
function getModules(){
	global $tbl_modules;
	$module_tmp=array();
	$all_mods=glob(LAB_PATH_MODULES.'/'."*", GLOB_ONLYDIR);
            $result=Db::i()->select("SELECT * FROM ?_modules ORDER BY NAME"); // Все установленные модули
            foreach($result as $modules)
                {
					$module[$modules['module']] = array('module_id'=>$modules['module_id'],
                                           'name'=>$modules['name'],
                                           'disable'=>$modules['disable']);
                }
            foreach($all_mods as $mod){
						$name= basename($mod);
						if (!array_key_exists($name, $module))
						{
							if (file_exists($mod."/config.xml"))
							{
								$xml = simplexml_load_file($mod."/config.xml");
								$xml=get_object_vars($xml);
								$module_tmp[]=array('name'=>$name, 'title'=>$xml['name']);
							}
						}
             }
	if (count($module_tmp))
	{
		usort($module_tmp, "msort");
		foreach($module_tmp as $item){$module[$item['name']]=array('name'=>$item['title']);}
	}
    return $module;
}

function msort($a, $b)
{
	return strcmp($a["title"], $b["title"]);
}

function getModuleInfo($mod) {
	if (file_exists(LAB_PATH_MODULES.'/'.$mod."/config.xml"))
	{
		$xml = simplexml_load_file(LAB_PATH_MODULES.'/'.$mod."/config.xml");
		$info=get_object_vars($xml);
		$info['module']=$mod;
		$info['version']=empty($info['version'])?LabConfig::$cfg['version']['current'].'.'.LabConfig::$cfg['version']['revision']:$info['version'];

		return $info;
	}
	else return false;
}

function create_deafult_settings($module, $module_id)
{
	$xml = simplexml_load_file(LAB_PATH_MODULES.'/'.$module.'/config.xml');
	if (count($xml->configs->config))
	{
		foreach($xml->configs->config as $conf)
		{
			$default_conf=get_object_vars($conf);
			$default[$default_conf['name']]=$default_conf['default'];
		}
		if (count($default))
		{
			Tpl::i()->assign('config_tmp', $default);
			file_put_contents(LAB_PATH_DATA.'/configs/-1_'.$module_id.'.conf', Tpl::i()->fetch(LAB_PATH_TEMPLATE.'/default/admin_groups/conf.tpl'));
			chmod(LAB_PATH_DATA.'/configs/-1_'.$module_id.'.conf', 0666);
		}
	}
}
