aboutsummaryrefslogtreecommitdiffstats
path: root/perllib/FixMyStreet
diff options
context:
space:
mode:
Diffstat (limited to 'perllib/FixMyStreet')
-rw-r--r--perllib/FixMyStreet/App.pm3
-rw-r--r--perllib/FixMyStreet/App/Controller/Admin.pm89
-rw-r--r--perllib/FixMyStreet/App/Controller/Alert.pm7
-rw-r--r--perllib/FixMyStreet/App/Controller/Photo.pm2
-rw-r--r--perllib/FixMyStreet/App/Controller/Report.pm11
-rw-r--r--perllib/FixMyStreet/App/Controller/Report/New.pm5
-rw-r--r--perllib/FixMyStreet/App/Controller/Report/Update.pm5
-rwxr-xr-xperllib/FixMyStreet/App/Controller/Rss.pm10
-rwxr-xr-xperllib/FixMyStreet/App/Controller/Status.pm2
-rw-r--r--perllib/FixMyStreet/App/Model/PhotoSet.pm23
-rw-r--r--perllib/FixMyStreet/Cobrand/Zurich.pm8
-rw-r--r--perllib/FixMyStreet/DB/Result/Problem.pm30
-rw-r--r--perllib/FixMyStreet/DB/Result/User.pm5
-rw-r--r--perllib/FixMyStreet/Email.pm111
-rw-r--r--perllib/FixMyStreet/Gaze.pm24
-rw-r--r--perllib/FixMyStreet/Map.pm10
-rw-r--r--perllib/FixMyStreet/Map/Google.pm4
-rw-r--r--perllib/FixMyStreet/Map/OSM.pm4
-rw-r--r--perllib/FixMyStreet/Script/Alerts.pm12
-rw-r--r--perllib/FixMyStreet/Script/Questionnaires.pm9
-rw-r--r--perllib/FixMyStreet/Script/Reports.pm9
-rw-r--r--perllib/FixMyStreet/SendReport/Email.pm17
-rw-r--r--perllib/FixMyStreet/SendReport/Zurich.pm2
23 files changed, 220 insertions, 182 deletions
diff --git a/perllib/FixMyStreet/App.pm b/perllib/FixMyStreet/App.pm
index 79ca7f9ee..1a651d282 100644
--- a/perllib/FixMyStreet/App.pm
+++ b/perllib/FixMyStreet/App.pm
@@ -320,8 +320,7 @@ sub send_email {
my $email = mySociety::Locale::in_gb_locale { FixMyStreet::Email::construct_email(
{
- _template_ => $c->view('Email')->render( $c, $template, $vars ),
- _parameters_ => {},
+ _body_ => $c->view('Email')->render( $c, $template, $vars ),
_attachments_ => $extra_stash_values->{attachments},
From => $vars->{from},
To => $vars->{to},
diff --git a/perllib/FixMyStreet/App/Controller/Admin.pm b/perllib/FixMyStreet/App/Controller/Admin.pm
index f54a862ab..4e288556f 100644
--- a/perllib/FixMyStreet/App/Controller/Admin.pm
+++ b/perllib/FixMyStreet/App/Controller/Admin.pm
@@ -71,25 +71,7 @@ sub index : Path : Args(0) {
return $c->cobrand->admin();
}
- my $problems = $c->cobrand->problems->summary_count;
-
- my %prob_counts =
- map { $_->state => $_->get_column('state_count') } $problems->all;
-
- %prob_counts =
- map { $_ => $prob_counts{$_} || 0 }
- ( FixMyStreet::DB::Result::Problem->all_states() );
- $c->stash->{problems} = \%prob_counts;
- $c->stash->{total_problems_live} += $prob_counts{$_} ? $prob_counts{$_} : 0
- for ( FixMyStreet::DB::Result::Problem->visible_states() );
- $c->stash->{total_problems_users} = $c->cobrand->problems->unique_users;
-
- my $comments = $c->cobrand->updates->summary_count;
-
- my %comment_counts =
- map { $_->state => $_->get_column('state_count') } $comments->all;
-
- $c->stash->{comments} = \%comment_counts;
+ $c->forward('stats_by_state');
my $alerts = $c->model('DB::Alert')->summary_report_alerts( $c->cobrand->restriction );
@@ -129,11 +111,7 @@ sub index : Path : Args(0) {
: _('n/a');
$c->stash->{questionnaires} = \%questionnaire_counts;
- if ($c->get_param('show_categories')) {
- $c->stash->{categories} = $c->cobrand->problems->categories_summary();
- }
-
- $c->stash->{total_bodies} = $c->model('DB::Body')->count();
+ $c->forward('fetch_all_bodies');
return 1;
}
@@ -196,7 +174,7 @@ sub timeline : Path( 'timeline' ) : Args(0) {
return 1;
}
-sub questionnaire : Path('questionnaire') : Args(0) {
+sub questionnaire : Path('stats/questionnaire') : Args(0) {
my ( $self, $c ) = @_;
my $questionnaires = $c->model('DB::Questionnaire')->search(
@@ -232,6 +210,11 @@ sub questionnaire : Path('questionnaire') : Args(0) {
sub bodies : Path('bodies') : Args(0) {
my ( $self, $c ) = @_;
+ if (my $body_id = $c->get_param('body')) {
+ $c->res->redirect( $c->uri_for( 'body', $body_id ) );
+ return;
+ }
+
$c->forward( 'get_token' );
my $edit_activity = $c->model('DB::ContactsHistory')->search(
@@ -784,7 +767,7 @@ sub report_edit : Path('report_edit') : Args(1) {
}
if ( $remove_photo_param || $new_state eq 'hidden' ) {
- unlink glob FixMyStreet->path_to( 'web', 'photo', $problem->id . '.*' );
+ $problem->get_photoset->delete_cached;
}
if ( $problem->is_visible() and $old_state eq 'unconfirmed' ) {
@@ -1005,7 +988,7 @@ sub update_edit : Path('update_edit') : Args(1) {
}
if ( $remove_photo_param || $new_state eq 'hidden' ) {
- unlink glob FixMyStreet->path_to( 'web', 'photo', 'c', $update->id . '.*' );
+ $update->get_photoset->delete_cached;
}
$update->name( $c->get_param('name') || '' );
@@ -1135,9 +1118,21 @@ sub user_edit : Path('user_edit') : Args(1) {
return;
}
- $user->update;
- if ($edited) {
- $c->forward( 'log_edit', [ $id, 'user', 'edit' ] );
+ my $existing_user = $c->model('DB::User')->search({ email => $user->email, id => { '!=', $user->id } })->first;
+ if ($existing_user) {
+ foreach (qw(Problem Comment Alert)) {
+ $c->model("DB::$_")
+ ->search({ user_id => $user->id })
+ ->update({ user_id => $existing_user->id });
+ }
+ $user->delete;
+ $c->forward( 'log_edit', [ $id, 'user', 'merge' ] );
+ $c->res->redirect( $c->uri_for( 'user_edit', $existing_user->id ) );
+ } else {
+ $user->update;
+ if ($edited) {
+ $c->forward( 'log_edit', [ $id, 'user', 'edit' ] );
+ }
}
$c->stash->{status_message} =
@@ -1175,6 +1170,36 @@ sub flagged : Path('flagged') : Args(0) {
return 1;
}
+sub stats_by_state : Path('stats/state') : Args(0) {
+ my ( $self, $c ) = @_;
+
+ my $problems = $c->cobrand->problems->summary_count;
+
+ my %prob_counts =
+ map { $_->state => $_->get_column('state_count') } $problems->all;
+
+ %prob_counts =
+ map { $_ => $prob_counts{$_} || 0 }
+ ( FixMyStreet::DB::Result::Problem->all_states() );
+ $c->stash->{problems} = \%prob_counts;
+ $c->stash->{total_problems_live} += $prob_counts{$_} ? $prob_counts{$_} : 0
+ for ( FixMyStreet::DB::Result::Problem->visible_states() );
+ $c->stash->{total_problems_users} = $c->cobrand->problems->unique_users;
+
+ my $comments = $c->cobrand->updates->summary_count;
+
+ my %comment_counts =
+ map { $_->state => $_->get_column('state_count') } $comments->all;
+
+ $c->stash->{comments} = \%comment_counts;
+}
+
+sub stats_fix_rate : Path('stats/fix-rate') : Args(0) {
+ my ( $self, $c ) = @_;
+
+ $c->stash->{categories} = $c->cobrand->problems->categories_summary();
+}
+
sub stats : Path('stats') : Args(0) {
my ( $self, $c ) = @_;
@@ -1273,7 +1298,6 @@ sub set_allowed_pages : Private {
'bodies' => [_('Bodies'), 1],
'reports' => [_('Reports'), 2],
'timeline' => [_('Timeline'), 3],
- 'questionnaire' => [_('Survey'), 4],
'users' => [_('Users'), 5],
'flagged' => [_('Flagged'), 6],
'stats' => [_('Stats'), 7],
@@ -1540,7 +1564,8 @@ sub check_page_allowed : Private {
$c->forward('set_allowed_pages');
- (my $page = $c->req->action) =~ s#admin/?##;
+ (my $page = $c->req->path) =~ s#admin/?##;
+ $page =~ s#/.*##;
$page ||= 'summary';
diff --git a/perllib/FixMyStreet/App/Controller/Alert.pm b/perllib/FixMyStreet/App/Controller/Alert.pm
index 6972bbc04..ddda02abd 100644
--- a/perllib/FixMyStreet/App/Controller/Alert.pm
+++ b/perllib/FixMyStreet/App/Controller/Alert.pm
@@ -5,6 +5,7 @@ use namespace::autoclean;
BEGIN { extends 'Catalyst::Controller'; }
use mySociety::EmailUtil qw(is_valid_email);
+use FixMyStreet::Gaze;
=head1 NAME
@@ -441,11 +442,7 @@ sub determine_location : Private {
$c->go('index');
}
- my $dist =
- mySociety::Gaze::get_radius_containing_population( $c->stash->{latitude},
- $c->stash->{longitude}, 200000 );
- $dist = int( $dist * 10 + 0.5 );
- $dist = $dist / 10.0;
+ my $dist = FixMyStreet::Gaze::get_radius_containing_population($c->stash->{latitude}, $c->stash->{longitude});
$c->stash->{population_radius} = $dist;
return 1;
diff --git a/perllib/FixMyStreet/App/Controller/Photo.pm b/perllib/FixMyStreet/App/Controller/Photo.pm
index d24d3ff71..2734491fa 100644
--- a/perllib/FixMyStreet/App/Controller/Photo.pm
+++ b/perllib/FixMyStreet/App/Controller/Photo.pm
@@ -43,7 +43,7 @@ sub during :LocalRegex('^(temp|fulltemp)\.([0-9a-f]{40}\.(?:jpeg|png|gif|tiff))$
$c->forward( 'output', [ $photo ] );
}
-sub index :LocalRegex('^(c/)?(\d+)(?:\.(\d+))?(?:\.(full|tn|fp))?\.(?:jpeg|png|gif|tiff)$') {
+sub index :LocalRegex('^(c/)?([1-9]\d*)(?:\.(\d+))?(?:\.(full|tn|fp))?\.(?:jpeg|png|gif|tiff)$') {
my ( $self, $c ) = @_;
my ( $is_update, $id, $photo_number, $size ) = @{ $c->req->captures };
diff --git a/perllib/FixMyStreet/App/Controller/Report.pm b/perllib/FixMyStreet/App/Controller/Report.pm
index 475a29fb5..b3e546c2c 100644
--- a/perllib/FixMyStreet/App/Controller/Report.pm
+++ b/perllib/FixMyStreet/App/Controller/Report.pm
@@ -130,6 +130,17 @@ sub load_problem_or_display_error : Private {
}
$c->stash->{problem} = $problem;
+ if ( $c->user_exists && $c->user->has_permission_to(moderate => $problem->bodies_str) ) {
+ $c->stash->{problem_original} = $problem->find_or_new_related(
+ moderation_original_data => {
+ title => $problem->title,
+ detail => $problem->detail,
+ photo => $problem->photo,
+ anonymous => $problem->anonymous,
+ }
+ );
+ }
+
return 1;
}
diff --git a/perllib/FixMyStreet/App/Controller/Report/New.pm b/perllib/FixMyStreet/App/Controller/Report/New.pm
index 5df182506..76098c119 100644
--- a/perllib/FixMyStreet/App/Controller/Report/New.pm
+++ b/perllib/FixMyStreet/App/Controller/Report/New.pm
@@ -1053,6 +1053,11 @@ sub save_user_and_report : Private {
$report->external_source( $c->config->{MESSAGE_MANAGER_URL} ) ;
}
+ if ( $report->is_from_abuser ) {
+ $c->stash->{template} = 'tokens/abuse.html';
+ $c->detach;
+ }
+
if ( $c->stash->{is_social_user} ) {
my $token = $c->model("DB::Token")->create( {
scope => 'problem/social',
diff --git a/perllib/FixMyStreet/App/Controller/Report/Update.pm b/perllib/FixMyStreet/App/Controller/Report/Update.pm
index af4ccff03..275a300bd 100644
--- a/perllib/FixMyStreet/App/Controller/Report/Update.pm
+++ b/perllib/FixMyStreet/App/Controller/Report/Update.pm
@@ -402,6 +402,11 @@ sub save_update : Private {
$update->photo($fileid);
}
+ if ( $update->is_from_abuser ) {
+ $c->stash->{template} = 'tokens/abuse.html';
+ $c->detach;
+ }
+
if ( $c->stash->{is_social_user} ) {
my $token = $c->model("DB::Token")->create( {
scope => 'update/social',
diff --git a/perllib/FixMyStreet/App/Controller/Rss.pm b/perllib/FixMyStreet/App/Controller/Rss.pm
index 8d4f8313c..183b233a8 100755
--- a/perllib/FixMyStreet/App/Controller/Rss.pm
+++ b/perllib/FixMyStreet/App/Controller/Rss.pm
@@ -8,7 +8,7 @@ use XML::RSS;
use FixMyStreet::App::Model::PhotoSet;
-use mySociety::Gaze;
+use FixMyStreet::Gaze;
use mySociety::Locale;
use mySociety::MaPit;
use mySociety::Sundries qw(ordinal);
@@ -143,10 +143,10 @@ sub local_problems_ll : Private {
$c->stash->{qs} .= ";d=$d";
$d = 100 if $d > 100;
} else {
- $d = mySociety::Gaze::get_radius_containing_population( $lat, $lon, 200000 );
- $d = int( $d * 10 + 0.5 ) / 10;
- mySociety::Locale::in_gb_locale {
- $d = sprintf("%f", $d);
+ $d = FixMyStreet::Gaze::get_radius_containing_population($lat, $lon);
+ # Needs to be with a '.' for db passing
+ $d = mySociety::Locale::in_gb_locale {
+ sprintf("%f", $d);
}
}
diff --git a/perllib/FixMyStreet/App/Controller/Status.pm b/perllib/FixMyStreet/App/Controller/Status.pm
index 931c7bd47..57c8f362e 100755
--- a/perllib/FixMyStreet/App/Controller/Status.pm
+++ b/perllib/FixMyStreet/App/Controller/Status.pm
@@ -58,7 +58,7 @@ sub index : Path : Args(0) {
alerts_unconfirmed => $c->stash->{alerts}{0},
questionnaires_sent => $c->stash->{questionnaires}{total},
questionnaires_answered => $c->stash->{questionnaires}{1},
- bodies => $c->stash->{total_bodies},
+ bodies => scalar @{$c->stash->{bodies}},
contacts => $c->stash->{contacts}{total},
};
my $body = JSON->new->utf8(1)->pretty->encode($data);
diff --git a/perllib/FixMyStreet/App/Model/PhotoSet.pm b/perllib/FixMyStreet/App/Model/PhotoSet.pm
index b44bf4b38..1c8a86e3a 100644
--- a/perllib/FixMyStreet/App/Model/PhotoSet.pm
+++ b/perllib/FixMyStreet/App/Model/PhotoSet.pm
@@ -254,12 +254,25 @@ sub get_image_data {
sub delete_cached {
my ($self) = @_;
my $object = $self->object or return;
+ my $id = $object->id or return;
- unlink glob FixMyStreet->path_to(
- 'web',
- 'photo',
- $object->id . '.*'
- );
+ my @dirs = ('web', 'photo');
+ push @dirs, 'c' if ref $object eq 'FixMyStreet::DB::Result::Comment';
+
+ # Old files without an index number; will always be .jpeg
+ foreach my $size ("", ".fp", ".tn", ".full") {
+ unlink FixMyStreet->path_to(@dirs, "$id$size.jpeg");
+ }
+
+ # New files with index number
+ my @images = $self->all_ids;
+ foreach (map [ $_, $images[$_] ], 0 .. $#images) {
+ my ($i, $file) = @$_;
+ my ($fileid, $type) = split /\./, $file;
+ foreach my $size ("", ".fp", ".tn", ".full") {
+ unlink FixMyStreet->path_to(@dirs, "$id.$i$size.$type");
+ }
+ }
}
sub remove_images {
diff --git a/perllib/FixMyStreet/Cobrand/Zurich.pm b/perllib/FixMyStreet/Cobrand/Zurich.pm
index d31b1c84e..987f0bb67 100644
--- a/perllib/FixMyStreet/Cobrand/Zurich.pm
+++ b/perllib/FixMyStreet/Cobrand/Zurich.pm
@@ -587,14 +587,6 @@ sub admin_report_edit {
# 2) setting $problem->whensent(undef) may make it eligible for generating an email
# to the body (internal or external). See DBRS::Problem->send_reports for Zurich-
# specific categories which are eligible for this.
- #
- # It looks like both of these will do:
- # a) TT processing of [% ... %] directives, in FMS::App->send_email(_cron)
- # b) pseudo-PHP substitution of <?=$values['name']?> which is done as
- # naive substitution
- # commonlib mySociety::Email
- #
- # So it makes sense to add new parameters as the more powerful TT (a).
my $redirect = 0;
my $new_cat = $c->get_param('category') || '';
diff --git a/perllib/FixMyStreet/DB/Result/Problem.pm b/perllib/FixMyStreet/DB/Result/Problem.pm
index 12dad073e..5b853134e 100644
--- a/perllib/FixMyStreet/DB/Result/Problem.pm
+++ b/perllib/FixMyStreet/DB/Result/Problem.pm
@@ -662,36 +662,6 @@ sub can_display_external_id {
return 0;
}
-# TODO Some/much of this could be moved to the template
-
-# either:
-# "sent to council 3 mins later"
-# "[Council name] ref: XYZ"
-# or
-# "sent to council 3 mins later, their ref: XYZ"
-#
-# Note: some silliness with pronouns and the adjacent comma mean this is
-# being presented as a single string rather than two
-sub processed_summary_string {
- my ( $problem, $c ) = @_;
- my ($duration_clause, $external_ref_clause);
- if ($problem->whensent) {
- $duration_clause = $problem->duration_string($c);
- }
- if ($problem->can_display_external_id) {
- if ($duration_clause) {
- $external_ref_clause = '<strong>' . sprintf(_('Council ref:&nbsp;%s'), $problem->external_id) . '.</strong>';
- } else {
- $external_ref_clause = '<strong>' . sprintf(_('%s ref:&nbsp;%s'), $problem->external_body, $problem->external_id) . '.</strong>';
- }
- }
- if ($duration_clause and $external_ref_clause) {
- return "$duration_clause. $external_ref_clause"
- } else {
- return $duration_clause || $external_ref_clause
- }
-}
-
sub duration_string {
my ( $problem, $c ) = @_;
my $body = $problem->body( $c );
diff --git a/perllib/FixMyStreet/DB/Result/User.pm b/perllib/FixMyStreet/DB/Result/User.pm
index 6bce415a6..054be3644 100644
--- a/perllib/FixMyStreet/DB/Result/User.pm
+++ b/perllib/FixMyStreet/DB/Result/User.pm
@@ -201,10 +201,11 @@ sub belongs_to_body {
my $self = shift;
my $bodies = shift;
- my %bodies = map { $_ => 1 } split ',', $bodies;
+ return 0 unless $bodies && $self->from_body;
- return 1 if $self->from_body && $bodies{ $self->from_body->id };
+ my %bodies = map { $_ => 1 } split ',', $bodies;
+ return 1 if $bodies{ $self->from_body->id };
return 0;
}
diff --git a/perllib/FixMyStreet/Email.pm b/perllib/FixMyStreet/Email.pm
index 49f4632a8..d4bfee14e 100644
--- a/perllib/FixMyStreet/Email.pm
+++ b/perllib/FixMyStreet/Email.pm
@@ -1,3 +1,9 @@
+package FixMyStreet::Email::Error;
+
+use Error qw(:try);
+
+@FixMyStreet::Email::Error::ISA = qw(Error::Simple);
+
package FixMyStreet::Email;
use Email::MIME;
@@ -5,7 +11,7 @@ use Encode;
use POSIX qw();
use Template;
use Digest::HMAC_SHA1 qw(hmac_sha1_hex);
-use mySociety::Email;
+use Text::Wrap;
use mySociety::Locale;
use mySociety::Random qw(random_bytes);
use Utils::Email;
@@ -64,50 +70,42 @@ sub is_abuser {
return $schema->resultset('Abuse')->search( { email => [ $email, $domain ] } )->first;
}
+sub _render_template {
+ my ($tt, $template, $vars, %options) = @_;
+ my $var;
+ $tt->process($template, $vars, \$var);
+ return $var;
+}
+
sub send_cron {
- my ( $schema, $params, $env_from, $nomail, $cobrand, $lang_code ) = @_;
+ my ( $schema, $template, $vars, $hdrs, $env_from, $nomail, $cobrand, $lang_code ) = @_;
my $sender = FixMyStreet->config('DO_NOT_REPLY_EMAIL');
$env_from ||= $sender;
- if (!$params->{From}) {
+ if (!$hdrs->{From}) {
my $sender_name = $cobrand->contact_name;
- $params->{From} = [ $sender, _($sender_name) ];
+ $hdrs->{From} = [ $sender, _($sender_name) ];
}
- return 1 if is_abuser($schema, $params->{To});
+ return 1 if is_abuser($schema, $hdrs->{To});
- $params->{'Message-ID'} = sprintf('<fms-cron-%s-%s@%s>', time(),
+ $hdrs->{'Message-ID'} = sprintf('<fms-cron-%s-%s@%s>', time(),
unpack('h*', random_bytes(5, 1)), FixMyStreet->config('EMAIL_DOMAIN')
);
- # This is all to set the path for the templates processor so we can override
- # signature and site names in emails using templates in the old style emails.
- # It's a bit involved as not everywhere we use it knows about the cobrand so
- # we can't assume there will be one.
- my $include_path = FixMyStreet->path_to( 'templates', 'email', 'default' )->stringify;
- if ( $cobrand ) {
- $include_path =
- FixMyStreet->path_to( 'templates', 'email', $cobrand->moniker )->stringify . ':'
- . $include_path;
- if ( $lang_code ) {
- $include_path =
- FixMyStreet->path_to( 'templates', 'email', $cobrand->moniker, $lang_code )->stringify . ':'
- . $include_path;
- }
- }
my $tt = Template->new({
- INCLUDE_PATH => $include_path
+ ENCODING => 'utf8',
+ INCLUDE_PATH => [
+ FixMyStreet->path_to( 'templates', 'email', $cobrand->moniker, $lang_code )->stringify,
+ FixMyStreet->path_to( 'templates', 'email', $cobrand->moniker )->stringify,
+ FixMyStreet->path_to( 'templates', 'email', 'default' )->stringify,
+ ],
});
- my ($sig, $site_name);
- $tt->process( 'signature.txt', $params, \$sig );
- $sig = Encode::decode('utf8', $sig);
- $params->{_parameters_}->{signature} = $sig;
+ $vars->{signature} = _render_template($tt, 'signature.txt', $vars);
+ $vars->{site_name} = Utils::trim_text(_render_template($tt, 'site-name.txt', $vars));
+ $hdrs->{_body_} = _render_template($tt, $template, $vars);
- $tt->process( 'site-name.txt', $params, \$site_name );
- $site_name = Utils::trim_text(Encode::decode('utf8', $site_name));
- $params->{_parameters_}->{site_name} = $site_name;
-
- my $email = mySociety::Locale::in_gb_locale { construct_email($params) };
+ my $email = mySociety::Locale::in_gb_locale { construct_email($hdrs) };
if ($nomail) {
print $email->as_string;
@@ -125,16 +123,12 @@ containing elements as given below. Returns an Email::MIME email.
=over 4
-=item _template_, _parameters_
+=item _body_
-Templated body text and an associative array of template parameters. _template
-contains optional substititutions <?=$values['name']?>, each of which is
-replaced by the value of the corresponding named value in _parameters_. It is
-an error to use a substitution when the corresponding parameter is not present
-or undefined. The first line of the template will be interpreted as contents of
+Body text. The first line of the template will be interpreted as contents of
the Subject: header of the mail if it begins with the literal string 'Subject:
-' followed by a blank line. The templated text will be word-wrapped to produce
-lines of appropriate length.
+' followed by a blank line. The text will be word-wrapped to produce lines of
+appropriate length.
=item _attachments_
@@ -175,21 +169,42 @@ templated body, From or Subject (perhaps from the template).
sub construct_email ($) {
my $p = shift;
- throw mySociety::Email::Error("Must specify both '_template_' and '_parameters_'")
- if !exists($p->{_template_}) || !exists($p->{_parameters_});
- throw mySociety::Email::Error("Template parameters '_parameters_' must be an associative array")
- if (ref($p->{_parameters_}) ne 'HASH');
+ throw FixMyStreet::Email::Error("Must specify '_body_'") if !exists($p->{_body_});
+
+ my $body = $p->{_body_};
+ my $subject;
+ if ($body =~ m#^Subject: ([^\n]*)\n\n#s) {
+ $subject = $1;
+ $body =~ s#^Subject: ([^\n]*)\n\n##s;
+ }
+
+ $body =~ s/\r\n/\n/gs;
+ $body =~ s/^\s+$//mg; # Note this also reduces any gap between paragraphs of >1 blank line to 1
+ $body =~ s/\s+$//;
+
+ # Merge paragraphs into their own line. Two blank lines separate a
+ # paragraph. End a line with two spaces to force a linebreak.
+
+ # regex means, "replace any line ending that is neither preceded (?<!\n)
+ # nor followed (?!\n) by a blank line with a single space".
+ $body =~ s#(?<!\n)(?<! )\n(?!\n)# #gs;
+
+ # Wrap text to 72-column lines.
+ local($Text::Wrap::columns) = 69;
+ local($Text::Wrap::huge) = 'overflow';
+ local($Text::Wrap::unexpand) = 0;
+ $body = Text::Wrap::wrap('', '', $body);
+ $body =~ s/^\s+$//mg; # Do it again because of wordwrapping indented lines
- (my $subject, $body) = mySociety::Email::do_template_substitution($p->{_template_}, $p->{_parameters_}, '');
$p->{Subject} = $subject if defined($subject);
if (!exists($p->{Subject})) {
# XXX Try to find out what's causing this very occasionally
(my $error = $body) =~ s/\n/ | /g;
$error = "missing field 'Subject' in MESSAGE - $error";
- throw mySociety::Email::Error($error);
+ throw FixMyStreet::Email::Error($error);
}
- throw mySociety::Email::Error("missing field 'From' in MESSAGE") unless exists($p->{From});
+ throw FixMyStreet::Email::Error("missing field 'From' in MESSAGE") unless exists($p->{From});
# Construct email headers
my %hdr;
@@ -203,7 +218,7 @@ sub construct_email ($) {
# Array of addresses or [address, name] pairs.
$hdr{$h} = join(', ', map { mailbox($_, $h) } @{$p->{$h}});
} else {
- throw mySociety::Email::Error("Field '$h' in MESSAGE should be single value or an array");
+ throw FixMyStreet::Email::Error("Field '$h' in MESSAGE should be single value or an array");
}
}
@@ -251,7 +266,7 @@ sub mailbox {
if (ref($e) eq '') {
return $e;
} elsif (ref($e) ne 'ARRAY' || @$e != 2) {
- throw mySociety::Email::Error("'$header' field should be string or 2-element array");
+ throw FixMyStreet::Email::Error("'$header' field should be string or 2-element array");
} else {
return Email::Address->new($e->[1], $e->[0]);
}
diff --git a/perllib/FixMyStreet/Gaze.pm b/perllib/FixMyStreet/Gaze.pm
new file mode 100644
index 000000000..a072cd246
--- /dev/null
+++ b/perllib/FixMyStreet/Gaze.pm
@@ -0,0 +1,24 @@
+package FixMyStreet::Gaze;
+
+use strict;
+use warnings;
+
+use mySociety::Gaze;
+
+sub get_radius_containing_population ($$) {
+ my ($lat, $lon) = @_;
+
+ my $dist = eval {
+ mySociety::Locale::in_gb_locale {
+ mySociety::Gaze::get_radius_containing_population($lat, $lon, 200_000);
+ };
+ };
+ if ($@) {
+ # Error fetching from gaze, let's fall back to 10km
+ $dist = 10;
+ }
+ $dist = int( $dist * 10 + 0.5 ) / 10.0;
+ return $dist;
+}
+
+1;
diff --git a/perllib/FixMyStreet/Map.pm b/perllib/FixMyStreet/Map.pm
index 81b81f656..6d641331f 100644
--- a/perllib/FixMyStreet/Map.pm
+++ b/perllib/FixMyStreet/Map.pm
@@ -16,7 +16,7 @@ use Module::Pluggable
# Get the list of maps we want and load map classes at compile time
my @ALL_MAP_CLASSES = allowed_maps();
-use mySociety::Gaze;
+use FixMyStreet::Gaze;
use mySociety::Locale;
use Utils;
@@ -105,13 +105,7 @@ sub _map_features {
? $c->cobrand->problems->around_map( @around_args, $around_limit, $category, $states )
: $around_map;
- my $dist;
- mySociety::Locale::in_gb_locale {
- $dist =
- mySociety::Gaze::get_radius_containing_population( $lat, $lon,
- 200000 );
- };
- $dist = int( $dist * 10 + 0.5 ) / 10;
+ my $dist = FixMyStreet::Gaze::get_radius_containing_population( $lat, $lon );
my $limit = 20;
my @ids = map { $_->id } @$around_map_list;
diff --git a/perllib/FixMyStreet/Map/Google.pm b/perllib/FixMyStreet/Map/Google.pm
index 46823f358..8ddf4f4e9 100644
--- a/perllib/FixMyStreet/Map/Google.pm
+++ b/perllib/FixMyStreet/Map/Google.pm
@@ -7,7 +7,7 @@
package FixMyStreet::Map::Google;
use strict;
-use mySociety::Gaze;
+use FixMyStreet::Gaze;
use Utils;
use constant ZOOM_LEVELS => 6;
@@ -30,7 +30,7 @@ sub display_map {
# Adjust zoom level dependent upon population density
my $dist = $c->stash->{distance}
- || mySociety::Gaze::get_radius_containing_population( $params{latitude}, $params{longitude}, 200_000 );
+ || FixMyStreet::Gaze::get_radius_containing_population( $params{latitude}, $params{longitude} );
my $default_zoom = $c->cobrand->default_map_zoom() ? $c->cobrand->default_map_zoom() : $numZoomLevels - 4;
$default_zoom = $numZoomLevels - 3 if $dist < 10;
diff --git a/perllib/FixMyStreet/Map/OSM.pm b/perllib/FixMyStreet/Map/OSM.pm
index 7d91a9ee7..ae9e73a0a 100644
--- a/perllib/FixMyStreet/Map/OSM.pm
+++ b/perllib/FixMyStreet/Map/OSM.pm
@@ -8,7 +8,7 @@ package FixMyStreet::Map::OSM;
use strict;
use Math::Trig;
-use mySociety::Gaze;
+use FixMyStreet::Gaze;
use Utils;
use constant ZOOM_LEVELS => 6;
@@ -59,7 +59,7 @@ sub display_map {
# Adjust zoom level dependent upon population density
my $dist = $c->stash->{distance}
- || mySociety::Gaze::get_radius_containing_population( $params{latitude}, $params{longitude}, 200_000 );
+ || FixMyStreet::Gaze::get_radius_containing_population( $params{latitude}, $params{longitude} );
my $default_zoom = $c->cobrand->default_map_zoom() ? $c->cobrand->default_map_zoom() : $numZoomLevels - 4;
$default_zoom = $numZoomLevels - 3 if $dist < 10;
diff --git a/perllib/FixMyStreet/Script/Alerts.pm b/perllib/FixMyStreet/Script/Alerts.pm
index e799a5446..062601044 100644
--- a/perllib/FixMyStreet/Script/Alerts.pm
+++ b/perllib/FixMyStreet/Script/Alerts.pm
@@ -7,7 +7,7 @@ use DateTime::Format::Pg;
use IO::String;
use mySociety::DBHandle qw(dbh);
-use mySociety::Gaze;
+use FixMyStreet::Gaze;
use mySociety::Locale;
use mySociety::MaPit;
use RABX;
@@ -175,10 +175,10 @@ sub send() {
my $longitude = $alert->parameter;
my $latitude = $alert->parameter2;
- my $d = mySociety::Gaze::get_radius_containing_population($latitude, $longitude, 200000);
+ my $d = FixMyStreet::Gaze::get_radius_containing_population($latitude, $longitude);
# Convert integer to GB locale string (with a ".")
$d = mySociety::Locale::in_gb_locale {
- sprintf("%f", int($d*10+0.5)/10);
+ sprintf("%f", $d);
};
my $states = "'" . join( "', '", FixMyStreet::DB::Result::Problem::visible_states() ) . "'";
my %data = (
@@ -248,8 +248,6 @@ sub _send_aggregated_alert_email(%) {
} );
$data{unsubscribe_url} = $cobrand->base_url( $data{cobrand_data} ) . '/A/' . $token->token;
- my $template = FixMyStreet->get_email_template($cobrand->moniker, $data{lang}, "$data{template}.txt");
-
my $sender = sprintf('<fms-%s@%s>',
FixMyStreet::Email::generate_verp_token('alert', $data{alert_id}),
FixMyStreet->config('EMAIL_DOMAIN')
@@ -257,9 +255,9 @@ sub _send_aggregated_alert_email(%) {
my $result = FixMyStreet::Email::send_cron(
$data{schema},
+ "$data{template}.txt",
+ \%data,
{
- _template_ => $template,
- _parameters_ => \%data,
To => $data{alert_email},
},
$sender,
diff --git a/perllib/FixMyStreet/Script/Questionnaires.pm b/perllib/FixMyStreet/Script/Questionnaires.pm
index f72f59077..c5bc6bfe0 100644
--- a/perllib/FixMyStreet/Script/Questionnaires.pm
+++ b/perllib/FixMyStreet/Script/Questionnaires.pm
@@ -52,8 +52,6 @@ sub send_questionnaires_period {
# call checks if this is the host that sends mail for this cobrand.
next unless $cobrand->email_host;
- my $template = FixMyStreet->get_email_template($cobrand->moniker, $row->lang, 'questionnaire.txt');
-
my %h = map { $_ => $row->$_ } qw/name title detail category/;
$h{created} = Utils::prettify_duration( time() - $row->confirmed->epoch, 'week' );
@@ -78,14 +76,15 @@ sub send_questionnaires_period {
my $result = FixMyStreet::Email::send_cron(
$rs->result_source->schema,
+ 'questionnaire.txt',
+ \%h,
{
- _template_ => $template,
- _parameters_ => \%h,
To => [ [ $row->user->email, $row->name ] ],
},
undef,
$params->{nomail},
- $cobrand
+ $cobrand,
+ $row->lang,
);
unless ($result) {
print " ...success\n" if $params->{verbose};
diff --git a/perllib/FixMyStreet/Script/Reports.pm b/perllib/FixMyStreet/Script/Reports.pm
index 75111b852..278c58af1 100644
--- a/perllib/FixMyStreet/Script/Reports.pm
+++ b/perllib/FixMyStreet/Script/Reports.pm
@@ -289,19 +289,18 @@ sub _send_report_sent_email {
my $nomail = shift;
my $cobrand = shift;
- my $template = FixMyStreet->get_email_template($row->cobrand, $row->lang, 'confirm_report_sent.txt');
-
FixMyStreet::Email::send_cron(
$row->result_source->schema,
+ 'confirm_report_sent.txt',
+ $h,
{
- _template_ => $template,
- _parameters_ => $h,
To => $row->user->email,
From => [ FixMyStreet->config('CONTACT_EMAIL'), $cobrand->contact_name ],
},
FixMyStreet->config('CONTACT_EMAIL'),
$nomail,
- $cobrand
+ $cobrand,
+ $row->lang,
);
}
diff --git a/perllib/FixMyStreet/SendReport/Email.pm b/perllib/FixMyStreet/SendReport/Email.pm
index 7e5c10469..8582ebb3b 100644
--- a/perllib/FixMyStreet/SendReport/Email.pm
+++ b/perllib/FixMyStreet/SendReport/Email.pm
@@ -52,15 +52,8 @@ sub build_recipient_list {
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;
+ return 'submit-oxfordshire.txt' if $row->cobrand eq 'fixmystreet' && $row->bodies_str eq 2237;
+ return 'submit.txt';
}
sub send_from {
@@ -88,8 +81,6 @@ sub send {
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 ),
};
@@ -108,7 +99,9 @@ sub send {
$params->{From} = [ $sender, $params->{From}[1] ];
}
- my $result = FixMyStreet::Email::send_cron($row->result_source->schema, $params, $sender, $nomail, $cobrand);
+ my $result = FixMyStreet::Email::send_cron($row->result_source->schema,
+ $self->get_template($row), $h,
+ $params, $sender, $nomail, $cobrand, $row->lang);
unless ($result) {
$self->success(1);
diff --git a/perllib/FixMyStreet/SendReport/Zurich.pm b/perllib/FixMyStreet/SendReport/Zurich.pm
index a8730bbe4..b38981d94 100644
--- a/perllib/FixMyStreet/SendReport/Zurich.pm
+++ b/perllib/FixMyStreet/SendReport/Zurich.pm
@@ -59,8 +59,6 @@ sub get_template {
}
}
- my $template_path = FixMyStreet->path_to( "templates", "email", "zurich", $template )->stringify;
- $template = Utils::read_file( $template_path );
return $template;
}