chiark / gitweb /
Split lookup options into separate query_ components
[ypp-sc-tools.web-live.git] / yarrg / web / lookup
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 main `lookup' page, including
33  all the entry boxes etc. for every query.
34
35
36 </%doc>
37 <%perl>
38 my %a;
39 my %ahtml;
40 my @vars;
41
42 #---------- "mode" argument parsing and mode menu at top of page ----------
43
44 # for debugging, invoke as
45 #  http://www.chiark.greenend.org.uk/ucgi/~clareb/mason/pirates/pirate-route?debug=1
46
47 @vars= ({       Name => 'Ocean',
48                 Before => 'Ocean: ',
49                 CmpCanon => sub { ucfirst lc $_[0] },
50                 Values => [ ocean_list() ]
51         }, {    Name => 'Dropdowns',
52                 Before => 'Interface: ',
53                 CmpCanon => sub { !!$_[0] },
54                 Values => [     [ 0, 'Type in names' ],
55                                 [ 4, 'Select from menus' ] ]
56         }, {    Name => 'Query',
57                 Before => 'Query: ',
58                 Values => [     [ 'route', 'Trades for route' ],
59                                 [ 'age', 'Data age' ] ]
60         });
61
62 foreach my $var (@vars) {
63         my $name= $var->{Name};
64         my $lname= lc $name;
65         $var->{Before}= '' unless exists $var->{Before};
66         $var->{CmpCanon}= sub { $_[0]; } unless exists $var->{CmpCanon};
67         foreach my $val (@{ $var->{Values} }) {
68                 next if ref $val;
69                 $val= [ $val, encode_entities($val) ];
70         }
71         if (exists $ARGS{$lname}) {
72                 $a{$name}= $ARGS{$lname};
73                 my @html= grep { $_->[0] eq $a{$name} } @{ $var->{Values} };
74                 $ahtml{$name}= @html==1 ? $html[0][1] : '???';
75         } else {
76                 $a{$name}= $var->{Values}[0][0];
77                 $ahtml{$name}= $var->{Values}[0][1];
78         }
79 }
80
81 </%perl>
82 <html><head><title><% ucfirst $ahtml{Query} %> - YARRG</title></head><body>
83
84 <a href="<% $m->current_comp()->name() |u %>">YARRG</a> -
85  Yet Another Revenue Research Gatherer
86 |
87 <a href="docs">documentation</a>
88 <p>
89 <%perl>
90
91 my %baseqf;
92 foreach my $var (@vars) {
93         my $lname= lc $var->{Name};
94         next unless exists $ARGS{$lname};
95         $baseqf{$lname}= $ARGS{$lname};
96 }
97
98 my %queryqf;
99 foreach my $var (keys %ARGS) {
100         next unless $var =~
101                 m/^(?:routestring|islandid\d|archipelago\d|debug)$/;
102         my $val= $ARGS{$var};
103         next if $val eq 'none';
104         $queryqf{$var}= $val;
105 }
106
107 my $quri= sub {
108         my $uri= URI->new('lookup');
109         $uri->query_form(@_);
110         $uri->path_query();
111 };
112
113 foreach my $var (@vars) {
114         my $name= $var->{Name};
115         my $lname= lc $var->{Name};
116         my $delim= $var->{Before};
117         my $canon= &{$var->{CmpCanon}}($a{$name});
118         my $cvalix= 0;
119         foreach my $valr (@{ $var->{Values} }) {
120                 print $delim;  $delim= "\n|\n";
121                 my ($value,$html) = @$valr;
122                 my $iscurrent= &{$var->{CmpCanon}}($value) eq $canon;
123                 my $after;
124                 if ($iscurrent) {
125                         print '<b>';
126                         $after= '</b>';
127                 } else {
128                         my %qf= (%baseqf,%queryqf);
129                         delete $qf{$lname};
130                         $qf{$lname}= $value if $cvalix;
131 </%perl>
132 <a href="<% $quri->(%qf) |h %>">
133 <%perl>
134                         $after= '</a>';
135                 }
136                 print $html, $after;
137                 $cvalix++;
138         }
139         print "<p>\n\n";
140 }
141
142 #---------- initial checks, startup, main entry form ----------
143
144 dbw_connect($a{Ocean});
145
146 </%perl>
147 <%args>
148 $debug => 0
149 </%args>
150
151 <hr>
152
153 %########### query `route' ##########
154 % if ($a{Query} eq 'route') {
155
156 <& query_route, %baseqf, %queryqf, quri => $quri, a => \%a &>
157
158 % } elsif ($a{Query} eq 'age') {
159 % ########### query `age' ##########
160
161 <& query_age, %baseqf, %queryqf &>
162
163 % } ########## end of `age' query ##########
164
165 %#---------- debugging and epilogue ----------
166
167 % if ($debug) {
168 <p>
169 <pre id="debug_log">
170 Debug log:
171 </pre>
172 % }
173
174 <&| script &>
175 function debug (m) {
176 % if ($debug) {
177   var node= document.getElementById('debug_log');
178   node.innerHTML += "\n" + m + "\n";
179 % }
180 }
181 </&script>
182
183 <& footer &>
184
185 <%init>
186 use CommodsWeb;
187 use HTML::Entities;
188 use URI::Escape;
189
190 </%init>