commit b992b8e45e81e5f2e2513e13404f7caace51f93d
Author: Hamish Coleman <hamish@zot.org>
Date:   Wed Jun 6 20:27:45 2007 +1000

    Start to move towards a more functional system

diff --git a/mrtg.cgi b/mrtg.cgi
index 9526bd7..20cd9a1 100755
--- a/mrtg.cgi
+++ b/mrtg.cgi
@@ -10,7 +10,7 @@
 #
 # Requires the CGI package (in mandriva this is perl-CGI)
 
-our $VERSION=2;
+our $VERSION=3;
 
 use strict;
 use warnings;
@@ -191,6 +191,130 @@ sub init_lists(@) {
 	return $l;
 }
 
+sub print_table($$) {
+	my ($lists,$style) = @_;
+
+	print "\n<table border=2 cellpadding=2 cellspacing=0 style=\"margin: 1em 1em 1em 0; background: #f9f9f9; border: 1px #aaa solid; border-collapse: collapse; font-size: 95%;\">\n";
+
+	if ($style eq 'wide') {
+		print "<tr>\n";
+
+		# print headings
+		for my $i (sort keys %{$lists}) {
+			print "<th>$i</th>\n";
+		}
+
+		print "</tr>";
+	}
+	
+	if ($style eq 'wide') {
+		print "<tr>\n";
+	}
+
+	# print contents
+	for my $i (sort keys %{$lists}) {
+		if ($style eq 'high') {
+			print "<tr><th>$i</tr>\n";
+			print "<tr>";
+		}
+		print " <td valign=top>\n";
+		for my $j (sort keys %{$lists->{$i}}) {
+			print "  ".a({-href=>"?list=$i,$j"},"$j")."<br>\n";
+		}
+		print " </td>\n";
+
+		if ($style eq 'high') {
+			print "</tr>\n";
+		}
+	}
+
+	if ($style eq 'wide') {
+		print "</tr>";
+	}
+
+	print "</table>\n";
+}
+
+sub print_list($$$) {
+	my ($lists,$graph,$list) = @_;
+
+	if (!defined $list) {
+		return undef;
+	}
+	my @wantlist = split /,/,$list;
+
+	my @list = @{$lists->{$wantlist[0]}->{$wantlist[1]}};
+	my $selfurl.='list='.param('list').'&';
+
+	if ($graph ne 'day') {
+		$selfurl.='graph='.$graph.'&';
+	}
+
+	my @display_order = (0..$#list);
+	@display_order = split /:/, param('ord') if defined param('ord');
+
+	for my $index (0..$#list){
+	    my @spliced = @display_order;
+	    my $router_num = splice @spliced, $index, 1; # Router removed
+	    my $router = $list[$router_num];
+		# FIXME - what if mtime is undef?
+	    my $time = localtime $router->{mtime}; # $st_mtime saved in above loop
+	    ($time) = $time =~ /(\d+:\d+:\d+)/; # Just the time
+	    print hr, "\n";
+
+	    print a({-name=>$index});
+	    # Print re-ordering links top, bot, up, dn
+	    my $mv_dn = "";
+	    my $mv_up = "";
+	    $" = ':';
+	    if($index > 0){
+		my @top = ($router_num, @spliced);
+		my @up = @spliced;
+		my $indxup = $index - 1;
+		splice @up, $indxup, 0, $router_num;
+
+		# FIXME - need a way to append this correctly ..
+		$mv_up = sprintf "%s %s ", a({-href=>$selfurl."ord=@top#0"}, "Top"), # move to top
+		a({-href=>$selfurl."ord=@up#$indxup"}, "Up"); # move up
+	    }
+	    if($index < $#list){
+		my @bot = (@spliced, $router_num);
+		my @down = @spliced;
+		my $indxdn = $index + 1;
+		splice @down, $indxdn, 0, $router_num;
+
+		my $indxbot = @list - 3;
+		while($indxbot < 0){$indxbot += 1}
+		$mv_dn = sprintf "%s %s ", a({-href=>$selfurl."ord=@down#$indxdn"}, "Down"), # move down
+		a({-href=>$selfurl."ord=@bot#$indxbot"}, "Bot"); # move to bottom
+	    }
+	    undef $";
+
+	    print table({-width=>"100\%"}, 
+			TR(
+				td({-align=>"left",-width=>"20\%"}, "$mv_up $mv_dn"),
+				td({-align=>"left"},
+					b($router->{title}),
+					" $time")
+				)
+			);
+
+	    my ($ysize, $xsize);
+	    if ( exists $router->{ysize} ) {
+		$ysize = $router->{ysize};
+	    } else {
+		$ysize = "135";
+	    }
+	    if ( exists $router->{xsize} ) {
+		$xsize = $router->{xsize};
+	    } else {
+		$xsize = "400";
+	    }
+	    print a({-href=>$router->{name}.".html"}, img{-src=>$router->{name}."-$graph.".$router->{imagetype}, -height=>"$ysize", -width=>"$xsize"});
+
+	}
+}
+
 my @tests = init_tests(@config_files);
 my $lists = init_lists(@tests);
 
@@ -207,7 +331,7 @@ $refresh = $interval if $refresh <= $guardband;
 my $expires = gmtime (time + $interval * 2 + $guardband);
 
 #selfurl is ready to append args to.
-my $selfurl = url(-relative=>1).'?';
+my $selfurl = '?';
 my $this_host = hostname;
 
 print header, start_html(-TITLE=>"$this_host MRTG Index (V$VERSION)", -BGCOLOR=>'#e6e6e6'),
@@ -219,24 +343,16 @@ print header, start_html(-TITLE=>"$this_host MRTG Index (V$VERSION)", -BGCOLOR=>
 my @wantlist = split /,/,param('list');
 
 if (!@wantlist) {
-	print table({-width=>"100\%"}, TR(td("Select which list to show")));
-	print "\n<table border=2 cellpadding=4 cellspacing=0 style=\"margin: 1em 1em 1em 0; background: #f9f9f9; border: 1px #aaa solid; border-collapse: collapse; font-size: 95%;\">\n";
-	print "<tr>\n";
-	for my $i (sort keys %{$lists}) {
-		print "<th>$i</th>\n";
-	}
-	print "</tr><tr>\n";
-	for my $i (sort keys %{$lists}) {
-		print " <td valign=top>\n";
-		for my $j (sort keys %{$lists->{$i}}) {
-			print "  ".a({-href=>$selfurl."list=$i,$j"},"$j")."<br>\n";
-		}
-		print " </td>\n";
-	}
-	print "</tr></table>\n";
+	print table(
+		{-width=>"100\%"},
+		TR(td("Select which list to show"))
+	);
+	print_table($lists,'wide');
 } elsif (!defined $lists->{$wantlist[0]}->{$wantlist[1]}) {
 	#FIXME
 	print table({-width=>"100\%"}, TR(td("That list is unavailable")));
+} elsif(defined param('test')) {
+	print_list($lists,'day',param('list'));
 } else {
 	my @list = @{$lists->{$wantlist[0]}->{$wantlist[1]}};
 	$selfurl.='list='.param('list').'&';
@@ -250,9 +366,9 @@ if (!@wantlist) {
 	print
 		"<meta http-equiv=\"expires\" content=\"$expires GMT\">\n",
 		"<meta http-equiv=\"refresh\" content=$refresh>\n";
-	print "<table>\n",
-		"<tr><td>Click graph for more info\n",
-		"<tr><td>";
+
+	print "<table cellpadding=2 cellspacing=0>\n",
+		"<tr><td colspan=2>";
 	if ($graph_unchecked ne 'day') {
 		print a({-href=>$selfurl.'graph=day&'}, 'Daily')
 	} else {
@@ -280,15 +396,20 @@ if (!@wantlist) {
 		$graph = $graph_unchecked;
 	}
 	print '&nbsp;';
-	print a({-href=>url(-relative=>1)}, 'Change List'),
+	print a({-href=>'?'}, 'Change List'),
 		'&nbsp;',
-		'(Current List is ',param('list'),")\n",
-		"</table>\n";
+		'(Current List is ',param('list'),")\n";
+
+	print "</tr><tr><td valign=top>";
+	print_table($lists,'high');
+	print "</td><td valign=top>\n";
 
 	if ($graph ne 'day') {
 		$selfurl.='graph='.$graph.'&';
 	}
 	
+	print "Click graph for more info\n";
+
 	my @display_order = (0..$#list);
 	@display_order = split /:/, param('ord') if defined param('ord');
 
@@ -352,6 +473,7 @@ if (!@wantlist) {
 	    print a({-href=>$router->{name}.".html"}, img{-src=>$router->{name}."-$graph.".$router->{imagetype}, -height=>"$ysize", -width=>"$xsize"});
 
 	}
+	print "</table>\n";
 }
 
 if ($warnings) {
