aboutsummaryrefslogtreecommitdiffstats
path: root/web/index.cgi
blob: ac35a1faeaa1a78026876a0794092bf9666ef11d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
#!/usr/bin/perl -w

# index.pl:
# Main code for BCI - not really.
#
# Copyright (c) 2006 UK Citizens Online Democracy. All rights reserved.
# Email: matthew@mysociety.org. WWW: http://www.mysociety.org
#
# $Id: index.cgi,v 1.17 2006-09-22 14:30:16 matthew Exp $

use strict;
require 5.8.0;

# Horrible boilerplate to set up appropriate library paths.
use FindBin;
use lib "$FindBin::Bin/../perllib";
use lib "$FindBin::Bin/../../perllib";
use Error qw(:try);
use LWP::Simple;
use RABX;

use Page;
use mySociety::Config;
BEGIN {
    mySociety::Config::set_file("$FindBin::Bin/../conf/general");
}
use mySociety::MaPit;
mySociety::MaPit::configure();
use mySociety::Web qw(ent NewURL);

# Main code for index.cgi
sub main {
    my $q = shift;

    my $out = '';
    if ($q->param('submit_problem')) {
        $out = submit_problem($q);
    } elsif ($q->param('submit_comment')) {
        $out = submit_comment($q);
    } elsif ($q->param('map')) {
        $out = display_form($q);
    } elsif ($q->param('pc')) {
        $out = display($q);
    } elsif ($q->param('id')) {
        $out = display_problem($q);
    } else {
        $out = front_page();
    }

    print Page::header($q, '');
    print $out;
    print Page::footer($q);
}
Page::do_fastcgi(\&main);

# Display front page
sub front_page {
    my $error = shift;
    my $out = '';
    $out .= '<p id="error">' . $error . '</p>' if ($error);
    $out .= <<EOF;
<p>Report a problem with refuse, recycling, fly tipping, pest control,
abandoned vechicles, street lighting, graffiti, street cleaning, litter or
similar to your local council. Or view reports other people have made.</p>

<p><strong>This is currently only for Newham and Lewisham Councils</strong></p>

<p>It&rsquo;s very simple:</p>

<ol>
<li>Enter a postcode;
<li>Locate the problem on a high-scale map;
<li>Enter details of the problem;
<li>Submit to your council.
</ol>

<form action="./" method="get">
<p>Enter your postcode: <input type="text" name="pc" value="">
<input type="submit" value="Go">
</form>
EOF
    return $out;
}

sub submit_comment {
    my $q = shift;
    my @vars = qw(id name email comment updates);
    my %input = map { $_ => $q->param($_) } @vars;
    my @errors;
    push(@errors, 'Please enter a comment') unless $input{comment};
    push(@errors, 'Please enter your name') unless $input{name};
    push(@errors, 'Please enter your email') unless $input{email};
    return display_problem($q, @errors) if (@errors);

    # Send confirmation email

    my $out = <<EOF;
<h2>Nearly Done! Now check your email...</h2>
<p>The confirmation email <strong>may</strong> take a few minutes to arrive &mdash; <em>please</em> be patient.</p>
<p>If you use web-based email or have 'junk mail' filters, you may wish to check your bulk/spam mail folders: sometimes, our messages are marked that way.</p>
<p>You must now click on the link within the email we've just sent you -
<br>if you do not, your comment will not be posted.</p>
<p>(Don't worry - we'll hang on to your comment while you're checking your email.)</p>
EOF
    return $out;
}

sub submit_problem {
    my $q = shift;
    my @vars = qw(title detail name email pc easting northing updates);
    my %input = map { $_ => $q->param($_) } @vars;
    my @errors;
    push(@errors, 'Please enter a title') unless $input{title};
    push(@errors, 'Please enter some details') unless $input{detail};
    push(@errors, 'Please enter your name') unless $input{name};
    push(@errors, 'Please enter your email') unless $input{email};
    return display_form($q, @errors) if (@errors);

    # Send confirmation email

    my $out = <<EOF;
<h2>Nearly Done! Now check your email...</h2>
<p>The confirmation email <strong>may</strong> take a few minutes to arrive &mdash; <em>please</em> be patient.</p>
<p>If you use web-based email or have 'junk mail' filters, you may wish to check your bulk/spam mail folders: sometimes, our messages are marked that way.</p>
<p>You must now click on the link within the email we've just sent you -
<br>if you do not, your problem will not be posted on the site.</p>
<p>(Don't worry - we'll hang on to your message while you're checking your email.)</p>
EOF
    return $out;
}

sub display_form {
    my ($q, @errors) = @_;
    my ($pin_x, $pin_y, $pin_tile_x, $pin_tile_y);
    my @vars = qw(title detail name email updates pc easting northing x y skipped);
    my %input = map { $_ => $q->param($_) || '' } @vars;
    my %input_h = map { $_ => $q->param($_) ? ent($q->param($_)) : '' } @vars;
    my @ps = $q->param;
    foreach (@ps) {
        ($pin_tile_x, $pin_tile_y, $pin_x) = ($1, $2, $q->param($_)) if /^tile_(\d+)\.(\d+)\.x$/;
        $pin_y = $q->param($_) if /\.y$/;
    }
    return display($q)
        unless $input{skipped} || ($pin_x && $pin_y)
	    || ($input{easting} && $input{northing});

    my $out = '';
    $out .= '<h2>Reporting a problem</h2>';
    if ($input{skipped}) {
        $out .= '<p>Please fill in the form below with details of the problem:</p>';
    } else {
        my ($px, $py, $easting, $northing);
        if ($pin_x && $pin_y) {
            $pin_x -= 254 while $pin_x > 254;
            $pin_y -= 254 while $pin_y > 254;
            $pin_y = 254 - $pin_y;
            $px = 508 - ($pin_tile_x - $input{x})*254 - $pin_x;
            $py = 508 - ($pin_tile_y - $input{y})*254 - $pin_y;
            $easting = 5000/31 * ($pin_tile_x + $pin_x/254);
            $northing = 5000/31 * ($pin_tile_y + $pin_y/254);
        } else {
            $px = 508 - ($input{easting} / (5000/31) - $input{x})*254;
	    $py = 508 - ($input{northing} / (5000/31) - $input{y})*254;
	    $easting = $input_h{easting};
	    $northing = $input_h{northing};
	}
        $out .= display_map($q, $input{x}, $input{y}, 1, 0);
        $out .= '<p>You have located the problem at the location marked with a yellow pin on the map. If this is not the correct location, simply click on the map again.</p>
<p>Please fill in details of the problem below:</p>';
        $out .= display_pin($px, $py, 'yellow');
        $out .= '<input type="hidden" name="easting" value="' . $easting . '">
<input type="hidden" name="northing" value="' . $northing . '">';
    }

    if (@errors) {
        $out .= '<ul id="error"><li>' . join('</li><li>', @errors) . '</li></ul>';
    }
    my $updates = $input{updates} ? ' checked' : '';
    $out .= <<EOF;
<fieldset>
<div><label for="form_title">Title:</label>
<input type="text" value="$input_h{title}" name="title" id="form_title" size="30"> (work out from details?)</div>
<div><label for="form_detail">Details:</label>
<textarea name="detail" id="form_detail" rows="7" cols="30">$input_h{detail}</textarea></div>
<div><label for="form_name">Name:</label>
<input type="text" value="$input_h{name}" name="name" id="form_name" size="30"></div>
<div><label for="form_email">Email:</label>
<input type="text" value="$input_h{email}" name="email" id="form_email" size="30"></div>
<div class="checkbox"><input type="checkbox" name="updates" id="form_updates" value="1"$updates>
<label for="form_updates">Receive updates about this problem</label></div>
<div class="checkbox"><input type="submit" name="submit_problem" value="Submit"></div>
</fieldset>
EOF
    $out .= display_map_end(1);
    return $out;
}

sub display {
    my $q = shift;

    my $pc = $q->param('pc');
    my($error, $x, $y, $name);
    try {
        ($name, $x, $y) = postcode_check($q, $pc);
    } catch RABX::Error with {
        my $e = shift;
        if ($e->value() == mySociety::MaPit::BAD_POSTCODE
           || $e->value() == mySociety::MaPit::POSTCODE_NOT_FOUND) {
            $error = 'That postcode was not recognised, sorry.';
        } else {
            $error = $e;
        }
    };
    return front_page($error) if ($error);

    my $out = "<h2>$name</h2>";
    $out .= display_map($q, $x, $y, 1, 1);
    $out .= <<EOF;
<p>To report a problem, please select the location of it on the map below.
Use the arrows to the left of the map to scroll around.</p>
<p>Or just view existing problems that have already been reported.</p>
EOF
    $out .= <<EOF;
    <div>
    <h2>Current problems</h2>
    <ul id="current">
EOF
    my %current = (
        1 => 'Broken lamppost',
        2 => 'Shards of glass',
        3 => 'Abandoned car',
    );
    my %fixed = (
        4 => 'Broken lamppost',
        5 => 'Shards of glass',
        6 => 'Abandoned car',
    );
    foreach (sort keys %current) {
        my $px = int(rand(508));
        my $py = int(rand(508));
        $out .= '<li><a href="/?id=' . $_ . '">';
        $out .= display_pin($px, $py);
        $out .= $current{$_};
        $out .= '</a></li>';
    }
    $out .= <<EOF;
    </ul>
    <h2>Recently fixed problems</h2>
    <ul>
EOF
    foreach (sort keys %fixed) {
        $out .= '<li><a href="/?id=' . $_ . '">';
        $out .= $fixed{$_};
        $out .= '</a></li>';
    }
    my $skipurl = NewURL($q, 'map'=>1, skipped=>1);
    $out .= '</ul></div>';
    $out .= display_map_end(1);
    $out .= <<EOF;
<p>If you cannot see a map &ndash; if you have images turned off,
or are using a text only browser, for example &ndash; please
<a href="$skipurl">skip this step</a> and we will ask you
to describe the location of your problem instead.</p>
EOF
    return $out;
}

sub display_pin {
    my ($px, $py, $col) = @_;
    $col = 'red' unless $col;
    return '' if ($px<0 || $px>508 || $py<0 || $py>508);
    return '<img src="/i/pin_'.$col.'.png" alt="Problem" style="top:' . ($py-20) . 'px; right:' . ($px-6) . 'px; position: absolute;">';
}

sub display_problem {
    my ($q, @errors) = @_;

    my @vars = qw(id name email comment updates x y);
    my %input = map { $_ => $q->param($_) } @vars;
    my %input_h = map { $_ => $q->param($_) ? ent($q->param($_)) : '' } @vars;
    $input{x} += 0;
    $input{y} += 0;

    # Get all information from database
    my $title = 'Broken lamppost';
    my $desc = 'The bulb has clearly gone. This is a shame, as there are no other lampposts nearby, so the whole area is dark.';
    my $name = 'Matthew Somerville';
    my $time = '5.30pm, yesterday';
    my $easting = 541120;
    my $northing = 185450;
    my $x = $easting / (5000/31);
    my $y = $northing / (5000/31);
    my $x_tile = $input{x} || int($x);
    my $y_tile = $input{y} || int($y);
    my $created = time();

    my $py = 508 - 254 * ($y - $y_tile);
    my $px = 508 - 254 * ($x - $x_tile);

    my $out = '';
    $out .= "<h2>$title</h2>";
    $out .= display_map($q, $x_tile, $y_tile, 0, 1);

    # Display information about problem
    $out .= '<p>';
    $out .= display_pin($px, $py);
    $out .= '<em>Reported by ' . $name . ' at ' . $time;
    $out .= '</em></p> <p>';
    $out .= ent($desc);
    $out .= '</p>';

    # Display comments
    my $comments = '';
    if ($comments) {
        $out .= '<h3>Comments</h3>';
    }
    $out .= '<h3>Add Comment</h3>';
    if (@errors) {
        $out .= '<ul id="error"><li>' . join('</li><li>', @errors) . '</li></ul>';
    }
    my $updates = $input{updates} ? ' checked' : '';
    $out .= <<EOF;
<form method="post" action="./">
<fieldset>
<input type="hidden" name="submit_comment" value="1">
<input type="hidden" name="id" value="$input_h{id}">
<div><label for="form_name">Name:</label>
<input type="text" name="name" id="form_name" value="$input_h{name}" size="30"></div>
<div><label for="form_email">Email:</label>
<input type="text" name="email" id="form_email" value="$input_h{email}" size="30"> (needed?)</div>
<div><label for="form_comment">Comment:</label>
<textarea name="comment" id="form_comment" rows="7" cols="30">$input_h{comment}</textarea></div>
<div class="checkbox"><input type="checkbox" name="updates" id="form_updates" value="1"$updates>
<label for="form_updates">Receive updates about this problem</label></div>
<div class="checkbox"><input type="submit" value="Post"></div>
</fieldset>
</form>
EOF
    $out .= display_map_end(0);
    return $out;
}

# display_map Q X Y TYPE COMPASS
# X,Y is bottom left tile of 2x2 grid
# TYPE is 1 if the map is clickable, 0 if not
# COMPASS is 1 to show the compass, 0 to not
sub display_map {
    my ($q, $x, $y, $type, $compass) = @_;
    my $url = mySociety::Config::get('TILES_URL');
    my $tiles = $url . $x . '-' . ($x+1) . ',' . $y . '-' . ($y+1) . '/RABX';
    $tiles = LWP::Simple::get($tiles);
    my $tileids = RABX::unserialise($tiles);
    my $tl = $x . '.' . ($y+1);
    my $tr = ($x+1) . '.' . ($y+1);
    my $bl = $x . '.' . $y;
    my $br = ($x+1) . '.' . $y;
    my $tl_src = $url . $tileids->[0][0];
    my $tr_src = $url . $tileids->[0][1];
    my $bl_src = $url . $tileids->[1][0];
    my $br_src = $url . $tileids->[1][1];

    my $out = '';
    my $img_type;
    if ($type) {
        my $pc_enc = ent($q->param('pc'));
        $out .= <<EOF;
<form action="./" method="get">
<input type="hidden" name="map" value="1">
<input type="hidden" name="x" value="$x">
<input type="hidden" name="y" value="$y">
<input type="hidden" name="pc" value="$pc_enc">
EOF
        $img_type = '<input type="image"';
    } else {
        $img_type = '<img';
    }
    $out .= <<EOF;
<div id="relativediv">
    <div id="map">
        $img_type id="2.2" name="tile_$tl" src="$tl_src" style="top:0px; left:0px;">$img_type id="3.2" name="tile_$tr" src="$tr_src" style="top:0px; left:254px;"><br>$img_type id="2.3" name="tile_$bl" src="$bl_src" style="top:254px; left:0px;">$img_type id="3.3" name="tile_$br" src="$br_src" style="top:254px; left:254px;">
    </div>
EOF
    $out .= Page::compass($q, $x, $y) if $compass;
    $out .= '<div id="content">';
    return $out;
}

sub display_map_end {
    my ($type) = @_;
    my $out = '</div></div>';
    $out .= '</form>' if ($type);
    return $out;
}

# Checks the postcode is in one of the two London boroughs
# and sets default X/Y co-ordinates if not provided in the URI
sub postcode_check {
    my ($q, $pc) = @_;
    my $areas;
    $areas = mySociety::MaPit::get_voting_areas($pc);

    # Check for London Borough
    throw RABX::Error("I'm afraid that postcode isn't in our covered area.", 123456) if (!$areas || !$areas->{LBO});

    # Check for Lewisham or Newham
    my $lbo = $areas->{LBO};
    throw RABX::Error("I'm afraid that postcode isn't in our covered London boroughs.", 123457) unless ($lbo == 2510 || $lbo == 2492);

    my $area_info = mySociety::MaPit::get_voting_area_info($lbo);
    my $name = $area_info->{name};

    my $x = $q->param('x') || 0;
    my $y = $q->param('y') || 0;
    $x += 0;
    $y += 0;
    if (!$x && !$y) {
        my $location = mySociety::MaPit::get_location($pc);
        my $northing = $location->{northing};
        my $easting = $location->{easting};
        $x = int($easting / (5000/31));
        $y = int($northing/ (5000/31));
    }
    return ($name, $x, $y);
}