<?php
// $Id: main.php,v 1.1 2004/07/11 10:28:54 Farsus Exp $
// -------------------------------------------------------------------------//
//			       RUNCMS                                                   //
//                                                                          //
//	 reliable - Unique - Nocost &  Simplicity & ease off use                //
//                       < http://www.runcms.org >                          //
// -------------------------------------------------------------------------//
// Original Author: Kazumi Ono
// Author Website : http://www.mywebaddons.com/ , http://www.myweb.ne.jp
// License Type   : GPL: See /manual/LICENSES/GPL.txt
// ------------------------------------------------------------------------- //

if ( !preg_match("/admin\.php/i", _PHP_SELF) ) {
	exit();
	}

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 ($HTTP_GET_VARS['op'] == 'edit') {
	xoops_cp_header();
	OpenTable();
	module_edit($HTTP_GET_VARS['mid']);
	CloseTable();
	xoops_cp_footer();
	exit();
}

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

if ( $op == "confirm" ) {
	xoops_cp_header();
	OpenTable();
	$error = array();
	if ( @!is_writable(XOOPS_ROOT_PATH."/modules/system/cache/adminmenu.php") ) {
		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 = count($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 = _DEACTIVATE;
					break;
				case "activate":
					$statustext = _ACTIVATE;
					break;
				case "install":
					$statustext = _INSTALL;
					break;
				case "uninstall":
					$statustext = _UNINSTALL;
					break;
				case "update":
					$statustext = _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])."' />";

		if ($HTTP_POST_VARS['edit'] == 1) {
			echo "
			<input type='hidden' name='edit' value='1' />
			<input type='hidden' name='modname' value='".$myts->makeTboxData4Edit($modname)."' />";
			if ( !empty($HTTP_POST_VARS['admin_access']) ) {
				foreach($HTTP_POST_VARS['admin_access'] as $key => $admin) {
					echo "<input type='hidden' name='admin_access[]' value='".$admin."' />";
				}
			}
			if ( !empty($HTTP_POST_VARS['read_access']) ) {
				foreach($HTTP_POST_VARS['read_access'] as $key => $user) {
					echo "<input type='hidden' name='read_access[]' value='".$user."' />";
				}
			}
		}
		echo "</td></tr>";
	}
	echo "
	<tr class='bg3' align='center'><td colspan='3'><input type='submit' class='button' value='"._SUBMIT."' />&nbsp;<input type='button' class='button' value='"._CANCEL."' onclick='location=\"admin.php?fct=modulesadmin\"' /></td></tr>
	</table>
	</td></tr></table>
	</form>";
	CloseTable();
	xoops_cp_footer();
}

if ( $op == "submit" ) {
	$size = count($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_admin_menu();
		if (!write_module_admin_menu($contents)) {
			$ret[] = "<p>"._MD_AM_FAILWRITE."</p>";
		}
	}


	if ($HTTP_POST_VARS['edit'] == 1) {
		$mymodule =& XoopsModule::getByDirname($module[0]);
		if ($mymodule) {
			$mid          = $mymodule->mid();
			$modname      = $myts->makeTboxData4Save($HTTP_POST_VARS['modname']);
			$read_access  = $HTTP_POST_VARS['read_access'];
			$admin_access = $HTTP_POST_VARS['admin_access'];

			if ( !empty($modname) ) {
				$sql    = 'UPDATE '.$db->prefix('modules').' SET name="'.$modname.'" WHERE mid='.$mid.'';
				$result = $db->query($sql);
				if (!$result) {
					$ret[]  = 'Could not update modules name.';
				}
				// echo $sql.'<br />';
			}

			if ( !empty($read_access) && @is_array($read_access) ) {
				$sql    = 'DELETE FROM '.$db->prefix('groups_modules_link').' WHERE (mid='.$mid.' AND type="R" AND groupid != 1)';
				$result = $db->query($sql);
				// echo $sql.'<br />';
				if (!$result) {
					$ret[]  = 'Could not delete old read access rights.';
					} else {
						foreach($read_access as $key => $read) {
							if ($read != 1) {
								$sql    = 'INSERT INTO '.$db->prefix('groups_modules_link').' SET mid='.$mid.', groupid='.$read.', type="R"';
								$result = $db->query($sql);
								// echo $sql.'<br />';
								if (!$result) {
									$ret[]  = 'Could not update read access rights.';
								}
							}
						}
					}
			}

			if ( !empty($admin_access) && @is_array($admin_access) ) {
				$sql    = 'DELETE FROM '.$db->prefix('groups_modules_link').' WHERE (mid='.$mid.' AND type="A" AND groupid != 1)';
				$result = $db->query($sql);
				// echo $sql.'<br />';
				if (!$result) {
					$ret[]  = 'Could not delete old admin access rights.';
					} else {
						foreach($admin_access as $key => $admin) {
							if ($admin != 1) {
								$sql    = 'INSERT INTO '.$db->prefix('groups_modules_link').' SET mid='.$mid.', groupid='.$admin.', type="A"';
								$result = $db->query($sql);
								// echo $sql.'<br />';
								if (!$result) {
									$ret[]  = 'Could not update admin access rights.';
								}
							}
						}
					}
			}
			} else {
				$ret[] = 'No module selected';
			}

		if ( count($ret) > 0 ) {
			foreach ($ret as $msg) {
				$message .= $msg;
			}
			} else {
				$message = _UPDATED;
			}
		redirect_header("admin.php?fct=modulesadmin&op=edit&mid=$mid", 3, $message);
		exit();
	}

	xoops_cp_header();
	OpenTable();
	if ( count($ret) > 0 ) {
		foreach ($ret as $msg) {
			echo $msg;
		}
	}

	echo "<li><a href='admin.php?fct=modulesadmin'>"._MD_AM_BTOMADMIN."</a></li>";
	echo "<br /><br />";
	echo "<li><a href='admin.php?fct=groups'>"._MD_AM_EDITACCESS."</a></li>";

	CloseTable();
	xoops_cp_footer();
}
?>
