aboutsummaryrefslogtreecommitdiffstats
path: root/perllib/FixMyStreet/Roles
diff options
context:
space:
mode:
Diffstat (limited to 'perllib/FixMyStreet/Roles')
-rw-r--r--perllib/FixMyStreet/Roles/BoroughEmails.pm68
-rw-r--r--perllib/FixMyStreet/Roles/ConfirmOpen311.pm43
-rw-r--r--perllib/FixMyStreet/Roles/ConfirmValidation.pm8
-rw-r--r--perllib/FixMyStreet/Roles/ContactExtra.pm11
-rw-r--r--perllib/FixMyStreet/Roles/Extra.pm72
-rw-r--r--perllib/FixMyStreet/Roles/PhotoSet.pm13
-rw-r--r--perllib/FixMyStreet/Roles/Translatable.pm13
7 files changed, 210 insertions, 18 deletions
diff --git a/perllib/FixMyStreet/Roles/BoroughEmails.pm b/perllib/FixMyStreet/Roles/BoroughEmails.pm
new file mode 100644
index 000000000..ba941f64f
--- /dev/null
+++ b/perllib/FixMyStreet/Roles/BoroughEmails.pm
@@ -0,0 +1,68 @@
+package FixMyStreet::Roles::BoroughEmails;
+use Moo::Role;
+
+=head1 NAME
+
+FixMyStreet::Roles::BoroughEmails - role for directing reports according to the
+borough_email_addresses COBRAND_FEATURE
+
+=cut
+
+=head2 munge_sendreport_params
+
+TfL want reports made in certain categories sent to different email addresses
+depending on what London Borough they were made in. To achieve this we have
+some config in COBRAND_FEATURES that specifies what address to direct reports
+to based on the MapIt area IDs it's in.
+
+Contacts that use this technique have a short code in their email field,
+which is looked up in the `borough_email_addresses` hash.
+
+For example, if you wanted Pothole reports in Bromley and Barnet to be sent to
+one email address, and Pothole reports in Hounslow to be sent to another,
+create a contact with category = "Potholes" and email = "BOROUGHPOTHOLES" and
+use the following config in general.yml:
+
+COBRAND_FEATURES:
+ borough_email_addresses:
+ tfl:
+ BOROUGHPOTHOLES:
+ - email: bromleybarnetpotholes@example.org
+ areas:
+ - 2482 # Bromley
+ - 2489 # Barnet
+ - email: hounslowpotholes@example.org
+ areas:
+ - 2483 # Hounslow
+
+=cut
+
+sub munge_sendreport_params {
+ my ($self, $row, $h, $params) = @_;
+
+ my $addresses = $self->feature('borough_email_addresses');
+ return unless $addresses;
+
+ my @report_areas = grep { $_ } split ',', $row->areas;
+
+ my $to = $params->{To};
+ my @munged_to = ();
+ for my $recip ( @$to ) {
+ my ($email, $name) = @$recip;
+ if (my $teams = $addresses->{$email}) {
+ for my $team (@$teams) {
+ my %team_area_ids = map { $_ => 1 } @{ $team->{areas} };
+ if ( grep { $team_area_ids{$_} } @report_areas ) {
+ $recip = [
+ $team->{email},
+ $name
+ ];
+ }
+ }
+ }
+ push @munged_to, $recip;
+ }
+ $params->{To} = \@munged_to;
+}
+
+1;
diff --git a/perllib/FixMyStreet/Roles/ConfirmOpen311.pm b/perllib/FixMyStreet/Roles/ConfirmOpen311.pm
new file mode 100644
index 000000000..0845105f1
--- /dev/null
+++ b/perllib/FixMyStreet/Roles/ConfirmOpen311.pm
@@ -0,0 +1,43 @@
+package FixMyStreet::Roles::ConfirmOpen311;
+use Moo::Role;
+
+=head1 NAME
+
+FixMyStreet::Roles::ConfirmOpen311 - role for adding various Open311 things specific to Confirm
+
+=cut
+
+sub open311_config {
+ my ($self, $row, $h, $params) = @_;
+
+ $params->{multi_photos} = 1;
+}
+
+sub open311_extra_data {
+ my ($self, $row, $h, $extra) = @_;
+
+ my $open311_only = [
+ { name => 'report_url',
+ value => $h->{url} },
+ { name => 'title',
+ value => $row->title },
+ { name => 'description',
+ value => $row->detail },
+ ];
+
+ # Reports made via FMS.com or the app probably won't have a USRN
+ # value because we don't display the adopted highways layer on those
+ # frontends. Instead we'll look up the closest asset from the WFS
+ # service at the point we're sending the report over Open311.
+ if (!$row->get_extra_field_value('site_code')) {
+ if (my $site_code = $self->lookup_site_code($row)) {
+ push @$extra,
+ { name => 'site_code',
+ value => $site_code };
+ }
+ }
+
+ return $open311_only;
+}
+
+1;
diff --git a/perllib/FixMyStreet/Roles/ConfirmValidation.pm b/perllib/FixMyStreet/Roles/ConfirmValidation.pm
index 776230287..27592b33c 100644
--- a/perllib/FixMyStreet/Roles/ConfirmValidation.pm
+++ b/perllib/FixMyStreet/Roles/ConfirmValidation.pm
@@ -17,6 +17,8 @@ Confirm field lengths.
has max_report_length => ( is => 'ro', default => 2000 );
+has max_title_length => ( is => 'ro', default => 0 );
+
sub report_validation {
my ($self, $report, $errors) = @_;
@@ -24,10 +26,14 @@ sub report_validation {
$errors->{name} = sprintf( _('Names are limited to %d characters in length.'), 50 );
}
- if ( length( $report->user->phone ) > 20 ) {
+ if ( $report->user->phone && length( $report->user->phone ) > 20 ) {
$errors->{phone} = sprintf( _('Phone numbers are limited to %s characters in length.'), 20 );
}
+ if ( $self->max_title_length > 0 && length( $report->title ) > $self->max_title_length ) {
+ $errors->{title} = sprintf( _('Summaries are limited to %d characters in length. Please shorten your summary'), 50 );
+ }
+
if ( length( $report->detail ) > $self->max_report_length ) {
$errors->{detail} = sprintf( _('Reports are limited to %s characters in length. Please shorten your report'), $self->max_report_length );
}
diff --git a/perllib/FixMyStreet/Roles/ContactExtra.pm b/perllib/FixMyStreet/Roles/ContactExtra.pm
index 55c055d99..e78d9b53f 100644
--- a/perllib/FixMyStreet/Roles/ContactExtra.pm
+++ b/perllib/FixMyStreet/Roles/ContactExtra.pm
@@ -25,8 +25,15 @@ sub for_bodies {
}
sub by_categories {
- my ($rs, $area_id, @contacts) = @_;
- my %body_ids = map { $_->body_id => 1 } FixMyStreet::DB->resultset('BodyArea')->search({ area_id => $area_id });
+ my ($rs, $contacts, %params) = @_;
+
+ my %body_ids = ();
+ if ( $params{body_id} ) {
+ %body_ids = ( $params{body_id} => 1 );
+ } else {
+ %body_ids = map { $_->body_id => 1 } FixMyStreet::DB->resultset('BodyArea')->search({ area_id => $params{area_id} });
+ }
+ my @contacts = @$contacts;
my @body_ids = keys %body_ids;
my %extras = ();
my @results = $rs->for_bodies(\@body_ids, undef);
diff --git a/perllib/FixMyStreet/Roles/Extra.pm b/perllib/FixMyStreet/Roles/Extra.pm
index 445f6d91c..530064b99 100644
--- a/perllib/FixMyStreet/Roles/Extra.pm
+++ b/perllib/FixMyStreet/Roles/Extra.pm
@@ -135,6 +135,58 @@ sub push_extra_fields {
$self->extra({ %$extra, $META_FIELD => [ @$existing, @fields ] });
}
+=head2 update_extra_field
+
+ $problem->update_extra_field( { ... } );
+
+Given an extra field, will replace one with the same code in the
+existing list of fields, or add to the end if not present.
+Returns true if it was already present, false if newly added.
+
+=cut
+
+sub update_extra_field {
+ my ($self, $field) = @_;
+
+ # Can operate on list that uses code (Contact) or name (Problem),
+ # but make sure we have one of them
+ my $attr;
+ $attr = 'code' if $field->{code};
+ $attr = 'name' if $field->{name};
+ die unless $attr;
+
+ my $existing = $self->get_extra_fields;
+ my $found;
+ foreach (@$existing) {
+ if ($_->{$attr} eq $field->{$attr}) {
+ $_ = $field;
+ $found = 1;
+ }
+ }
+ if (!$found) {
+ push @$existing, $field;
+ }
+
+ $self->set_extra_fields(@$existing);
+ return $found;
+}
+
+=head2 remove_extra_field
+
+ $problem->remove_extra_field( $code );
+
+Given an extra field code, will remove it from the list of fields.
+
+=cut
+
+sub remove_extra_field {
+ my ($self, $code) = @_;
+
+ my @fields = @{ $self->get_extra_fields() };
+ @fields = grep { ($_->{code} || $_->{name}) ne $code } @fields;
+ $self->set_extra_fields(@fields);
+}
+
=head1 HELPER METHODS
For internal use mostly.
@@ -191,4 +243,24 @@ sub get_extra_field_value {
return $field->{value};
}
+=head2 get_extra_field
+
+ my $field = $problem->get_extra_field(name => 'field_name');
+
+Return a field stored in `_fields` in extra, or undefined if it's not present.
+Can use either `name` or `code` to identify the field.
+
+=cut
+
+sub get_extra_field {
+ my ($self, %opts) = @_;
+
+ my @fields = @{ $self->get_extra_fields() };
+
+ my $comparison = $opts{code} ? 'code' : 'name';
+
+ my ($field) = grep { $_->{$comparison} && $_->{$comparison} eq $opts{$comparison} } @fields;
+ return $field;
+}
+
1;
diff --git a/perllib/FixMyStreet/Roles/PhotoSet.pm b/perllib/FixMyStreet/Roles/PhotoSet.pm
index 4a40ef3f9..3d0027f8c 100644
--- a/perllib/FixMyStreet/Roles/PhotoSet.pm
+++ b/perllib/FixMyStreet/Roles/PhotoSet.pm
@@ -31,6 +31,11 @@ sub get_first_image_fp {
return $self->get_photoset->get_image_data( num => 0, size => 'fp' );
}
+sub get_first_image_og {
+ my ($self) = @_;
+ return $self->get_photoset->get_image_data( num => 0, size => 'og' );
+}
+
sub photos {
my $self = shift;
my $photoset = $self->get_photoset;
@@ -38,15 +43,18 @@ sub photos {
my $id = $self->id;
my $typ = $self->result_source->name eq 'comment' ? 'c/' : '';
+ my $non_public = $self->result_source->name eq 'comment'
+ ? $self->problem->non_public : $self->non_public;
+
my @photos = map {
my $cachebust = substr($_, 0, 8);
# Some Varnish configurations (e.g. on mySociety infra) strip cookies from
# images, which means image requests will be redirected to the login page
- # if LOGIN_REQUIRED is set. To stop this happening, Varnish should be
+ # if e.g. LOGIN_REQUIRED is set. To stop this happening, Varnish should be
# configured to not strip cookies if the cookie_passthrough param is
# present, which this line ensures will be if LOGIN_REQUIRED is set.
my $extra = '';
- if (FixMyStreet->config('LOGIN_REQUIRED')) {
+ if (FixMyStreet->config('LOGIN_REQUIRED') || $non_public) {
$cachebust .= '&cookie_passthrough=1';
$extra = '?cookie_passthrough=1';
}
@@ -59,6 +67,7 @@ sub photos {
url_full => "/photo/$typ$id.$i.full.$format?$cachebust",
url_tn => "/photo/$typ$id.$i.tn.$format?$cachebust",
url_fp => "/photo/$typ$id.$i.fp.$format?$cachebust",
+ url_og => "/photo/$typ$id.$i.og.$format?$cachebust",
idx => $i++,
}
} $photoset->all_ids;
diff --git a/perllib/FixMyStreet/Roles/Translatable.pm b/perllib/FixMyStreet/Roles/Translatable.pm
index d39d97bf8..0c84bbf0f 100644
--- a/perllib/FixMyStreet/Roles/Translatable.pm
+++ b/perllib/FixMyStreet/Roles/Translatable.pm
@@ -40,19 +40,6 @@ sub _translate {
my $translated = $self->translated->{$col}{$lang};
return $translated if $translated;
- # Deal with the fact problem table has denormalized copy of category string
- if ($table eq 'problem' && $col eq 'category') {
- my $body_id = $self->bodies_str_ids->[0];
- return $fallback unless $body_id && $body_id =~ /^[0-9]+$/;
- my $contact = $schema->resultset("Contact")->find( {
- body_id => $body_id,
- category => $fallback,
- } );
- return $fallback unless $contact; # Shouldn't happen, but some tests
- $table = 'contact';
- $id = $contact->id;
- }
-
if (ref $schema) {
my $translation = $schema->resultset('Translation')->find({
lang => $lang,