chiark / gitweb /
18c3ecb1390af1f2d3a3fc4f2108d2a64f5c90ce
[ypp-sc-tools.web-live.git] / yarrg / web / query_route
1 <%doc>
2
3  This is part of the YARRG website.  YARRG is a tool and website
4  for assisting players of Yohoho Puzzle Pirates.
5
6  Copyright (C) 2009 Ian Jackson <ijackson@chiark.greenend.org.uk>
7  Copyright (C) 2009 Clare Boothby
8
9   YARRG's client code etc. is covered by the ordinary GNU GPL (v3 or later).
10   The YARRG website is covered by the GNU Affero GPL v3 or later, which
11    basically means that every installation of the website will let you
12    download the source.
13
14  This program is free software: you can redistribute it and/or modify
15  it under the terms of the GNU Affero General Public License as
16  published by the Free Software Foundation, either version 3 of the
17  License, or (at your option) any later version.
18
19  This program is distributed in the hope that it will be useful,
20  but WITHOUT ANY WARRANTY; without even the implied warranty of
21  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  GNU Affero General Public License for more details.
23
24  You should have received a copy of the GNU Affero General Public License
25  along with this program.  If not, see <http://www.gnu.org/licenses/>.
26
27  Yohoho and Puzzle Pirates are probably trademarks of Three Rings and
28  are used without permission.  This program is not endorsed or
29  sponsored by Three Rings.
30
31
32  This Mason component generates the core of the `trade route' query.
33
34
35 </%doc>
36 <%args>
37 $quri
38 $dbh
39 $prselector
40 $routestring => '';
41 $someresults
42 $emsgokorprint
43 </%args>
44
45 <%perl>
46 my $emsg;
47 my @archipelagoes;
48 my @islandids;
49 my %islandid2;
50
51 my $qa= \%ARGS;
52 </%perl>
53
54 <h1>Specify route</h1>
55
56 % $prselector->('ShowStalls');
57
58 %#---------- textbox, user enters route as string ----------
59 % if (!$qa->{Dropdowns}) {
60
61 Enter route (islands, or archipelagoes, separated by |s or commas;
62  abbreviations are OK):<br>
63
64 <form action="<% $quri->() |h %>" method="get">
65
66 <&| qtextstring, qa => $qa, dbh => $dbh,
67     thingstring => 'routestring', emsgstore => \$emsg,
68     perresult => sub {
69         my ($canonname, $island, $arch) = @_;
70         push @islandids, $island;
71         push @archipelagoes, defined $island ? undef : $arch;
72     }
73  &>
74  size=80
75 </&>
76
77 % } else { #---------- dropdowns, user selects from menus ----------
78
79 <%perl>
80 my ($sth,$row);
81 my @archlistdata;
82 my %islandlistdata;
83 $islandlistdata{'none'}= [ [ "none", "Select island..." ] ];
84
85 my $optionlistmap= sub {
86         my ($optlist, $selected) = @_;
87         my $out='';
88         foreach my $entry (@$optlist) {
89                 $out.= sprintf('<option value="%s" %s>%s</option>',
90                         encode_entities($entry->[0]),
91                         defined $selected && $entry->[0] eq $selected
92                                 ? 'selected' : '',
93                         encode_entities($entry->[1]));
94         }
95         return $out;
96 };
97
98 $sth= $dbh->prepare("SELECT DISTINCT archipelago FROM islands
99                             ORDER BY archipelago;");
100 $sth->execute();
101
102 while ($row=$sth->fetchrow_arrayref) {
103         my ($arch)= @$row;
104         push @archlistdata, [ $arch, $arch ];
105         $islandlistdata{$arch}= [ [ "none", "Whole arch" ] ];
106 }
107
108 $sth= $dbh->prepare("SELECT islandid,islandname,archipelago
109                             FROM islands
110                             ORDER BY islandname;");
111 $sth->execute();
112
113 while ($row=$sth->fetchrow_arrayref) {
114         my $arch= $row->[2];
115         push @{ $islandlistdata{'none'} }, [ @$row ];
116         push @{ $islandlistdata{$arch} }, [ @$row ];
117         $islandid2{$row->[0]}= { Name => $row->[1], Arch => $arch };
118 }
119
120 my %resetislandlistdata;
121 foreach my $arch (keys %islandlistdata) {
122         $resetislandlistdata{$arch}=
123                 $optionlistmap->($islandlistdata{$arch}, '');
124 }
125
126 </%perl>
127
128 <&| script &>
129 ms_lists= <% to_json_protecttags(\%resetislandlistdata) %>;
130 function ms_Setarch(dd) {
131   debug('ms_SetArch '+dd+' arch='+arch);
132   var arch= document.getElementsByName('archipelago'+dd).item(0).value;
133   var got= ms_lists[arch];
134   if (got == undefined) return; // unknown arch ?  hrm
135   debug('ms_SetArch '+dd+' arch='+arch+' got ok');
136   var select= document.getElementsByName('islandid'+dd).item(0);
137   select.innerHTML= got;
138   debug('ms_SetArch '+dd+' arch='+arch+' innerHTML set');
139 }
140 </&script>
141
142 <table style="table-layout:fixed; width:90%;">
143
144 <tr>
145 %       for my $dd (0..$qa->{Dropdowns}-1) {
146 <td>
147 <select name="archipelago<% $dd %>" onchange="ms_Setarch(<% $dd %>)">
148 <option value="none">Whole ocean</option>
149 <% $optionlistmap->(\@archlistdata, $ARGS{"archipelago$dd"}) %></select></td>
150 %       }
151 </tr>
152
153 <tr>
154 %       for my $dd (0..$qa->{Dropdowns}-1) {
155 %               my $arch= $ARGS{"archipelago$dd"};
156 %               $arch= 'none' if !defined $arch;
157 <td>
158 <select name="islandid<% $dd %>">
159 <% $optionlistmap->($islandlistdata{$arch}, $ARGS{"islandid$dd"}) %>
160 </select></td>
161 %       }
162 </tr>
163
164 </table>
165
166 % } #---------- end of dropdowns, now common middle of page code ----------
167
168 <input type=submit name=submit value="Go">
169 % my $ours= sub { $_[0] =~ m/^island|^archipelago|^routestring|^rtsel[ag]_/; };
170 <& "lookup:formhidden", ours => $ours &>
171
172 <%perl>
173 #========== results ==========
174
175 $emsgokorprint->($emsg) or @islandids=();
176
177 my $argorundef= sub {
178         my ($dd,$base) = @_;
179         my $thing= $ARGS{"${base}${dd}"};
180         $thing= undef if defined $thing and $thing eq 'none';
181         return $thing;
182 };
183
184 for my $dd (0..$qa->{Dropdowns}-1) {
185         my $arch= $argorundef->($dd,'archipelago');
186         my $island= $argorundef->($dd,'islandid');
187         next unless defined $arch or defined $island;
188         if (defined $island and defined $arch) {
189                 my $ii= $islandid2{$island};
190                 my $iarch= $ii->{Arch};
191                 if ($iarch ne $arch) {
192                         $someresults->();
193 </%perl>
194  Specified archipelago <% $arch %> but
195  island <% $ii->{Name} %>
196  which is in <% $iarch %>; using the island.<br>
197 <%perl>
198                 }
199                 $arch= undef;
200         }
201         push @archipelagoes, $arch;
202         push @islandids, $island;
203 }
204
205 </%perl>
206
207 % if (@islandids) {
208 %       $someresults->();
209 <& routetrade,
210    dbh => $dbh,
211    islandids => \@islandids,
212    archipelagoes => \@archipelagoes,
213    qa => $qa
214  &>
215 </form>
216 % }