chiark / gitweb /
Split lookup options into separate query_ components
[ypp-sc-tools.db-live.git] / yarrg / web / query_route
1 <%args>
2 $quri
3 $a
4 $routestring => '';
5 </%args>
6 <%perl>
7 #my $routestring= $queryqf{'routestring'};
8 #
9 # for output:
10 my @archipelagoes;
11 my @islandids;
12 my %islandid2;
13
14 </%perl>
15
16 <h1>Specify route</h1>
17 <form action="<% $quri->() |h %>" method="get">
18
19 %#---------- textbox, user enters route as string ----------
20 % if (!$a->{Dropdowns}) {
21
22 Enter route (islands, or archipelagoes, separated by |s or commas;
23  abbreviations are OK):<br>
24
25 <&| script &>
26 tr_uri= "routetextstring?format=json&type=text/xml"
27                 + "&ocean=<% uri_escape($a->{Ocean}) %>";
28
29 tr_timeout=false;
30 tr_request=false;
31 tr_done='';
32 tr_needed='';
33 function tr_Later(){
34   window.clearTimeout(tr_timeout);
35   tr_timeout = window.setTimeout(tr_Needed, 500);
36 }
37 function tr_Needed(){
38   window.clearTimeout(tr_timeout);
39   tr_element= document.getElementById('routestring');
40   tr_needed= tr_element.value;
41   tr_Request();
42 }
43 function tr_Request(){
44   if (tr_request || tr_needed==tr_done) return;
45   tr_done= tr_needed;
46   tr_request= new XMLHttpRequest();
47   uri= tr_uri+'&string='+encodeURIComponent(tr_needed);
48   tr_request.open('GET', uri);
49   tr_request.onreadystatechange= tr_Ready;
50   tr_request.send(null);
51 }
52 function tr_Ready() {
53   if (tr_request.readyState != 4) return;
54   if (tr_request.status == 200) {
55     response= tr_request.responseText;
56     eval('results='+response);
57     toedit= document.getElementById('routeresults');
58     toedit.innerHTML= results.show;
59   }
60   tr_request= false;
61   tr_Request();
62 }
63 window.onload= tr_Needed;
64 </&script>
65
66 <input type="text" id="routestring" name="routestring" size=80
67  value="<% $routestring |h %>"
68  onchange="tr_Needed();"
69  onkeyup="tr_Later();"><br>
70 <div id="routeresults">&nbsp;</div><br>
71
72 % } else { #---------- dropdowns, user selects from menus ----------
73
74 <%perl>
75 my ($sth,$row);;
76 my @archlistdata;
77 my %islandlistdata;
78 $islandlistdata{'none'}= [ [ "none", "Select island..." ] ];
79
80 my $optionlistmap= sub {
81         my ($optlist, $selected) = @_;
82         my $out='';
83         foreach my $entry (@$optlist) {
84                 $out.= sprintf('<option value="%s" %s>%s</option>',
85                         encode_entities($entry->[0]),
86                         defined $selected && $entry->[0] eq $selected
87                                 ? 'selected' : '',
88                         encode_entities($entry->[1]));
89         }
90         return $out;
91 };
92
93 my $dbh= dbw_connect($a->{Ocean});
94
95 $sth= $dbh->prepare("SELECT DISTINCT archipelago FROM islands
96                             ORDER BY archipelago;");
97 $sth->execute();
98
99 while ($row=$sth->fetchrow_arrayref) {
100         my ($arch)= @$row;
101         push @archlistdata, [ $arch, $arch ];
102         $islandlistdata{$arch}= [ [ "none", "Whole arch" ] ];
103 }
104
105 $sth= $dbh->prepare("SELECT islandid,islandname,archipelago
106                             FROM islands
107                             ORDER BY islandname;");
108 $sth->execute();
109
110 while ($row=$sth->fetchrow_arrayref) {
111         my $arch= $row->[2];
112         push @{ $islandlistdata{'none'} }, [ @$row ];
113         push @{ $islandlistdata{$arch} }, [ @$row ];
114         $islandid2{$row->[0]}= { Name => $row->[1], Arch => $arch };
115 }
116
117 my %resetislandlistdata;
118 foreach my $arch (keys %islandlistdata) {
119         $resetislandlistdata{$arch}=
120                 $optionlistmap->($islandlistdata{$arch}, '');
121 }
122
123 </%perl>
124
125 <input type=hidden name=dropdowns value="<% $a->{Dropdowns} |h %>">
126
127 <&| script &>
128 ms_lists= <% to_json_protecttags(\%resetislandlistdata) %>;
129 function ms_Setarch(dd) {
130   debug('ms_SetArch '+dd+' arch='+arch);
131   var arch= document.getElementsByName('archipelago'+dd).item(0).value;
132   var got= ms_lists[arch];
133   if (got == undefined) return; // unknown arch ?  hrm
134   debug('ms_SetArch '+dd+' arch='+arch+' got ok');
135   var select= document.getElementsByName('islandid'+dd).item(0);
136   select.innerHTML= got;
137   debug('ms_SetArch '+dd+' arch='+arch+' innerHTML set');
138 }
139 </&script>
140
141 <table style="table-layout:fixed; width:90%;">
142
143 <tr>
144 %       for my $dd (0..$a->{Dropdowns}-1) {
145 <td>
146 <select name="archipelago<% $dd %>" onchange="ms_Setarch(<% $dd %>)">
147 <option value="none">Whole ocean</option>
148 <% $optionlistmap->(\@archlistdata, $ARGS{"archipelago$dd"}) %></select></td>
149 %       }
150 </tr>
151
152 <tr>
153 %       for my $dd (0..$a->{Dropdowns}-1) {
154 %               my $arch= $ARGS{"archipelago$dd"};
155 %               $arch= 'none' if !defined $arch;
156 <td>
157 <select name="islandid<% $dd %>">
158 <% $optionlistmap->($islandlistdata{$arch}, $ARGS{"islandid$dd"}) %>
159 </select></td>
160 %       }
161 </tr>
162
163 </table>
164
165 % } #---------- end of dropdowns, now common middle of page code ----------
166
167 <input type=submit name=submit value="Go">
168 </form>
169
170 <%perl>
171 #========== result computations ==========
172
173 my $results_head;
174 $results_head= sub {
175         print "<h1>Results</h1>\n";
176         $results_head= sub { };
177 };
178
179 #---------- result computation - textstring ----------
180 if (!$a->{Dropdowns}) {
181   if (length $routestring) {
182         $results_head->();
183         my $rsr= $m->comp('routetextstring',
184                 ocean => $a->{Ocean},
185                 string => $routestring,
186                 format => 'return'
187         );
188         if (length $rsr->{Error}) {
189                 print encode_entities($rsr->{Error});
190         } else {
191                 foreach my $entry (@{ $rsr->{Results} }) {
192                         push @archipelagoes,
193                                 defined $entry->[1] ? undef : $entry->[0];
194                         push @islandids, $entry->[1];
195                 } 
196         }
197   }
198
199 } else { #---------- results - dropdowns ----------
200
201 my $argorundef= sub {
202         my ($dd,$base) = @_;
203         my $thing= $ARGS{"${base}${dd}"};
204         $thing= undef if defined $thing and $thing eq 'none';
205         return $thing;
206 };
207
208 for my $dd (0..$a->{Dropdowns}-1) {
209         my $arch= $argorundef->($dd,'archipelago');
210         my $island= $argorundef->($dd,'islandid');
211         next unless defined $arch or defined $island;
212         if (defined $island and defined $arch) {
213                 my $ii= $islandid2{$island};
214                 my $iarch= $ii->{Arch};
215                 if ($iarch ne $arch) {
216                         $results_head->();
217 </%perl>
218  Specified archipelago <% $arch %> but
219  island <% $ii->{Name} %>
220  which is in <% $iarch %>; using the island.<br>
221 <%perl>
222                 }
223                 $arch= undef;
224         }
225         push @archipelagoes, $arch;
226         push @islandids, $island;
227 }
228
229 }#---------- result processing, common stuff
230 </%perl>
231
232 % if (@islandids) {
233 %       $results_head->();
234
235 <& routetrade, islandids => \@islandids, archipelagoes => \@archipelagoes &>
236
237 % }