diff options
author | Matthew Somerville <matthew@mysociety.org> | 2019-09-11 17:53:28 +0100 |
---|---|---|
committer | Matthew Somerville <matthew@mysociety.org> | 2019-10-25 14:07:13 +0100 |
commit | 1016181d70363325e989c0ec8598341ec1f34ec6 (patch) | |
tree | 255cfc9e30ad8c76444cb080251a951e2b7a67cd | |
parent | 3a149039167145ef6d8cd0218a30ec8d98741bc4 (diff) |
Allow cobrands to override category display.
-rw-r--r-- | perllib/FixMyStreet/App/Controller/My.pm | 2 | ||||
-rw-r--r-- | perllib/FixMyStreet/Cobrand/Bexley.pm | 5 | ||||
-rw-r--r-- | perllib/FixMyStreet/DB/Result/Contact.pm | 2 | ||||
-rw-r--r-- | perllib/FixMyStreet/DB/Result/Problem.pm | 23 | ||||
-rw-r--r-- | perllib/FixMyStreet/Roles/Translatable.pm | 13 | ||||
-rw-r--r-- | t/roles/translatable.t | 7 | ||||
-rw-r--r-- | templates/web/base/admin/bodies/_category_field.html | 17 | ||||
-rw-r--r-- | templates/web/base/admin/bodies/contact-form.html | 16 |
8 files changed, 50 insertions, 35 deletions
diff --git a/perllib/FixMyStreet/App/Controller/My.pm b/perllib/FixMyStreet/App/Controller/My.pm index ed890ad82..b181acd04 100644 --- a/perllib/FixMyStreet/App/Controller/My.pm +++ b/perllib/FixMyStreet/App/Controller/My.pm @@ -161,7 +161,7 @@ sub setup_page_data : Private { my @categories = $c->stash->{problems_rs}->search({ state => [ FixMyStreet::DB::Result::Problem->visible_states() ], }, { - columns => [ 'category' ], + columns => [ 'category', 'bodies_str' ], distinct => 1, order_by => [ 'category' ], } )->all; diff --git a/perllib/FixMyStreet/Cobrand/Bexley.pm b/perllib/FixMyStreet/Cobrand/Bexley.pm index 0fbb8944b..1a303d633 100644 --- a/perllib/FixMyStreet/Cobrand/Bexley.pm +++ b/perllib/FixMyStreet/Cobrand/Bexley.pm @@ -31,10 +31,7 @@ sub open311_munge_update_params { $params->{service_request_id_ext} = $comment->problem->id; - my $contact = $comment->result_source->schema->resultset("Contact")->not_deleted->find({ - body_id => $body->id, - category => $comment->problem->category - }); + my $contact = $comment->problem->category_row; $params->{service_code} = $contact->email; } diff --git a/perllib/FixMyStreet/DB/Result/Contact.pm b/perllib/FixMyStreet/DB/Result/Contact.pm index a99915fb4..d8695683c 100644 --- a/perllib/FixMyStreet/DB/Result/Contact.pm +++ b/perllib/FixMyStreet/DB/Result/Contact.pm @@ -95,7 +95,7 @@ __PACKAGE__->many_to_many( defect_types => 'contact_defect_types', 'defect_type' sub category_display { my $self = shift; - $self->translate_column('category'); + $self->get_extra_metadata('display_name') || $self->translate_column('category'); } sub groups { diff --git a/perllib/FixMyStreet/DB/Result/Problem.pm b/perllib/FixMyStreet/DB/Result/Problem.pm index 08b768719..97f0666e0 100644 --- a/perllib/FixMyStreet/DB/Result/Problem.pm +++ b/perllib/FixMyStreet/DB/Result/Problem.pm @@ -404,7 +404,28 @@ sub confirm { sub category_display { my $self = shift; - $self->translate_column('category'); + my $contact = $self->category_row; + return $self->category unless $contact; # Fallback; shouldn't happen, but some tests + return $contact->category_display; +} + +=head2 category_row + +Returns the corresponding Contact object for this problem's category and body. +If the report was sent to multiple bodies, only returns the first. + +=cut + +sub category_row { + my $self = shift; + my $schema = $self->result_source->schema; + my $body_id = $self->bodies_str_ids->[0]; + return unless $body_id && $body_id =~ /^[0-9]+$/; + my $contact = $schema->resultset("Contact")->find({ + body_id => $body_id, + category => $self->category, + }); + return $contact; } sub bodies_str_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, diff --git a/t/roles/translatable.t b/t/roles/translatable.t index e13f49fc6..9f8c67394 100644 --- a/t/roles/translatable.t +++ b/t/roles/translatable.t @@ -74,4 +74,11 @@ FixMyStreet::override_config { $mech->content_contains('Hull i veien'); }; +subtest 'Check display_name override' => sub { + $contact->set_extra_metadata( display_name => 'Override name' ); + $contact->update; + is $contact->category_display, "Override name"; + is $problem->category_display, "Override name"; +}; + done_testing; diff --git a/templates/web/base/admin/bodies/_category_field.html b/templates/web/base/admin/bodies/_category_field.html new file mode 100644 index 000000000..8c5a1c352 --- /dev/null +++ b/templates/web/base/admin/bodies/_category_field.html @@ -0,0 +1,17 @@ +<div class="admin-hint"> + <p> + [% loc('Choose a <strong>category</strong> name that makes sense to the public (e.g., "Pothole", "Street lighting") but is helpful + to the body too. These will appear in the drop-down menu on the report-a-problem page.') %] + <br> + [% loc("If two or more bodies serve the same location, FixMyStreet combines identical categories into a single entry in + the menu. Make sure you use the same category name in the bodies if you want this to happen.") %] + </p> +</div> + +<p> + <strong>[% loc('Category') %] </strong><input type="text" class="form-control" name="category" size="30" value="[% contact.category | html %]" required> +</p> + +[% IF contact.in_storage %] + <input type="hidden" name="current_category" value="[% current_contact.category | html %]" > +[% END %] diff --git a/templates/web/base/admin/bodies/contact-form.html b/templates/web/base/admin/bodies/contact-form.html index b28b74d92..ba47327cd 100644 --- a/templates/web/base/admin/bodies/contact-form.html +++ b/templates/web/base/admin/bodies/contact-form.html @@ -1,20 +1,6 @@ <form method="post" action="[% c.uri_for_action('admin/bodies/edit', [ body_id ] ) %]" enctype="application/x-www-form-urlencoded" accept-charset="utf-8" id="category_edit"> - <div class="admin-hint"> - <p> - [% loc('Choose a <strong>category</strong> name that makes sense to the public (e.g., "Pothole", "Street lighting") but is helpful - to the body too. These will appear in the drop-down menu on the report-a-problem page.') %] - <br> - [% loc("If two or more bodies serve the same location, FixMyStreet combines identical categories into a single entry in - the menu. Make sure you use the same category name in the bodies if you want this to happen.") %] - </p> - </div> - <p> - <strong>[% loc('Category') %] </strong><input type="text" class="form-control" name="category" size="30" value="[% contact.category | html %]" required> - </p> - [% IF contact.in_storage %] - <input type="hidden" name="current_category" value="[% current_contact.category | html %]" > - [% END %] + [% PROCESS 'admin/bodies/_category_field.html' %] [% INCLUDE 'admin/bodies/_translations.html' %] |