<?php
if ( !eregi("admin.php", $PHP_SELF) ) {
	die ("Access Denied"); 
}
include_once(XOOPS_ROOT_PATH."/modules/system/admin/modulesadmin/modulesadmin.php");
$op = "list";
if ( isset($HTTP_POST_VARS) ) {
	foreach ( $HTTP_POST_VARS as $k => $v ) {
		$$k = $v;
  	}
}

if ( $op == "list" ) {
	module_list();
}

if ( $op == "confirm" ) {
	xoops_cp_header();
	OpenTable();
	$error = array();
	if ( !is_writable(XOOPS_ROOT_PATH."/modules/system/cache/mainmenu.php") ) {
		// attempt to chmod 666
		if ( !chmod(XOOPS_ROOT_PATH."/modules/system/cache/mainmenu.php", 0666) ) {
		
			$error[] = sprintf(_MUSTWABLE, "<b>".XOOPS_ROOT_PATH."/modules/system/cache/mainmenu.php</b>");
		}
	}
	if ( !is_writable(XOOPS_ROOT_PATH."/modules/system/cache/adminmenu.php") ) {
		// attempt to chmod 666
		if ( !chmod(XOOPS_ROOT_PATH."/modules/system/cache/adminmenu.php", 0666) ) {
		
			$error[] = sprintf(_MUSTWABLE, "<b>".XOOPS_ROOT_PATH."/modules/system/cache/adminmenu.php</b>");
		}
	}
	if ( count($error) > 0 ) {
		foreach ( $error as $er ) {
			echo $er."<br />";
		}
		echo "<p><a href='admin.php?fct=modulesadmin'>"._MD_AM_BTOMADMIN."</a></p>";
		CloseTable();
		xoops_cp_footer();
		exit();
	}
	echo "<h4 style='text-align:left;'>"._MD_AM_PCMFM."</h4>
	<form action='admin.php' method='post'>
	<input type='hidden' name='fct' value='modulesadmin' />
	<input type='hidden' name='op' value='submit' />
	<table border='0' cellpadding='0' cellspacing='0' valign='top' width='50%'><tr><td class='bg2'>
	<table width='100%' border='0' cellpadding='4' cellspacing='1'>
	<tr valign='middle' class='bg3' align='center'><td><b>"._MD_AM_MODULE."</b></td><td><b>"._MD_AM_ACTION."</b></td><td><b>"._MD_AM_ORDER."</b></td></tr>";
	$size = sizeof($module);
	for ( $i = 0 ; $i < $size; $i++ ) {
		echo "<tr class='bg1'><td>".$name[$i]."</td><td align='center'>";
		if ( $newstatus[$i] != "nochange" ) {
			switch ($newstatus[$i]) {
				case "deactivate":
					$statustext = _MD_AM_DEACTIVATE;
					break;
				case "activate":
					$statustext = _MD_AM_ACTIVATE;
					break;
				case "install":
					$statustext = _MD_AM_INSTALL;
					break;
				case "uninstall":
					$statustext = _MD_AM_UNINSTALL;
					break;
				case "update":
					$statustext = _MD_AM_UPDATE;
					break;
			}
			echo "<span style='color:#ff0000;font-weight:bold;'>".$statustext."</span>";
		} else {
			echo _MD_AM_NOCHANGE;
		}
		echo "</td><td align='center'>";
		if ( $oldweight[$i] != $weight[$i] ) {
			echo "<span style='color:#ff0000;font-weight:bold;'>".$weight[$i]."</span>";
		} else {
			echo $weight[$i];
		}
		echo "
		<input type='hidden' name='module[]' value='".$module[$i]."' />
		<input type='hidden' name='oldstatus[]' value='".$oldstatus[$i]."' />
		<input type='hidden' name='newstatus[]' value='".$newstatus[$i]."' />
		<input type='hidden' name='oldweight[]' value='".intval($oldweight[$i])."' />
		<input type='hidden' name='weight[]' value='".intval($weight[$i])."' />
		</td></tr>";
	}
	echo "
	<tr class='bg3' align='center'><td colspan='3'><input type='submit' value='"._MD_AM_SUBMIT."' />&nbsp;<input type='button' value='"._MD_AM_CANCEL."' onclick='location=\"admin.php?fct=modulesadmin\"' /></td></tr>
	</table>
	</td></tr></table>
	</form>";
	CloseTable();
	xoops_cp_footer();
}
if ( $op == "submit" ) {
	$size = sizeof($newstatus);
	$ret = array();
	$write = false;
	for ( $i = 0 ; $i < $size; $i++ ) {
		$reorder = true;
		switch ($newstatus[$i]) {
			case "install":
				$ret[] = module_install($module[$i]);
				$write = true;
				break;
			case "activate":
				$ret[] = module_activate($module[$i]);
				$write = true;
				break;
			case "deactivate":
				$ret[] = module_deactivate($module[$i]);
				$write = true;
				break;
			case "update":
				$ret[] = module_update($module[$i]);
				$write = true;
				break;
			case "uninstall":
				$ret[] = module_uninstall($module[$i]);
				$reorder = false;
				$write = true;
				break;
			default:
				break;
		}
		if ( $oldstatus[$i] == 0 && $newstatus[$i] != "install" ) {
			$reorder = false;
		}
		if ( $reorder == true && $oldweight[$i] != $weight[$i] ) {
			$ret[] = module_order($module[$i], $weight[$i]);
			$write = true;
		}
	}
	if ( $write ) {
		$contents = get_module_main_menu();
		if (!write_module_main_menu($contents)) {
			$ret[] = "<p>"._MD_AM_FAILWRITE."</p>";
		}
		$contents = get_module_admin_menu();
		if (!write_module_admin_menu($contents)) {
			$ret[] = "<p>"._MD_AM_FAILWRITE."</p>";
		}
	}
	xoops_cp_header();
	OpenTable();
	if ( count($ret) > 0 ) {
		foreach ($ret as $msg) {
			echo $msg;
		}
	}
	echo "<p><a href='admin.php?fct=modulesadmin'>"._MD_AM_BTOMADMIN."</a></p>";
	CloseTable();
	xoops_cp_footer();
}
?>