diff options
Diffstat (limited to 'perllib/FixMyStreet/App/View')
-rw-r--r-- | perllib/FixMyStreet/App/View/Email.pm | 44 | ||||
-rw-r--r-- | perllib/FixMyStreet/App/View/Web.pm | 146 |
2 files changed, 190 insertions, 0 deletions
diff --git a/perllib/FixMyStreet/App/View/Email.pm b/perllib/FixMyStreet/App/View/Email.pm new file mode 100644 index 000000000..86d5c1d60 --- /dev/null +++ b/perllib/FixMyStreet/App/View/Email.pm @@ -0,0 +1,44 @@ +package FixMyStreet::App::View::Email; +use base 'Catalyst::View::TT'; + +use strict; +use warnings; + +use mySociety::Locale; +use FixMyStreet; + +__PACKAGE__->config( + TEMPLATE_EXTENSION => '.txt', + INCLUDE_PATH => [ # + FixMyStreet->path_to( 'templates', 'email', 'default' ), + ], + ENCODING => 'utf8', + render_die => 1, + expose_methods => ['loc'], +); + +=head1 NAME + +FixMyStreet::App::View::Email - TT View for FixMyStreet::App + +=head1 DESCRIPTION + +TT View for FixMyStreet::App. + +=cut + +=head2 loc + + [% loc('Some text to localize') %] + +Passes the text to the localisation engine for translations. + +=cut + +sub loc { + my ( $self, $c, @args ) = @_; + return _(@args); +} + +1; + diff --git a/perllib/FixMyStreet/App/View/Web.pm b/perllib/FixMyStreet/App/View/Web.pm new file mode 100644 index 000000000..df2d0ac20 --- /dev/null +++ b/perllib/FixMyStreet/App/View/Web.pm @@ -0,0 +1,146 @@ +package FixMyStreet::App::View::Web; +use base 'Catalyst::View::TT'; + +use strict; +use warnings; + +use mySociety::Locale; +use mySociety::Web qw(ent); +use FixMyStreet; +use CrossSell; +use Utils; + +__PACKAGE__->config( + TEMPLATE_EXTENSION => '.html', + INCLUDE_PATH => [ # + FixMyStreet->path_to( 'templates', 'web', 'default' ), + ], + ENCODING => 'utf8', + render_die => 1, + expose_methods => [ + 'loc', 'nget', 'tprintf', 'display_crosssell_advert', 'prettify_epoch', + 'add_links', + ], + FILTERS => { + escape_js => \&escape_js, + }, +); + +=head1 NAME + +FixMyStreet::App::View::Web - TT View for FixMyStreet::App + +=head1 DESCRIPTION + +TT View for FixMyStreet::App. + +=cut + +=head2 loc + + [% loc('Some text to localize') %] + +Passes the text to the localisation engine for translations. + +=cut + +sub loc { + my ( $self, $c, @args ) = @_; + return _(@args); +} + +=head2 nget + + [% nget( 'singular', 'plural', $number ) %] + +Use first or second srting depending on the number. + +=cut + +sub nget { + my ( $self, $c, @args ) = @_; + return mySociety::Locale::nget(@args); +} + +=head2 tprintf + + [% tprintf( 'foo %s bar', 'insert' ) %] + +sprintf (different name to avoid clash) + +=cut + +sub tprintf { + my ( $self, $c, $format, @args ) = @_; + return sprintf $format, @args; +} + +=head2 display_crosssell_advert + + [% display_crosssell_advert( email, name ) %] + +Displays a crosssell advert if permitted by the cobrand. + +=cut + +sub display_crosssell_advert { + my ( $self, $c, $email, $name, %data ) = @_; + + return unless $c->cobrand->allow_crosssell_adverts(); + return CrossSell::display_advert( $c, $email, $name, %data ); +} + +=head2 Utils::prettify_epoch + + [% pretty = prettify_epoch( $epoch, $short_bool ) %] + +Return a pretty version of the epoch. + + $short_bool = 1; # 16:02, 29 Mar 2011 + $short_bool = 0; # 16:02, Tuesday 29 March 2011 + +=cut + +sub prettify_epoch { + my ( $self, $c, $epoch, $short_bool ) = @_; + return Utils::prettify_epoch( $epoch, $short_bool ); +} + +=head2 add_links + + [% add_links( text ) | html_para %] + +Add some links to some text (and thus HTML-escapes the other text. + +=cut + +sub add_links { + my ( $self, $c, $text ) = @_; + + $text =~ s/\r//g; + $text = ent($text); + $text =~ s{(https?://[^\s]+)}{<a href="$1">$1</a>}g; + return $text; +} + +=head2 escape_js + +Used to escape strings that are going to be put inside JavaScript. + +=cut + +sub escape_js { + my $text = shift; + my %lookup = ( + '\\' => 'u005c', + '"' => 'u0022', + "'" => 'u0027', + '<' => 'u003c', + '>' => 'u003e', + ); + $text =~ s/([\\"'<>])/\\$lookup{$1}/g; + return $text; +} + +1; + |