From 34795d026faf45b0a1bfc103362eb118766c46d7 Mon Sep 17 00:00:00 2001 From: Matthew Somerville Date: Thu, 22 Sep 2016 16:12:40 +0100 Subject: Add cobrand call_hook fn, calls a fn if defined. --- perllib/FixMyStreet/App.pm | 2 +- perllib/FixMyStreet/App/Controller/Report/New.pm | 9 +++------ perllib/FixMyStreet/Cobrand/Base.pm | 12 ++++++++++++ perllib/FixMyStreet/DB/Result/Problem.pm | 19 +++++-------------- perllib/FixMyStreet/Script/Reports.pm | 4 +--- perllib/FixMyStreet/SendReport/Email.pm | 2 +- 6 files changed, 23 insertions(+), 25 deletions(-) diff --git a/perllib/FixMyStreet/App.pm b/perllib/FixMyStreet/App.pm index 7c5ef488b..2fc560bc8 100644 --- a/perllib/FixMyStreet/App.pm +++ b/perllib/FixMyStreet/App.pm @@ -163,7 +163,7 @@ sub setup_request { my $cobrand = $c->cobrand; - $cobrand->add_response_headers if $cobrand->can('add_response_headers'); + $cobrand->call_hook('add_response_headers'); # append the cobrand templates to the include path $c->stash->{additional_template_paths} = $cobrand->path_to_web_templates; diff --git a/perllib/FixMyStreet/App/Controller/Report/New.pm b/perllib/FixMyStreet/App/Controller/Report/New.pm index 6d90b6ee9..2a68b170e 100644 --- a/perllib/FixMyStreet/App/Controller/Report/New.pm +++ b/perllib/FixMyStreet/App/Controller/Report/New.pm @@ -659,8 +659,7 @@ sub setup_categories_and_bodies : Private { push @category_options, _('Other') if $seen{_('Other')}; } - $c->cobrand->munge_category_list(\@category_options, \@contacts, \%category_extras) - if $c->cobrand->can('munge_category_list'); + $c->cobrand->call_hook(munge_category_list => \@category_options, \@contacts, \%category_extras); # put results onto stash for display $c->stash->{bodies} = \%bodies; @@ -903,7 +902,7 @@ sub contacts_to_bodies : Private { if ($c->stash->{unresponsive}{$category} || $c->stash->{unresponsive}{ALL}) { []; } else { - if ( $c->cobrand->can('singleton_bodies_str') && $c->cobrand->singleton_bodies_str ) { + if ( $c->cobrand->call_hook('singleton_bodies_str') ) { # Cobrands like Zurich can only ever have a single body: 'x', because some functionality # relies on string comparison against bodies_str. [ $contacts[0]->body ]; @@ -1033,9 +1032,7 @@ sub send_problem_confirm_email : Private { $template = 'problem-confirm-not-sending.txt' unless $report->bodies_str; $c->stash->{token_url} = $c->uri_for_email( '/P', $token->token ); - if ($c->cobrand->can('problem_confirm_email_extras')) { - $c->cobrand->problem_confirm_email_extras($report); - } + $c->cobrand->call_hook(problem_confirm_email_extras => $report); $c->send_email( $template, { to => [ $report->name ? [ $report->user->email, $report->name ] : $report->user->email ], diff --git a/perllib/FixMyStreet/Cobrand/Base.pm b/perllib/FixMyStreet/Cobrand/Base.pm index a9eed0018..ea2b8f410 100644 --- a/perllib/FixMyStreet/Cobrand/Base.pm +++ b/perllib/FixMyStreet/Cobrand/Base.pm @@ -65,6 +65,18 @@ sub is_default { return $self->moniker eq 'default'; } +=head2 call_hook + + $cobrand->call_hook(foo => 1, 2, 3); # calls $cobrand->foo(1, 2, 3) if it exists + +=cut + +sub call_hook { + my ($self, $method_name, @args) = @_; + my $method = $self->can($method_name) or return; + return $self->$method(@args); +} + # NB: this Base class is for 'meta' features. To add base methods for all cobrands, # you may want to look at FMS::Cobrand::Default instead! diff --git a/perllib/FixMyStreet/DB/Result/Problem.pm b/perllib/FixMyStreet/DB/Result/Problem.pm index 97cb28fe8..f469c4275 100644 --- a/perllib/FixMyStreet/DB/Result/Problem.pm +++ b/perllib/FixMyStreet/DB/Result/Problem.pm @@ -613,9 +613,7 @@ sub meta_line { my $meta = ''; my $category = $problem->category; - if ($c->cobrand->can('change_category_text')) { - $category = $c->cobrand->change_category_text($category); - } + $category = $c->cobrand->call_hook(change_category_text => $category) || $category; if ( $problem->anonymous ) { if ( $problem->service and $category && $category ne _('Other') ) { @@ -748,17 +746,10 @@ sub can_display_external_id { sub duration_string { my ( $problem, $c ) = @_; - my $body; - if ( $c->cobrand->can('link_to_council_cobrand') ) { - $body = $c->cobrand->link_to_council_cobrand($problem); - } else { - $body = $problem->body( $c ); - } - if ( $c->cobrand->can('get_body_handler_for_problem') ) { - my $handler = $c->cobrand->get_body_handler_for_problem( $problem ); - if ( $handler->can('is_council_with_case_management') && $handler->is_council_with_case_management ) { - return sprintf(_('Received by %s moments later'), $body); - } + my $body = $c->cobrand->call_hook(link_to_council_cobrand => $problem) || $problem->body($c); + my $handler = $c->cobrand->call_hook(get_body_handler_for_problem => $problem); + if ( $handler && $handler->call_hook('is_council_with_case_management') ) { + return sprintf(_('Received by %s moments later'), $body); } return unless $problem->whensent; return sprintf(_('Sent to %s %s later'), $body, diff --git a/perllib/FixMyStreet/Script/Reports.pm b/perllib/FixMyStreet/Script/Reports.pm index 193c5fa41..a7f3cb84b 100644 --- a/perllib/FixMyStreet/Script/Reports.pm +++ b/perllib/FixMyStreet/Script/Reports.pm @@ -112,9 +112,7 @@ sub send(;$) { $h{user_details} .= sprintf(_('Email: %s'), $row->user->email) . "\n\n"; } - if ($cobrand->can('process_additional_metadata_for_email')) { - $cobrand->process_additional_metadata_for_email($row, \%h); - } + $cobrand->call_hook(process_additional_metadata_for_email => $row, \%h); my $bodies = FixMyStreet::DB->resultset('Body')->search( { id => $row->bodies_str_ids }, diff --git a/perllib/FixMyStreet/SendReport/Email.pm b/perllib/FixMyStreet/SendReport/Email.pm index 4cee58d42..5a287a208 100644 --- a/perllib/FixMyStreet/SendReport/Email.pm +++ b/perllib/FixMyStreet/SendReport/Email.pm @@ -84,7 +84,7 @@ sub send { From => $self->send_from( $row ), }; - $cobrand->munge_sendreport_params($row, $h, $params) if $cobrand->can('munge_sendreport_params'); + $cobrand->call_hook(munge_sendreport_params => $row, $h, $params); $params->{Bcc} = $self->bcc if @{$self->bcc}; -- cgit v1.2.3