chiark / gitweb /
e062e60c6c4b072ca6daf1f96709f0d111e87cad
[ypp-sc-tools.db-live.git] / yarrg / web / lookup
1 <html><head><title>Route - YARRG</title></head><body>
2
3 <a href="<% $m->current_comp()->name() %>">YARRG</a> -
4  Yet Another Revenue Research Gatherer
5 <p>
6
7 <%perl>
8 my %a;
9 my @vars;
10
11 # for output:
12 my @archipelagoes;
13 my @islandids;
14 my %islandid2;
15
16 #---------- "mode" argument parsing and mode menu at top of page ----------
17
18 # for debugging, invoke as
19 #  http://www.chiark.greenend.org.uk/ucgi/~clareb/mason/pirates/pirate-route?debug=1
20
21 @vars= ({       Name => 'Ocean',
22                 Before => 'Ocean: ',
23                 CmpCanon => sub { ucfirst lc $_[0] },
24                 Values => [ ocean_list() ]
25         }, {    Name => 'Dropdowns',
26                 Before => 'Interface: ',
27                 CmpCanon => sub { !!$_[0] },
28                 Values => [     [ 0, 'Type in names' ],
29                                 [ 4, 'Select from menus' ] ]
30         });
31
32 foreach my $var (@vars) {
33         my $name= $var->{Name};
34         my $lname= lc $name;
35         $var->{Before}= '' unless exists $var->{Before};
36         $var->{CmpCanon}= sub { $_[0]; } unless exists $var->{CmpCanon};
37         foreach my $val (@{ $var->{Values} }) {
38                 next if ref $val;
39                 $val= [ $val, encode_entities($val) ];
40         }
41         if (exists $ARGS{$lname}) {
42                 $a{$name}= $ARGS{$lname};
43         } else {
44                 $a{$name}= $var->{Values}[0][0];
45         }
46 }
47
48 my %baseqf;
49 foreach my $var (@vars) {
50         my $lname= lc $var->{Name};
51         next unless exists $ARGS{$lname};
52         $baseqf{$lname}= $ARGS{$lname};
53 }
54
55 my %queryqf;
56 foreach my $var (keys %ARGS) {
57         next unless $var =~
58                 m/^(?:routestring|islandid\d|archipelago\d|debug)$/;
59         $queryqf{$var}= $ARGS{$var};
60 }
61
62 my $uri= URI->new($m->current_comp()->name());
63 my $quri= sub { $uri->query_form(@_); $uri->path_query(); };
64
65 foreach my $var (@vars) {
66         my $name= $var->{Name};
67         my $lname= lc $var->{Name};
68         my $delim= $var->{Before};
69         my $canon= &{$var->{CmpCanon}}($a{$name});
70         my $cvalix= 0;
71         foreach my $valr (@{ $var->{Values} }) {
72                 print $delim;  $delim= "\n|\n";
73                 my ($value,$html) = @$valr;
74                 my $iscurrent= &{$var->{CmpCanon}}($value) eq $canon;
75                 my $after;
76                 if ($iscurrent) {
77                         print '<b>';
78                         $after= '</b>';
79                 } else {
80                         my %qf= (%baseqf,%queryqf);
81                         delete $qf{$lname};
82                         $qf{$lname}= $value if $cvalix;
83                         print '<a href="',$quri->(%qf),'">';
84                         $after= '</a>';
85                 }
86                 print $html, $after;
87                 $cvalix++;
88         }
89         print "<p>\n\n";
90 }
91
92 #---------- initial checks, startup, main entry form ----------
93
94 dbw_connect($a{Ocean});
95
96 </%perl>
97 <%args>
98 $debug => 0
99 $routestring => ''
100 </%args>
101
102 <h1>Specify route</h1>
103 <form action="<% $quri->() %>" method="get">
104
105 %#---------- textbox, user enters route as string ----------
106 % if (!$a{Dropdowns}) {
107
108 Enter route (islands, or archipelagoes, separated by |s or commas;
109  abbreviations are OK):<br/>
110
111 <script type="text/javascript">
112 tr_uri= "routetextstring?format=json&type=text/xml"
113                 + "&ocean=<% uri_escape($a{Ocean}) %>";
114
115 tr_timeout=false;
116 tr_request=false;
117 tr_done='';
118 tr_needed='';
119 function tr_Later(){
120   window.clearTimeout(tr_timeout);
121   tr_timeout = window.setTimeout(tr_Needed, 500);
122 }
123 function tr_Needed(){
124   window.clearTimeout(tr_timeout);
125   tr_element= document.getElementById('routestring');
126   tr_needed= tr_element.value;
127   tr_Request();
128 }
129 function tr_Request(){
130   if (tr_request || tr_needed==tr_done) return;
131   tr_done= tr_needed;
132   tr_request= new XMLHttpRequest();
133   uri= tr_uri+'&string='+encodeURIComponent(tr_needed);
134   tr_request.open('GET', uri);
135   tr_request.onreadystatechange= tr_Ready;
136   tr_request.send(null);
137 }
138 function tr_Ready() {
139   if (tr_request.readyState != 4) return;
140   if (tr_request.status == 200) {
141     response= tr_request.responseText;
142     eval('results='+response);
143     toedit= document.getElementById('routeresults');
144     toedit.innerHTML= results.show;
145   }
146   tr_request= false;
147   tr_Request();
148 }
149 window.onload= tr_Needed;
150 </script>
151
152 <input type="text" id="routestring" name="routestring" size=80
153  value="<% $routestring |h %>"
154  onchange="tr_Needed();"
155  onkeyup="tr_Later();"><br>
156 <div id="routeresults">&nbsp;</div><br/>
157
158 % } else { #---------- dropdowns, user selects from menus ----------
159
160 <%perl>
161 my ($sth,$row);;
162 my @archlistdata;
163 my %islandlistdata;
164 $islandlistdata{'none'}= [ [ "none", "Select island..." ] ];
165
166 my $optionlistmap= sub {
167         my ($optlist, $selected) = @_;
168         my $out='';
169         foreach my $entry (@$optlist) {
170                 $out.= sprintf('<option value="%s" %s>%s</option>',
171                         encode_entities($entry->[0]),
172                         defined $selected && $entry->[0] eq $selected
173                                 ? 'selected' : '',
174                         encode_entities($entry->[1]));
175         }
176         return $out;
177 };
178
179 my $dbh= dbw_connect($a{Ocean});
180
181 $sth= $dbh->prepare("SELECT DISTINCT archipelago FROM islands
182                             ORDER BY archipelago;");
183 $sth->execute();
184
185 while ($row=$sth->fetchrow_arrayref) {
186         my ($arch)= @$row;
187         push @archlistdata, [ $arch, $arch ];
188         $islandlistdata{$arch}= [ [ "none", "Whole arch" ] ];
189 }
190
191 $sth= $dbh->prepare("SELECT islandid,islandname,archipelago
192                             FROM islands
193                             ORDER BY islandname;");
194 $sth->execute();
195
196 while ($row=$sth->fetchrow_arrayref) {
197         my $arch= $row->[2];
198         push @{ $islandlistdata{'none'} }, [ @$row ];
199         push @{ $islandlistdata{$arch} }, [ @$row ];
200         $islandid2{$row->[0]}= { Name => $row->[1], Arch => $arch };
201 }
202
203 my %resetislandlistdata;
204 foreach my $arch (keys %islandlistdata) {
205         $resetislandlistdata{$arch}=
206                 $optionlistmap->($islandlistdata{$arch}, '');
207 }
208
209 </%perl>
210
211 <input type=hidden name=dropdowns value="<% $a{Dropdowns} %>">
212
213 <script type="text/javascript">
214 ms_lists= <% to_json(\%resetislandlistdata) %>;
215 function ms_Setarch(dd) {
216   debug('ms_SetArch '+dd+' arch='+arch);
217   var arch= document.getElementsByName('archipelago'+dd).item(0).value;
218   var got= ms_lists[arch];
219   if (got == undefined) return; // unknown arch ?  hrm
220   debug('ms_SetArch '+dd+' arch='+arch+' got ok');
221   var select= document.getElementsByName('islandid'+dd).item(0);
222   select.innerHTML= got;
223   debug('ms_SetArch '+dd+' arch='+arch+' innerHTML set');
224 }
225 </script>
226
227 <table style="table-layout:fixed; width:90%;">
228
229 <tr>
230 %       for my $dd (0..$a{Dropdowns}-1) {
231 <td>
232 <select name="archipelago<% $dd %>" onchange="ms_Setarch(<% $dd %>)">
233 <option value="none">Whole ocean</option>
234 <% $optionlistmap->(\@archlistdata, $ARGS{"archipelago$dd"}) %></select></td>
235 %       }
236 </tr>
237
238 <tr>
239 %       for my $dd (0..$a{Dropdowns}-1) {
240 %               my $arch= $ARGS{"archipelago$dd"};
241 %               $arch= 'none' if !defined $arch;
242 <td>
243 <select name="islandid<% $dd %>">
244 <% $optionlistmap->($islandlistdata{$arch}, $ARGS{"islandid$dd"}) %>
245 </select></td>
246 %       }
247 </tr>
248
249 </table>
250
251 % } #---------- end of dropdowns, now common middle of page code ----------
252
253 <input type=submit name=submit value="Go">
254 </form>
255
256 <%perl>
257 #========== result computations ==========
258
259 my $results_head;
260 $results_head= sub {
261         print "<h1>Results</h1>\n";
262         $results_head= sub { };
263 };
264
265 #---------- result computation - textstring ----------
266 if (!$a{Dropdowns}) {
267   if (length $routestring) {
268         $results_head->();
269         my $rsr= $m->comp('routetextstring',
270                 ocean => $a{Ocean},
271                 string => $routestring,
272                 format => 'return'
273         );
274         if (length $rsr->{Error}) {
275                 print encode_entities($rsr->{Error});
276         } else {
277                 foreach my $entry (@{ $rsr->{Results} }) {
278                         push @archipelagoes,
279                                 defined $entry->[1] ? undef : $entry->[0];
280                         push @islandids, $entry->[1];
281                 } 
282         }
283   }
284
285 } else { #---------- results - dropdowns ----------
286
287 my $argorundef= sub {
288         my ($dd,$base) = @_;
289         my $thing= $ARGS{"${base}${dd}"};
290         $thing= undef if defined $thing and $thing eq 'none';
291         return $thing;
292 };
293
294 for my $dd (0..$a{Dropdowns}-1) {
295         my $arch= $argorundef->($dd,'archipelago');
296         my $island= $argorundef->($dd,'islandid');
297         next unless defined $arch or defined $island;
298         if (defined $island and defined $arch) {
299                 my $ii= $islandid2{$island};
300                 my $iarch= $ii->{Arch};
301                 if ($iarch ne $arch) {
302                         $results_head->();
303 </%perl>
304  Specified archipelago <% $arch %> but
305  island <% $ii->{Name} %>
306  which is in <% $iarch %>; using the island.<br>
307 <%perl>
308                 }
309                 $arch= undef;
310         }
311         push @archipelagoes, $arch;
312         push @islandids, $island;
313 }
314
315 }#---------- result processing, common stuff
316 </%perl>
317
318 % if (@islandids) {
319 %       $results_head->();
320
321 <& routetrade, islandids => \@islandids, archipelagoes => \@archipelagoes &>
322
323 % }
324
325 %#---------- debugging and epilogue ----------
326
327 % if ($debug) {
328 <p>
329 <pre id="debug_log">
330 Debug log:
331 </pre>
332 % }
333
334 <script type="text/javascript">
335 function debug (m) {
336 % if ($debug) {
337   var node= document.getElementById('debug_log');
338   node.innerHTML += "\n" + m + "\n";
339 % }
340 }
341 </script>
342
343 <%init>
344 use CommodsWeb;
345 use HTML::Entities;
346 use URI::Escape;
347 use JSON;
348
349 </%init>