diff options
Diffstat (limited to 'perllib/FixMyStreet')
-rw-r--r-- | perllib/FixMyStreet/App.pm | 78 | ||||
-rw-r--r-- | perllib/FixMyStreet/App/Controller/Root.pm | 18 | ||||
-rw-r--r-- | perllib/FixMyStreet/App/View/Web.pm | 7 | ||||
-rw-r--r-- | perllib/FixMyStreet/Cobrand.pm | 53 | ||||
-rw-r--r-- | perllib/FixMyStreet/Cobrand/Default.pm | 67 | ||||
-rw-r--r-- | perllib/FixMyStreet/Cobrand/EmptyHomes.pm | 28 | ||||
-rw-r--r-- | perllib/FixMyStreet/Cobrand/FiksGataMi.pm | 4 |
7 files changed, 176 insertions, 79 deletions
diff --git a/perllib/FixMyStreet/App.pm b/perllib/FixMyStreet/App.pm index 870453b09..bc9bd7672 100644 --- a/perllib/FixMyStreet/App.pm +++ b/perllib/FixMyStreet/App.pm @@ -3,6 +3,10 @@ use Moose; use namespace::autoclean; use Catalyst::Runtime 5.80; +use FixMyStreet; +use FixMyStreet::Cobrand; +use Memcached; +use Problems; use Catalyst qw/ ConfigLoader @@ -13,11 +17,6 @@ extends 'Catalyst'; our $VERSION = '0.01'; -BEGIN { - use mySociety::Config; - mySociety::Config::set_file( __PACKAGE__->path_to("conf/general") ); -} - # Configure the application. # # Note that settings in fixmystreet_app.conf (or other external @@ -28,7 +27,9 @@ BEGIN { # local deployment. __PACKAGE__->config( - %{ mySociety::Config::get_list() }, + + # get the config from the core object + %{ FixMyStreet->config() }, name => 'FixMyStreet::App', @@ -37,7 +38,7 @@ __PACKAGE__->config( # Serve anything in web dir that is not a .cgi script static => { # - include_path => [ __PACKAGE__->path_to("web") . "" ], + include_path => [ FixMyStreet->path_to("web") . "" ], ignore_extensions => ['cgi'], } ); @@ -55,20 +56,67 @@ FixMyStreet::App - Catalyst based application =head1 DESCRIPTION -[enter your description here] +FixMyStreet.com codebase -=head1 SEE ALSO +=head1 METHODS -L<FixMyStreet::App::Controller::Root>, L<Catalyst> +=head2 cobrand + + $cobrand = $c->cobrand(); + +Returns the cobrand object. If not already determined this request finds it and +caches it to the stash. + +=cut + +sub cobrand { + my $c = shift; + return $c->stash->{cobrand} ||= $c->_get_cobrand(); +} + +sub _get_cobrand { + my $c = shift; + my $host = $c->req->uri->host; + my $cobrand_class = FixMyStreet::Cobrand->get_class_for_host($host); + return $cobrand_class->new( { request => $c->req } ); +} -=head1 AUTHOR +=head2 setup_cobrand -Edmund von der Burg,,, + $cobrand = $c->setup_cobrand(); -=head1 LICENSE +Work out which cobrand we should be using. Set the environment correctly - eg +template paths -This library is free software. You can redistribute it and/or modify -it under the same terms as Perl itself. +=cut + +sub setup_cobrand { + my $c = shift; + my $cobrand = $c->cobrand; + + # append the cobrand templates to the include path + $c->stash->{additional_template_paths} = + [ $cobrand->path_to_web_templates . '' ]; + + my $host = $c->req->uri->host; + my $lang = + $host =~ /^en\./ ? 'en-gb' + : $host =~ /cy/ ? 'cy' + : undef; + + # set the language and the translation file to use + $cobrand->set_lang_and_domain( $lang, 1 ); + + Problems::set_site_restriction_with_cobrand_object($cobrand); + + Memcached::set_namespace( FixMyStreet->config('BCI_DB_NAME') . ":" ); + + return $cobrand; +} + +=head1 SEE ALSO + +L<FixMyStreet::App::Controller::Root>, L<Catalyst> =cut diff --git a/perllib/FixMyStreet/App/Controller/Root.pm b/perllib/FixMyStreet/App/Controller/Root.pm index b61650f79..debddef38 100644 --- a/perllib/FixMyStreet/App/Controller/Root.pm +++ b/perllib/FixMyStreet/App/Controller/Root.pm @@ -22,23 +22,13 @@ Set up general things for this instance =cut -sub auto { +sub auto : Private { my ( $self, $c ) = @_; - return; -} - -=head2 index - -The root page (/) - -=cut - -sub index : Path : Args(0) { - my ( $self, $c ) = @_; + # decide which cobrand this request should use + $c->setup_cobrand(); - # Hello World - $c->response->body( $c->welcome_message ); + return 1; } =head2 default diff --git a/perllib/FixMyStreet/App/View/Web.pm b/perllib/FixMyStreet/App/View/Web.pm index dcda54db9..306e4c5a7 100644 --- a/perllib/FixMyStreet/App/View/Web.pm +++ b/perllib/FixMyStreet/App/View/Web.pm @@ -4,12 +4,13 @@ use base 'Catalyst::View::TT'; use strict; use warnings; -use FixMyStreet::App; +use mySociety::Locale; +use FixMyStreet; __PACKAGE__->config( TEMPLATE_EXTENSION => '.html', INCLUDE_PATH => [ # - FixMyStreet::App->path_to( 'templates', 'web', 'default' ), + FixMyStreet->path_to( 'templates', 'web', 'default' ), ], render_die => 1, expose_methods => ['loc'], @@ -37,7 +38,7 @@ FIXME - currently just passes through. sub loc { my ( $self, $c, @args ) = @_; - return join ' ', @args; + return _(@args); } 1; diff --git a/perllib/FixMyStreet/Cobrand.pm b/perllib/FixMyStreet/Cobrand.pm index 74414e270..91155db6e 100644 --- a/perllib/FixMyStreet/Cobrand.pm +++ b/perllib/FixMyStreet/Cobrand.pm @@ -6,6 +6,7 @@ package FixMyStreet::Cobrand; use strict; use warnings; +use FixMyStreet; use Carp; use Module::Pluggable @@ -13,42 +14,58 @@ use Module::Pluggable search_path => ['FixMyStreet::Cobrand'], require => 1; -=item get_allowed_cobrands +my @ALL_COBRAND_CLASSES = __PACKAGE__->_cobrands; + +=head2 get_allowed_cobrands Return an array reference of allowed cobrand subdomains =cut sub get_allowed_cobrands { - - - my $allowed_cobrand_string = mySociety::Config::get('ALLOWED_COBRANDS'); + my $allowed_cobrand_string = FixMyStreet->config('ALLOWED_COBRANDS'); my @allowed_cobrands = split( /\|/, $allowed_cobrand_string ); return \@allowed_cobrands; } -=item cobrand_handle Q +=head2 available_cobrand_classes + + @available_cobrand_classes = + FixMyStreet::Cobrand->available_cobrand_classes(); -Given a query that has the name of a site set, return a handle to the Util module for that -site, if one exists, or zero if not. +Return an array of all the classes that were found and that have monikers that +match the values from get_allowed_cobrands. =cut -sub cobrand_handle { - my $cobrand = shift; +sub available_cobrand_classes { + my $class = shift; + + my %allowed = map { $_ => 1 } @{ $class->get_allowed_cobrands }; + my @avail = grep { $allowed{ $_->moniker } } @ALL_COBRAND_CLASSES; + + return @avail; +} + +=head2 get_class_for_host + + $cobrand_class = FixMyStreet::Cobrand->get_class_for_host( $host ); - our %handles; +Given a host determine which cobrand we should be using. + +=cut - # Once we have a handle defined, return it. - return $handles{$cobrand} if defined $handles{$cobrand}; +sub get_class_for_host { + my $class = shift; + my $host = shift; - my $cobrand_class = ucfirst($cobrand); - my $class = "Cobrands::" . $cobrand_class . "::Util"; - eval "use $class"; + foreach my $avail ( $class->available_cobrand_classes ) { + my $moniker = $avail->moniker; + return $avail if $host =~ m{$moniker}; + } - eval { $handles{$cobrand} = $class->new }; - $handles{$cobrand} = 0 if $@; - return $handles{$cobrand}; + # if none match then use the default + return 'FixMyStreet::Cobrand::Default'; } 1; diff --git a/perllib/FixMyStreet/Cobrand/Default.pm b/perllib/FixMyStreet/Cobrand/Default.pm index 8b6f7f5f0..608a754f7 100644 --- a/perllib/FixMyStreet/Cobrand/Default.pm +++ b/perllib/FixMyStreet/Cobrand/Default.pm @@ -24,6 +24,22 @@ sub new { return bless $self, $class; } +=head2 moniker + + $moniker = $cobrand_class->moniker(); + +Returns a moniker that can be used to identify this cobrand. By default this is +the last part of the class name lowercased - eg 'F::C::SomeCobrand' becomes +'somecobrand'. + +=cut + +sub moniker { + my $class = ref( $_[0] ) || $_[0]; # deal with object or class + my ($last_part) = $class =~ m{::(\w+)$}; + return lc($last_part); +} + =head2 q $request = $cobrand->q; @@ -36,11 +52,26 @@ use a request-related method out of a request-context. =cut sub q { + my $self = shift; return $self->{request} || croak "No request has been set" . " - should you be calling this method outside of a web request?"; } +=head2 path_to_web_templates + + $path = $cobrand->path_to_web_templates( ); + +Returns the path to the templates for this cobrand - by default +"templates/web/$moniker" + +=cut + +sub path_to_web_templates { + my $self = shift; + return FixMyStreet->path_to( 'templates/web', $self->moniker ); +} + =head1 site_restriction Return a site restriction clause and a site key if the cobrand uses a subset of @@ -100,7 +131,8 @@ sub base_url { mySociety::Config::get('BASE_URL') } =head2 enter_postcode_text -Return the text that prompts the user to enter their postcode/place name. Parameter is QUERY +Return the text that prompts the user to enter their postcode/place name. +Parameter is QUERY =cut @@ -124,7 +156,8 @@ sub set_lang_and_domain { =head2 alert_list_options -Return HTML for a list of alert options for the cobrand, given QUERY and OPTIONS. +Return HTML for a list of alert options for the cobrand, given QUERY and +OPTIONS. =cut @@ -184,7 +217,8 @@ sub prettify_epoch { 0 } =head2 form_elements -Parameters are FORM_NAME, QUERY. Return HTML for any extra needed elements for FORM_NAME +Parameters are FORM_NAME, QUERY. Return HTML for any extra needed elements for +FORM_NAME =cut @@ -192,7 +226,8 @@ sub form_elements { '' } =head2 cobrand_data_for_generic_update -Parameter is UPDATE_DATA, a reference to a hash of non-cobranded update data. Return cobrand extra data for the update +Parameter is UPDATE_DATA, a reference to a hash of non-cobranded update data. +Return cobrand extra data for the update =cut @@ -200,7 +235,8 @@ sub cobrand_data_for_generic_update { '' } =head2 cobrand_data_for_generic_update -Parameter is PROBLEM_DATA, a reference to a hash of non-cobranded problem data. Return cobrand extra data for the problem +Parameter is PROBLEM_DATA, a reference to a hash of non-cobranded problem data. +Return cobrand extra data for the problem =cut @@ -265,7 +301,8 @@ sub extra_update_meta_text { '' } =head2 url -Given a URL ($_[1]), QUERY, EXTRA_DATA, return a URL with any extra params needed appended to it. +Given a URL ($_[1]), QUERY, EXTRA_DATA, return a URL with any extra params +needed appended to it. =cut @@ -345,7 +382,7 @@ string LOCATION passes the cobrands checks. =cut -geocoded_string_check { return 1; } +sub geocoded_string_check { return 1; } =head2 council_check @@ -354,7 +391,7 @@ COUNCILS pass any extra checks. CONTEXT is where we are on the site. =cut -sub council_check { return ( 1, '' ) } +sub council_check { return ( 1, '' ); } =head2 feed_xsl @@ -424,11 +461,11 @@ Get the value for KEY from the config file for COBRAND sub get_cobrand_conf { my ( $self, $key ) = @_; - my $value = undef; - my $cobrand_code = $self->code; + my $value = undef; + my $cobrand_moniker = $self->moniker; my $cobrand_config_file = - FixMyStreet->path_to("conf/cobrands/$cobrand_code/general"); + FixMyStreet->path_to("conf/cobrands/$cobrand_moniker/general"); my $normal_config_file = FixMyStreet->path_to('conf/general'); if ( -e $cobrand_config_file ) { @@ -437,7 +474,7 @@ sub get_cobrand_conf { # change mySociety::Config so that it can return values from a # particular config file instead mySociety::Config::set_file("$cobrand_config_file"); - my $config_key = $key . "_" . uc($cobrand_code); + my $config_key = $key . "_" . uc($cobrand_moniker); $value = mySociety::Config::get( $config_key, undef ); mySociety::Config::set_file("$normal_config_file"); } @@ -457,11 +494,11 @@ Return if we are the virtual host that sends email for this cobrand =cut sub email_host { - my $self = shift; - my $cobrand_code_uc = uc( $self->code ); + my $self = shift; + my $cobrand_moniker_uc = uc( $self->moniker ); my $email_vhost = - mySociety::Config::get("EMAIL_VHOST_$cobrand_code_uc") + mySociety::Config::get("EMAIL_VHOST_$cobrand_moniker_uc") || mySociety::Config::get("EMAIL_VHOST") || ''; diff --git a/perllib/FixMyStreet/Cobrand/EmptyHomes.pm b/perllib/FixMyStreet/Cobrand/EmptyHomes.pm index 2d0798989..6b907cbd0 100644 --- a/perllib/FixMyStreet/Cobrand/EmptyHomes.pm +++ b/perllib/FixMyStreet/Cobrand/EmptyHomes.pm @@ -1,9 +1,11 @@ -package FixMyStreet::Cobrands::EmptyHomes; -use base 'FixMyStreet::Cobrands::Default'; +package FixMyStreet::Cobrand::EmptyHomes; +use base 'FixMyStreet::Cobrand::Default'; use strict; use warnings; +use FixMyStreet; +use mySociety::Locale; use Carp; =item @@ -13,11 +15,11 @@ Return the base url for this cobranded site =cut sub base_url { - my $base_url = mySociety::Config::get('BASE_URL'); - if ($base_url !~ /emptyhomes/) { - $base_url =~ s/http:\/\//http:\/\/emptyhomes\./g; - } - return $base_url; + my $base_url = FixMyStreet->config('BASE_URL'); + if ( $base_url !~ /emptyhomes/ ) { + $base_url =~ s/http:\/\//http:\/\/emptyhomes\./g; + } + return $base_url; } sub admin_base_url { @@ -25,7 +27,7 @@ sub admin_base_url { } sub area_types { - return qw(DIS LBO MTD UTA LGD COI); # No CTY + return qw(DIS LBO MTD UTA LGD COI); # No CTY } =item set_lang_and_domain LANG UNICODE @@ -35,9 +37,10 @@ Set the language and text domain for the site based on the query and host. =cut sub set_lang_and_domain { - my ($self, $lang, $unicode) = @_; - mySociety::Locale::negotiate_language('en-gb,English,en_GB|cy,Cymraeg,cy_GB', $lang); - mySociety::Locale::gettext_domain('FixMyStreet-EmptyHomes', $unicode); + my ( $self, $lang, $unicode ) = @_; + mySociety::Locale::negotiate_language( + 'en-gb,English,en_GB|cy,Cymraeg,cy_GB', $lang ); + mySociety::Locale::gettext_domain( 'FixMyStreet-EmptyHomes', $unicode ); mySociety::Locale::change(); } @@ -47,7 +50,7 @@ Return the title to be used in page heads =cut -sub site_title { +sub site_title { my ($self) = @_; return _('Report Empty Homes'); } @@ -57,6 +60,7 @@ sub site_title { Return the XSL file path to be used for feeds' =cut + sub feed_xsl { my ($self) = @_; return '/xsl.eha.xsl'; diff --git a/perllib/FixMyStreet/Cobrand/FiksGataMi.pm b/perllib/FixMyStreet/Cobrand/FiksGataMi.pm index feec6b0f0..25a4ad83c 100644 --- a/perllib/FixMyStreet/Cobrand/FiksGataMi.pm +++ b/perllib/FixMyStreet/Cobrand/FiksGataMi.pm @@ -1,5 +1,5 @@ -package FixMyStreet::Cobrands::FiksGataMi; -use base 'FixMyStreet::Cobrands::Default'; +package FixMyStreet::Cobrand::FiksGataMi; +use base 'FixMyStreet::Cobrand::Default'; use strict; use warnings; |