<? 
   function action($db, $params) {
		if ($params['function'] == false) {
         printf("Sella Network Management System v0.3");

         return true; 
		} 

      switch ($params['function']) {
			case 'commit_view_edit':
				action__commit_view_edit($db, $params);
				action__view($db, $params);
				break;

         case 'commit_view_create':
				action__commit_view_create($db, $params);
				action__view($db, $params);
				break;

         case 'commit_view_delete':
				action__commit_view_delete($db, $params);
				action__view($db, $params);
				break;

			case 'view':
			case 'abort_view_create':
			case 'abort_view_edit':
			case 'abort_view_delete':
				action__view($db, $params);
				break;

			case 'view_create':
				action__view_create($db, $params);
				break;

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

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

			case 'view_group':
				action__view_group($db, $params);
				break;

			case 'commit_group_create':
				action__commit_group_create($db, $params);
				break;

			case 'view_element':
				action__view_element($db, $params);
				break;

			case 'view_interface':
				action__view_interface($db, $params);
				break;
		}
   }

	function action__commit_group_create($db, $data) {
      if ($data['group_descriptor'] == false) {
         printf("Error: Group descriptor not supplied.<BR>\n");

         return false;
		} else if ($data['view'] == false) {
         printf("Error: View not supplied.<BR>\n");

         return false;
		}

		if ($data['group'] == false) {
			$sql = sprintf("INSERT INTO display_group VALUES(NULL, %d, NULL, '%s')", $data['view'], $data['group_descriptor']);
			$result = mysql_query($sql, $db);

			$sql = sprintf("SELECT id FROM display_group WHERE display_view = %d AND parent IS NULL AND descriptor = '%s'", $data['view'], $data['group_descriptor']);
		} else {
			$sql = sprintf("INSERT INTO display_group VALUES(NULL, %d, %d, '%s')", $data['view'], $data['group'], $data['group_descriptor']);
			$result = mysql_query($sql, $db);

			$sql = sprintf("SELECT id FROM display_group WHERE display_view = %d AND parent = %d AND descriptor = '%s'", $data['view'], $data['group'], $data['group_descriptor']);
		}
		$result = mysql_query($sql, $db);
		$row = mysql_fetch_array($result);
		$new_group = $row['id'];

		reset($data);
		while (list($key, $value) = each($data)) {
			if (ereg('group_group_([0-9]+)', $key, $v)) {
				$sql = sprintf("UPDATE display_group SET parent = %d WHERE id = %d", $new_group, $v[1]);
				$result = mysql_query($sql, $db);
			} else if (ereg('element_group_([0-9]+)', $key, $v)) {
				$sql = sprintf("SELECT display_group_member.id FROM display_group_member, display_group WHERE display_group_member.element = %d AND display_group_member.display_group = display_group.id AND display_group.display_view = %d", $v[1], $data['view']);
				$result = mysql_query($sql, $db);
				$row = mysql_fetch_array($result);

				if ($row['id']) {
					$sql = sprintf("UPDATE display_group_member SET display_group = %d WHERE id = %d", $new_group, $row['id']);
					$result = mysql_query($sql, $db);
				} else {
					$sql = sprintf("INSERT INTO display_group_member VALUES(NULL, %d, %d)", $new_group, $v[1]);
					$result = mysql_query($sql, $db);
				}
			}
		}
	}

	function action__commit_view_create($db, $data) {
      if ($data['descriptor'] == false) {
         printf("Error: View descriptor not supplied.<BR>\n");

         return false;
      } else if ($data['element_oid'] == false) {
         printf("Error: Element descriptor OID not supplied.<BR>\n");

         return false;
      } else if ($data['interface_oid'] == false) {
         printf("Error: Interface descriptor OID not supplied.<BR>\n");

         return false;
      }

      $sql = sprintf("INSERT INTO display_view VALUES(NULL, '%s', '%s', '%s')", $data['descriptor'], $data['element_oid'], $data['in
terface_oid']);
      if (!mysql_query($sql, $db)) {
         printf("Error: could not create new view.<BR>\n");

         return false;
      }

      return true;
   }

	function action__commit_view_edit($db, $data) {
      if ($data['id'] == false) {
         printf("Error: View ID not supplied.<BR>\n");

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

         return false;
      } else if ($data['element_oid'] == false) {
         printf("Error: Element descriptor OID not supplied.<BR>\n");

         return false;
      } else if ($data['interface_oid'] == false) {
         printf("Error: Interface descriptor OID not supplied.<BR>\n");

         return false;
      }

		$sql = sprintf("UPDATE display_view SET descriptor = '%s', element_descriptor_oid = '%s', element_interface_descriptor_oid = '%s' WHERE id = %d", $data['descriptor'], $data['element_oid'], $data['interface_oid'], $data['id']);
      if (!mysql_query($sql, $db)) {
         printf("Error: could not edit view.<BR>\n");

         return false;
      }

      return true;
	}

	function action__commit_view_delete($db, $data) {
		while (list($key, $value) = each($data)) {
			if (ereg('id_([0-9]+)', $key, $v)) {
				$sql = sprintf("DELETE FROM display_view WHERE id = %d", $value);

		      if (!mysql_query($sql, $db)) {
   		      printf("Error: could not delete view (%d).<BR>\n", $value);

  	      		return false;
	      	}
			}
		}

      return true;
	}

	function action__view($db, $data) {
		printf("<A HREF=\"admin.php?\">sNMS</A> &gt; View");

		return true;
	}

	function action__view_create($db, $data) {
		printf("<A HREF=\"admin.php?\">sNMS</A> &gt; <A HREF=\"admin.php?function=view\">View</A> &gt; Create");

		return true;
	}

	function action__view_edit($db, $data) {
		printf("<A HREF=\"admin.php?\">sNMS</A> &gt; <A HREF=\"admin.php?function=view\">View</A> &gt; Edit");

		return true;
	}

	function action__view_delete($db, $data) {
		printf("<A HREF=\"admin.php?\">sNMS</A> &gt; <A HREF=\"admin.php?function=view\">View</A> &gt; Delete");

		return true;
	}

	function action__view_group($db, $data) {
		$sql = sprintf("SELECT descriptor FROM display_view WHERE id = %d", $data['view']);
		$result = mysql_query($sql);
		$row = mysql_fetch_array($result);

		$descriptor = $row['descriptor'];
		if ($data['group'] != false) {
			$group = $data['group'];
			$path = "";

			do {	
				$sql = sprintf("SELECT descriptor, parent FROM display_group WHERE id = %d", $group);
				$result = mysql_query($sql, $db);
				$row = mysql_fetch_array($result);

				if ($group == $data['group']) {
					$path = sprintf("%s", $row['descriptor']);
				} else {
					$path = sprintf("<A HREF=\"admin.php?view=%d&function=view_group&group=%s\">%s</A> &gt; %s", $data['view'], $group, $row['descriptor'], $path);
				}

				$group = $row['parent'];
			} while ($group);

			printf("<A HREF=\"admin.php?\">sNMS</A> &gt; <A HREF=\"admin.php?function=view\">View</A> &gt; <A HREF=\"admin.php?view=%d&function=view_group\">%s</A> &gt; %s\n", $data['view'], $descriptor, $path);
		} else {
			printf("<A HREF=\"admin.php?\">sNMS</A> &gt; <A HREF=\"admin.php?function=view\">View</A> &gt; %s\n", $row['descriptor']);
		}

		return true;
	}

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

		$sql = sprintf("SELECT value FROM element_data WHERE element = %d AND variable = '%s'", $data['id'], $row['element_descriptor_oid']);
		$result = mysql_query($sql);
		$row = mysql_fetch_array($result);
		$element_descriptor = $row['value'];

		$sql = sprintf("SELECT display_group.id FROM display_group_member, display_group WHERE display_group_member.element = %d AND display_group_member.display_group = display_group.id", $data['id']);
		$result = mysql_query($sql);
		$row = mysql_fetch_array($result);

		if ($row['id'] != false) {
			$group = $row['id'];
			$path = "";

			do {	
				$sql = sprintf("SELECT descriptor, parent FROM display_group WHERE id = %d", $group);
				$result = mysql_query($sql, $db);
				$row = mysql_fetch_array($result);

				$path = sprintf("<A HREF=\"admin.php?view=%d&function=view_group&group=%s\">%s</A> &gt; %s", $data['view'], $group, $row['descriptor'], $path);

				$group = $row['parent'];
			} while ($group);
	
			printf("<A HREF=\"admin.php?\">sNMS</A> &gt; <A HREF=\"admin.php?function=view\">View</A> &gt; <A HREF=\"admin.php?view=%d&function=view_group\">%s</A> &gt; %s %s\n", $data['view'], $view_descriptor, $path, $element_descriptor);
		} else {
			printf("<A HREF=\"admin.php?\">sNMS</A> &gt; <A HREF=\"admin.php?function=view\">View</A> &gt; <A HREF=\"admin.php?view=%d&function=view_group\">%s</A> &gt; %s\n", $data['view'], $view_descriptor, $element_descriptor);
		}

		return true;
	}

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

		$sql = sprintf("SELECT value FROM interface_data WHERE interface = %d AND variable = '%s'", $data['id'], $i_oid);
		$result = mysql_query($sql, $db);
		$row = mysql_fetch_array($result);
		$interface = $row['value'];

		$sql = sprintf("SELECT element.id FROM element, interface WHERE interface.element = element.id AND interface.id = %d", $data['id']);
		$result = mysql_query($sql, $db);
		$row = mysql_fetch_array($result);
		$element = $row['id'];

		$sql = sprintf("SELECT value FROM element_data WHERE element = %d AND variable = '%s'", $element, $e_oid);
		$result = mysql_query($sql);
		$row = mysql_fetch_array($result);
		$element_descriptor = $row['value'];

		$sql = sprintf("SELECT display_group.id FROM display_group_member, display_group WHERE display_group_member.element = %d AND display_group_member.display_group = display_group.id", $element);
		$result = mysql_query($sql);
		$row = mysql_fetch_array($result);

		if ($row['id'] != false) {
			$group = $row['id'];
			$path = "";

			do {	
				$sql = sprintf("SELECT descriptor, parent FROM display_group WHERE id = %d", $group);
				$result = mysql_query($sql, $db);
				$row = mysql_fetch_array($result);

				$path = sprintf("<A HREF=\"admin.php?view=%d&function=view_group&group=%s\">%s</A> &gt; %s", $data['view'], $group, $row['descriptor'], $path);

				$group = $row['parent'];
			} while ($group);
	
			printf("<A HREF=\"admin.php?\">sNMS</A> &gt; <A HREF=\"admin.php?function=view\">View</A> &gt; <A HREF=\"admin.php?view=%d&function=view_group\">%s</A> &gt; %s <A HREF=\"admin.php?view=%d&function=view_element&id=%d\">%s</A> &gt; %s\n", $data['view'], $view_descriptor, $path, $data['view'], $element, $element_descriptor, $interface);
		} else {
			printf("<A HREF=\"admin.php?\">sNMS</A> &gt; <A HREF=\"admin.php?function=view\">View</A> &gt; <A HREF=\"admin.php?view=%d&function=view_group\">%s</A> &gt; <A HREF=\"admin.php?view=%d&function=view_element&id=%d\">%s</A> &gt; %s\n", $data['view'], $view_descriptor, $data['view'], $element, $element_descriptor, $interface);
		}

		return true;
	}
?>
