<?php
	function information($db, $params) {
		switch ($params['function']) {
			case 'view':
				/*
					Display a menu to allow creation or deletion of views.
				*/
				information__view($db);
				break;

			case 'view_group':
				/*
					Display a menu to allow grouping or ungrouping of network elements. 
				*/
				information__view_group($db, $params['view'], $params['id']);
				break;

			case 'view_element':
				/*
					Display a menu to allow hiding or unhiding of interfaces.
				*/
				information__view_element($db, $params['view'], $params['id']);
				break;

			case 'view_interface':
				/*
					Display a menu to allow hiding or unhiding of IP addresses.
				*/
				information__view_interface($db, $params['view'], $params['id']);
				break;

			case 'view_create':
				/*
					Display a form to create a new view.
				*/
				information__view_create($db);
				break;

			case 'view_edit':
				/*
					Display a form to edit a view or views.
				*/
				information__view_edit($db, $params);
				break;

			case 'view_delete':
				/*
					Display information about a view or views to delete.
				*/
				information__view_delete($db, $params);
				break;

			case 'group_create':
				/*
					Display information about a group to create.
				*/
				information__group_create($db, $params);
				break;

			case 'group_edit':

			default:
				/*
					Display the contributors title bar. 
				*/
				information__default();
		}
	}

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

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

		printf("\t<TR>\n");
		printf("\t\t<TD COLSPAN=\"2\">Group Descriptor</TD>\n");
		printf("\t\t<TD><INPUT TYPE=\"text\" NAME=\"group_descriptor\"></TD>\n");
		printf("\t</TR>\n");

		printf("\t<TR>\n");
		printf("\t\t<TD COLSPAN=\"3\">Member Elements</TD>\n");
		printf("\t</TR>\n");

		reset($params);
		while(list($key, $value) = each($params)) {
			if (ereg('group_([0-9]+)', $key, $v)) {
				$sql = sprintf("SELECT descriptor FROM display_group WHERE id = %d", $v[1]);
				$result = mysql_query($sql);
				$row = mysql_fetch_array($result);

				printf("\t<TR>\n");
				printf("\t<TD><INPUT CHECKED TYPE=\"checkbox\" NAME=\"group_group_%d\"></TD>\n", $v['1']);
				printf("\t<TD COLSPAN=\"2\"><B>%s</B></TD>\n", $row['descriptor']);
				printf("\t</TR>\n");
			}
		}

		reset($params);
		while(list($key, $value) = each($params)) {
			if (ereg('element_([0-9]+)', $key, $v)) {
				$sql = sprintf("SELECT value FROM element_data WHERE element = %d AND variable = '%s'", $v[1], $descriptor_oid);
				$result = mysql_query($sql, $db);
				$row = mysql_fetch_array($result);

				printf("\t<TR>\n");
				printf("\t<TD><INPUT CHECKED TYPE=\"checkbox\" NAME=\"element_group_%d\"></TD>\n", $v['1']);
				printf("\t<TD COLSPAN=\"2\">%s</TD>\n", $row['value']);
				printf("\t</TR>\n");
			}
		}

		printf("\n</TABLE>\n");
	}
	
	function information__view_edit($db, $params) {
		$views = array();
		$eoids = array();
		$ioids = array();
	
		$sql = sprintf("SELECT DISTINCT variable, value FROM element_data GROUP BY oid");
		$element_oids = mysql_query($sql, $db);
		while ($row = mysql_fetch_array($element_oids)) {
			$eoids[$row['variable']] = $row['value'];
		}

		$sql = sprintf("SELECT DISTINCT variable, value FROM interface_data GROUP BY oid");
		$interface_oids = mysql_query($sql, $db);
		while ($row = mysql_fetch_array($interface_oids)) {
			$ioids[$row['variable']] = $row['value'];
		}

		while (list($view, $value) = each($params)) {
			if (ereg('view_([0-9]+)', $view, $v)) {
				$sql = sprintf("SELECT descriptor, element_descriptor_variable, element_interface_descriptor_oid FROM display_view WHERE id = %d", $v[1]);

				$result = mysql_query($sql, $db);
				while ($row = mysql_fetch_array($result)) {
					printf("\n<INPUT TYPE=\"hidden\" NAME=\"id\" VALUE=\"%d\">\n", $v[1]);
					printf("\n<TABLE>\n");
					printf("\t<TR>\n");
					printf("\t\t<TD>%s</TD>\n", "<B>View Descriptor</B>");
					printf("\t\t<TD><INPUT TYPE=\"text\" NAME=\"descriptor\" VALUE=\"%s\"></TD>", $row['descriptor']);
					printf("\t</TR>\n");
					printf("\t<TR>\n");
					printf("\t\t<TD>%s</TD>\n", "<B>Element Descriptor OID</B>");
					printf("\t\t<TD><SELECT NAME=\"element_oid\">");
	
					reset($eoids);
					while (list($variable, $value) = each($eoids)) {	
						printf("\t\t<OPTION %s VALUE=\"%s\">%s (ie: %s)</OPTION>\n", strcmp($row['element_descriptor_oid'], $oid) ? "" : "SELECTED", $variable, $variable, $value);
					}
			
					printf("\t\t</SELECT></TD>");
					printf("\t</TR>\n");
					printf("\t<TR>\n");
					printf("\t\t<TD>%s</TD>\n", "<B>Interface Descriptor OID</B>");
					printf("\t\t<TD><SELECT NAME=\"interface_oid\">");
		
					reset($ioids);
					while (list($variable, $value) = each($ioids)) {	
						printf("\t\t<OPTION %s VALUE=\"%s\">%s (ie: %s)</OPTION>\n", strcmp($row['element_interface_descriptor_oid'], $oid) ? "" : "SELECTED", $variable, $variable, $value);
					}
			
					printf("\t\t</SELECT></TD>");
					printf("\t</TR>\n");
					printf("</TABLE>\n");
				}
				break;
			}
		}

		return true;		
	}

	function information__view_delete($db, $params) {
		$views = array();
		$eoids = array();
		$ioids = array();
	
		$sql = sprintf("SELECT DISTINCT variable, value FROM element_data GROUP BY oid");
		$element_oids = mysql_query($sql, $db);
		while ($row = mysql_fetch_array($element_oids)) {
			$eoids[$row['variable']] = $row['value'];
		}

		$sql = sprintf("SELECT DISTINCT variable, value FROM interface_data GROUP BY oid");
		$interface_oids = mysql_query($sql, $db);
		while ($row = mysql_fetch_array($interface_oids)) {
			$ioids[$row['variable']] = $row['value'];
		}

		while (list($view, $value) = each($params)) {
			if (ereg('view_([0-9]+)', $view, $v)) {
				$sql = sprintf("SELECT descriptor, element_descriptor_variable, element_interface_descriptor_oid FROM display_view WHERE id = %d", $v[1]);

				$result = mysql_query($sql, $db);
				while ($row = mysql_fetch_array($result)) {
					printf("\n<INPUT TYPE=\"hidden\" NAME=\"id_%d\" VALUE=\"%d\">\n", $v[1], $v[1]);
					printf("\n<TABLE>\n");
					printf("\t<TR>\n");
					printf("\t\t<TD>%s</TD>\n", "<B>View Descriptor</B>");
					printf("\t\t<TD>%s</TD>", $row['descriptor']);
					printf("\t</TR>\n");
					printf("</TABLE>\n");
				}
			}
		}

		return true;		
	}

	function information__view_create($db) {
		$sql = sprintf("SELECT DISTINCT variable, value FROM element_data GROUP BY oid");
		$element_oids = mysql_query($sql, $db);

		$sql = sprintf("SELECT DISTINCT variable, value FROM interface_data GROUP BY oid");
		$interface_oids = mysql_query($sql, $db);

		printf("\n<TABLE>\n");
		printf("\t<TR>\n");
		printf("\t\t<TD>%s</TD>\n", "<B>View Descriptor</B>");
		printf("\t\t<TD>%s</TD>\n", "<INPUT TYPE=\"text\" NAME=\"descriptor\">");
		printf("\t</TR>\n");
		printf("\t<TR>\n");
		printf("\t\t<TD>%s</TD>\n", "<B>Element Descriptor OID</B>");
		printf("\t\t<TD><SELECT NAME=\"element_oid\">");

		while ($row = mysql_fetch_array($element_oids)) {
			printf("\t\t<OPTION VALUE=\"%s\">%s (ie: %s)</OPTION>\n", $row['variable'], $row['variable'], $row['value']);
		}

		printf("\t\t</SELECT></TD>");
		printf("\t</TR>\n");
		printf("\t<TR>\n");
		printf("\t\t<TD>%s</TD>\n", "<B>Interface Descriptor OID</B>");
		printf("\t\t<TD><SELECT NAME=\"interface_oid\">");

		while ($row = mysql_fetch_array($interface_oids)) {
			printf("\t\t<OPTION VALUE=\"%s\">%s (ie: %s)</OPTION>\n", $row['variable'], $row['variable'], $row['value']);
		}

		printf("\t\t</SELECT></TD>");
		printf("\t</TR>\n");
		printf("</TABLE>\n");
	}

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

         return false;
      } 
      if ($data['id']) {
         printf("<INPUT TYPE=\"hidden\" NAME=\"parent\" VALUE=\"%d\">", $data['id']);
      }
      $groups = array();
      $elements = array();

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

         return false;
      }
      printf("<TABLE>\n");

      printf("\t<TR>\n");
      printf("\t\t<TD>Descriptor</TD>");
      printf("\t\t<TD><INPUT TYPE=\"text\" NAME=\"descriptor\"></TD>");
      printf("\t</TR>\n");

      printf("\t<TR>\n");
      printf("\t\t<TD VALIGN=\"top\">\n");

      reset($groups);
      printf("\t\t\t<TABLE>\n");
      printf("\t\t\t\t<TR>\n");
      printf("\t\t\t\t\t<TD>Member Groups</TD>\n");
      printf("\t\t\t\t</TR>\n");
      while (list(, $i) = each($groups)) {
         $sql = sprintf("SELECT descriptor, parent FROM display_group WHERE id = %d ORDER BY descriptor", $i[1]);
         $result = mysql_query($sql, $db);
         $d = mysql_fetch_array($result);

         printf("\t\t\t\t<TR>\n");
         printf("\t\t\t\t\t<TD>%s<INPUT TYPE=\"hidden\" NAME=\"%s\" VALUE=\"%d\"></TD>\n", $d['descriptor'], sprintf("group_%d", $i[
1]), $i[1]);
         printf("\t\t\t\t</TR>\n");
      }
      printf("\t\t\t</TABLE>\n");

      printf("\t\t</TD>\n");
      printf("\t\t<TD VALIGN=\"top\">\n");

      reset($elements);
      printf("\t\t\t<TABLE>\n");
      printf("\t\t\t\t<TR>\n");
      printf("\t\t\t\t\t<TD>Member Devices</TD>\n");
      printf("\t\t\t\t</TR>\n");
      while (list(, $i) = each($elements)) {
         $sql = sprintf("SELECT value FROM element_data, display_view WHERE element_data.element = %d AND ne
twork_element_data.variable = display_view.element_descriptor_oid AND display_view.id = %d ORDER BY descriptor", $i[1], $data['view']);
         $result = mysql_query($sql, $db);
         $d = mysql_fetch_array($result);

         printf("\t\t\t\t<TR>\n");
         printf("\t\t\t\t\t<TD>%s<INPUT TYPE=\"hidden\" NAME=\"%s\" VALUE=\"%d\"></TD>\n", $d['value'], sprintf("element_%d", $i[1])
, $i[1]);
         printf("\t\t\t\t</TR>\n");
      }
      printf("\t\t\t</TABLE>\n");
      printf("\t\t</TD>\n");

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

   function information__view($db) {
      printf("\n<TABLE>\n");
      printf("\t<TR>\n");
      printf("\t\t<TD>%s</TD>\n", "&nbsp;");
      printf("\t</TR>\n");
      printf("</TABLE>\n");
   }

	function information__view_element($db, $view, $element) {
		$sql = sprintf("SELECT id, variable, value FROM element_data WHERE element = %d ORDER BY oid", $element);

		$result = mysql_query($sql, $db);

     	printf("\n<TABLE WIDTH=\"100%%\">\n");
		while ($row = mysql_fetch_array($result)) {
  	   	printf("\t<TR>\n");
  		   printf("\t\t<TD BGCOLOR=\"bb00bb\"><INPUT TYPE=\"checkbox\" NAME=\"check_%d\"></TD>\n", $row['id']);
  		   printf("\t\t<TD BGCOLOR=\"bb00bb\">%s</TD>\n", $row['variable']);
      	printf("\t\t<TD BGCOLOR=\"bb00bb\" WIDTH=\"100%%\">%s</TD>\n", $row['value']);
      	printf("\t</TR>\n");
		}
      printf("</TABLE>\n");
	}

	function information__view_group($db, $view, $group) {
		$sql = sprintf("SELECT id, descriptor FROM display_group WHERE id = %d", $group);

		$result = mysql_query($sql, $db);
		$row = mysql_fetch_array($result);

      printf("\n<TABLE WIDTH=\"100%%\">\n");
		
		if ($row['id']) {
     		printf("\t<TR>\n");
     	 	printf("\t\t<TD BGCOLOR=\"bb00bb\">%s</TD>\n", "Group Descriptor");
   	   printf("\t\t<TD BGCOLOR=\"bb00bb\" WIDTH=\"100%%\">%s</TD>\n", $row['descriptor']);
	      printf("\t</TR>\n");
		}

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

	function information__view_interface($db, $view, $interface) {
		$sql = sprintf("SELECT id, variable, value FROM interface_data WHERE interface = %d ORDER BY oid", $element);

		$result = mysql_query($sql, $db);

     	printf("\n<TABLE>\n");
		while ($row = mysql_fetch_array($result)) {
  	   	printf("\t<TR>\n");
  		   printf("\t\t<TD><INPUT TYPE=\"checkbox\" NAME=\"check_%d\"></TD>\n", $row['id']);
  		   printf("\t\t<TD>%s</TD>\n", $row['variable']);
      	printf("\t\t<TD>%s</TD>\n", $row['value']);
      	printf("\t</TR>\n");
		}
      printf("</TABLE>\n");
	}

	function information__default() {
		printf("\n<TABLE>\n");
		printf("\t<TR>\n");
		printf("\t\t<TD>The Sella Network Management System</TD>\n");
		printf("\t</TR>\n");
		printf("</TABLE>\n");
	}
?>
