chiark / gitweb /
66dee1dd08135de011871b15d71d963f7db235af
[ypp-sc-tools.db-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 $routestring => '';
39 $someresults
40 $emsgokorprint
41 </%args>
42
43 <%perl>
44 my $emsg;
45 my @archipelagoes;
46 my @islandids;
47 my %islandid2;
48
49 my $qa= \%ARGS;
50 </%perl>
51
52 %#---------- textbox, user enters route as string ----------
53 % if (!$qa->{Dropdowns}) {
54
55 <h1>Specify route</h1>
56
57 Enter route (islands, or archipelagoes, separated by |s or commas;
58  abbreviations are OK):<br>
59
60 <form action="<% $quri->() |h %>" method="get">
61
62 <&| qtextstring, qa => $qa, thingstring => 'routestring', emsgstore => \$emsg,
63     perresult => sub {
64         my ($canonname, $island, $arch) = @_;
65         push @islandids, $island;
66         push @archipelagoes, defined $island ? undef : $arch;
67     }
68  &>
69  size=80
70 </&>
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($qa->{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 $dbh->rollback();
124
125 </%perl>
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..$qa->{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..$qa->{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 % my $ours= sub { $_[0] =~ m/^island|^archipelago|^routestring/; };
169 <& "lookup:formhidden", ours => $ours &>
170 </form>
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, islandids => \@islandids, archipelagoes => \@archipelagoes &>
210 % }