diff options
author | Dave Whiteland <dave@mysociety.org> | 2012-05-29 15:57:41 +0100 |
---|---|---|
committer | Dave Whiteland <dave@mysociety.org> | 2012-05-29 15:57:41 +0100 |
commit | 67da8efc720d2d0bd22bd9fe8655b7e983b35bb4 (patch) | |
tree | 38b8570647124df06c637d4b923f6010211ef328 /perllib/FixMyStreet/SendReport/Email.pm | |
parent | 40b3a51d33caefa8f5fb97ce9be18ef936c7e260 (diff) | |
parent | 131ff6e9bf3626d6a8fff6ae54669d250148a63a (diff) |
Merge remote branch 'origin/master' into fmb-read-only
Conflicts:
bin/send-reports
perllib/FixMyStreet/Cobrand/Default.pm
perllib/FixMyStreet/Cobrand/FixMyStreet.pm
templates/web/fixmystreet/alert/index.html
templates/web/fixmystreet/around/display_location.html
web/cobrands/fixmystreet/_layout.scss
web/js/map-OpenLayers.js
Diffstat (limited to 'perllib/FixMyStreet/SendReport/Email.pm')
-rw-r--r-- | perllib/FixMyStreet/SendReport/Email.pm | 111 |
1 files changed, 111 insertions, 0 deletions
diff --git a/perllib/FixMyStreet/SendReport/Email.pm b/perllib/FixMyStreet/SendReport/Email.pm new file mode 100644 index 000000000..239bee715 --- /dev/null +++ b/perllib/FixMyStreet/SendReport/Email.pm @@ -0,0 +1,111 @@ +package FixMyStreet::SendReport::Email; + +use Moose; + +BEGIN { extends 'FixMyStreet::SendReport'; } + +use mySociety::EmailUtil; + +sub build_recipient_list { + my $self = shift; + my $row = shift; + my %recips; + + my $all_confirmed = 1; + foreach my $council ( keys %{ $self->councils } ) { + my $contact = FixMyStreet::App->model("DB::Contact")->find( { + deleted => 0, + area_id => $council, + category => $row->category + } ); + + my ($council_email, $confirmed, $note) = ( $contact->email, $contact->confirmed, $contact->note ); + + $council_email = essex_contact($row->latitude, $row->longitude) if $council == 2225; + $council_email = oxfordshire_contact($row->latitude, $row->longitude) if $council == 2237 && $council_email eq 'SPECIAL'; + + unless ($confirmed) { + $all_confirmed = 0; + #$note = 'Council ' . $row->council . ' deleted' + #unless $note; + $council_email = 'N/A' unless $council_email; + #$notgot{$council_email}{$row->category}++; + #$note{$council_email}{$row->category} = $note; + } + + push @{ $self->to }, [ $council_email, $self->councils->{ $council } ]; + $recips{$council_email} = 1; + } + + return () unless $all_confirmed; + return keys %recips; +} + +sub send { + my $self = shift; + my ( $row, $h, $to, $template, $recips, $nomail, $areas_info ) = @_; + + my @recips; + + @recips = $self->build_recipient_list( $row, $areas_info ); + + # on a staging server send emails to ourselves rather than the councils + if (mySociety::Config::get('STAGING_SITE')) { + @recips = ( mySociety::Config::get('CONTACT_EMAIL') ); + } + + return unless @recips; + + my $result = FixMyStreet::App->send_email_cron( + { + _template_ => $template, + _parameters_ => $h, + To => $self->to, + From => [ $row->user->email, $row->name ], + }, + mySociety::Config::get('CONTACT_EMAIL'), + \@recips, + $nomail + ); + + if ( $result == mySociety::EmailUtil::EMAIL_SUCCESS ) { + $self->success(1); + } else { + $self->error( 'Failed to send email' ); + } + + return $result; +} + +# Essex has different contact addresses depending upon the district +# Might be easier if we start storing in the db all areas covered by a point +# Will do for now :) +sub essex_contact { + my $district = _get_district_for_contact(@_); + my $email; + $email = 'eastarea' if $district == 2315 || $district == 2312; + $email = 'midarea' if $district == 2317 || $district == 2314 || $district == 2316; + $email = 'southarea' if $district == 2319 || $district == 2320 || $district == 2310; + $email = 'westarea' if $district == 2309 || $district == 2311 || $district == 2318 || $district == 2313; + die "Returned district $district which is not in Essex!" unless $email; + return "highways.$email\@essexcc.gov.uk"; +} + +# Oxfordshire has different contact addresses depending upon the district +sub oxfordshire_contact { + my $district = _get_district_for_contact(@_); + my $email; + $email = 'northernarea' if $district == 2419 || $district == 2420 || $district == 2421; + $email = 'southernarea' if $district == 2417 || $district == 2418; + die "Returned district $district which is not in Oxfordshire!" unless $email; + return "$email\@oxfordshire.gov.uk"; +} + +sub _get_district_for_contact { + my ( $lat, $lon ) = @_; + my $district = + mySociety::MaPit::call( 'point', "4326/$lon,$lat", type => 'DIS' ); + ($district) = keys %$district; + return $district; +} +1; |