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