aboutsummaryrefslogtreecommitdiffstats
path: root/perllib/FixMyStreet/App/Controller/Reports.pm
blob: 6304092271a61f0763bb7710dd26794e74a992da (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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
package FixMyStreet::App::Controller::Reports;
use Moose;
use namespace::autoclean;

use File::Slurp;
use List::MoreUtils qw(zip);
use POSIX qw(strcoll);
use mySociety::MaPit;

BEGIN { extends 'Catalyst::Controller'; }

=head1 NAME

FixMyStreet::App::Controller::Reports - Catalyst Controller

=head1 DESCRIPTION

Catalyst Controller.

=head1 METHODS

=cut

=head2 index

Show the summary page of all reports.

=cut

sub index : Path : Args(0) {
    my ( $self, $c ) = @_;

    # Fetch all areas of the types we're interested in
    my @bodies = $c->model('DB::Body')->all;
    @bodies = sort { strcoll($a->name, $b->name) } @bodies;
    $c->stash->{bodies} = \@bodies;

    eval {
        my $data = File::Slurp::read_file(
            FixMyStreet->path_to( '../data/all-reports.json' )->stringify
        );
        my $j = JSON->new->utf8->decode($data);
        $c->stash->{fixed} = $j->{fixed};
        $c->stash->{open} = $j->{open};
    };
    if ($@) {
        $c->stash->{message} = _("There was a problem showing the All Reports page. Please try again later.");
        if ($c->config->{STAGING_SITE}) {
            $c->stash->{message} .= '</p><p>Perhaps the bin/update-all-reports script needs running. Use: bin/cron-wrapper bin/update-all-reports</p><p>'
                . sprintf(_('The error was: %s'), $@);
        }
        $c->stash->{template} = 'errors/generic.html';
        return;
    }

    # Down here so that error pages aren't cached.
    $c->response->header('Cache-Control' => 'max-age=3600');
}

=head2 index

Show the summary page for a particular body.

=cut

sub body : Path : Args(1) {
    my ( $self, $c, $body ) = @_;
    $c->detach( 'ward', [ $body ] );
}

=head2 index

Show the summary page for a particular ward.

=cut

sub ward : Path : Args(2) {
    my ( $self, $c, $body, $ward ) = @_;

    $c->forward( 'body_check', [ $body ] );
    $c->forward( 'ward_check', [ $ward ] )
        if $ward;
    $c->forward( 'check_canonical_url', [ $body ] );
    $c->forward( 'load_and_group_problems' );

    my $body_short = $c->cobrand->short_name( $c->stash->{body} );
    $c->stash->{rss_url} = '/rss/reports/' . $body_short;
    $c->stash->{rss_url} .= '/' . $c->cobrand->short_name( $c->stash->{ward} )
        if $c->stash->{ward};

    $c->stash->{body_url} = '/reports/' . $body_short;

    $c->stash->{stats} = $c->cobrand->get_report_stats();

    my $pins = $c->stash->{pins};

    $c->stash->{page} = 'reports'; # So the map knows to make clickable pins
    FixMyStreet::Map::display_map(
        $c,
        latitude  => @$pins ? $pins->[0]{latitude} : 0,
        longitude => @$pins ? $pins->[0]{longitude} : 0,
        area      => $c->stash->{ward} ? $c->stash->{ward}->{id} : $c->stash->{body}->area_id,
        pins      => $pins,
        any_zoom  => 1,
    );

    $c->cobrand->tweak_all_reports_map( $c );

    # List of wards
    # Ignore external_body special body thing
    unless ($c->stash->{ward} || !$c->stash->{body}->id) {
        my $children = mySociety::MaPit::call('area/children', [ $c->stash->{body}->area_id ],
            type => $c->cobrand->area_types_children,
        );
        unless ($children->{error}) {
            foreach (values %$children) {
                $_->{url} = $c->uri_for( $c->stash->{body_url}
                    . '/' . $c->cobrand->short_name( $_ )
                );
            }
            $c->stash->{children} = $children;
        }
    }
}

sub rss_area : Path('/rss/area') : Args(1) {
    my ( $self, $c, $area ) = @_;
    $c->detach( 'rss_area_ward', [ $area ] );
}

sub rss_area_ward : Path('/rss/area') : Args(2) {
    my ( $self, $c, $area, $ward ) = @_;

    $c->stash->{rss} = 1;

    # area_check

    $area =~ s/\+/ /g;
    $area =~ s/\.html//;

    # If we're passed an ID number (don't think this is used anywhere, it
    # certainly shouldn't be), just look that up on mapit and redirect
    if ($area =~ /^\d+$/) {
        my $council = mySociety::MaPit::call('area', $area);
        $c->detach( 'redirect_index') if $council->{error};
        $c->stash->{body} = $council;
        $c->detach( 'redirect_body' );
    }

    # We must now have a string to check on mapit
    my $areas = mySociety::MaPit::call( 'areas', $area,
        type => $c->cobrand->area_types,
    );

    if (keys %$areas == 1) {
        ($c->stash->{area}) = values %$areas;
    } else {
        foreach (keys %$areas) {
            if (lc($areas->{$_}->{name}) eq lc($area) || $areas->{$_}->{name} =~ /^\Q$area\E (Borough|City|District|County) Council$/i) {
                $c->stash->{area} = $areas->{$_};
            }
        }
    }

    $c->forward( 'ward_check', [ $ward ] ) if $ward;

    my $url = $c->cobrand->short_name( $c->stash->{area} );
    $url .= '/' . $c->cobrand->short_name( $c->stash->{ward} ) if $c->stash->{ward};
    $c->stash->{qs} = "/$url";

    if ($c->stash->{area}{type} ne 'DIS' && $c->stash->{area}{type} ne 'CTY') {
        # UK-specific types - two possibilites are the same for one-tier councils, so redirect one to the other
        # With bodies, this should presumably redirect if only one body covers
        # the area, and then it will need that body's name (rather than
        # assuming as now it is the same as the area)
        $c->stash->{body} = $c->stash->{area};
        $c->detach( 'redirect_body' );
    }

    $c->stash->{type} = 'area_problems';
    if ( $c->stash->{ward} ) {
        # All problems within a particular ward
        $c->stash->{title_params} = { NAME => $c->stash->{ward}{name} };
        $c->stash->{db_params}    = [ $c->stash->{ward}->{id} ];
    } else {
        # Problems within a particular area
        $c->stash->{title_params} = { NAME => $c->stash->{area}->{name} };
        $c->stash->{db_params}    = [ $c->stash->{area}->{id} ];
    }

    # Send on to the RSS generation
    $c->forward( '/rss/output' );

}

sub rss_body : Path('/rss/reports') : Args(1) {
    my ( $self, $c, $body ) = @_;
    $c->detach( 'rss_ward', [ $body ] );
}

sub rss_ward : Path('/rss/reports') : Args(2) {
    my ( $self, $c, $body, $ward ) = @_;

    $c->stash->{rss} = 1;

    $c->forward( 'body_check', [ $body ] );
    $c->forward( 'ward_check', [ $ward ] ) if $ward;

    my $url =       $c->cobrand->short_name( $c->stash->{body} );
    $url   .= '/' . $c->cobrand->short_name( $c->stash->{ward} ) if $c->stash->{ward};
    $c->stash->{qs} = "/$url";

    if ($c->stash->{ward}) {
        # Problems sent to a council, restricted to a ward
        $c->stash->{type} = 'ward_problems';
        $c->stash->{title_params} = { COUNCIL => $c->stash->{body}->name, WARD => $c->stash->{ward}{name} };
        $c->stash->{db_params} = [ $c->stash->{body}->id, $c->stash->{ward}->{id} ];
    } else {
        # Problems sent to a council
        $c->stash->{type} = 'council_problems';
        $c->stash->{title_params} = { COUNCIL => $c->stash->{body}->name };
        # XXX This looks up in both bodies_str and areas, but is only using body ID.
        # This will not work properly in any install where body IDs are not === area IDs.
        $c->stash->{db_params} = [ $c->stash->{body}->id, $c->stash->{body}->id ];
    }

    # Send on to the RSS generation
    $c->forward( '/rss/output' );
}

=head2 body_check

This action checks the body name (or code) given in a URI exists, is valid and
so on. If it is, it stores the body in the stash, otherwise it redirects to the
all reports page.

=cut

sub body_check : Private {
    my ( $self, $c, $q_body ) = @_;

    $q_body =~ s/\+/ /g;
    $q_body =~ s/\.html//;

    # Check cobrand specific incantations - e.g. ONS codes for UK,
    # Oslo/ kommunes sharing a name in Norway
    return if $c->cobrand->reports_body_check( $c, $q_body );

    # If we're passed an ID number (don't think this is used anywhere, it
    # certainly shouldn't be), just look that up on MaPit and redirect
    if ($q_body =~ /^\d+$/) {
        my $area = mySociety::MaPit::call('area', $q_body);
        $c->detach( 'redirect_index') if $area->{error};
        $c->stash->{body} = $area;
        $c->detach( 'redirect_body' );
    }

    if ( $c->cobrand->reports_by_body ) {
        my $problem = $c->cobrand->problems->search({ 'lower(me.external_body)' => lc $q_body }, { columns => [ 'external_body' ], rows => 1 })->single;
        if ( $problem ) {
            # If external_body, put as a body with ID 0 for the moment.
            $c->stash->{body} = $c->model('DB::Body')->new( { id => 0, name => $problem->external_body } );
            return;
        }
    }

    # We must now have a string to check
    my @bodies = $c->model('DB::Body')->search( { name => { -like => "$q_body%" } } )->all;

    if (@bodies == 1) {
        $c->stash->{body} = $bodies[0];
        return;
    } else {
        foreach (@bodies) {
            if (lc($_->name) eq lc($q_body) || $_->name =~ /^\Q$q_body\E (Borough|City|District|County) Council$/i) {
                $c->stash->{body} = $_;
                return;
            }
        }
    }

    # No result, bad body name.
    $c->detach( 'redirect_index' );
}

=head2 ward_check

This action checks the ward name from a URI exists and is part of the right
parent, already found with body_check. It either stores the ward Area if
okay, or redirects to the body page if bad.

=cut

sub ward_check : Private {
    my ( $self, $c, $ward ) = @_;

    $ward =~ s/\+/ /g;
    $ward =~ s/\.html//;
    $ward =~ s{_}{/}g;

    # Could be from RSS area, or body...
    my $parent_id;
    if ( $c->stash->{body} ) {
        $parent_id = $c->stash->{body}->area_id;
    } else {
        $parent_id = $c->stash->{area}->{id};
    }

    my $qw = mySociety::MaPit::call('areas', $ward,
        type => $c->cobrand->area_types_children,
    );
    foreach my $area (sort { $a->{name} cmp $b->{name} } values %$qw) {
        if ($area->{parent_area} == $parent_id) {
            $c->stash->{ward} = $area;
            return;
        }
    }
    # Given a false ward name
    $c->detach( 'redirect_body' );
}

=head2 check_canonical_url

Given an already found (case-insensitively) body, check what URL
we are at and redirect accordingly if different.

=cut

sub check_canonical_url : Private {
    my ( $self, $c, $q_body ) = @_;

    my $body_short = $c->cobrand->short_name( $c->stash->{body} );
    my $url_short = URI::Escape::uri_escape_utf8($q_body);
    $url_short =~ s/%2B/+/g;
    $c->detach( 'redirect_body' ) unless $body_short eq $url_short;
}

sub load_and_group_problems : Private {
    my ( $self, $c ) = @_;

    my $page = $c->req->params->{p} || 1;

    my $where = {
        non_public => 0,
        state      => [ FixMyStreet::DB::Result::Problem->visible_states() ]
    };
    if ($c->stash->{ward}) {
        $where->{areas} = { 'like', '%,' . $c->stash->{ward}->{id} . ',%' };
        $where->{bodies_str} = [
            undef,
            $c->stash->{body}->id,
            { 'like', $c->stash->{body}->id . ',%' },
            { 'like', '%,' . $c->stash->{body}->id },
        ];
    } elsif ($c->stash->{body} && $c->stash->{body}->id == 0) {
        # A proxy for an external_body
        $where->{'lower(external_body)'} = lc $c->stash->{body}->name;
    } elsif ($c->stash->{body}) {
        $where->{areas} = { 'like', '%,' . $c->stash->{body}->id . ',%' };
        $where->{bodies_str} = [
            undef,
            $c->stash->{body}->id,
            { 'like', $c->stash->{body}->id . ',%' },
            { 'like', '%,' . $c->stash->{body}->id },
        ];
    }
    my $problems = $c->cobrand->problems->search(
        $where,
        {
            columns => [
                'id', 'bodies_str', 'state', 'areas', 'latitude', 'longitude', 'title', 'cobrand',
                #{ duration => { extract => "epoch from current_timestamp-lastupdate" } },
                #{ age      => { extract => "epoch from current_timestamp-confirmed"  } },
                { confirmed  => { extract => 'epoch from confirmed' } },
                { whensent   => { extract => 'epoch from whensent' } },
                { lastupdate => { extract => 'epoch from lastupdate' } },
                { photo    => 'photo is not null' },
            ],
            order_by => { -desc => 'lastupdate' },
            rows => $c->cobrand->reports_per_page,
        }
    )->page( $page );
    $c->stash->{pager} = $problems->pager;
    $problems = $problems->cursor; # Raw DB cursor for speed

    my ( %problems, @pins );
    my @cols = ( 'id', 'bodies_str', 'state', 'areas', 'latitude', 'longitude', 'title', 'cobrand', 'confirmed', 'whensent', 'lastupdate', 'photo' );
    while ( my @problem = $problems->next ) {
        my %problem = zip @cols, @problem;
        $problem{is_fixed} = FixMyStreet::DB::Result::Problem->fixed_states()->{$problem{state}};
        $c->log->debug( $problem{'cobrand'} . ', cobrand is ' . $c->cobrand->moniker );
        if ( !$c->stash->{body}->id ) {
            # An external_body entry
            add_row( \%problem, 0, \%problems, \@pins );
            next;
        }
        if ( !$problem{bodies_str} ) {
            # Problem was not sent to any body, add to all possible areas XXX
            $problem{bodies} = 0;
            while ($problem{areas} =~ /,(\d+)(?=,)/g) {
                add_row( \%problem, $1, \%problems, \@pins );
            }
        } else {
            # Add to bodies it was sent to
            # XXX Assumes body ID matches "council ID"
            (my $bodies = $problem{bodies_str}) =~ s/\|.*$//;
            my @bodies = split( /,/, $bodies );
            $problem{bodies} = scalar @bodies;
            foreach ( @bodies ) {
                next if $_ != $c->stash->{body}->id;
                add_row( \%problem, $_, \%problems, \@pins );
            }
        }
    }

    $c->stash(
        problems      => \%problems,
        pins          => \@pins,
    );

    return 1;
}

sub redirect_index : Private {
    my ( $self, $c ) = @_;
    my $url = '/reports';
    $c->res->redirect( $c->uri_for($url) );
}

sub redirect_body : Private {
    my ( $self, $c ) = @_;
    my $url = '';
    $url   .= "/rss" if $c->stash->{rss};
    $url   .= '/reports';
    $url   .= '/' . $c->cobrand->short_name( $c->stash->{body} );
    $url   .= '/' . $c->cobrand->short_name( $c->stash->{ward} )
        if $c->stash->{ward};
    $c->res->redirect( $c->uri_for($url) );
}

sub add_row {
    my ( $problem, $body, $problems, $pins ) = @_;
    push @{$problems->{$body}}, $problem;
    push @$pins, {
        latitude  => $problem->{latitude},
        longitude => $problem->{longitude},
        colour    => 'yellow', # FixMyStreet::DB::Result::Problem->fixed_states()->{$problem->{state}} ? 'green' : 'red',
        id        => $problem->{id},
        title     => $problem->{title},
    };
}

=head1 AUTHOR

Matthew Somerville

=head1 LICENSE

Copyright (c) 2011 UK Citizens Online Democracy. All rights reserved.
Licensed under the Affero GPL.

=cut

__PACKAGE__->meta->make_immutable;

1;