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
|
package FixMyStreet::SendReport::Email;
use Moose;
BEGIN { extends 'FixMyStreet::SendReport'; }
sub build_recipient_list {
my ( $self, $row, $h ) = @_;
my $all_confirmed = 1;
foreach my $body ( @{ $self->bodies } ) {
my $contact = FixMyStreet::App->model("DB::Contact")->find( {
deleted => 0,
body_id => $body->id,
category => $row->category
} );
my ($body_email, $confirmed, $note) = ( $contact->email, $contact->confirmed, $contact->note );
$body_email = essex_contact($row->latitude, $row->longitude) if $body->areas->{2225};
$body_email = oxfordshire_contact($row->latitude, $row->longitude) if $body->areas->{2237} && $body_email eq 'SPECIAL';
unless ($confirmed) {
$all_confirmed = 0;
$note = 'Body ' . $row->bodies_str . ' deleted'
unless $note;
$body_email = 'N/A' unless $body_email;
$self->unconfirmed_counts->{$body_email}{$row->category}++;
$self->unconfirmed_notes->{$body_email}{$row->category} = $note;
}
my $body_name = $body->name;
# see something uses council areas but doesn't send to councils so just use a
# generic name here to minimise confusion
if ( $row->cobrand eq 'seesomething' ) {
$body_name = 'See Something, Say Something';
}
my @emails;
# allow multiple emails per contact
if ( $body_email =~ /,/ ) {
@emails = split(/,/, $body_email);
} else {
@emails = ( $body_email );
}
for my $email ( @emails ) {
push @{ $self->to }, [ $email, $body_name ];
}
}
return $all_confirmed && @{$self->to};
}
sub get_template {
my ( $self, $row ) = @_;
my $template = 'submit.txt';
if ($row->cobrand eq 'fixmystreet') {
$template = 'submit-oxfordshire.txt' if $row->bodies_str eq 2237;
}
$template = FixMyStreet->get_email_template($row->cobrand, $row->lang, $template);
return $template;
}
sub send_from {
my ( $self, $row ) = @_;
return [ $row->user->email, $row->name ];
}
sub send {
my $self = shift;
my ( $row, $h ) = @_;
my $recips = $self->build_recipient_list( $row, $h );
# on a staging server send emails to ourselves rather than the bodies
if (mySociety::Config::get('STAGING_SITE') && !mySociety::Config::get('SEND_REPORTS_ON_STAGING') && !FixMyStreet->test_mode) {
$recips = 1;
@{$self->to} = [ $row->user->email, $self->to->[0][1] || $row->name ];
}
unless ($recips) {
$self->error( 'No recipients' );
return 1;
}
my ($verbose, $nomail) = CronFns::options();
my $cobrand = FixMyStreet::Cobrand->get_class_for_moniker($row->cobrand)->new();
my $params = {
_template_ => $self->get_template( $row ),
_parameters_ => $h,
To => $self->to,
From => $self->send_from( $row ),
};
$params->{Bcc} = $self->bcc if @{$self->bcc};
my $result = FixMyStreet::App->send_email_cron(
$params,
mySociety::Config::get('CONTACT_EMAIL'),
$nomail,
$cobrand
);
unless ($result) {
$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;
|