<?php
	function dig_down_bar($db, $params) {
		switch ($params['function']) {
			case 'view':
			case 'view_create':
			case 'view_edit':
			case 'view_delete':
			case 'abort_view_create':
			case 'commit_view_create':
			case 'abort_view_edit':
			case 'commit_view_edit':
			case 'abort_view_delete':
			case 'commit_view_delete':
				/*
					Display the names of all configured views.
				*/
				dig_down_bar__view($db);
				break;

			case 'abort_group_create':
			case 'commit_group_create':
			case 'group_create':
			case 'view_group':
				/*
					Display the network elements in the currently selected group.
				*/
				dig_down_bar__view_group($db, $params['view'], $params['group']);
				break;

			case 'view_element':
				/*
					Display the interfaces of the selected network element.
				*/
				dig_down_bar__view_element($db, $params['view'], $params['id']);
				break;

			case 'view_interface':
				/*
					Display the IP addresses of the selected inteface.
				*/
				dig_down_bar__view_interface($db, $params['view'], $params['id']);
				break;

			default:
				/*
					Display the SellaNMS contributors.
				*/
				dig_down_bar__default();
		}
	}

   function dig_down_bar__view($db) {
      $sql = sprintf("SELECT id, descriptor FROM display_view ORDER BY descriptor");
      $result = mysql_query($sql, $db);

      printf("<TABLE WIDTH=\"100%%\">\n");
      while ($row = mysql_fetch_array($result)) {
         printf("\t<TR BGCOLOR=\"00dd00\">\n");
         printf("\t\t<TD><INPUT TYPE=\"checkbox\" NAME=\"view_%d\"></TD>\n", $row['id']);
         printf("\t\t<TD NOWRAP WIDTH=\"100%%\"><A HREF=\"admin.php?view=%d&function=view_group\">%s</A>\n", $row['id'], $row['descriptor']);
         printf("\t</TR>\n");
      }
      printf("</TABLE>\n");
   }

	function dig_down_bar__view_element($db, $view, $element) {
		$precision = 20; // TODO: make this a parameter defined in the view table.

		$interval = 180;
		$loss = 99;
		$sql = sprintf("SELECT (100 * (1 - (SUM(monitor_icmp.rx) / SUM(monitor_icmp.tx)))) AS loss, interface.id AS iface_id FROM monitor_icmp, ip_data, ip, interface WHERE timestamp > (NOW() - INTERVAL %d MINUTE) AND ip_data.value = monitor_icmp.ip AND ip_data.ip = ip.id AND ip.interface = interface.id GROUP BY ip HAVING loss > %d ORDER BY loss", $interval, $loss);
		
		$red_interfaces = array();

		$result = mysql_query($sql, $db);
		while ($row = mysql_fetch_array($result)) {
			$red_interfaces[$row['iface_id']] = $row['iface_id'];
		}

		if ($view == false) {
			printf("\nError: View not supplied.\n", $view);

			return false;
		} else if ($element == false) {
			printf("\nError: Element not supplied.\n", $view);

			return false;
		} else {
			$sql_descriptors = sprintf("SELECT interface.id, LEFT(interface_data.value, %d) AS value FROM display_view, element, interface, interface_data WHERE display_view.id = %d AND element.id = %d AND interface.element = element.id AND interface_data.interface = interface.id AND interface_data.variable = display_view.element_interface_descriptor_oid ORDER BY interface_data.value", $precision, $view, $element);
		}
		$result = mysql_query($sql_descriptors, $db);

		printf("\n<TABLE WIDTH=\"100%%\" COLSPACING=\"0\" CELLPADDING\"0\" BORDER=\"0\">\n");

		while ($interface = mysql_fetch_array($result)) {
			if (in_array($interface['id'], $red_interfaces)) {
				$state_color = "dd0000";
			} else {
				$state_color = "00dd00";
			}

			printf("\t<TR BGCOLOR=\"%s\">\n", $state_color);
			printf("\t\t<TD><INPUT TYPE=\"checkbox\" NAME=\"interface_%d\"></TD>\n", $interface['id']);
			printf("\t\t<TD NOWRAP><A HREF=admin.php?view=%d&function=view_interface&id=%d>%s</A></TD>\n", $view, $interface['id'], $interface['value']);
			printf("\t</TR>\n");
		}
		printf("</TABLE>\n");
	}

	function dig_down_bar__view_group($db, $view, $group) {
		$precision = 20; // TODO: make this a parameter defined in the view table.

		$sql = sprintf("SELECT element_descriptor_oid FROM display_view WHERE id = %d", $view);
		$result = mysql_query($sql);
		$row = mysql_fetch_array($result);
		$element_oid = $row['element_descriptor_oid'];

		$interval = 180;
		$loss = 99;
		$sql = sprintf("SELECT (100 * (1 - (SUM(monitor_icmp.rx) / SUM(monitor_icmp.tx)))) AS loss, element.id, display_group_member.display_group FROM monitor_icmp, ip_data, ip, interface, element LEFT JOIN display_group_member ON (display_group_member.element = element.id) WHERE timestamp > (NOW() - INTERVAL %d MINUTE) AND ip_data.value = monitor_icmp.ip AND ip_data.ip = ip.id AND ip.interface = interface.id AND interface.element = element.id GROUP BY ip HAVING loss > %d ORDER BY loss", $interval, $loss); 

		$red_groups = array();
		$red_elements = array();

		$result = mysql_query($sql, $db);
		while ($row = mysql_fetch_array($result)) {
			if ($row['display_group']) {
				$red_groups[$row['display_group']] = $row['display_group'];
			}
			$red_elements[$row['id']] = $row['id'];
		}

		if ($view == false) {
			printf("Error: View not supplied.\n", $view);

			return false;
		} else if ($group == false) {
			$sql_descriptors = sprintf("SELECT LEFT(element_data.value, %d) AS value, element_data.id, element_data.element FROM element_data LEFT JOIN display_group_member ON ( element_data.element = display_group_member.element ) LEFT JOIN display_group ON ( display_group_member.display_group = display_group.id AND display_group.display_view = %d ) LEFT JOIN display_group_member AS excludes ON ( display_group_member.element = excludes.element AND display_group_member.display_group != excludes.display_group ) LEFT JOIN display_group AS exclude_groups ON ( excludes.display_group = exclude_groups.id ) WHERE element_data.variable = '%s' AND display_group.id IS NULL AND ( excludes.id IS NULL OR exclude_groups.display_view != %d ) GROUP BY element_data.element ORDER BY element_data.value", $precision, $view, $element_variable, $view);

			$sql_groups = sprintf("SELECT display_group.id, display_group.descriptor, display_group.parent FROM display_group WHERE display_group.display_view = %d AND display_group.parent IS NULL", $view);
		} else {
			$sql_descriptors = sprintf("SELECT element_data.id, element_data.element, LEFT(element_data.value, %d) AS value, element_data.id FROM element_data, display_group_member WHERE display_group_member.display_group = %d AND display_group_member.element = element_data.element AND element_data.variable = '%s' ORDER BY element_data.value", $precision, $group, $element_oid);

			$sql_groups = sprintf("SELECT display_group.id, display_group.descriptor, display_group.parent FROM display_group WHERE display_group.display_view = %d AND display_group.parent = %d", $view, $group);
		}
		$result_a = mysql_query($sql_descriptors, $db);
		$result_b = mysql_query($sql_groups, $db);

		$element = mysql_fetch_array($result_a);
		$groups = mysql_fetch_array($result_b);

		printf("\n<TABLE WIDTH=\"100%%\" COLSPACING=\"0\" CELLPADDING\"0\" BORDER=\"0\">\n");

		while ($groups) {
			if (in_array($groups['id'], $red_groups)) {
				$state_color = "dd0000";
			} else {
				$state_color = "00dd00";

				$g = $groups['id'];
				while ($g) {
					$sql = sprintf("SELECT id FROM display_group WHERE parent = %d", $g);
					$result = mysql_query($sql);
					$row = mysql_fetch_array($result);

					if (in_array($row['id'], $red_groups)) {
						$state_color = "dd0000";						

						$g = false;
					} else {
						$g = $row['id'];
					}
				}
			}

			printf("\t<TR BGCOLOR=\"%s\" NOWRAP>\n", $state_color);
			printf("\t\t<TD><INPUT NAME=\"group_%d\" TYPE=\"checkbox\"></TD>\n", $groups['id']);
			printf("\t\t<TD><A HREF=\"admin.php?view=%d&function=view_group&group=%d\"><B>%s</B></A></TD>\n", $view, $groups['id'], $groups['descriptor']);
			printf("\t</TR>\n");

			$groups = mysql_fetch_array($result_b);
		}

		while ($element) {
			if (in_array($element['element'], $red_elements)) {
				$state_color = "dd0000";
			} else {
				$state_color = "00dd00";
			}

			printf("\t<TR BGCOLOR=\"%s\" NOWRAP>\n", $state_color);
			printf("\t\t<TD><INPUT NAME=\"element_%d\" TYPE=\"checkbox\"></TD>\n", $element['element']);
			printf("\t\t<TD><A HREF=\"admin.php?view=%d&function=view_element&id=%d\">%s</A></TD>\n", $view, $element['element'], $element['value']);
			printf("\t</TR>\n");

			$element = mysql_fetch_array($result_a);
		}

		printf("</TABLE>\n");
	}

	function dig_down_bar__view_interface($db, $view, $interface) {
		// TODO: this function.
	}

	function dig_down_bar__default() {
		printf("\n<TABLE WIDTH=\"100%%\">\n");
		printf("\t<TR WIDTH=\"100%%\" BGCOLOR=\"00aa00\">\n");
		printf("\t\t<TD NOWRAP><A HREF=\"mailto:bill@digitalgenesis.com\">William T. Sella</A></A>\n");
		printf("\t</TR>\n");
		printf("\t<TR WIDTH=\"100%%\" BGCOLOR=\"00aa00\">\n");
		printf("\t\t<TD NOWRAP><A HREF=\"mailto:sella@digitalgenesis.com\">James M. Sella</A></A>\n");
		printf("\t</TR>\n");
		printf("</TABLE>\n");
	}
?>
