aboutsummaryrefslogtreecommitdiffstats
path: root/bin/send-reports
diff options
context:
space:
mode:
Diffstat (limited to 'bin/send-reports')
-rwxr-xr-xbin/send-reports152
1 files changed, 105 insertions, 47 deletions
diff --git a/bin/send-reports b/bin/send-reports
index 15037cc55..a0fac7578 100755
--- a/bin/send-reports
+++ b/bin/send-reports
@@ -6,7 +6,7 @@
# Copyright (c) 2007 UK Citizens Online Democracy. All rights reserved.
# Email: matthew@mysociety.org. WWW: http://www.mysociety.org
#
-# $Id: send-reports,v 1.59 2008-10-09 15:23:08 matthew Exp $
+# $Id: send-reports,v 1.60 2008-10-16 16:18:18 matthew Exp $
use strict;
require 5.8.0;
@@ -15,8 +15,10 @@ require 5.8.0;
use FindBin;
use lib "$FindBin::Bin/../perllib";
use lib "$FindBin::Bin/../../perllib";
+use Error qw(:try);
use File::Slurp;
+use EastHantsWSDL;
use mySociety::Config;
use mySociety::DBHandle qw(dbh);
use mySociety::Email;
@@ -35,7 +37,7 @@ BEGIN {
);
}
-use mySociety::Dress;
+use mySociety::Dress; # Has to come after the BEGIN, my poor coding
die "Either no arguments, --nomail or --verbose" if (@ARGV>1);
my $nomail = 0;
@@ -46,7 +48,7 @@ $verbose = 1 if $nomail;
my $base_url = mySociety::Config::get('BASE_URL');
my $site = 'fixmystreet';
-$site = 'emptyhomes' if $base_url =~ 'emptyhomes' || $base_url eq 'http://matthew.fixmystreet.com'; # XXX
+$site = 'emptyhomes' if $base_url =~ 'emptyhomes'; # || $base_url eq 'http://matthew.fixmystreet.com'; # XXX
my $query = "SELECT id, council, category, title, detail, name, email, phone,
used_map, easting, northing, (photo is not null) as has_photo
@@ -63,12 +65,20 @@ foreach my $row (@$unsent) {
next;
}
+ my $send_email = 0;
+ my $send_web = 0;
+
# Template variables for the email
my %h = map { $_ => $row->{$_} } qw/title detail name email phone category easting northing/;
- $h{phone} = "Phone: $h{phone}\n\n" if $h{phone};
- $h{has_photo} = '';
- $h{has_photo} = "This web page also contains a photo of the problem, provided by the user.\n\n" if $row->{has_photo};
$h{url} = $base_url . '/report/' . $row->{id};
+ $h{phone_line} = $h{phone} ? "Phone: $h{phone}\n\n" : '';
+ if ($row->{has_photo}) {
+ $h{has_photo} = "This web page also contains a photo of the problem, provided by the user.\n\n";
+ $h{image_url} = $base_url . '/photo?id=' . $row->{id};
+ } else {
+ $h{has_photo} = '';
+ $h{image_url} = '';
+ }
$h{fuzzy} = $row->{used_map} ? 'To view a map of the precise location of this issue'
: 'The user could not locate the problem on a map, but to see the area around the location they entered';
$h{closest_address} = '';
@@ -94,11 +104,12 @@ foreach my $row (@$unsent) {
$council_email = 'N/A' unless $council_email;
$notgot{$council_email}{$row->{category}}++;
$note{$council_email}{$row->{category}} = $note;
- next;
+ next;
}
push @to, [ $council_email, $name ];
@recips = ($council_email);
+ $send_email = 1;
$template = File::Slurp::read_file("$FindBin::Bin/../templates/emails/submit-eha");
} else {
@@ -112,32 +123,31 @@ foreach my $row (@$unsent) {
my $all_confirmed = 1;
foreach my $council (@councils) {
my $name = $areas_info->{$council}->{name};
- my ($council_email, $confirmed, $note) = dbh()->selectrow_array(
- "SELECT email,confirmed,note FROM contacts WHERE deleted='f'
- and area_id=? AND category=?", {}, $council, $row->{category});
- $council_email = essex_contact($row->{easting}, $row->{northing}) if $council == 2225;
- $council_email = notts_contact($row->{easting}, $row->{northing}) if $council == 2236;
- 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 @to, [ $council_email, $name ];
push @dear, $name;
- $recips{$council_email} = 1;
- }
- @recips = keys %recips;
- if (!@to) {
- if ($verbose) {
- print 'Need to send problem #' . $row->{id} . ' to council(s) ' . join(',',@recips) . "\n";
- print " ...but we have no contact details for any of them!\n";
+ if ($council == 2330) { # E. Hants have a web service
+ $send_web = 1;
+ $h{category} = 'Customer Services' if $h{category} eq 'Other';
+ } else {
+ my ($council_email, $confirmed, $note) = dbh()->selectrow_array(
+ "SELECT email,confirmed,note FROM contacts WHERE deleted='f'
+ and area_id=? AND category=?", {}, $council, $row->{category});
+ $council_email = essex_contact($row->{easting}, $row->{northing}) if $council == 2225;
+ $council_email = notts_contact($row->{easting}, $row->{northing}) if $council == 2236;
+ 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 @to, [ $council_email, $name ];
+ $recips{$council_email} = 1;
+ $send_email = 1;
}
- next;
}
- next unless ($all_confirmed);
+ @recips = keys %recips;
+ next unless $all_confirmed;
$template = 'submit-council';
$template = 'submit-brent' if $row->{council} eq 2488;
@@ -145,10 +155,10 @@ foreach my $row (@$unsent) {
if ($h{category} eq 'Other') {
$h{category_footer} = 'this type of local problem';
- $h{category} = '';
+ $h{category_line} = '';
} else {
$h{category_footer} = "'" . $h{category} . "'";
- $h{category} = "Category: $h{category}\n\n";
+ $h{category_line} = "Category: $h{category}\n\n";
}
$h{councils_name} = join(' and ', @dear);
@@ -164,8 +174,8 @@ If you know of an appropriate contact address, please do get in touch. ]\n\n";
}
- unless (@recips) {
- die 'Email not going anywhere for ID ' . $row->{id} . '!';
+ unless ($send_email || $send_web) {
+ die 'Report not going anywhere for ID ' . $row->{id} . '!';
}
if (mySociety::Config::get('STAGING_SITE')) {
@@ -182,20 +192,29 @@ If you know of an appropriate contact address, please do get in touch. ]\n\n";
# @recips = ($e);
# }
- my $email = mySociety::Email::construct_email({
- _template_ => $template,
- _parameters_ => \%h,
- To => \@to,
- From => [ $row->{email}, $row->{name} ],
- 'Message-ID' => sprintf('<report-%s-%s@mysociety.org>', time(), unpack('h*', random_bytes(5, 1))),
- });
-
- my $result;
- if ($nomail) {
- $result = -1;
- } else {
- $result = mySociety::EmailUtil::send_email($email, mySociety::Config::get('CONTACT_EMAIL'), @recips);
+ # Multiply results together, so one success counts as a success.
+ my $result = -1;
+
+ if ($send_email) {
+ my $email = mySociety::Email::construct_email({
+ _template_ => $template,
+ _parameters_ => \%h,
+ To => \@to,
+ From => [ $row->{email}, $row->{name} ],
+ 'Message-ID' => sprintf('<report-%s-%s@mysociety.org>', time(), unpack('h*', random_bytes(5, 1))),
+ });
+ if (!$nomail) {
+ $result *= mySociety::EmailUtil::send_email($email, mySociety::Config::get('CONTACT_EMAIL'), @recips);
+ }
}
+
+ if ($send_web) {
+ $h{message} = construct_easthants_message(%h);
+ if (!$nomail) {
+ $result *= post_easthants_message(%h);
+ }
+ }
+
if ($result == mySociety::EmailUtil::EMAIL_SUCCESS) {
dbh()->do('UPDATE problem SET whensent=ms_current_timestamp(),
lastupdate=ms_current_timestamp() WHERE id=?', {}, $row->{id});
@@ -243,3 +262,42 @@ sub notts_contact {
die "Returned district $district which is not in Nottinghamshire!" unless $email;
return "$email\@nottscc.gov.uk";
}
+
+sub construct_easthants_message {
+ my %h = @_;
+ my $message = '';
+ $message .= "[ This report was also sent to the district council covering the location of the problem, as the user did not categorise it; please ignore if you're not the correct council to deal with the issue. ]\n\n"
+ if $h{multiple};
+ $message .= <<EOF;
+Subject: $h{title}
+
+Details: $h{detail}
+
+$h{fuzzy}, or to provide an update on the problem, please visit the following link:
+
+$h{url}
+
+$h{closest_address}
+EOF
+ return $message;
+}
+
+my $eh_service;
+sub post_easthants_message {
+ my %h = @_;
+ my $return = 1;
+ $eh_service ||= EastHantsWSDL->on_fault(sub { my($soap, $res) = @_; die ref $res ? $res->faultstring : $soap->transport->status, "\n"; });
+ try {
+ # ServiceName, RemoteCreatedBy, Salutation, FirstName, Name, Email, Telephone, HouseNoName, Street, Town, County, Country, Postcode, Comments, FurtherInfo, ImageURL
+ my $result = $eh_service->INPUTFEEDBACK(
+ $h{category}, 'FixMyStreet', '', '', $h{name}, $h{email}, $h{phone},
+ '', '', '', '', '', '', $h{message}, 'Yes', $h{image_url}
+ );
+ $return = 0 if $result eq 'Report received';
+ } otherwise {
+ my $e = shift;
+ print "Caught an error: $e\n";
+ };
+ return $return;
+}
+