diff options
Diffstat (limited to 'perllib/FixMyStreet/DB/ResultSet/Problem.pm')
-rw-r--r-- | perllib/FixMyStreet/DB/ResultSet/Problem.pm | 97 |
1 files changed, 91 insertions, 6 deletions
diff --git a/perllib/FixMyStreet/DB/ResultSet/Problem.pm b/perllib/FixMyStreet/DB/ResultSet/Problem.pm index 4036c4b05..faed3b8ac 100644 --- a/perllib/FixMyStreet/DB/ResultSet/Problem.pm +++ b/perllib/FixMyStreet/DB/ResultSet/Problem.pm @@ -85,7 +85,8 @@ sub _recent { $key .= ":$site_key:$num"; my $query = { - state => [ FixMyStreet::DB::Result::Problem->visible_states() ], + non_public => 0, + state => [ FixMyStreet::DB::Result::Problem->visible_states() ], }; $query->{photo} = { '!=', undef } if $photos; @@ -141,6 +142,7 @@ sub around_map { $attr->{rows} = $limit if $limit; my $q = { + non_public => 0, state => [ FixMyStreet::DB::Result::Problem->visible_states() ], latitude => { '>=', $min_lat, '<', $max_lat }, longitude => { '>=', $min_lon, '<', $max_lon }, @@ -286,6 +288,16 @@ sub send_reports { $h{closest_address} = $cobrand->find_closest( $h{latitude}, $h{longitude}, $row ); } + if ( $cobrand->allow_anonymous_reports && + $row->user->email eq $cobrand->anonymous_account->{'email'} + ) { + $h{anonymous_report} = 1; + $h{user_details} = _('This report was submitted anonymously'); + } else { + $h{user_details} = sprintf(_('Name: %s'), $row->name) . "\n\n"; + $h{user_details} .= sprintf(_('Email: %s'), $row->user->email) . "\n\n"; + } + my %reporters = (); my ( $sender_count ); if ($site eq 'emptyhomes') { @@ -308,8 +320,8 @@ sub send_reports { foreach my $council (@councils) { my $name = $areas_info->{$council}->{name}; - my $sender = $cobrand->get_council_sender( $council, $areas_info->{$council} ); - $sender = "FixMyStreet::SendReport::$sender"; + my $sender_info = $cobrand->get_council_sender( $council, $areas_info->{$council}, $row->category ); + my $sender = "FixMyStreet::SendReport::" . $sender_info->{method}; if ( ! exists $senders->{ $sender } ) { warn "No such sender [ $sender ] for council $name ( $council )"; @@ -322,7 +334,7 @@ sub send_reports { $reporters{ $sender }->skipped; } else { push @dear, $name; - $reporters{ $sender }->add_council( $council, $areas_info->{$council} ); + $reporters{ $sender }->add_council( $council, $areas_info->{$council}, $sender_info->{config} ); } } @@ -334,6 +346,10 @@ sub send_reports { $h{category_line} = sprintf(_("Category: %s"), $h{category}) . "\n\n"; } + if ( $row->subcategory ) { + $h{subcategory_line} = sprintf(_("Subcategory: %s"), $row->subcategory) . "\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" @@ -361,8 +377,33 @@ sub send_reports { if (mySociety::Config::get('STAGING_SITE')) { # on a staging server send emails to ourselves rather than the councils - my @testing_councils = split( '\|', mySociety::Config::get('TESTING_COUNCILS') || '' ); - unless ( grep { $row->council eq $_ } @testing_councils ) { + # however, we can configure a list of councils that we use non email + # delivery, e.g. Open311, for testing purposes. For those we want to + # send using the non email method and for everyone else we want to use + # email + my @testing_councils = split( '\|', mySociety::Config::get('TESTING_COUNCILS') ); + + # we only care about non missing councils so we get the missing ones + # and then essentially throw them away as we're not going to have + # configured them to do anything. + my %councils = map { $_ => 1 } @{ $row->councils }; + + # We now take the councils that we have contact details for and if any of them + # are in the list of testing councils we look a bit harder otherwise we throw + # away all the non email delivery methods + if ( grep { $councils{ $_ } } @testing_councils ) { + my %tc = map { $_ => 1 } @testing_councils; + my @non_matching = grep { !$tc{$_} } keys %councils; + for my $sender ( keys %reporters ) { + next if $sender =~ /FixMyStreet::SendReport::(Email|NI)/; + for my $council ( @non_matching ) { + $reporters{$sender}->delete_council( $council ); + } + } + if ( @non_matching ) { + $reporters{'FixMyStreet::SendReport::Email'} = FixMyStreet::SendReport::Email->new(); + } + } else { %reporters = map { $_ => $reporters{$_} } grep { /FixMyStreet::SendReport::(Email|NI)/ } keys %reporters; unless (%reporters) { %reporters = ( 'FixMyStreet::SendReport::Email' => FixMyStreet::SendReport::Email->new() ); @@ -393,6 +434,9 @@ sub send_reports { whensent => \'ms_current_timestamp()', lastupdate => \'ms_current_timestamp()', } ); + if ( $cobrand->report_sent_confirmation_email && !$h{anonymous_report}) { + _send_report_sent_email( $row, \%h, $nomail ); + } } else { my @errors; for my $sender ( keys %reporters ) { @@ -420,7 +464,48 @@ sub send_reports { } printf " %-24s %4d\n", "Total:", $c; } + my $sending_errors = ''; + my $unsent = FixMyStreet::App->model("DB::Problem")->search( { + state => [ 'confirmed', 'fixed' ], + whensent => undef, + council => { '!=', undef }, + send_fail_count => { '>', 0 } + } ); + while (my $row = $unsent->next) { + $sending_errors .= "* http://www.fixmystreet.com/report/" . $row->id . ", failed " + . $row->send_fail_count . " times, last at " . $row->send_fail_timestamp + . ", reason " . $row->send_fail_reason . "\n"; + } + if ($sending_errors) { + print "The following reports had problems sending:\n$sending_errors"; + } } } +sub _send_report_sent_email { + my $row = shift; + my $h = shift; + my $nomail = shift; + + my $template = 'confirm_report_sent.txt'; + my $template_path = FixMyStreet->path_to( "templates", "email", $row->cobrand, $row->lang, $template )->stringify; + $template_path = FixMyStreet->path_to( "templates", "email", $row->cobrand, $template )->stringify + unless -e $template_path; + $template_path = FixMyStreet->path_to( "templates", "email", "default", $template )->stringify + unless -e $template_path; + $template = Utils::read_file( $template_path ); + + my $result = FixMyStreet::App->send_email_cron( + { + _template_ => $template, + _parameters_ => $h, + To => $row->user->email, + From => mySociety::Config::get('CONTACT_EMAIL'), + }, + mySociety::Config::get('CONTACT_EMAIL'), + [ $row->user->email ], + $nomail + ); +} + 1; |