aboutsummaryrefslogtreecommitdiffstats
path: root/perllib/FixMyStreet
diff options
context:
space:
mode:
Diffstat (limited to 'perllib/FixMyStreet')
-rw-r--r--perllib/FixMyStreet/Cobrand/Default.pm2
-rw-r--r--perllib/FixMyStreet/Cobrand/FixMyStreet.pm13
-rw-r--r--perllib/FixMyStreet/DB/ResultSet/Problem.pm196
-rw-r--r--perllib/FixMyStreet/Geocode/Bing.pm2
-rw-r--r--perllib/FixMyStreet/SendReport.pm37
-rw-r--r--perllib/FixMyStreet/SendReport/EastHants.pm60
-rw-r--r--perllib/FixMyStreet/SendReport/Email.pm103
-rw-r--r--perllib/FixMyStreet/SendReport/EmptyHomes.pm51
-rw-r--r--perllib/FixMyStreet/SendReport/London.pm111
-rw-r--r--perllib/FixMyStreet/SendReport/Open311.pm69
10 files changed, 643 insertions, 1 deletions
diff --git a/perllib/FixMyStreet/Cobrand/Default.pm b/perllib/FixMyStreet/Cobrand/Default.pm
index 77518e142..cee320cca 100644
--- a/perllib/FixMyStreet/Cobrand/Default.pm
+++ b/perllib/FixMyStreet/Cobrand/Default.pm
@@ -916,6 +916,8 @@ Get stats to display on the council reports page
sub get_report_stats { return 0; }
+sub get_council_sender { return 'Email' };
+
sub example_places {
return [ 'B2 4QA', 'Tib St, Manchester' ];
}
diff --git a/perllib/FixMyStreet/Cobrand/FixMyStreet.pm b/perllib/FixMyStreet/Cobrand/FixMyStreet.pm
index c971a5a82..6c0089d26 100644
--- a/perllib/FixMyStreet/Cobrand/FixMyStreet.pm
+++ b/perllib/FixMyStreet/Cobrand/FixMyStreet.pm
@@ -6,6 +6,19 @@ sub restriction {
return {};
}
+sub get_council_sender {
+ my ( $self, $area_id, $area_info ) = shift;
+
+ my $sender_conf = mySociety::Config::get( 'SENDERS' );
+ return $sender_conf->{ $council } if exists $sender_conf->{ $council };
+
+ return 'London' if $area_info->{type} eq 'LBO';
+
+ return 'Open311' if FixMyStreet::App->model("DB::Open311conf")->search( { area_id => $council, endpoint => { '!=', '' } } )->first;
+
+ return 'Email';
+}
+
sub generate_problem_banner {
my ( $self, $problem ) = @_;
diff --git a/perllib/FixMyStreet/DB/ResultSet/Problem.pm b/perllib/FixMyStreet/DB/ResultSet/Problem.pm
index 3557e77c7..ae184e87b 100644
--- a/perllib/FixMyStreet/DB/ResultSet/Problem.pm
+++ b/perllib/FixMyStreet/DB/ResultSet/Problem.pm
@@ -4,6 +4,16 @@ use base 'DBIx::Class::ResultSet';
use strict;
use warnings;
+use CronFns;
+
+use Utils;
+use mySociety::Config;
+use mySociety::EmailUtil;
+use mySociety::MaPit;
+
+use FixMyStreet::App;
+use FixMyStreet::SendReport;
+
my $site_restriction;
my $site_key;
@@ -209,4 +219,190 @@ sub categories_summary {
return \%categories;
}
+sub send_reports {
+ # Set up site, language etc.
+ my ($verbose, $nomail) = CronFns::options();
+ my $base_url = mySociety::Config::get('BASE_URL');
+ my $site = CronFns::site($base_url);
+
+ my $unsent = FixMyStreet::App->model("DB::Problem")->search( {
+ state => [ 'confirmed', 'fixed' ],
+ whensent => undef,
+ council => { '!=', undef },
+ } );
+ my (%notgot, %note);
+
+ my $s = FixMyStreet::SendReport->new();
+ my $senders = $s->get_senders;
+
+ while (my $row = $unsent->next) {
+
+ my $cobrand = FixMyStreet::Cobrand->get_class_for_moniker($row->cobrand)->new();
+
+ # Cobranded and non-cobranded messages can share a database. In this case, the conf file
+ # should specify a vhost to send the reports for each cobrand, so that they don't get sent
+ # more than once if there are multiple vhosts running off the same database. The email_host
+ # call checks if this is the host that sends mail for this cobrand.
+ next unless $cobrand->email_host();
+ $cobrand->set_lang_and_domain($row->lang, 1);
+ if ( $row->is_from_abuser ) {
+ $row->update( { state => 'hidden' } );
+ next;
+ }
+
+ my $send_email = 0;
+ my $send_web = 0;
+
+ # Template variables for the email
+ my $email_base_url = $cobrand->base_url_for_emails($row->cobrand_data);
+ my %h = map { $_ => $row->$_ } qw/id title detail name category latitude longitude used_map/;
+ map { $h{$_} = $row->user->$_ } qw/email phone/;
+ $h{confirmed} = DateTime::Format::Pg->format_datetime( $row->confirmed->truncate (to => 'second' ) );
+
+ $h{query} = $row->postcode;
+ $h{url} = $email_base_url . '/report/' . $row->id;
+ $h{phone_line} = $h{phone} ? _('Phone:') . " $h{phone}\n\n" : '';
+ if ($row->photo) {
+ $h{has_photo} = _("This web page also contains a photo of the problem, provided by the user.") . "\n\n";
+ $h{image_url} = $email_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} = '';
+
+ # If we are in the UK include eastings and northings, and nearest stuff
+ $h{easting_northing} = '';
+ if ( $cobrand->country eq 'GB' ) {
+
+ ( $h{easting}, $h{northing} ) = Utils::convert_latlon_to_en( $h{latitude}, $h{longitude} );
+
+ # email templates don't have conditionals so we need to farmat this here
+ $h{easting_northing} #
+ = "Easting: $h{easting}\n\n" #
+ . "Northing: $h{northing}\n\n";
+
+ }
+
+ if ( $row->used_map ) {
+ $h{closest_address} = $cobrand->find_closest( $h{latitude}, $h{longitude}, $row );
+ }
+
+ my %reporters = ();
+ my (@to, @recips, $template, $areas_info );
+ if ($site eq 'emptyhomes') {
+
+ my $council = $row->council;
+ $areas_info = mySociety::MaPit::call('areas', $council);
+ my $name = $areas_info->{$council}->{name};
+ my $sender = "FixMyStreet::SendReport::EmptyHomes";
+ $reporters{ $sender } = $sender->new() unless $reporters{$sender};
+ $reporters{ $sender }->add_council( $council, $name );
+ $template = Utils::read_file("$FindBin::Bin/../templates/email/emptyhomes/" . $row->lang . "/submit.txt");
+
+ } else {
+
+ # XXX Needs locks!
+ my @all_councils = split /,|\|/, $row->council;
+ my ($councils, $missing) = $row->council =~ /^([\d,]+)(?:\|([\d,]+))?/;
+ my @councils = split(/,/, $councils);
+ $areas_info = mySociety::MaPit::call('areas', \@all_councils);
+ my @dear;
+
+ foreach my $council (@councils) {
+ my $name = $areas_info->{$council}->{name};
+ push @dear, $name;
+ my $sender = $cobrand->get_council_sender( $council, $areas_info->{$council} );
+ $sender = "FixMyStreet::SendReport::$sender";
+ if ( ! exists $senders->{ $sender } ) {
+ warn "No such sender [ $sender ] for council $name ( $council )";
+ next;
+ }
+ $reporters{ $sender } = $sender->new() unless $reporters{$sender};
+ $reporters{ $sender }->add_council( $council, $name );
+ }
+
+ $template = 'submit.txt';
+ $template = 'submit-brent.txt' if $row->council eq 2488 || $row->council eq 2237;
+ my $template_path = FixMyStreet->path_to( "templates", "email", $cobrand->moniker, $template )->stringify;
+ $template_path = FixMyStreet->path_to( "templates", "email", "default", $template )->stringify
+ unless -e $template_path;
+ $template = Utils::read_file( $template_path );
+
+ if ($h{category} eq _('Other')) {
+ $h{category_footer} = _('this type of local problem');
+ $h{category_line} = '';
+ } else {
+ $h{category_footer} = "'" . $h{category} . "'";
+ $h{category_line} = sprintf(_("Category: %s"), $h{category}) . "\n\n";
+ }
+
+ $h{councils_name} = join(_(' and '), @dear);
+ if ($h{category} eq _('Other')) {
+ $h{multiple} = @dear>1 ? "[ " . _("This email has been sent to both councils covering the location of the problem, as the user did not categorise it; please ignore it if you're not the correct council to deal with the issue, or let us know what category of problem this is so we can add it to our system.") . " ]\n\n"
+ : '';
+ } else {
+ $h{multiple} = @dear>1 ? "[ " . _("This email has been sent to several councils covering the location of the problem, as the category selected is provided for all of them; please ignore it if you're not the correct council to deal with the issue.") . " ]\n\n"
+ : '';
+ }
+ $h{missing} = '';
+ if ($missing) {
+ my $name = $areas_info->{$missing}->{name};
+ $h{missing} = '[ '
+ . sprintf(_('We realise this problem might be the responsibility of %s; however, we don\'t currently have any contact details for them. If you know of an appropriate contact address, please do get in touch.'), $name)
+ . " ]\n\n";
+ }
+
+ }
+
+ unless ($send_email || $send_web || keys %reporters ) {
+ die 'Report not going anywhere for ID ' . $row->id . '!';
+ }
+
+ if (mySociety::Config::get('STAGING_SITE')) {
+ # on a staging server send emails to ourselves rather than the councils
+ $send_web = 0;
+ $send_email = 1;
+ %reporters = (
+ 'FixMyStreet::SendReport::Email' => $reporters{ 'FixMyStreet::SendReport::Email' }
+ );
+ }
+
+ # Special case for this parish council
+ # if ($address && $address =~ /Sprowston/ && $row->council == 2233 && $row->category eq 'Street lighting') {
+ # $h{councils_name} = 'Sprowston Parish Council';
+ # my $e = 'parishclerk' . '@' . 'sprowston-pc.gov.uk';
+ # @to = ( [ $e, $h{councils_name} ] );
+ # @recips = ($e);
+ # }
+
+ # Multiply results together, so one success counts as a success.
+ my $result = -1;
+
+ for my $sender ( keys %reporters ) {
+ $result *= $reporters{ $sender }->send(
+ $row, \%h, \@to, $template, \@recips, $nomail, $areas_info
+ );
+ }
+
+ if ($result == mySociety::EmailUtil::EMAIL_SUCCESS) {
+ $row->update( {
+ whensent => \'ms_current_timestamp()',
+ lastupdate => \'ms_current_timestamp()',
+ } );
+ }
+ }
+
+ if ($verbose) {
+ print "Council email addresses that need checking:\n" if keys %notgot;
+ foreach my $e (keys %notgot) {
+ foreach my $c (keys %{$notgot{$e}}) {
+ print $notgot{$e}{$c} . " problem, to $e category $c (" . $note{$e}{$c}. ")\n";
+ }
+ }
+ }
+}
+
1;
diff --git a/perllib/FixMyStreet/Geocode/Bing.pm b/perllib/FixMyStreet/Geocode/Bing.pm
index 99197a5f9..4ba00dbfe 100644
--- a/perllib/FixMyStreet/Geocode/Bing.pm
+++ b/perllib/FixMyStreet/Geocode/Bing.pm
@@ -62,7 +62,7 @@ sub string {
# Getting duplicate, yet different, results from Bing sometimes
next if @valid_locations
- && $valid_locations[-1]{address}{postalCode} eq $_->{address}{postalCode}
+ && $_->{address}{postalCode} && $valid_locations[-1]{address}{postalCode} eq $_->{address}{postalCode}
&& ( $valid_locations[-1]{address}{locality} eq $_->{address}{adminDistrict2}
|| $valid_locations[-1]{address}{adminDistrict2} eq $_->{address}{locality}
|| $valid_locations[-1]{address}{locality} eq $_->{address}{locality}
diff --git a/perllib/FixMyStreet/SendReport.pm b/perllib/FixMyStreet/SendReport.pm
new file mode 100644
index 000000000..84a89f877
--- /dev/null
+++ b/perllib/FixMyStreet/SendReport.pm
@@ -0,0 +1,37 @@
+package FixMyStreet::SendReport;
+
+use Moose;
+
+use Module::Pluggable
+ sub_name => 'senders',
+ search_path => __PACKAGE__,
+ require => 1;
+
+has 'councils' => (is => 'rw', isa => 'HashRef', default => sub { {} } );
+has 'to' => (is => 'rw', isa => 'ArrayRef', default => sub { [] } );
+
+sub get_senders {
+ my $self = shift;
+
+ my %senders = map { $_ => 1 } $self->senders;
+
+ return \%senders;
+}
+
+sub reset {
+ my $self = shift;
+
+ $self->councils( {} );
+ $self->to( [] );
+}
+
+sub add_council {
+ my $self = shift;
+ my $council = shift;
+ my $name = shift;
+
+ $self->councils->{ $council } = $name;
+}
+
+
+1;
diff --git a/perllib/FixMyStreet/SendReport/EastHants.pm b/perllib/FixMyStreet/SendReport/EastHants.pm
new file mode 100644
index 000000000..61c38e591
--- /dev/null
+++ b/perllib/FixMyStreet/SendReport/EastHants.pm
@@ -0,0 +1,60 @@
+package FixMyStreet::SendReport::EastHants;
+
+use Moose;
+
+BEGIN { extends 'FixMyStreet::SendReport'; }
+
+# export just what we need as error if we use :try
+use Error qw(try otherwise);
+use Encode;
+use mySociety::Web qw(ent);
+use EastHantsWSDL;
+
+sub construct_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;
+}
+
+sub send {
+ return if mySociety::Config::get('STAGING_SITE');
+
+ my ( $row, $h, $to, $template, $recips, $nomail ) = @_;
+
+ # FIXME: should not recreate this each time
+ my $eh_service;
+
+ $h->{category} = 'Customer Services' if $h->{category} eq 'Other';
+ $h->{message} = construct_message( %$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 $message = ent(encode_utf8($h->{message}));
+ my $name = ent(encode_utf8($h->{name}));
+ my $result = $eh_service->INPUTFEEDBACK(
+ $h->{category}, 'FixMyStreet', '', '', $name, $h->{email}, $h->{phone},
+ '', '', '', '', '', '', $message, 'Yes', $h->{image_url}
+ );
+ $return = 0 if $result eq 'Report received';
+ } otherwise {
+ my $e = shift;
+ print "Caught an error: $e\n";
+ };
+ return $return;
+}
+
+1;
diff --git a/perllib/FixMyStreet/SendReport/Email.pm b/perllib/FixMyStreet/SendReport/Email.pm
new file mode 100644
index 000000000..0b8ac8115
--- /dev/null
+++ b/perllib/FixMyStreet/SendReport/Email.pm
@@ -0,0 +1,103 @@
+package FixMyStreet::SendReport::Email;
+
+use Moose;
+
+BEGIN { extends 'FixMyStreet::SendReport'; }
+
+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
+ );
+
+ 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;
diff --git a/perllib/FixMyStreet/SendReport/EmptyHomes.pm b/perllib/FixMyStreet/SendReport/EmptyHomes.pm
new file mode 100644
index 000000000..9453b4ca5
--- /dev/null
+++ b/perllib/FixMyStreet/SendReport/EmptyHomes.pm
@@ -0,0 +1,51 @@
+package FixMyStreet::SendReport::EmptyHomes;
+
+use Moose;
+use namespace::autoclean;
+
+BEGIN { extends 'FixMyStreet::SendReport::Email'; }
+
+sub build_recipient_list {
+ my $self = shift;
+ my $row = shift;
+ my $areas_info = 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 => 'Empty property',
+ } );
+
+ 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;
+
+ my $country = $areas_info->{$council}->{country};
+ if ($country eq 'W') {
+ $recips{ 'shelter@' . mySociety::Config::get('EMAIL_DOMAIN') } = 1;
+ } else {
+ $recips{ 'eha@' . mySociety::Config::get('EMAIL_DOMAIN') } = 1;
+ }
+ }
+
+ return () unless $all_confirmed;
+ return keys %recips;
+}
+
+1;
diff --git a/perllib/FixMyStreet/SendReport/London.pm b/perllib/FixMyStreet/SendReport/London.pm
new file mode 100644
index 000000000..9296f2446
--- /dev/null
+++ b/perllib/FixMyStreet/SendReport/London.pm
@@ -0,0 +1,111 @@
+package FixMyStreet::SendReport::London;
+
+use Moose;
+
+BEGIN { extends 'FixMyStreet::SendReport'; }
+
+use Digest::MD5;
+use LWP::UserAgent;
+use LWP::Simple;
+
+use Utils;
+
+sub construct_message {
+ my %h = @_;
+ return <<EOF,
+A user of FixMyStreet has submitted the following report of a local
+problem that they believe might require your attention.
+
+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}
+Yours,
+The FixMyStreet team
+EOF
+}
+
+sub send {
+ return if mySociety::Config::get('STAGING_SITE');
+ my ( $row, $h, $to, $template, $recips, $nomail ) = @_;
+
+ $h->{message} = construct_message( %$h );
+ my $phone = $h->{phone};
+ my $mobile = '';
+ if ($phone && $phone =~ /^\s*07/) {
+ $mobile = $phone;
+ $phone = '';
+ }
+ my ($first, $last) = $h->{name} =~ /^(\S*)(?: (.*))?$/;
+ my %params = (
+ Key => mySociety::Config::get('LONDON_REPORTIT_KEY'),
+ Signature => Digest::MD5::md5_hex( $h->{confirmed} . mySociety::Config::get('LONDON_REPORTIT_SECRET') ),
+ Type => Utils::london_categories()->{$h->{category}},
+ RequestDate => $h->{confirmed},
+ RequestMethod => 'Web',
+ ExternalId => $h->{url},
+ 'Customer.Title' => '',
+ 'Customer.FirstName' => $first,
+ 'Customer.Surname' => $last,
+ 'Customer.Email' => $h->{email},
+ 'Customer.Phone' => $phone,
+ 'Customer.Mobile' => $mobile,
+ 'ProblemDescription' => $h->{message},
+ );
+ if ($h->{used_map}) {
+ $params{'Location.Latitude'} = $h->{latitude};
+ $params{'Location.Longitude'} = $h->{longitude};
+ } elsif (mySociety::PostcodeUtil::is_valid_postcode($h->{query})) {
+ # Didn't use map, and entered postcode, so use that.
+ $params{'Location.Postcode'} = $h->{query};
+ } else {
+ # Otherwise, lat/lon is all we have, even if it's wrong.
+ $params{'Location.Latitude'} = $h->{latitude};
+ $params{'Location.Longitude'} = $h->{longitude};
+ }
+ if ($h->{has_photo}) {
+ $params{'Document1.Name'} = 'Photograph';
+ $params{'Document1.MimeType'} = 'image/jpeg';
+ $params{'Document1.URL'} = $h->{image_url};
+ $params{'Document1.URLPublic'} = 'true';
+ }
+ my $browser = LWP::UserAgent->new;
+ my $response = $browser->post( mySociety::Config::get('LONDON_REPORTIT_URL'), \%params );
+ my $out = $response->content;
+ if ($response->code ne 200) {
+ print "Failed to post $h->{id} to London API, response was " . $response->code . " $out\n";
+ return 1;
+ }
+ my ($id) = $out =~ /<caseid>(.*?)<\/caseid>/;
+ my ($org) = $out =~ /<organisation>(.*?)<\/organisation>/;
+ my ($team) = $out =~ /<team>(.*?)<\/team>/;
+
+ $org = london_lookup($org);
+ $row->external_id( $id );
+ $row->external_body( $org );
+ $row->external_team( $team );
+ return 0;
+}
+
+sub london_lookup {
+ my $org = shift || '';
+ my $str = "Unknown ($org)";
+ open(FP, "$FindBin::Bin/../data/dft.csv");
+ while (<FP>) {
+ /^(.*?),(.*)/;
+ if ($org eq $1) {
+ $str = $2;
+ last;
+ }
+ }
+ close FP;
+ return $str;
+}
+
+1;
diff --git a/perllib/FixMyStreet/SendReport/Open311.pm b/perllib/FixMyStreet/SendReport/Open311.pm
new file mode 100644
index 000000000..cb41de336
--- /dev/null
+++ b/perllib/FixMyStreet/SendReport/Open311.pm
@@ -0,0 +1,69 @@
+package FixMyStreet::SendReport::Open311;
+
+use Moose;
+use namespace::autoclean;
+
+BEGIN { extends 'FixMyStreet::SendReport'; }
+
+use FixMyStreet::App;
+use mySociety::Config;
+use Open311;
+
+sub send {
+ return if mySociety::Config::get('STAGING_SITE');
+ my $self = shift;
+ my ( $row, $h, $to, $template, $recips, $nomail ) = @_;
+
+ my $result = -1;
+
+ foreach my $council ( keys %{ $self->councils } ) {
+ my $conf = FixMyStreet::App->model("DB::Open311conf")->search( { area_id => $self->councils->{ $council }, endpoint => { '!=', '' } } )->first;
+ #print 'posting to end point for ' . $conf->area_id . "\n" if $verbose;
+
+
+ # FIXME: we've already looked this up before
+ my $contact = FixMyStreet::App->model("DB::Contact")->find( {
+ deleted => 0,
+ area_id => $conf->area_id,
+ category => $row->category
+ } );
+
+ my $open311 = Open311->new(
+ jurisdiction => $conf->jurisdiction,
+ endpoint => $conf->endpoint,
+ api_key => $conf->api_key,
+ );
+
+ # non standard west berks end points
+ if ( $row->council =~ /2619/ ) {
+ $open311->endpoints( { services => 'Services', requests => 'Requests' } );
+ }
+
+ # required to get round issues with CRM constraints
+ if ( $row->council =~ /2218/ ) {
+ $row->user->name( $row->user->id . ' ' . $row->user->name );
+ }
+
+ my $resp = $open311->send_service_request( $row, $h, $contact->email );
+
+ # make sure we don't save user changes from above
+ if ( $row->council =~ /2218/ ) {
+ $row->discard_changes();
+ }
+
+ if ( $resp ) {
+ $row->external_id( $resp );
+ $result *= 0;
+ } else {
+ $result *= 1;
+ # temporary fix to resolve some issues with west berks
+ if ( $row->council =~ /2619/ ) {
+ $result *= 0;
+ }
+ }
+ }
+
+ return $result;
+}
+
+1;