aboutsummaryrefslogtreecommitdiffstats
path: root/perllib/FixMyStreet/App/Controller/Alert.pm
blob: c1b164a7c9ad73f43c2c01d6d299f4ee4578cbaa (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
package FixMyStreet::App::Controller::Alert;
use Moose;
use namespace::autoclean;

BEGIN {extends 'Catalyst::Controller'; }

=head1 NAME

FixMyStreet::App::Controller::Alert - Catalyst Controller

=head1 DESCRIPTION

Catalyst Controller.

=head1 METHODS

=cut


=head2 alert

Show the alerts page

=cut

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

#    my $q = shift;
#    my $cobrand = Page::get_cobrand($q);
#    my $error = shift;
#    my $errors = '';
#    $errors = '<ul class="error"><li>' . $error . '</li></ul>' if $error;
#
#    my $form_action = Cobrand::url(Page::get_cobrand($q), '/alert', $q);
#    my $cobrand_form_elements = Cobrand::form_elements($cobrand, 'alerts', $q);
#    my $cobrand_extra_data = Cobrand::extra_data($cobrand, $q);
#
#    $out .= $errors . qq(<form method="get" action="$form_action">);
#    $out .= $q->p($pc_label, '<input type="text" name="pc" value="' . $input_h{pc} . '">
#<input type="submit" value="' . $submit_text . '">');
#    $out .= $cobrand_form_elements;
#
#    my %vars = (error => $error, 
#                header => $header, 
#                intro => $intro, 
#                pc_label => $pc_label, 
#                form_action => $form_action, 
#                input_h => \%input_h, 
#                submit_text => $submit_text, 
#                cobrand_form_elements => $cobrand_form_elements, 
#                cobrand_extra_data => $cobrand_extra_data, 
#                url_home => Cobrand::url($cobrand, '/', $q));
#
#    my $cobrand_page = Page::template_include('alert-front-page', $q, Page::template_root($q), %vars);
#    $out = $cobrand_page if ($cobrand_page);
#
#    return $out if $q->referer() && $q->referer() =~ /fixmystreet\.com/;
#    my $recent_photos = Cobrand::recent_photos($cobrand, 10);
#    $out .= '<div id="alert_recent">' . $q->h2(_('Some photos of recent reports')) . $recent_photos . '</div>' if $recent_photos;
#
#    return $out;
}



sub list :Path('list') :Args(0) {
    my ( $self, $c ) = @_;
#    my ($q, @errors) = @_;
#    my @vars = qw(pc rznvy lat lon);
#    my %input = map { $_ => scalar $q->param($_) } @vars;
#    my %input_h = map { $_ => $q->param($_) ? ent($q->param($_)) : '' } @vars;
#
    # Try to create a location for whatever we have
    unless ($c->forward('/location/determine_location_from_coords')
          || $c->forward('/location/determine_location_from_pc') ) {

      if ( $c->stash->{possible_location_matches} ) {
        $c->stash->{choose_target_uri} = $c->uri_for( '/alert/list' );
        $c->detach('choose');
      }

      $c->go('index') if $c->stash->{ location_error };
    }

    $c->log->debug( $_ ) for ( $c->stash->{pc}, $c->stash->{latitude}, $c->stash->{longitude} );

    $c->forward('prettify_pc');

    # truncate the lat,lon for nicer urls
    ( $c->stash->{latitude}, $c->stash->{longitude} ) = map { Utils::truncate_coordinate($_) } ( $c->stash->{latitude}, $c->stash->{longitude} );
    $c->log->debug( $_ ) for ( $c->stash->{pc}, $c->stash->{latitude}, $c->stash->{longitude} );
#    
#    my $errors = '';
#    $errors = '<ul class="error"><li>' . join('</li><li>', @errors) . '</li></ul>' if @errors;
#
    $c->stash->{council_check_action} = 'alert';
    unless ( $c->forward( '/council/load_and_check_councils_and_wards' ) ) {
      $c->go( 'index' );
    }

    $c->forward('council_options');

   my $dist = mySociety::Gaze::get_radius_containing_population($c->stash->{latitude}, $c->stash->{longitude}, 200000);
   $dist = int($dist * 10 + 0.5);
   $dist = $dist / 10.0;
   $c->stash->{population_radius} = $dist;
#
#    my $checked = '';
#    $checked = ' checked' if $q->param('feed') && $q->param('feed') eq "local:$lat:$lon";
#    my $cobrand_form_elements = Cobrand::form_elements($cobrand, 'alerts', $q);
#    my $pics = Cobrand::recent_photos($cobrand, 5, $lat, $lon, $dist);
#    $pics = '<div id="alert_photos">' . $q->h2(_('Photos of recent nearby reports')) . $pics . '</div>' if $pics;

#    my $form_action = Cobrand::url($cobrand, '/alert', $q);
#$cobrand_form_elements
#$pics
#
#EOF
#    $out .= $errors;
#    $out .= <<EOF;
  $c->stash->{rss_feed_id} = sprintf( 'local:%s:%s', $c->stash->{latitude}, $c->stash->{longitude} );

  my $rss_feed;
  if ($c->stash->{pretty_pc_text}) {
     $rss_feed = $c->cobrand->uri("/rss/pc/" . $c->stash->{pretty_pc_text}, $c->fake_q);
  } else {
     $rss_feed = $c->cobrand->uri( sprintf("/rss/l/%s,%s", $c->stash->{latitude},$c->stash->{longitude}), $c->fake_q);
  }

  $c->stash->{rss_feed_uri} = $rss_feed;

#    my $default_link = Cobrand::url($cobrand, "/alert?type=local;feed=local:$lat:$lon", $q);

   $c->stash->{rss_feed_2k}  = $c->cobrand->uri($rss_feed.'/2', $c->fake_q);
   $c->stash->{rss_feed_5k}  = $c->cobrand->uri($rss_feed.'/5', $c->fake_q);
   $c->stash->{rss_feed_10k} = $c->cobrand->uri($rss_feed.'/10', $c->fake_q);
   $c->stash->{rss_feed_20k} = $c->cobrand->uri($rss_feed.'/20', $c->fake_q);

#    $out .= $q->p(_('Or you can subscribe to an alert based upon what ward or council you&rsquo;re in:'));
#    $out .= $options_start;
#    $out .= $options;
#    $out .= $options_end;
#    $out .= $q->p('<input type="submit" name="rss" value="' . _('Give me an RSS feed') . '">');
#    $out .= $q->p({-id=>'alert_or'}, _('or'));
#    $out .= '<p>' . _('Your email:') . ' <input type="text" id="rznvy" name="rznvy" value="' . $input_h{rznvy} . '" size="30"></p>
#<p><input type="submit" name="alert" value="' . _('Subscribe me to an email alert') . '"></p>
#</div>
#</form>';
#    my %vars = (header => $header, 
#                cobrand_form_elements => $cobrand_form_elements, 
#                error => $errors,
#                rss_label => $rss_label,
#                rss_feed => $rss_feed,
#                default_link => $default_link, 
#                rss_details => $rss_details, 
#                rss_feed_2k => $rss_feed_2k, 
#                rss_feed_5k => $rss_feed_5k,   
#                rss_feed_10k => $rss_feed_10k,   
#                rss_feed_20k => $rss_feed_20k, 
#                lat => $lat,
#                lon => $lon, 
#                options => $options );
#    my $cobrand_page = Page::template_include('alert-options', $q, Page::template_root($q), %vars);
#    $out = $cobrand_page if ($cobrand_page);
#    return $out;
}

=head2 prettify_pc

This will canonicalise and prettify the postcode and stick a pretty_pc and pretty_pc_text in the stash.

=cut

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

    # FIXME previously this had been run through ent so need to do similar here or in template
    my $pretty_pc = $c->req->params->{'pc'};

#    my $pretty_pc = $input_h{pc};
#    my $pretty_pc_text;# This one isnt't getting the nbsp.
    if (mySociety::PostcodeUtil::is_valid_postcode($c->req->params->{'pc'})) {
        $pretty_pc = mySociety::PostcodeUtil::canonicalise_postcode($c->req->params->{'pc'});
        my $pretty_pc_text = $pretty_pc;
        $pretty_pc_text =~ s/ //g;
        $c->stash->{pretty_pc_text} = $pretty_pc_text;
        # this may be better done in template
        $pretty_pc =~ s/ /&nbsp;/;
    }

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


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


  if ( $c->config->{COUNTRY} eq 'NO' ) {
#    my ($options, $options_start, $options_end);
#    if (mySociety::Config::get('COUNTRY') eq 'NO') {
#
#        my (@options, $fylke, $kommune);
#        foreach (values %$areas) {
#            if ($_->{type} eq 'NKO') {
#                $kommune = $_;
#            } else {
#                $fylke = $_;
#            }
#        }
#        my $kommune_name = $kommune->{name};
#        my $fylke_name = $fylke->{name};
#
#        if ($fylke->{id} == 3) { # Oslo
#
#            push @options, [ 'council', $fylke->{id}, Page::short_name($fylke),
#                sprintf(_("Problems within %s"), $fylke_name) ];
#        
#            $options_start = "<div><ul id='rss_feed'>";
#            $options = alert_list_options($q, @options);
#            $options_end = "</ul>";
#
#        } else {
#
#            push @options,
#                [ 'area', $kommune->{id}, Page::short_name($kommune), $kommune_name ],
#                [ 'area', $fylke->{id}, Page::short_name($fylke), $fylke_name ];
#            $options_start = '<div id="rss_list">';
#            $options = $q->p($q->strong(_('Problems within the boundary of:'))) .
#                $q->ul(alert_list_options($q, @options));
#            @options = ();
#            push @options,
#                [ 'council', $kommune->{id}, Page::short_name($kommune), $kommune_name ],
#                [ 'council', $fylke->{id}, Page::short_name($fylke), $fylke_name ];
#            $options .= $q->p($q->strong(_('Or problems reported to:'))) .
#                $q->ul(alert_list_options($q, @options));
#            $options_end = $q->p($q->small(_('FixMyStreet sends different categories of problem
#to the appropriate council, so problems within the boundary of a particular council
#might not match the problems sent to that council. For example, a graffiti report
#will be sent to the district council, so will appear in both of the district
#council&rsquo;s alerts, but will only appear in the "Within the boundary" alert
#for the county council.'))) . '</div><div id="rss_buttons">';
#
#        }
#
  } elsif( keys %{ $c->stash->{ all_councils } } == 2 ) {
    $c->log->debug( 'one tier council' );
    $c->forward('generate_council_and_ward_options');
  } elsif( keys %{ $c->stash->{ all_councils } } == 1 ) {
    $c->log->debug( 'one tier council. no ward' );
    $c->forward('generate_council_and_ward_options');
  } elsif( keys %{ $c->stash->{ all_councils } } == 4 ) {
    $c->log->debug( 'two tier council' );
    $c->stash->{two_tier_council} = 1;
#
#        # Two-tier council
#        my (@options, $county, $district, $c_ward, $d_ward);
#        foreach (values %$areas) {
#            if ($_->{type} eq 'CTY') {
#                $county = $_;
#            } elsif ($_->{type} eq 'DIS') {
#                $district = $_;
#            } elsif ($_->{type} eq 'CED') {
#                $c_ward = $_;
#            } elsif ($_->{type} eq 'DIW') {
#                $d_ward = $_;
#            }
#        }
#        my $district_name = $district->{name};
#        my $d_ward_name = $d_ward->{name};
#        my $county_name = $county->{name};
#        my $c_ward_name = $c_ward->{name};
#        push @options,
#            [ 'area', $district->{id}, Page::short_name($district), $district_name ],
#            [ 'area', $district->{id}.':'.$d_ward->{id}, Page::short_name($district) . '/'
#              . Page::short_name($d_ward), "$d_ward_name ward, $district_name" ],
#            [ 'area', $county->{id}, Page::short_name($county), $county_name ],
#            [ 'area', $county->{id}.':'.$c_ward->{id}, Page::short_name($county) . '/'
#              . Page::short_name($c_ward), "$c_ward_name ward, $county_name" ];
#        $options_start = '<div id="rss_list">';
#        $options = $q->p($q->strong(_('Problems within the boundary of:'))) .
#            $q->ul(alert_list_options($q, @options));
#        @options = ();
#        push @options,
#            [ 'council', $district->{id}, Page::short_name($district), $district_name ],
#            [ 'ward', $district->{id}.':'.$d_ward->{id}, Page::short_name($district) . '/' . Page::short_name($d_ward),
#              "$district_name, within $d_ward_name ward" ];
#        if ($q->{site} ne 'emptyhomes') {
#            push @options,
#                [ 'council', $county->{id}, Page::short_name($county), $county_name ],
#                [ 'ward', $county->{id}.':'.$c_ward->{id}, Page::short_name($county) . '/'
#                  . Page::short_name($c_ward), "$county_name, within $c_ward_name ward" ];
#            $options .= $q->p($q->strong(_('Or problems reported to:'))) .
#                $q->ul(alert_list_options($q, @options));
#            $options_end = $q->p($q->small(_('FixMyStreet sends different categories of problem
#to the appropriate council, so problems within the boundary of a particular council
#might not match the problems sent to that council. For example, a graffiti report
#will be sent to the district council, so will appear in both of the district
#council&rsquo;s alerts, but will only appear in the "Within the boundary" alert
#for the county council.'))) . '</div><div id="rss_buttons">';
#        } else {
#            $options_end = '';
#        }
    } else {
        # Hopefully impossible in the UK!
        throw Error::Simple('An area with three tiers of council? Impossible! '. $c->stash->{latitude}. ' ' . $c->stash->{longitude} . ' ' . join('|',keys %{ $c->stash->{all_councils} } ));
    }
}

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

  my %councils = map { $_ => 1 } $c->cobrand->area_types();

  my (@options, $council, $ward);
  foreach (values %{ $c->stash->{all_councils} }) {
      if ($councils{$_->{type}}) {
          $council = $_;
          $council->{short_name} = Page::short_name( $council );
          ( $council->{id_name} = $council->{short_name} ) =~ tr/+/_/;
      } else {
          $ward = $_;
          $ward->{short_name} = Page::short_name( $ward );
          ( $ward->{id_name} = $ward->{short_name} ) =~ tr/+/_/;
      }
  }

  push @options,
    {
      type  => 'council',
      id    => sprintf( 'council:%s:%s', $council->{id}, $council->{id_name} ),
      text  => sprintf( _('Problems within %s'), $council->{name}),
      uri   => $c->cobrand->uri( '/rss/reports/' . $council->{short_name} ),
    };
  push @options,
    {
      type  => 'ward',
      id    => sprintf( 'ward:%s:%s:%s:%s', $council->{id}, $ward->{id}, $council->{id_name}, $ward->{id_name} ),
      text  => sprintf( _('Problems within %s ward'), $ward->{name}),
      uri   => $c->cobrand->uri( '/rss/reports/' . $council->{short_name} . '/' . $ward->{short_name} ),
    } if $ward;

  $c->stash->{options} = \@options;
}

sub choose : Private {
  my ( $self, $c ) = @_;
  $c->stash->{template} = 'alert/choose.html';
}


=head1 AUTHOR

Struan Donald

=head1 LICENSE

This library is free software. You can redistribute it and/or modify
it under the same terms as Perl itself.

=cut

__PACKAGE__->meta->make_immutable;

1;