#!/usr/bin/perl -w # send-reports: # Send new problem reports to councils # # Copyright (c) 2007 UK Citizens Online Democracy. All rights reserved. # Email: matthew@mysociety.org. WWW: http://www.mysociety.org # # $Id: send-reports,v 1.10 2007-02-06 10:32:04 matthew Exp $ use strict; require 5.8.0; # Horrible boilerplate to set up appropriate library paths. use FindBin; use lib "$FindBin::Bin/../perllib"; use lib "$FindBin::Bin/../../perllib"; use File::Slurp; use mySociety::Config; use mySociety::DBHandle qw(dbh select_all); use mySociety::Email; use mySociety::MaPit; use mySociety::Util; BEGIN { mySociety::Config::set_file("$FindBin::Bin/../conf/general"); mySociety::DBHandle::configure( Name => mySociety::Config::get('BCI_DB_NAME'), User => mySociety::Config::get('BCI_DB_USER'), Password => mySociety::Config::get('BCI_DB_PASS'), Host => mySociety::Config::get('BCI_DB_HOST', undef), Port => mySociety::Config::get('BCI_DB_PORT', undef) ); } die "No arguments or specify --nomail" if (@ARGV>1); my $nomail = 0; $nomail = 1 if (@ARGV==1 && $ARGV[0] eq '--nomail'); my $unsent = dbh()->selectall_arrayref( "SELECT id, council, title, detail, name, email, phone, used_map FROM problem WHERE state = 'confirmed' AND whensent IS NULL", { Slice => {} }); foreach my $row (@$unsent) { # XXX Needs locks! my @councils = split ',', $row->{council}; my $areas_info = mySociety::MaPit::get_voting_areas_info(\@councils); my (@to, @recips); foreach my $council (@councils) { my $name = $areas_info->{$council}->{name}; my $council_email = dbh()->selectrow_array('SELECT email FROM contacts WHERE area_id=?', {}, $council); next unless $council_email; # Ignore missing councils push @to, [ $council_email, $name ]; push @recips, $council_email; } print 'Need to send problem #' . $row->{id} . ' to council(s) ' . join(',',@recips) . "\n"; if (!@to) { print " ...but we have no contact details for any of them!\n"; next; } push @recips, mySociety::Config::get('CONTACT_EMAIL'); my $template = File::Slurp::read_file("$FindBin::Bin/../templates/emails/submit-council"); my %h = map { $_ => $row->{$_} } qw/title detail/; $h{user_details} = $row->{name} . ' <' . $row->{email} . '>'; $h{user_details} .= ' (' . $row->{phone} . ')' if $row->{phone}; $h{url} = mySociety::Config::get('BASE_URL') . '/?id=' . $row->{id}; $h{councils_name} = join(' and ', map { $areas_info->{$_}->{name} } @councils); $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'; my $email = mySociety::Email::construct_email({ _template_ => $template, _parameters_ => \%h, From => [ mySociety::Config::get('CONTACT_EMAIL'), mySociety::Config::get('CONTACT_NAME') ], To => \@to, 'Reply-To' => [ [ $row->{email}, $row->{name} ] ] }); my $result; if (mySociety::Config::get('STAGING_SITE') || $nomail) { $result = -1; } else { $result = mySociety::Util::send_email($email, mySociety::Config::get('CONTACT_EMAIL'), @recips); } if ($result == mySociety::Util::EMAIL_SUCCESS) { dbh()->do('UPDATE problem SET whensent=ms_current_timestamp() WHERE id=?', {}, $row->{id}); dbh()->commit(); } else { dbh()->rollback(); } }