<?php
	require_once("../utils.php");
	require_once("../config/config.php");

	/*
		Interface Modules
	*/
	require_once("modules/action.php");
	require_once("modules/dig_down_bar.php");
	require_once("modules/main_menu.php");
	require_once("modules/information.php");
	require_once("modules/internal.php");
	require_once("modules/external.php");

	$db_host = $config['databaseServer'];
	$db_login = $config['databaseUsername'];
	$db_passwd = $config['databasePassword'];
	$db_name = $config['databaseName'];

	$db = mysql_connect($db_host, $db_login, $db_passwd);
	mysql_select_db($db_name, $db);

	$data = getFormData();
?>

<HTML>
	<BODY BGCOLOR="000000" VLINK="white" ALINK="white" LINK="white">
		<FORM> 
			<?
				if ($data['view']) {
					printf("<INPUT TYPE=\"hidden\" NAME=\"view\" VALUE=\"%d\">", $data['view']);
				}
				if ($data['group']) {
					printf("<INPUT TYPE=\"hidden\" NAME=\"group\" VALUE=\"%d\">", $data['group']);
				}
			?>
			<TABLE BORDER="0" CELLSPACING="0" CELLPADDING="0" WIDTH="100%">
				<TR>
					<TD BGCOLOR="4444ff" ALIGN="left" VALIGN="top" COLSPAN="2" ROWSPAN="1">
						<FONT SIZE="+1"><B>
							<?php action($db, $data); ?>
						</B></FONT>
					</TD>
				</TR>
				<TR>
					<TD ALIGN="left" VALIGN="top" COLSPAN="1" ROWSPAN="1">
						<TABLE BORDER="0" CELLSPACING="0" CELLPADDING="0">
							<TR>
								<TD ALIGN="left" VALIGN="top" COLSPAN="1" ROWSPAN="1">
									<?php dig_down_bar($db, $data); ?>
								</TD>
							</TR>
						</TABLE>
					</TD>
					<TD ALIGN="left" VALIGN="top" COLSPAN="1" ROWSPAN="1" WIDTH="100%">
						<TABLE BORDER="0" CELLSPACING="0" CELLPADDING="0" WIDTH="100%">
							<TR>
								<TD ALIGN="right" VALIGN="top" COLSPAN="2" ROWSPAN="1">
									<?php main_menu($db, $data); ?>
								</TD>
							</TR>
							<TR>
								<TD ALIGN="left" VALIGN="top" COLSPAN="1" ROWSPAN="1">
									<TABLE BORDER="0" CELLSPACING="0" CELLPADDING="0" WIDTH="100%">
										<TR>
											<TD ALIGN="left" VALIGN="top" COLSPAN="1" ROWSPAN="1" WIDTH="100%">
												<?php information($db, $data); ?>
											</TD>
											<TD ALIGN="left" VALIGN="top" COLSPAN="1" ROWSPAN="2" WIDTH="100%" NOWRAP>
												<?php external($db, $data); ?>
											</TD>
										</TR>
										<TR>
											<TD ALIGN="center" VALIGN="middle" COLSPAN="1" ROWSPAN="1">
												<?php internal($db, $data); ?>
											</TD>
										</TR>
									</TABLE>
								</TD>
							</TR>
						</TABLE>
					</TD>
				</TR>
			</TABLE>
		</FORM>
	</BODY>
</HTML>

<?php
	function commit_group_create($db, $data) {
		if ($data['view'] == false) {
			printf("Error: View not supplied.\n");

			return false;
		} else if ($data['descriptor'] == false) {
			printf("Error: Group descriptor not supplied.\n");

			return false;
		}
		$groups = array();
		$elements = array();

		reset($data);
		while (list($key, $value) = each($data)) {
			if (ereg('element_([0-9]+)', $key, $v)) {
				$elements[] = $v[1];
			} else if (ereg('group_([0-9]+)', $key, $v)) {
				$groups[] = $v[1];
			}
		}
		if (count($elements) + count($groups) == 0) {
			printf("Error: Can not create empty group.\n");

			return false;
		}
		if ($data['parent'] == false) {
			$sql = sprintf("INSERT INTO display_group VALUES(NULL, %d, NULL, '%s')", $data['view'], $data['descriptor']);
		} else {
			$sql = sprintf("INSERT INTO display_group VALUES(NULL, %d, %d, '%s')", $data['view'], $data['parent'], $data['descriptor']);
		}
		if (mysql_query($sql, $db) == false) {
			printf("Error: Could not insert new group into database.\n");

			return false;
		} else {
			$sql = sprintf("SELECT LAST_INSERT_ID() AS id");
			$result = mysql_query($sql, $db);
			if ($result == false) {
				printf("Error: Could not get ID of new group.\n");

				return false;
			} else {
				$row = mysql_fetch_array($result);

				$new_group = $row['id'];
			}
		}
		reset($groups);
		while (list(, $value) = each($groups)) {
			$sql = sprintf("UPDATE display_group SET parent = %d WHERE id = %d", $new_group, $value);
			if (mysql_query($sql, $db) == false) {
				printf("Error: Could not alter parent of group (%d) in database.\n", $value);
			}
		}
		reset($elements);
		while (list(, $value) = each($elements)) {
			if ($data['parent'] == false) {
				$sql = sprintf("INSERT INTO display_group_member VALUES(NULL, %d, %d)", $new_group, $value);
			} else {
				$sql = sprintf("SELECT display_group_member.id FROM display_group_member, display_group WHERE display_group_member.display_group = display_group.id AND display_group_member.element = %d AND display_group.display_view = %d", $value, $data['view']);
				$result = mysql_query($sql, $db);
				if ($row = mysql_fetch_array($result)) {
					$sql = sprintf("UPDATE display_group_member SET display_group = %d WHERE id = %d", $new_group, $id);
				} else {
					printf("Error: Parent exists for element, but it is not in a group.\n");
				}
			}
			if (mysql_query($sql, $db) == false) {
				printf("Error: Could not add element (%d) to group (%d) in database.\n", $value, $new_group);
			}
		}
	}
?>

<?php
	function display_internal_connections($db, $data) {
		printf("This is a map.\n");

	}
?>
