aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--perllib/FixMyStreet/App/Controller/Alert.pm97
-rw-r--r--t/app/controller/alert_new.t83
-rw-r--r--templates/web/default/alert/list.html8
3 files changed, 184 insertions, 4 deletions
diff --git a/perllib/FixMyStreet/App/Controller/Alert.pm b/perllib/FixMyStreet/App/Controller/Alert.pm
index ef37e53c3..3d2c86970 100644
--- a/perllib/FixMyStreet/App/Controller/Alert.pm
+++ b/perllib/FixMyStreet/App/Controller/Alert.pm
@@ -4,6 +4,8 @@ use namespace::autoclean;
BEGIN { extends 'Catalyst::Controller'; }
+use mySociety::EmailUtil qw(is_valid_email);
+
=head1 NAME
FixMyStreet::App::Controller::Alert - Catalyst Controller
@@ -65,6 +67,9 @@ sub index : Path('') : Args(0) {
sub list : Path('list') : Args(0) {
my ( $self, $c ) = @_;
+ $c->stash->{rznvy} = $c->req->param('rznvy');
+ $c->stash->{selected_feed} = $c->req->param('feed');
+
# my ($q, @errors) = @_;
# my @vars = qw(pc rznvy lat lon);
# my %input = map { $_ => scalar $q->param($_) } @vars;
@@ -169,6 +174,8 @@ sub subscribe : Path('subscribe') : Args(0) {
if ( $c->req->param( 'rss' ) ) {
$c->detach( 'rss' );
+ } elsif ( $c->req->param( 'rznvy' ) ) {
+ $c->detach( 'subscribe_email' );
}
}
@@ -210,6 +217,96 @@ sub rss : Private {
}
}
+=head2 subscribe_email
+
+Sign up to email alerts
+
+=cut
+
+sub subscribe_email : Private {
+ my ( $self, $c ) = @_;
+
+ my $type = $c->req->param('type');
+ $c->stash->{email_type} = 'alert';
+
+ my @errors;
+ push @errors, _('Please enter a valid email address') unless is_valid_email($c->req->param('rznvy'));
+ push @errors, _('Please select the type of alert you want') if $type && $type eq 'local' && !$c->req->param('feed');
+ if (@errors) {
+ $c->stash->{errors} = \@errors;
+ $c->go('list');
+# return alert_updates_form($q, @errors) if $type && $type eq 'updates';
+# return alert_list($q, @errors) if $type && $type eq 'local';
+# return alert_front_page($q, @errors);
+ }
+
+ my $alert_id;
+ my $email = $c->req->param('rznvy');
+# my $cobrand = Page::get_cobrand($q);
+# my $cobrand_data = Cobrand::extra_alert_data($cobrand, $q);
+ if ($type eq 'updates') {
+# my $id = $q->param('id');
+# $alert_id = FixMyStreet::Alert::create($email, 'new_updates', $cobrand, $cobrand_data, $id);
+ } elsif ($type eq 'problems') {
+# $alert_id = FixMyStreet::Alert::create($email, 'new_problems', $cobrand, $cobrand_data);
+ } elsif ($type eq 'local') {
+ my $feed = $c->req->param('feed');
+
+ my ( $type, @params );
+ if ($feed =~ /^area:(?:\d+:)?(\d+)/) {
+ $type = 'area_problems';
+ push @params, $1;
+ } elsif ($feed =~ /^council:(\d+)/) {
+ $type = 'council_problems';
+ push @params, $1, $1;
+ } elsif ($feed =~ /^ward:(\d+):(\d+)/) {
+ $type = 'ward_problems';
+ push @params, $1, $2;
+ } elsif ($feed =~ m{ \A local: ( [\+\-]? \d+ \.? \d* ) : ( [\+\-]? \d+ \.? \d* ) }xms ) {
+ $type = 'local_problems';
+ push @params, $1, $2;
+# my $lat = $1;
+# my $lon = $2;
+# $alert_id = FixMyStreet::Alert::create($email, 'local_problems', $cobrand, $cobrand_data, $lon, $lat);
+ }
+
+ my $options = {
+ email => $email,
+ alert_type => $type
+ };
+
+ if ( scalar @params == 1 ) {
+ $options->{parameter} = $params[0];
+ } elsif ( scalar @params == 2 ) {
+ $options->{parameter} = $params[0];
+ $options->{parameter2} = $params[1];
+ }
+
+ my $alert = $c->model('DB::Alert')->find(
+ $options
+ );
+
+ unless ( $alert ) {
+ $options->{cobrand} = $c->cobrand->moniker();
+ $options->{cobrand_data} = $c->cobrand->extra_update_data();
+
+ $alert = $c->model('DB::Alert')->new( $options );
+ $alert->insert();
+ }
+
+ $c->log->debug( 'created alert ' . $alert->id );
+ $c->stash->{template} = 'email_sent.html';
+ } else {
+ throw FixMyStreet::Alert::Error('Invalid type');
+ }
+#
+# my %h = ();
+# $h{url} = Page::base_url_with_lang($q, undef, 1) . '/A/'
+# . mySociety::AuthToken::store('alert', { id => $alert_id, type => 'subscribe', email => $email } );
+# dbh()->commit();
+# return Page::send_confirmation_email($q, $email, undef, 'alert', %h);
+}
+
=head2 prettify_pc
This will canonicalise and prettify the postcode and stick a pretty_pc and pretty_pc_text in the stash.
diff --git a/t/app/controller/alert_new.t b/t/app/controller/alert_new.t
new file mode 100644
index 000000000..3d1bfeabf
--- /dev/null
+++ b/t/app/controller/alert_new.t
@@ -0,0 +1,83 @@
+use strict;
+use warnings;
+use Test::More;
+
+
+use FixMyStreet::TestMech;
+
+my $mech = FixMyStreet::TestMech->new;
+
+foreach my $test (
+ {
+ email => 'test@example.com',
+ type => 'area',
+ content => 'your alert will not be activated',
+ email_text => 'confirm the alert',
+ uri =>
+ '/alert/subscribe?type=local&rznvy=test@example.com&feed=area:1000:A_Location',
+ param1 => 1000
+ },
+ {
+ email => 'test@example.com',
+ type => 'council',
+ content => 'your alert will not be activated',
+ email_text => 'confirm the alert',
+ uri =>
+ '/alert/subscribe?type=local&rznvy=test@example.com&feed=council:1000:A_Location',
+ param1 => 1000,
+ param2 => 1000,
+ },
+ {
+ email => 'test@example.com',
+ type => 'ward',
+ content => 'your alert will not be activated',
+ email_text => 'confirm the alert',
+ uri =>
+ '/alert/subscribe?type=local&rznvy=test@example.com&feed=ward:1000:1001:A_Location:Diff_Location',
+ param1 => 1000,
+ param2 => 1001,
+ },
+ {
+ email => 'test@example.com',
+ type => 'local',
+ content => 'your alert will not be activated',
+ email_text => 'confirm the alert',
+ uri =>
+ '/alert/subscribe?type=local&rznvy=test@example.com&feed=local:10.2:20.1',
+ param1 => 10.2,
+ param2 => 20.1,
+ }
+) {
+ subtest "$test->{type} alert correctly created" => sub {
+ $mech->clear_emails_ok;
+
+ my $type = $test->{type} . '_problems';
+
+ # we don't want an alert
+ my $alert = FixMyStreet::App->model('DB::Alert')->find( { email =>
+ $test->{email}, alert_type => $type } );
+ $alert->delete() if $alert;
+
+ $mech->get_ok($test->{uri});
+ $mech->content_contains($test->{content});
+
+ $alert = FixMyStreet::App->model('DB::Alert')->find( { email =>
+ $test->{email}, alert_type => $type, parameter =>
+ $test->{param1}, parameter2 => $test->{param2} } );
+
+ ok $alert, "Found the alert";
+
+ SKIP: {
+ skip 'sending email not yet implemented', 4;
+
+ my $email = $mech->get_email;
+ ok $email, "got an email";
+ like $email->body, qr/$test->{email_text}/i, "Correct email text";
+
+ my ($url) = $email->body =~ m{(http://\S+)};
+ ok $url, "extracted confirm url '$url'";
+ };
+ };
+}
+
+done_testing();
diff --git a/templates/web/default/alert/list.html b/templates/web/default/alert/list.html
index 3673d6db3..8b76fd04a 100644
--- a/templates/web/default/alert/list.html
+++ b/templates/web/default/alert/list.html
@@ -9,7 +9,7 @@
<h1>[% title %]</h1>
-<form id="alerts" name="alerts" method="post" action="">
+<form id="alerts" name="alerts" method="post" action="/alert/subscribe">
<input type="hidden" name="type" value="local">
<input type="hidden" name="pc" value="[% pc | html %]">
@@ -35,7 +35,7 @@
</p>
<p id="rss_local">
- <input type="radio" name="feed" id="[% rss_feed_id %]" value="[% rss_feed_id %]">
+ <input type="radio" name="feed" id="[% rss_feed_id %]" value="[% rss_feed_id %]"[% IF rss_feed_id == selected_feed %] checked[% END %]>
<label for="[% rss_feed_id %]">[% tprintf( loc('Problems within %.1fkm of this location'), population_radius ) %]</label>
([% loc('a default distance which covers roughly 200,000 people') %]) <a href='[% rss_feed_uri %]'>
<img src='/i/feed.png' width='16' height='16' title='[% loc('RSS feed of nearby problems') %]' alt='[% loc('RSS feed') %]' border='0'></a>
@@ -63,7 +63,7 @@
[% FOREACH option IN options %]
<li>
- <input type="radio" name="feed" id="[% option.id %]" value="[% option.id %]">
+ <input type="radio" name="feed" id="[% option.id %]" value="[% option.id %]"[% IF option.id == selected_feed %] checked[% END %]>
<label for="[% option.id %]">[% option.text %]</label>
<a href="[% option.uri %]"><img src="/i/feed.png" width="16" height="16"
title="[% option.rss_text %]" alt="RSS feed" border="0"></a>
@@ -77,7 +77,7 @@ title="[% option.rss_text %]" alt="RSS feed" border="0"></a>
<ul>
[% FOREACH option IN reported_to_options %]
<li>
- <input type="radio" name="feed" id="[% option.id %]" value="[% option.id %]">
+ <input type="radio" name="feed" id="[% option.id %]" value="[% option.id %]"[% IF option.id == selected_feed %] checked[% END %]>
<label for="[% option.id %]">[% option.text %]</label>
<a href="[% option.uri %]"><img src="/i/feed.png" width="16" height="16"
title="[% option.rss_text %]" alt="RSS feed" border="0"></a>