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