chiark / gitweb /
dd8644f1675694d200e28341d0b32bc5d6396422
[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 </%args>
40 <%perl>
41
42 my @archipelagoes;
43 my @islandids;
44 my %islandid2;
45
46 my $qa= \%ARGS;
47 </%perl>
48
49 %#---------- textbox, user enters route as string ----------
50 % if (!$qa->{Dropdowns}) {
51
52 <h1>Specify route</h1>
53
54 <form action="<% $quri->() |h %>" method="get">
55
56 <&| qtextstring, qa => $qa, thingstring => 'routestring' &>
57  size=80
58 </&>
59
60 % } else { #---------- dropdowns, user selects from menus ----------
61
62 <%perl>
63 my ($sth,$row);;
64 my @archlistdata;
65 my %islandlistdata;
66 $islandlistdata{'none'}= [ [ "none", "Select island..." ] ];
67
68 my $optionlistmap= sub {
69         my ($optlist, $selected) = @_;
70         my $out='';
71         foreach my $entry (@$optlist) {
72                 $out.= sprintf('<option value="%s" %s>%s</option>',
73                         encode_entities($entry->[0]),
74                         defined $selected && $entry->[0] eq $selected
75                                 ? 'selected' : '',
76                         encode_entities($entry->[1]));
77         }
78         return $out;
79 };
80
81 my $dbh= dbw_connect($qa->{Ocean});
82
83 $sth= $dbh->prepare("SELECT DISTINCT archipelago FROM islands
84                             ORDER BY archipelago;");
85 $sth->execute();
86
87 while ($row=$sth->fetchrow_arrayref) {
88         my ($arch)= @$row;
89         push @archlistdata, [ $arch, $arch ];
90         $islandlistdata{$arch}= [ [ "none", "Whole arch" ] ];
91 }
92
93 $sth= $dbh->prepare("SELECT islandid,islandname,archipelago
94                             FROM islands
95                             ORDER BY islandname;");
96 $sth->execute();
97
98 while ($row=$sth->fetchrow_arrayref) {
99         my $arch= $row->[2];
100         push @{ $islandlistdata{'none'} }, [ @$row ];
101         push @{ $islandlistdata{$arch} }, [ @$row ];
102         $islandid2{$row->[0]}= { Name => $row->[1], Arch => $arch };
103 }
104
105 my %resetislandlistdata;
106 foreach my $arch (keys %islandlistdata) {
107         $resetislandlistdata{$arch}=
108                 $optionlistmap->($islandlistdata{$arch}, '');
109 }
110
111 $dbh->rollback();
112
113 </%perl>
114
115 <input type=hidden name=dropdowns value="<% $qa->{Dropdowns} |h %>">
116
117 <&| script &>
118 ms_lists= <% to_json_protecttags(\%resetislandlistdata) %>;
119 function ms_Setarch(dd) {
120   debug('ms_SetArch '+dd+' arch='+arch);
121   var arch= document.getElementsByName('archipelago'+dd).item(0).value;
122   var got= ms_lists[arch];
123   if (got == undefined) return; // unknown arch ?  hrm
124   debug('ms_SetArch '+dd+' arch='+arch+' got ok');
125   var select= document.getElementsByName('islandid'+dd).item(0);
126   select.innerHTML= got;
127   debug('ms_SetArch '+dd+' arch='+arch+' innerHTML set');
128 }
129 </&script>
130
131 <table style="table-layout:fixed; width:90%;">
132
133 <tr>
134 %       for my $dd (0..$qa->{Dropdowns}-1) {
135 <td>
136 <select name="archipelago<% $dd %>" onchange="ms_Setarch(<% $dd %>)">
137 <option value="none">Whole ocean</option>
138 <% $optionlistmap->(\@archlistdata, $ARGS{"archipelago$dd"}) %></select></td>
139 %       }
140 </tr>
141
142 <tr>
143 %       for my $dd (0..$qa->{Dropdowns}-1) {
144 %               my $arch= $ARGS{"archipelago$dd"};
145 %               $arch= 'none' if !defined $arch;
146 <td>
147 <select name="islandid<% $dd %>">
148 <% $optionlistmap->($islandlistdata{$arch}, $ARGS{"islandid$dd"}) %>
149 </select></td>
150 %       }
151 </tr>
152
153 </table>
154
155 % } #---------- end of dropdowns, now common middle of page code ----------
156
157 <input type=submit name=submit value="Go">
158 </form>
159
160 <%perl>
161 #========== result computations ==========
162
163 my $results_head;
164 $results_head= sub {
165         print "<h1>Results</h1>\n";
166         $results_head= sub { };
167 };
168
169 #---------- result computation - textstring ----------
170 if (!$qa->{Dropdowns}) {
171   if (length $routestring) {
172         $results_head->();
173         my ($emsg,$canonstring,$results)= $m->comp('qtextstringcheck',
174                 what => 'routestring',
175                 ocean => $qa->{Ocean},
176                 string => $routestring,
177                 format => 'return'
178         );
179         if (length $emsg) {
180                 print encode_entities($emsg);
181         } else {
182                 foreach my $entry (@$results) {
183                         push @islandids, $entry->[1];
184                         push @archipelagoes,
185                                 defined $entry->[1] ? undef : $entry->[2];
186                 } 
187         }
188   }
189
190 } else { #---------- results - dropdowns ----------
191
192 my $argorundef= sub {
193         my ($dd,$base) = @_;
194         my $thing= $ARGS{"${base}${dd}"};
195         $thing= undef if defined $thing and $thing eq 'none';
196         return $thing;
197 };
198
199 for my $dd (0..$qa->{Dropdowns}-1) {
200         my $arch= $argorundef->($dd,'archipelago');
201         my $island= $argorundef->($dd,'islandid');
202         next unless defined $arch or defined $island;
203         if (defined $island and defined $arch) {
204                 my $ii= $islandid2{$island};
205                 my $iarch= $ii->{Arch};
206                 if ($iarch ne $arch) {
207                         $results_head->();
208 </%perl>
209  Specified archipelago <% $arch %> but
210  island <% $ii->{Name} %>
211  which is in <% $iarch %>; using the island.<br>
212 <%perl>
213                 }
214                 $arch= undef;
215         }
216         push @archipelagoes, $arch;
217         push @islandids, $island;
218 }
219
220 }#---------- result processing, common stuff
221 </%perl>
222
223 % if (@islandids) {
224 %       $results_head->();
225
226 <& routetrade, islandids => \@islandids, archipelagoes => \@archipelagoes &>
227
228 % }