<?php
	function external($db, $params) {
		switch ($params['function']) {
			case 'view':
				external__view($db);
				break;

			case 'view_group':
				external__view_group($db, $params['view'], $params['group']);
				break;

			case 'view_element':
				external__view_element($db, $params['view'], $params['id']);
				break;

			case 'view_interface':
//				external__view_interface($db, $params['view'], $params['id']);
//				break;

			case 'view_create':
//				external__view_create($db);
//				break;

			case 'view_edit':
//				external__view_edit($db, $params);
//				break;

			case 'view_delete':
//				external__view_delete($db, $params);
//				break;

			case 'group_create':
//				external__group_create($db, $params);
//				break;

			case 'group_edit':

			default:
				external__default();
		}
	}

	function get_match_group($group, $db) {
		$ret_val = sprintf("display_group_member.display_group = %d", $group);

		$sql = sprintf("SELECT id FROM display_group WHERE parent = %d", $group);
		$result = mysql_query($sql, $db);
		while ($row = mysql_fetch_array($result)) {
			$ret_val = sprintf("%s OR %s", $ret_val, get_match_group($row['id'], $db));
		}
		return $ret_val;
	}

	function get_not_match_group($group, $db) {
		$ret_val = sprintf("internal.display_group != %d", $group);

		$sql = sprintf("SELECT id FROM display_group WHERE parent = %d", $group);
		$result = mysql_query($sql, $db);
		while ($row = mysql_fetch_array($result)) {
			$ret_val = sprintf("%s AND %s", $ret_val, get_not_match_group($row['id'], $db));
		}
		return $ret_val;
	}

	function external__view_group($db, $view, $group) {
		$sql = sprintf("SELECT element_descriptor_oid FROM display_view WHERE id = %d", $view);
		$result = mysql_query($sql, $db);
		$row = mysql_fetch_array($result);
		$oid = $row['element_descriptor_oid'];

		printf("<TABLE>\n");
		if ($group) {
			$sql = sprintf("SELECT destination_data.element, destination_data.value FROM element_data, element_data AS destination_data, connection, element, element AS destination, interface, interface AS destination_interface, ip, ip AS destination_interface_ip LEFT JOIN display_group_member ON ( display_group_member.element = element.id ) LEFT JOIN display_group_member AS internal ON ( internal.element = destination.id ) WHERE connection.layer = 3 AND element.id = interface.element AND interface.id = ip.interface AND ip.id = connection.a_loc AND destination.id = destination_interface.element AND destination_interface.id = destination_interface_ip.interface AND destination_interface_ip.id = connection.z_loc AND (%s) AND ( internal.display_group IS NULL OR (%s) ) AND element_data.element = element.id AND destination_data.element = destination.id AND element_data.variable = '%s' AND destination_data.variable = '%s' GROUP BY destination_data.value ORDER BY element_data.value, destination_data.value", get_match_group($group, $db), get_not_match_group($group, $db), $variable, $oid);
			$result = mysql_query($sql, $db);

			while ($row = mysql_fetch_array($result)) {
				printf("	<TR BGCOLOR=\"%s\">\n", "00bb00");
				printf("		<TD><A HREF=\"admin.php?view=%d&function=view_element&id=%d\">%s</A></TD>\n", $view, $row['element'], $row['value']);
				printf("	</TR>\n");
			}
		}
		printf("</TABLE>\n");
	}

	function external__view_element($db, $view, $id) {
		$sql = sprintf("SELECT element_descriptor_variable, element_interface_descriptor_oid FROM display_view WHERE id = %d", $view);
		$result = mysql_query($sql, $db);
		$row = mysql_fetch_array($result);
		$e_oid = $row['element_descriptor_oid'];
		$i_oid = $row['element_interface_descriptor_oid'];

		$sql = sprintf("SELECT destination_data.element AS d_id, destination_data.value AS d_name, destination_interface_data.interface AS di_id, destination_interface_data.value AS di_name FROM connection, element_data, element_data AS destination_data, interface, interface AS destination_interface, interface_data, interface_data AS destination_interface_data, ip, ip AS destination_interface_ip WHERE connection.layer = 3 AND connection.a_loc = ip.id AND ip.interface = interface.id AND interface.element = element_data.element AND interface_data.interface = interface.id AND connection.z_loc = destination_interface_ip.id AND destination_interface_ip.interface = destination_interface.id AND destination_interface.element = destination_data.element AND destination_interface_data.interface = destination_interface.id AND element_data.variable = '%s' AND interface_data.variable = '%s' AND destination_data.variable = element_data.variable AND destination_interface_data.variable = interface_data.variable AND element_data.element = %d ORDER BY element_data.value", $e_variable, $i_variable, $id);
		$result = mysql_query($sql, $db);
	
		printf("<TABLE>\n");
		while ($row = mysql_fetch_array($result)) {
			printf("	<TR BGCOLOR=\"%s\">\n", "00bb00");
			printf("		<TD><A HREF=\"admin.php?view=%d&function=view_element&id=%d\">%s</A></TD>\n", $view, $row['d_id'], $row['d_name']);
			printf("		<TD><A HREF=\"admin.php?view=%d&function=view_interface&id=%d\">%s</A></TD>\n", $view, $row['di_id'], $row['di_name']);
			printf("	</TR>\n");
		}
		printf("</TABLE>\n");
	}

	function external__view($db) {
		printf("Open a View");
	}

	function external__default() {
		printf("All By Myself");
	}
