chiark / gitweb /
Generalise route[text]string
[ypp-sc-tools.main.git] / yarrg / web / query_route
1 <%args>
2 $quri
3 $qa
4 $routestring => '';
5 </%args>
6 <%perl>
7
8 my @archipelagoes;
9 my @islandids;
10 my %islandid2;
11
12 </%perl>
13
14 %#---------- textbox, user enters route as string ----------
15 % if (!$qa->{Dropdowns}) {
16
17 <h1>Specify route</h1>
18
19 <form action="<% $quri->() |h %>" method="get">
20
21 <&| qtextstring, qa => $qa, thingstring => 'routestring' &>
22  size=80
23 </&>
24
25 % } else { #---------- dropdowns, user selects from menus ----------
26
27 <%perl>
28 my ($sth,$row);;
29 my @archlistdata;
30 my %islandlistdata;
31 $islandlistdata{'none'}= [ [ "none", "Select island..." ] ];
32
33 my $optionlistmap= sub {
34         my ($optlist, $selected) = @_;
35         my $out='';
36         foreach my $entry (@$optlist) {
37                 $out.= sprintf('<option value="%s" %s>%s</option>',
38                         encode_entities($entry->[0]),
39                         defined $selected && $entry->[0] eq $selected
40                                 ? 'selected' : '',
41                         encode_entities($entry->[1]));
42         }
43         return $out;
44 };
45
46 my $dbh= dbw_connect($qa->{Ocean});
47
48 $sth= $dbh->prepare("SELECT DISTINCT archipelago FROM islands
49                             ORDER BY archipelago;");
50 $sth->execute();
51
52 while ($row=$sth->fetchrow_arrayref) {
53         my ($arch)= @$row;
54         push @archlistdata, [ $arch, $arch ];
55         $islandlistdata{$arch}= [ [ "none", "Whole arch" ] ];
56 }
57
58 $sth= $dbh->prepare("SELECT islandid,islandname,archipelago
59                             FROM islands
60                             ORDER BY islandname;");
61 $sth->execute();
62
63 while ($row=$sth->fetchrow_arrayref) {
64         my $arch= $row->[2];
65         push @{ $islandlistdata{'none'} }, [ @$row ];
66         push @{ $islandlistdata{$arch} }, [ @$row ];
67         $islandid2{$row->[0]}= { Name => $row->[1], Arch => $arch };
68 }
69
70 my %resetislandlistdata;
71 foreach my $arch (keys %islandlistdata) {
72         $resetislandlistdata{$arch}=
73                 $optionlistmap->($islandlistdata{$arch}, '');
74 }
75
76 </%perl>
77
78 <input type=hidden name=dropdowns value="<% $qa->{Dropdowns} |h %>">
79
80 <&| script &>
81 ms_lists= <% to_json_protecttags(\%resetislandlistdata) %>;
82 function ms_Setarch(dd) {
83   debug('ms_SetArch '+dd+' arch='+arch);
84   var arch= document.getElementsByName('archipelago'+dd).item(0).value;
85   var got= ms_lists[arch];
86   if (got == undefined) return; // unknown arch ?  hrm
87   debug('ms_SetArch '+dd+' arch='+arch+' got ok');
88   var select= document.getElementsByName('islandid'+dd).item(0);
89   select.innerHTML= got;
90   debug('ms_SetArch '+dd+' arch='+arch+' innerHTML set');
91 }
92 </&script>
93
94 <table style="table-layout:fixed; width:90%;">
95
96 <tr>
97 %       for my $dd (0..$qa->{Dropdowns}-1) {
98 <td>
99 <select name="archipelago<% $dd %>" onchange="ms_Setarch(<% $dd %>)">
100 <option value="none">Whole ocean</option>
101 <% $optionlistmap->(\@archlistdata, $ARGS{"archipelago$dd"}) %></select></td>
102 %       }
103 </tr>
104
105 <tr>
106 %       for my $dd (0..$qa->{Dropdowns}-1) {
107 %               my $arch= $ARGS{"archipelago$dd"};
108 %               $arch= 'none' if !defined $arch;
109 <td>
110 <select name="islandid<% $dd %>">
111 <% $optionlistmap->($islandlistdata{$arch}, $ARGS{"islandid$dd"}) %>
112 </select></td>
113 %       }
114 </tr>
115
116 </table>
117
118 % } #---------- end of dropdowns, now common middle of page code ----------
119
120 <input type=submit name=submit value="Go">
121 </form>
122
123 <%perl>
124 #========== result computations ==========
125
126 my $results_head;
127 $results_head= sub {
128         print "<h1>Results</h1>\n";
129         $results_head= sub { };
130 };
131
132 #---------- result computation - textstring ----------
133 if (!$qa->{Dropdowns}) {
134   if (length $routestring) {
135         $results_head->();
136         my $rsr= $m->comp('routetextstring',
137                 ocean => $qa->{Ocean},
138                 string => $routestring,
139                 format => 'return'
140         );
141         if (length $rsr->{Error}) {
142                 print encode_entities($rsr->{Error});
143         } else {
144                 foreach my $entry (@{ $rsr->{Results} }) {
145                         push @archipelagoes,
146                                 defined $entry->[1] ? undef : $entry->[0];
147                         push @islandids, $entry->[1];
148                 } 
149         }
150   }
151
152 } else { #---------- results - dropdowns ----------
153
154 my $argorundef= sub {
155         my ($dd,$base) = @_;
156         my $thing= $ARGS{"${base}${dd}"};
157         $thing= undef if defined $thing and $thing eq 'none';
158         return $thing;
159 };
160
161 for my $dd (0..$qa->{Dropdowns}-1) {
162         my $arch= $argorundef->($dd,'archipelago');
163         my $island= $argorundef->($dd,'islandid');
164         next unless defined $arch or defined $island;
165         if (defined $island and defined $arch) {
166                 my $ii= $islandid2{$island};
167                 my $iarch= $ii->{Arch};
168                 if ($iarch ne $arch) {
169                         $results_head->();
170 </%perl>
171  Specified archipelago <% $arch %> but
172  island <% $ii->{Name} %>
173  which is in <% $iarch %>; using the island.<br>
174 <%perl>
175                 }
176                 $arch= undef;
177         }
178         push @archipelagoes, $arch;
179         push @islandids, $island;
180 }
181
182 }#---------- result processing, common stuff
183 </%perl>
184
185 % if (@islandids) {
186 %       $results_head->();
187
188 <& routetrade, islandids => \@islandids, archipelagoes => \@archipelagoes &>
189
190 % }