aboutsummaryrefslogtreecommitdiffstats
path: root/perllib/FixMyStreet/App/Controller/Report.pm
blob: 11130205e8067649b648dc2a1f21d74eab879886 (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
package FixMyStreet::App::Controller::Report;

use Moose;
use namespace::autoclean;
BEGIN { extends 'Catalyst::Controller'; }

=head1 NAME

FixMyStreet::App::Controller::Report - display a report

=head1 DESCRIPTION

Show a report

=head1 ACTIONS

=head2 index

Redirect to homepage unless C<id> parameter in query, in which case redirect to
'/report/$id'.

=cut

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

    my $id = $c->req->param('id');

    my $uri =
        $id
      ? $c->uri_for( '/report', $id )
      : $c->uri_for('/');

    $c->res->redirect($uri);
}

=head2 report_display

Display a report.

=cut

sub display : Path('') : Args(1) {
    my ( $self, $c, $id ) = @_;

#     my ($q, $errors, $field_errors) = @_;
#     my @errors = @$errors;
#     my %field_errors = %{$field_errors};
#     my $cobrand = Page::get_cobrand($q);
#     push @errors, _('There were problems with your update. Please see below.') if (scalar keys %field_errors);

#     my @vars = qw(id name rznvy update fixed add_alert upload_fileid submit_update);
#     my %input = map { $_ => $q->param($_) || '' } @vars;
#     my %input_h = map { $_ => $q->param($_) ? ent($q->param($_)) : '' } @vars;
#     my $base = Cobrand::base_url($cobrand);

    if (
        $id =~ m{ ^ 3D (\d+) $ }x         # Some council with bad email software
        || $id =~ m{ ^(\d+) \D .* $ }x    # trailing garbage
      )
    {
        return $c->res->redirect( $c->uri_for($1), 301 );
    }

    $c->forward('load_problem_or_display_error', [ $id ] );

    #     my $extra_data = Cobrand::extra_data($cobrand, $q);
    #     my $google_link = Cobrand::base_url_for_emails($cobrand, $extra_data)
    #         . '/report/' . $problem->{id};
    #     # truncate the lat,lon for nicer rss urls
    #     my ( $short_lat, $short_lon ) =
    #       map { Utils::truncate_coordinate($_) }    #
    #       ( $problem->{latitude}, $problem->{longitude} );

#     my $map_links = '';
#     $map_links = "<p id='sub_map_links'>"
#       . "<a href=\"http://maps.google.co.uk/maps?output=embed&amp;z=16&amp;q="
#       . URI::Escape::uri_escape_utf8( $problem->{title} . ' - ' . $google_link )
#       . "\@$short_lat,$short_lon\">View on Google Maps</a></p>"
#         if mySociety::Config::get('COUNTRY') eq 'GB';

    $c->stash->{banner} = $c->cobrand->generate_problem_banner($c->stash->{problem});

#     my $banner;
#     if ($q->{site} ne 'emptyhomes' && $problem->{state} eq 'confirmed' && $problem->{duration} > 8*7*24*60*60) {
#         $banner = $q->p({id => 'unknown'}, _('This problem is old and of unknown status.'));
#     }
#     if ($problem->{state} eq 'fixed') {
#         $banner = $q->p({id => 'fixed'}, _('This problem has been fixed') . '.');
#     }

#     my $contact_url = Cobrand::url($cobrand, NewURL($q, -retain => 1, pc => undef, x => undef, 'y' => undef, -url=>'/contact?id=' . $input{id}), $q);
#     my $back = Cobrand::url($cobrand, NewURL($q, -url => '/',
#         lat => $short_lat, lon => $short_lon,
#         -retain => 1, pc => undef, x => undef, 'y' => undef, id => undef
#     ), $q);
#     my $fixed = ($input{fixed}) ? ' checked' : '';

    $c->forward( 'format_problem_for_display' );
#     my %vars = (
#         unsuitable => $q->a({rel => 'nofollow', href => $contact_url}, _('Offensive? Unsuitable? Tell us')),
#         more_problems => '<a href="' . $back . '">' . _('More problems nearby') . '</a>',
#         url_home => Cobrand::url($cobrand, '/', $q),
#         alert_link => Cobrand::url($cobrand, NewURL($q, -url => '/alert?type=updates;id='.$input_h{id}, -retain => 1, pc => undef, x => undef, 'y' => undef ), $q),
#         alert_text => _('Email me updates'),
#         email_label => _('Email:'),
#         subscribe => _('Subscribe'),
#         blurb => _('Receive email when updates are left on this problem'),
#         cobrand_form_elements1 => Cobrand::form_elements($cobrand, 'alerts', $q),
#         form_alert_action => Cobrand::url($cobrand, '/alert', $q),
#         rss_url => Cobrand::url($cobrand,  NewURL($q, -retain=>1, -url => '/rss/'.$input_h{id}, pc => undef, x => undef, 'y' => undef, id => undef), $q),
#         rss_title => _('RSS feed'),
#         rss_alt => _('RSS feed of updates to this problem'),
#         update_heading => $q->h2(_('Provide an update')),
#         field_errors => \%field_errors,
#         add_alert_checked => ($input{add_alert} || !$input{submit_update}) ? ' checked' : '',
#         name_label => _('Name:'),
#         update_label => _('Update:'),
#         alert_label => _('Alert me to future updates'),
#         post_label => _('Post'),
#         cobrand_form_elements => Cobrand::form_elements($cobrand, 'updateForm', $q),
#         form_action => Cobrand::url($cobrand, '/', $q),
#         input_h => \%input_h,
#         optional => _('(optional)'),
#     );
#
#     $vars{update_blurb} = $q->p($q->small(_('Please note that updates are not sent to the council. If you leave your name it will be public. Your information will only be used in accordance with our <a href="/faq#privacy">privacy policy</a>')))
#         unless $q->{site} eq 'emptyhomes'; # No council blurb
#
#     if (@errors) {
#         $vars{errors} = '<ul class="error"><li>' . join('</li><li>', @errors) . '</li></ul>';
#     }
#
#     my $allow_photo_upload = Cobrand::allow_photo_upload($cobrand);
#     if ($allow_photo_upload) {
#         my $photo_label = _('Photo:');
#         $vars{enctype} = ' enctype="multipart/form-data"';
#         $vars{photo_element} = <<EOF;
# <div id="fileupload_normalUI">
# <label for="form_photo">$photo_label</label>
# <input type="file" name="photo" id="form_photo">
# </div>
# EOF
#     }
#
#     my %params = (
#         rss => [ _('Updates to this problem, FixMyStreet'), "/rss/$input_h{id}" ],
#         robots => 'index, nofollow',
#         js => FixMyStreet::Map::header_js(),
#         title => $problem->{title}
#     );
#
#     my $page = Page::template_include('problem', $q, Page::template_root($q), %vars);
#     return ($page, %params);

}

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

    # try to load a report if the id is a number
    my $problem    #
      = $id =~ m{\D}    # is id non-numeric?
      ? undef           # ...don't even search
      : $c->model('DB::Problem')->find(
        { id => $id },
        {
            select => [
                'id',
                'latitude',
                'longitude',
                'council',
                'category',
                'title', 'detail', 'photo',
                'used_map',
                'name',
                'anonymous',
                'state',
                'service',
                'cobrand',
                'cobrand_data',
                'external_body',
                {
                    extract => 'epoch from confirmed',
                    -as     => 'time',
                },
                {
                    extract => 'epoch from whensent-confirmed',
                    -as     => 'whensent'
                },
                {
                    extract => 'epoch from ms_current_timestamp()-lastupdate',
                    -as     => 'duration'
                }

            ]
        }
      );

    # check that the problem is suitable to show.
    if ( !$problem || $problem->state eq 'unconfirmed' ) {
        $c->detach( '/page_error_404_not_found', [ _('Unknown problem ID') ] );
    }
    elsif ( $problem->state eq 'hidden' ) {
        $c->detach(
            '/page_error_410_gone',
            [ _('That report has been removed from FixMyStreet.') ]    #
        );
    }

    $c->stash->{problem} = $problem;

    return 1;
}

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

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

    ( my $detail = $problem->detail ) =~ s/\r//g;
    my @detail = split /\n{2,}/, $detail;
    $c->stash->{detail} = \@detail;

    $c->forward('generate_map_tags');
    $c->forward('generate_problem_photo');
    $c->forward('generate_problem_meta');

    return 1;
}

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

    my $map_links = '';
    my $problem   = $c->stash->{problem};

    $c->stash->{map_start_html} = FixMyStreet::Map::display_map(
        $c->fake_q,
        latitude  => $problem->latitude,
        longitude => $problem->longitude,
        type      => 0,
        pins      => $problem->used_map
        ? [ [ $problem->latitude, $problem->longitude, 'blue' ] ]
        : [],
        post => $map_links
    );
    $c->stash->{map_end_html} = FixMyStreet::Map::display_map_end(0),

      return 1;
}

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

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

    if ( $c->cobrand->allow_photo_display and $problem->photo ) {
        my $photo = {};
        ( $photo->{width}, $photo->{height} ) =
          Image::Size::imgsize( \$problem->photo );
        $photo->{url} = '/photo/?id=' . $problem->id;
        $c->stash->{photo} = $photo;
    }
}

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

    my $problem = $c->stash->{problem};
    my $date_time =
      Page::prettify_epoch( $c->req, $problem->get_column('time') );
    my $meta = '';
    if ( $problem->anonymous ) {
        if (    $problem->service
            and $problem->category && $problem->category ne _('Other') )
        {
            $meta =
              sprintf( _('Reported by %s in the %s category anonymously at %s'),
                $problem->service, $problem->category, $date_time );
        }
        elsif ( $problem->service ) {
            $meta = sprintf( _('Reported by %s anonymously at %s'),
                $problem->service, $date_time );
        }
        elsif ( $problem->category and $problem->category ne _('Other') ) {
            $meta = sprintf( _('Reported in the %s category anonymously at %s'),
                $problem->category, $date_time );
        }
        else {
            $meta = sprintf( _('Reported anonymously at %s'), $date_time );
        }
    }
    else {
        if (    $problem->service
            and $problem->category && $problem->category ne _('Other') )
        {
            $meta = sprintf(
                _('Reported by %s in the %s category by %s at %s'),
                $problem->service, $problem->category,
                $problem->name,    $date_time
            );
        }
        elsif ( $problem->service ) {
            $meta = sprintf( _('Reported by %s by %s at %s'),
                $problem->service, $problem->name, $date_time );
        }
        elsif ( $problem->category and $problem->category ne _('Other') ) {
            $meta = sprintf( _('Reported in the %s category by %s at %s'),
                $problem->category, $problem->name, $date_time );
        }
        else {
            $meta =
              sprintf( _('Reported by %s at %s'), $problem->name, $date_time );
        }
    }

    $c->stash->{meta} = $meta;

    return 1;
}

__PACKAGE__->meta->make_immutable;

1;