chiark / gitweb /
abolish $dbh global in Mason code to shorten db connection lifetime
[ypp-sc-tools.web-live.git] / yarrg / web / route
index 1366345c44ab2d0e2361c7779ae589192919ac47..9c7200c39480df0343ab70b7615e4e994316b3d2 100644 (file)
@@ -4,6 +4,11 @@
 my %a;
 my @vars;
 
+# for output:
+my @archipelagoes;
+my @islandids;
+my %islandid2;
+
 #---------- "mode" argument parsing and mode menu at top of page ----------
 
 # for debugging, invoke as
@@ -71,7 +76,7 @@ foreach my $var (@vars) {
                        my %qf= (%baseqf,%queryqf);
                        delete $qf{$lname};
                        $qf{$lname}= $value if $cvalix;
-                       print '<a href="',&$quri(%qf),'">';
+                       print '<a href="',$quri->(%qf),'">';
                        $after= '</a>';
                }
                print $html, $after;
@@ -82,11 +87,7 @@ foreach my $var (@vars) {
 
 #---------- initial checks, startup, main entry form ----------
 
-die "unknown ocean $a{Ocean} ?"
-       unless grep { $_ eq $a{Ocean} } ocean_list();
-
-db_setocean($a{Ocean});
-db_connect();
+dbw_connect($a{Ocean});
 
 </%perl>
 <%args>
@@ -95,7 +96,7 @@ $routestring => ''
 </%args>
 
 <h1>Specify route</h1>
-<form action="<% &$quri() %>" method="get">
+<form action="<% $quri->() %>" method="get">
 
 %#---------- textbox, user enters route as string ----------
 % if (!$a{Dropdowns}) {
@@ -141,6 +142,7 @@ function tr_Ready() {
   tr_request= false;
   tr_Request();
 }
+window.onload= tr_Needed;
 </script>
 
 <input type="text" id="routestring" name="routestring" size=80
@@ -149,16 +151,6 @@ function tr_Ready() {
  onkeyup="tr_Later();"><br>
 <div id="routeresults">&nbsp;</div><br/>
 
-% if (length $routestring) {
-
-<pre>
-DATA FOR 
-<% $routestring |h %>
-WOULD GO HERE
-</pre>
-
-% }
-
 % } else { #---------- dropdowns, user selects from menus ----------
 
 <%perl>
@@ -180,6 +172,8 @@ my $optionlistmap= sub {
        return $out;
 };
 
+my $dbh= dbw_connect($a{Ocean});
+
 $sth= $dbh->prepare("SELECT DISTINCT archipelago FROM islands
                            ORDER BY archipelago;");
 $sth->execute();
@@ -199,12 +193,13 @@ while ($row=$sth->fetchrow_arrayref) {
        my $arch= $row->[2];
        push @{ $islandlistdata{'none'} }, [ @$row ];
        push @{ $islandlistdata{$arch} }, [ @$row ];
+       $islandid2{$row->[0]}= { Name => $row->[1], Arch => $arch };
 }
 
 my %resetislandlistdata;
 foreach my $arch (keys %islandlistdata) {
        $resetislandlistdata{$arch}=
-               &$optionlistmap($islandlistdata{$arch}, '');
+               $optionlistmap->($islandlistdata{$arch}, '');
 }
 
 </%perl>
@@ -232,7 +227,7 @@ function ms_Setarch(dd) {
 <td>
 <select name="archipelago<% $dd %>" onchange="ms_Setarch(<% $dd %>)">
 <option value="none">Whole ocean</option>
-<% &$optionlistmap(\@archlistdata, $ARGS{"archipelago$dd"}) %></select></td>
+<% $optionlistmap->(\@archlistdata, $ARGS{"archipelago$dd"}) %></select></td>
 %      }
 </tr>
 
@@ -242,18 +237,87 @@ function ms_Setarch(dd) {
 %              $arch= 'none' if !defined $arch;
 <td>
 <select name="islandid<% $dd %>">
-<% &$optionlistmap($islandlistdata{$arch}, $ARGS{"islandid$dd"}) %>
+<% $optionlistmap->($islandlistdata{$arch}, $ARGS{"islandid$dd"}) %>
 </select></td>
 %      }
 </tr>
 
 </table>
 
-% } #---------- end of dropdowns, now common code ----------
+% } #---------- end of dropdowns, now common middle of page code ----------
 
 <input type=submit name=submit value="Go">
 </form>
 
+<%perl>
+#========== result computations ==========
+
+my $results_head;
+$results_head= sub {
+       print "<h1>Results</h1>\n";
+       $results_head= sub { };
+};
+
+#---------- result computation - textstring ----------
+if (!$a{Dropdowns}) {
+  if (length $routestring) {
+       $results_head->();
+       my $rsr= $m->comp('routetextstring',
+               ocean => $a{Ocean},
+               string => $routestring,
+               format => 'return'
+       );
+       if (length $rsr->{Error}) {
+               print encode_entities($rsr->{Error});
+       } else {
+               foreach my $entry (@{ $rsr->{Results} }) {
+                       push @archipelagoes,
+                               defined $entry->[1] ? undef : $entry->[0];
+                       push @islandids, $entry->[1];
+               } 
+       }
+  }
+
+} else { #---------- results - dropdowns ----------
+
+my $argorundef= sub {
+       my ($dd,$base) = @_;
+       my $thing= $ARGS{"${base}${dd}"};
+       $thing= undef if defined $thing and $thing eq 'none';
+       return $thing;
+};
+
+for my $dd (0..$a{Dropdowns}-1) {
+       my $arch= $argorundef->($dd,'archipelago');
+       my $island= $argorundef->($dd,'islandid');
+       next unless defined $arch or defined $island;
+       if (defined $island and defined $arch) {
+               my $ii= $islandid2{$island};
+               my $iarch= $ii->{Arch};
+               if ($iarch ne $arch) {
+                       $results_head->();
+</%perl>
+ Specified archipelago <% $arch %> but
+ island <% $ii->{Name} %>
+ which is in <% $iarch %>; using the island.<br>
+<%perl>
+               }
+               $arch= undef;
+       }
+       push @archipelagoes, $arch;
+       push @islandids, $island;
+}
+
+}#---------- result processing, common stuff
+</%perl>
+
+% if (@islandids) {
+%      $results_head->();
+
+<& routetrade, islandids => \@islandids, archipelagoes => \@archipelagoes &>
+
+% }
+
 %#---------- debugging and epilogue ----------
 
 % if ($debug) {
@@ -276,5 +340,6 @@ function debug (m) {
 use CommodsWeb;
 use HTML::Entities;
 use URI::Escape;
+use JSON;
 
 </%init>