chiark / gitweb /
f4ad1d7ed003283e80aabc2babeda6d4f8414800
[ypp-sc-tools.db-test.git] / yarrg / web / enter_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 form contents for selecting a list
33  of locations (eg, a route).
34
35
36 </%doc>
37 <%args>
38 $qa
39 $dbh
40 $emsg_r
41 $warningfs_r
42
43 $enterwhat
44 $islandids_r
45 $archipelagoes_r
46 </%args>
47
48 %#---------- textbox, user enters route as string ----------
49 % if (!$qa->{Dropdowns}) {
50
51 <% $enterwhat %> (islands, or archipelagoes, separated by |s or commas;
52  abbreviations are OK):<br>
53
54 <&| qtextstring, qa => $qa, dbh => $dbh,
55     thingstring => 'routestring', emsgstore => $emsg_r,
56     onresults => sub {
57         foreach (@_) {
58         my ($canonname, $island, $arch) = @$_;
59                 push @$islandids_r, $island;
60                 push @$archipelagoes_r, defined $island ? undef : $arch;
61         }
62     }
63  &>
64  size=80
65 </&>
66
67 % } else { #---------- dropdowns, user selects from menus ----------
68
69 <%perl>
70 my %islandid2;
71 my ($sth,$row);
72 my @archlistdata;
73 my %islandlistdata;
74 $islandlistdata{'none'}= [ [ "none", "Select island..." ] ];
75
76 my $optionlistmap= sub {
77         my ($optlist, $selected) = @_;
78         my $out='';
79         foreach my $entry (@$optlist) {
80                 $out.= sprintf('<option value="%s" %s>%s</option>',
81                         encode_entities($entry->[0]),
82                         defined $selected && $entry->[0] eq $selected
83                                 ? 'selected' : '',
84                         encode_entities($entry->[1]));
85         }
86         return $out;
87 };
88
89 $sth= $dbh->prepare("SELECT DISTINCT archipelago FROM islands
90                             ORDER BY archipelago;");
91 $sth->execute();
92
93 while ($row=$sth->fetchrow_arrayref) {
94         my ($arch)= @$row;
95         push @archlistdata, [ $arch, $arch ];
96         $islandlistdata{$arch}= [ [ "none", "Whole arch" ] ];
97 }
98
99 $sth= $dbh->prepare("SELECT islandid,islandname,archipelago
100                             FROM islands
101                             ORDER BY islandname;");
102 $sth->execute();
103
104 while ($row=$sth->fetchrow_arrayref) {
105         my $arch= $row->[2];
106         push @{ $islandlistdata{'none'} }, [ @$row ];
107         push @{ $islandlistdata{$arch} }, [ @$row ];
108         $islandid2{$row->[0]}= { Name => $row->[1], Arch => $arch };
109 }
110
111 my %resetislandlistdata;
112 foreach my $arch (keys %islandlistdata) {
113         $resetislandlistdata{$arch}=
114                 $optionlistmap->($islandlistdata{$arch}, '');
115 }
116
117 </%perl>
118
119 <&| script &>
120 ms_lists= <% to_json_protecttags(\%resetislandlistdata) %>;
121 function ms_Setarch(dd) {
122   debug('ms_SetArch '+dd+' arch='+arch);
123   var arch= document.getElementsByName('archipelago'+dd).item(0).value;
124   var got= ms_lists[arch];
125   if (got == undefined) return; // unknown arch ?  hrm
126   debug('ms_SetArch '+dd+' arch='+arch+' got ok');
127   var select= document.getElementsByName('islandid'+dd).item(0);
128   select.innerHTML= got;
129   debug('ms_SetArch '+dd+' arch='+arch+' innerHTML set');
130 }
131 </&script>
132
133 <table style="table-layout:fixed; width:90%;">
134
135 <tr>
136 %       for my $dd (0..$qa->{Dropdowns}-1) {
137 <td>
138 <select name="archipelago<% $dd %>" onchange="ms_Setarch(<% $dd %>)">
139 <option value="none">Whole ocean</option>
140 <% $optionlistmap->(\@archlistdata, $qa->{"archipelago$dd"}) %></select></td>
141 %       }
142 </tr>
143
144 <tr>
145 %       for my $dd (0..$qa->{Dropdowns}-1) {
146 %               my $arch= $qa->{"archipelago$dd"};
147 %               $arch= 'none' if !defined $arch;
148 <td>
149 <select name="islandid<% $dd %>">
150 <% $optionlistmap->($islandlistdata{$arch}, $qa->{"islandid$dd"}) %>
151 </select></td>
152 %       }
153 </tr>
154
155 </table>
156
157 <%perl>
158
159 my $argorundef= sub {
160         my ($dd,$base) = @_;
161         my $thing= $qa->{"${base}${dd}"};
162         $thing= undef if defined $thing and $thing eq 'none';
163         return $thing;
164 };
165
166 for my $dd (0..$qa->{Dropdowns}-1) {
167         my $arch= $argorundef->($dd,'archipelago');
168         my $island= $argorundef->($dd,'islandid');
169         next unless defined $arch or defined $island;
170         if (defined $island and defined $arch) {
171                 my $ii= $islandid2{$island};
172                 my $iarch= $ii->{Arch};
173                 if ($iarch ne $arch) {
174                         push @$warningfs_r, sub {
175 </%perl>
176  Specified archipelago <% $arch %> but
177  island <% $ii->{Name} %>
178  which is in <% $iarch %>; using the island.<p>
179 <%perl>
180                         };
181                 }
182                 $arch= undef;
183         }
184         push @$archipelagoes_r, $arch;
185         push @$islandids_r, $island;
186 }
187
188 </%perl>
189 <p>
190
191 % }