<?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
*/
	$mod_template="delete.tpl";
	
	$module=Db::i()->selectRow("SELECT name, module FROM ?_modules WHERE module_id=?", $_GET['var1']);
	//проверяем есть ли зависимые модули
	$depends=Db::i()->select("SELECT module_id, name, module FROM ?_modules WHERE depends=?", $module['module']);
	
	addBreadcrumb($module['name'], _url($module['module'], 'info'));
	addBreadcrumb('Удаление');

	
	$hasCopy=(Db::i()->selectCell("SELECT COUNT(*) FROM ?_modules WHERE module=?", $module['module'])>1)?true:false;
		
	if (count($depends) && !$hasCopy)
	{
		if ($_SERVER['REQUEST_METHOD']=='POST')
		{
			User::checkToken();
			$modules=array($_GET['var1']);
			foreach($depends as $depend)
			{
				if (file_exists(LAB_PATH_MODULES.'/'.$depend["module"]."/uninstall.sql")) Db::parse_mysql_dump(LAB_PATH_MODULES.'/'.$depend["module"]."/uninstall.sql", false);
				$modules[]=$depend['module_id'];
			}
			if (file_exists(LAB_PATH_MODULES.'/'.$module["module"]."/uninstall.sql")) Db::parse_mysql_dump(LAB_PATH_MODULES.'/'.$module["module"]."/uninstall.sql", false);
			Db::i()->query("DELETE FROM ?_modules WHERE module_id IN (?a)", $modules);
			Db::i()->query("DELETE FROM ?_includes WHERE module_id IN (?a)", $modules);
			LabCMS::i()->reset_cache('all');
			refresh();
		}
		Tpl::i()->assign('module', $module);
		Tpl::i()->assign('depends', $depends);
	}
	elseif ($_SERVER['REQUEST_METHOD']=='POST')
	{
		User::checkToken();
		if (!$hasCopy && file_exists(LAB_PATH_MODULES.'/'.$module["module"]."/uninstall.sql")) Db::parse_mysql_dump(LAB_PATH_MODULES.'/'.$module["module"]."/uninstall.sql", false);
		Db::i()->query("DELETE FROM ?_modules WHERE module_id=?", $_GET['var1']);
		Db::i()->query("DELETE FROM ?_includes WHERE module_id=?", $_GET['var1']);
		LabCMS::i()->reset_cache('all');
		refresh();
	}
