diff options
-rw-r--r-- | perllib/Cobrand.pm | 331 |
1 files changed, 40 insertions, 291 deletions
diff --git a/perllib/Cobrand.pm b/perllib/Cobrand.pm index a781b1db0..dcea6a179 100644 --- a/perllib/Cobrand.pm +++ b/perllib/Cobrand.pm @@ -7,7 +7,7 @@ # Copyright (c) 2009 UK Citizens Online Democracy. All rights reserved. # Email: louise@mysociety.org. WWW: http://www.mysociety.org # -# $Id: Cobrand.pm,v 1.53 2009-12-15 15:54:37 matthew Exp $ +# $Id: Cobrand.pm,v 1.54 2009-12-15 17:21:08 matthew Exp $ package Cobrand; use strict; @@ -24,6 +24,14 @@ sub get_allowed_cobrands { return \@allowed_cobrands; } +sub call { + my ($cobrand, $fn, $default, @args) = @_; + return $default unless $cobrand; + my $handle = cobrand_handle($cobrand); + return $default unless $handle && $handle->can($fn); + return $handle->$fn(@args); +} + =item cobrand_handle Q Given a query that has the name of a site set, return a handle to the Util module for that @@ -71,15 +79,7 @@ Return a contact restriction clause if the cobrand uses a subset of the FixMyStr sub contact_restriction { my ($cobrand) = @_; - my $handle; - if ($cobrand){ - $handle = cobrand_handle($cobrand); - } - if ( !$cobrand || !$handle || ! $handle->can('contact_restriction')){ - return ''; - }{ - return $handle->contact_restriction(); - } + return call($cobrand, 'contact_restriction', ''); } =item base_url COBRAND @@ -104,15 +104,7 @@ version of the site sub base_url_for_emails { my ($cobrand, $cobrand_data) = @_; - my $handle; - if ($cobrand){ - $handle = cobrand_handle($cobrand); - } - if ( !$cobrand || !$handle || ! $handle->can('base_url_for_emails')){ - return base_url($cobrand); - }{ - return $handle->base_url_for_emails($cobrand_data); - } + return call($cobrand, 'base_url_for_emails', base_url($cobrand), $cobrand_data); } =item admin_base_url COBRAND @@ -122,16 +114,7 @@ Base URL for the admin interface. =cut sub admin_base_url { my ($cobrand) = @_; - my $handle; - if ($cobrand){ - $handle = cobrand_handle($cobrand); - } - if ( !$cobrand || !$handle || ! $handle->can('admin_base_url')){ - return 0; - }{ - return $handle->admin_base_url(); - } - + return call($cobrand, 'admin_base_url', 0); } =item writetothem_url COBRAND COBRAND_DATA @@ -141,16 +124,7 @@ URL for writetothem =cut sub writetothem_url { my ($cobrand, $cobrand_data) = @_; - my $handle; - if ($cobrand){ - $handle = cobrand_handle($cobrand); - } - if ( !$cobrand || !$handle || ! $handle->can('writetothem_url')){ - return 0; - }{ - return $handle->writetothem_url($cobrand_data); - } - + return call($cobrand, 'writetothem_url', 0, $cobrand_data); } =item email_host COBRAND @@ -202,15 +176,7 @@ Return HTML for a list of alert options for the cobrand. =cut sub alert_list_options { my ($cobrand, $q, @options) = @_; - my $handle; - if ($cobrand){ - $handle = cobrand_handle($cobrand); - } - if ( !$cobrand || !$handle || ! $handle->can('alert_list_options')){ - return 0; - }{ - return $handle->alert_list_options($q, @options); - } + return call($cobrand, 'alert_list_options', 0, $q, @options); } =item get_cobrand_conf COBRAND KEY @@ -320,17 +286,8 @@ Return the text that prompts the user to enter their postcode/place name =cut sub enter_postcode_text { - my ($cobrand, $q) = @_; - my $handle; - if ($cobrand){ - $handle = cobrand_handle($cobrand); - } - if ( !$cobrand || !$handle || !$handle->can('enter_postcode_text')){ - return _("Enter a nearby GB postcode, or street name and area:"); - } else{ - return $handle->enter_postcode_text($q); - } + return call($cobrand, 'enter_postcode_text', _("Enter a nearby GB postcode, or street name and area:"), $q); } =item disambiguate_location COBRAND S Q @@ -342,15 +299,7 @@ information available sub disambiguate_location { my ($cobrand, $s, $q) = @_; - my $handle; - if ($cobrand){ - $handle = cobrand_handle($cobrand); - } - if ( !$cobrand || !$handle || !$handle->can('disambiguate_location')){ - return $s; - } else{ - return $handle->disambiguate_location($s, $q); - } + return call($cobrand, 'disambiguate_location', $s, $s, $q); } =item prettify_epoch COBRAND EPOCHTIME @@ -359,16 +308,7 @@ sub disambiguate_location { sub prettify_epoch { my ($cobrand, $epochtime) = @_; - my $handle; - if ($cobrand){ - $handle = cobrand_handle($cobrand); - } - if ( !$cobrand || !$handle || !$handle->can('prettify_epoch')){ - return 0; - } else{ - return $handle->prettify_epoch($epochtime); - } - + return call($cobrand, 'prettify_epoch', 0, $epochtime); } =item form_elements FORM_NAME Q @@ -379,16 +319,7 @@ Return HTML for any extra needed elements for FORM_NAME sub form_elements { my ($cobrand, $form_name, $q) = @_; - my $handle; - if ($cobrand){ - $handle = cobrand_handle($cobrand); - } - if ( !$cobrand || !$handle || !$handle->can('form_elements')){ - return ''; - } else{ - return $handle->form_elements($form_name, $q); - } - + return call($cobrand, 'form_elements', '', $form_name, $q); } =item extra_problem_data COBRAND Q @@ -398,17 +329,8 @@ Return a string of extra data to be stored with a problem =cut sub extra_problem_data { - my ($cobrand, $q) = @_; - my $handle; - if ($cobrand){ - $handle = cobrand_handle($cobrand); - } - if ( !$cobrand || !$handle || !$handle->can('extra_problem_data')){ - return ''; - } else{ - return $handle->extra_problem_data($q); - } + return call($cobrand, 'extra_problem_data', '', $q); } =item extra_update_data COBRAND Q @@ -419,15 +341,7 @@ Return a string of extra data to be stored with a problem sub extra_update_data { my ($cobrand, $q) = @_; - my $handle; - if ($cobrand){ - $handle = cobrand_handle($cobrand); - } - if ( !$cobrand || !$handle || !$handle->can('extra_update_data')){ - return ''; - } else{ - return $handle->extra_update_data($q); - } + return call($cobrand, 'extra_update_data', '', $q); } =item extra_alert_data COBRAND Q @@ -438,15 +352,7 @@ Return a string of extra data to be stored with an alert sub extra_alert_data { my ($cobrand, $q) = @_; - my $handle; - if ($cobrand){ - $handle = cobrand_handle($cobrand); - } - if ( !$cobrand || !$handle || !$handle->can('extra_alert_data')){ - return ''; - } else{ - return $handle->extra_alert_data($q); - } + return call($cobrand, 'extra_alert_data', '', $q); } =item extra_data COBRAND Q @@ -457,15 +363,7 @@ Given a query Q, extract any extra data required by the cobrand sub extra_data { my ($cobrand, $q) = @_; - my $handle; - if ($cobrand){ - $handle = cobrand_handle($cobrand); - } - if ( !$cobrand || !$handle || !$handle->can('extra_data')){ - return ''; - } else{ - return $handle->extra_data($q); - } + return call($cobrand, 'extra_data', '', $q); } =item extra_params COBRAND Q @@ -476,16 +374,7 @@ any URLs in links produced on the page returned by that query. =cut sub extra_params { my ($cobrand, $q) = @_; - my $handle; - if ($cobrand){ - $handle = cobrand_handle($cobrand); - } - if ( !$cobrand || !$handle || !$handle->can('extra_params')){ - return ''; - } else{ - return $handle->extra_params($q); - } - + return call($cobrand, 'extra_params', '', $q); } =item show_watermark @@ -495,15 +384,7 @@ Returns a boolean indicating whether the map watermark should be displayed =cut sub show_watermark { my ($cobrand) = @_; - my $handle; - if ($cobrand){ - $handle = cobrand_handle($cobrand); - } - if ( !$cobrand || !$handle || !$handle->can('show_watermark')){ - return 1; - } else{ - return $handle->show_watermark(); - } + return call($cobrand, 'show_watermark', 1); } =item extra_problem_meta_text COBRAND PROBLEM @@ -513,15 +394,7 @@ Returns any extra text to be displayed with a problem. =cut sub extra_problem_meta_text { my ($cobrand, $problem) = @_; - my $handle; - if ($cobrand){ - $handle = cobrand_handle($cobrand); - } - if ( !$cobrand || !$handle || !$handle->can('extra_problem_meta_text')){ - return ''; - } else{ - return $handle->extra_problem_meta_text($problem); - } + return call($cobrand, 'extra_problem_meta_text', '', $problem); } =item extra_update_meta_text COBRAND PROBLEM @@ -531,15 +404,7 @@ Returns any extra text to be displayed with an update. =cut sub extra_update_meta_text { my ($cobrand, $update) = @_; - my $handle; - if ($cobrand){ - $handle = cobrand_handle($cobrand); - } - if ( !$cobrand || !$handle || !$handle->can('extra_update_meta_text')){ - return ''; - } else{ - return $handle->extra_update_meta_text($update); - } + return call($cobrand, 'extra_update_meta_text', '', $update); } =item url @@ -549,15 +414,7 @@ Given a URL, return a URL with any extra params needed appended to it. =cut sub url { my ($cobrand, $url, $q, $extra_data) = @_; - my $handle; - if ($cobrand){ - $handle = cobrand_handle($cobrand); - } - if ( !$cobrand || !$handle || !$handle->can('url')){ - return $url; - } else{ - return $handle->url($url, $q, $extra_data); - } + return call($cobrand, 'url', $url, $url, $q, $extra_data); } =item header_params @@ -587,15 +444,7 @@ based on the cobrand and current query Q sub root_path_js { my ($cobrand, $q) = @_; - my $handle; - if ($cobrand){ - $handle = cobrand_handle($cobrand); - } - if ( !$cobrand || !$handle || !$handle->can('root_path_js')){ - return 'var root_path = "";'; - } else{ - return $handle->root_path_js($q); - } + return call($cobrand, 'root_path_js', 'var root_path = "";', $q); } =item site_title COBRAND @@ -605,15 +454,7 @@ Return the title to be used in page heads. =cut sub site_title { my ($cobrand) = @_; - my $handle; - if ($cobrand){ - $handle = cobrand_handle($cobrand); - } - if ( !$cobrand || !$handle || !$handle->can('site_title')){ - return ''; - } else{ - return $handle->site_title(); - } + return call($cobrand, 'site_title', ''); } =item on_map_list_limit COBRAND @@ -624,15 +465,7 @@ on the map =cut sub on_map_list_limit { my ($cobrand) = @_; - my $handle; - if ($cobrand){ - $handle = cobrand_handle($cobrand); - } - if ( !$cobrand || !$handle || !$handle->can('on_map_list_limit')){ - return undef; - } else{ - return $handle->on_map_list_limit(); - } + return call($cobrand, 'on_map_list_limit', undef); } =item allow_photo_upload COBRAND @@ -643,15 +476,7 @@ Return a boolean indicating whether the cobrand allows photo uploads sub allow_photo_upload { my ($cobrand) = @_; - my $handle; - if ($cobrand){ - $handle = cobrand_handle($cobrand); - } - if ( !$cobrand || !$handle || !$handle->can('allow_photo_upload')){ - return 1; - } else{ - return $handle->allow_photo_upload(); - } + return call($cobrand, 'allow_photo_upload', 1); } =item allow_crosssell_adverts COBRAND @@ -661,15 +486,7 @@ Return a boolean indicating whether the cobrand allows the display of crosssell =cut sub allow_crosssell_adverts { my ($cobrand) = @_; - my $handle; - if ($cobrand){ - $handle = cobrand_handle($cobrand); - } - if ( !$cobrand || !$handle || !$handle->can('allow_crosssell_adverts')){ - return 1; - } else{ - return $handle->allow_crosssell_adverts(); - } + return call($cobrand, 'allow_crosssell_adverts', 1); } =item allow_photo_display COBRAND @@ -680,15 +497,7 @@ Return a boolean indicating whether the cobrand allows photo display sub allow_photo_display { my ($cobrand) = @_; - my $handle; - if ($cobrand){ - $handle = cobrand_handle($cobrand); - } - if ( !$cobrand || !$handle || !$handle->can('allow_photo_display')){ - return 1; - } else{ - return $handle->allow_photo_display(); - } + return call($cobrand, 'allow_photo_display', 1); } =item allow_update_reporting COBRAND @@ -700,16 +509,7 @@ to report them as offensive. sub allow_update_reporting { my ($cobrand) = @_; - my $handle; - if ($cobrand){ - $handle = cobrand_handle($cobrand); - } - if ( !$cobrand || !$handle || !$handle->can('allow_update_reporting')){ - return 0; - } else{ - return $handle->allow_update_reporting(); - } - + return call($cobrand, 'allow_update_reporting', 0); } =item geocoded_string_check LOCATION QUERY @@ -720,15 +520,7 @@ checks. =cut sub geocoded_string_check { my ($cobrand, $location, $query) = @_; - my $handle; - if ($cobrand){ - $handle = cobrand_handle($cobrand); - } - if ( !$cobrand || !$handle || !$handle->can('geocoded_string_check')){ - return 1; - } else{ - return $handle->geocoded_string_check($location, $query); - } + return call($cobrand, 'geocoded_string_check', 1, $location, $query); } =item council_check COBRAND COUNCILS QUERY @@ -756,16 +548,7 @@ Return an XSL to be used in rendering feeds =cut sub feed_xsl { my ($cobrand) = @_; - my $handle; - if ($cobrand){ - $handle = cobrand_handle($cobrand); - } - if ( !$cobrand || !$handle || !$handle->can('feed_xsl')){ - return '/xsl.xsl'; - } else{ - return $handle->feed_xsl(); - } - + return call($cobrand, 'feed_xsl', '/xsl.xsl'); } =item all_councils_report COBRAND @@ -776,15 +559,7 @@ Return a boolean indicating whether the cobrand displays a report of all council sub all_councils_report { my ($cobrand) = @_; - my $handle; - if ($cobrand){ - $handle = cobrand_handle($cobrand); - } - if ( !$cobrand || !$handle || !$handle->can('all_councils_report')){ - return 1; - } else{ - return $handle->all_councils_report(); - } + return call($cobrand, 'all_councils_report', 1); } =item ask_ever_reported @@ -796,15 +571,7 @@ is the first time they've reported a problem. sub ask_ever_reported { my ($cobrand) = @_; - my $handle; - if ($cobrand){ - $handle = cobrand_handle($cobrand); - } - if ( !$cobrand || !$handle || !$handle->can('ask_ever_reported')){ - return 1; - } else{ - return $handle->ask_ever_reported(); - } + return call($cobrand, 'ask_ever_reported', 1); } =item admin_pages COBRAND @@ -814,16 +581,7 @@ List of names of pages to display on the admin interface =cut sub admin_pages { my ($cobrand) = @_; - my $handle; - if ($cobrand){ - $handle = cobrand_handle($cobrand); - } - if ( !$cobrand || !$handle || !$handle->can('admin_pages')){ - return 0; - } else{ - return $handle->admin_pages(); - } - + return call($cobrand, 'admin_pages', 0); } =item admin_show_creation_graph COBRAND @@ -832,17 +590,8 @@ Show the problem creation graph in the admin interface =cut sub admin_show_creation_graph { my ($cobrand) = @_; - my $handle; - if ($cobrand){ - $handle = cobrand_handle($cobrand); - } - if ( !$cobrand || !$handle || !$handle->can('admin_show_creation_graph')){ - return 1; - } else{ - return $handle->admin_show_creation_graph(); - } + return call($cobrand, 'admin_show_creation_graph', 1); } 1; - |