aboutsummaryrefslogtreecommitdiffstats
path: root/perllib/Page.pm
diff options
context:
space:
mode:
Diffstat (limited to 'perllib/Page.pm')
-rw-r--r--perllib/Page.pm192
1 files changed, 110 insertions, 82 deletions
diff --git a/perllib/Page.pm b/perllib/Page.pm
index e659c0af5..b2e579888 100644
--- a/perllib/Page.pm
+++ b/perllib/Page.pm
@@ -25,6 +25,7 @@ use IO::String;
use POSIX qw(strftime);
use URI::Escape;
use Text::Template;
+use Template;
use Memcached;
use Problems;
@@ -267,117 +268,144 @@ sub template_include {
return $template->fill_in(HASH => \%params);
}
+=item tt2_template_include
+
+ $html = tt2_template_include( 'header', $q, $vars );
+
+Return HTML for a template, given a template name, request, and
+any parameters needed. This uses the TT2 templates that the Catalyst port uses.
+Intended to prevent having duplicate headers and footers whilst the migration is
+in progress.
+
+=cut
+
+sub _tt2_template_include_path {
+ my $q = shift;
+
+ # work out where the emplate dir is relative to the current file
+ ( my $project_dir = __FILE__ ) =~ s{/[^/]*?$}{};
+ my $template_root = "$project_dir/../templates/web";
+
+ # tidy up the '/foo/..' cruft
+ 1 while $template_root =~ s{[^/]+/../}{};
+
+ my @paths = ();
+ push @paths, "$template_root/$q->{site}" if $q->{site}; # cobrand
+ push @paths, "$template_root/default"; # fallback
+
+ # warn "template path: $_" for @paths;
+
+ return \@paths;
+}
+
+sub tt2_template_include {
+ my ( $template, $q, $params ) = @_;
+
+ # check that the template is 'header.html' or 'footer.html' - this is for
+ # transition only
+ unless ( $template eq 'header.html' || $template eq 'footer.html' ) {
+ warn "template not '(header|footer).html': '$template'";
+ return undef;
+ }
+
+ # create the template object
+ my $tt2 = Template->new(
+ {
+ INCLUDE_PATH => _tt2_template_include_path($q),
+ ENCODING => 'utf8',
+ }
+ );
+
+ # add/edit bits on the params to suit new templates
+ $params->{loc} = sub { return _(@_) }; # create the loc function for i18n
+ $params->{legacy_title} =
+ ( $params->{title} || '' ) . ( $params->{site_title} || '' );
+ $params->{legacy_rss} = delete $params->{rss};
+
+ # fake parts of the config that the templates need
+ $params->{c}{config}{STAGING_SITE} = mySociety::Config::get('STAGING_SITE');
+ $params->{c}{req}{uri}{path} = $ENV{REQUEST_URI};
+
+
+ my $html = '';
+ $tt2->process( $template, $params, \$html );
+
+ return $html;
+}
+
=item header Q [PARAM VALUE ...]
-Return HTML for the top of the page, given PARAMs (TITLE is required).
+ $html = Page::header( $q, %params );
+
+Return HTML for the top of the page, given %params ('title' is required).
+
+Also prints the HTTP headers for the page to STDOUT.
=cut
+
sub header ($%) {
- my ($q, %params) = @_;
- my $context = $params{context};
- my $default_params = Cobrand::header_params(get_cobrand($q), $q, %params);
+ my ( $q, %params ) = @_;
+
+ # get the context
+ my $context = $params{context};
+
+ # get default header parameters for the cobrand
+ my $default_params = Cobrand::header_params( get_cobrand($q), $q, %params );
my %default_params = %{$default_params};
- %params = (%default_params, %params);
- my %permitted_params = map { $_ => 1 } qw(title rss expires lastmodified template cachecontrol context status_code robots js);
- foreach (keys %params) {
- croak "bad parameter '$_'" if (!exists($permitted_params{$_}));
+ %params = ( %default_params, %params );
+
+ # check that all the params given ar allowed
+ my %permitted_params = map { $_ => 1 } (
+ 'title', 'rss', 'expires', 'lastmodified',
+ 'template', 'cachecontrol', 'context', 'status_code',
+ 'robots', 'js',
+ );
+ foreach ( keys %params ) {
+ croak "bad parameter '$_'" if ( !exists( $permitted_params{$_} ) );
}
+ # create the HTTP header
my %head = ();
- $head{-expires} = $params{expires} if $params{expires};
- $head{'-last-modified'} = time2str($params{lastmodified}) if $params{lastmodified};
+ $head{'-expires'} = $params{expires} if $params{expires};
+ $head{'-last-modified'} = time2str( $params{lastmodified} )
+ if $params{lastmodified};
$head{'-last-modified'} = time2str($lastmodified) if $lastmodified;
- $head{'-Cache-Control'} = $params{cachecontrol} if $params{cachecontrol};
- $head{'-status'} = $params{status_code} if $params{status_code};
+ $head{'-Cache-Control'} = $params{cachecontrol} if $params{cachecontrol};
+ $head{'-status'} = $params{status_code} if $params{status_code};
print $q->header(%head);
+
+ # mangle the title
$params{title} ||= '';
$params{title} .= ' - ' if $params{title};
- $params{title} = ent($params{title});
- $params{lang} = $mySociety::Locale::lang;
+ $params{title} = ent( $params{title} );
+
+ # get the language
+ $params{lang} = $mySociety::Locale::lang;
- my $vars = template_vars($q, %params);
- my $html = template_include('header', $q, template_root($q), %$vars);
+ # produce the html
+ my $vars = template_vars( $q, %params );
+ my $html = tt2_template_include( 'header.html', $q, $vars );
my $cache_val = $default_params{cachecontrol};
- if (mySociety::Config::get('STAGING_SITE')) {
- $html .= '<p class="error">' . _("This is a developer site; things might break at any time, and the database will be periodically deleted.") . '</p>';
- }
return $html;
}
+
=item footer
=cut
+
sub footer {
- my ($q, %params) = @_;
+ my ( $q, %params ) = @_;
my $pc = $q->param('pc') || '';
$pc = '?pc=' . URI::Escape::uri_escape_utf8($pc) if $pc;
- my $creditline = _('Built by <a href="http://www.mysociety.org/">mySociety</a>, using some <a href="http://github.com/mysociety/fixmystreet">clever</a>&nbsp;<a href="https://secure.mysociety.org/cvstrac/dir?d=mysociety/services/TilMa">code</a>.');
- if (mySociety::Config::get('COUNTRY') eq 'NO') {
- $creditline = _('Built by <a href="http://www.mysociety.org/">mySociety</a> and maintained by <a href="http://www.nuug.no/">NUUG</a>, using some <a href="http://github.com/mysociety/fixmystreet">clever</a>&nbsp;<a href="https://secure.mysociety.org/cvstrac/dir?d=mysociety/services/TilMa">code</a>.');
- }
+ %params = ( %params, pc => $pc, );
- %params = (%params,
- navigation => _('Navigation'),
- report => _("Report a problem"),
- reports => _("All reports"),
- alerts => _("Local alerts"),
- help => _("Help"),
- contact => _("Contact"),
- pc => $pc,
- orglogo => _('<a href="http://www.mysociety.org/"><img id="logo" width="133" height="26" src="/i/mysociety-dark.png" alt="View mySociety.org"><span id="logoie"></span></a>'),
- creditline => $creditline,
- );
-
- my $html = template_include('footer', $q, template_root($q), %params);
- if ($html) {
- my $lang = $mySociety::Locale::lang;
- if ($q->{site} eq 'emptyhomes' && $lang eq 'cy') {
- $html =~ s/25 Walter Road<br>Swansea/25 Heol Walter<br>Abertawe/;
- }
- return $html;
- }
-
- my $piwik = "";
- if (mySociety::Config::get('BASE_URL') eq "http://www.fixmystreet.com") {
- $piwik = <<EOF;
-<!-- Piwik -->
-<script type="text/javascript">
-var pkBaseURL = (("https:" == document.location.protocol) ? "https://piwik.mysociety.org/" : "http://piwik.mysociety.org/");
-document.write(unescape("%3Cscript src='" + pkBaseURL + "piwik.js' type='text/javascript'%3E%3C/script%3E"));
-</script><script type="text/javascript">
-try {
-var piwikTracker = Piwik.getTracker(pkBaseURL + "piwik.php", 8);
-piwikTracker.trackPageView();
-piwikTracker.enableLinkTracking();
-} catch( err ) {}
-</script><noscript><p><img src="http://piwik.mysociety.org/piwik.php?idsite=8" style="border:0" alt=""/></p></noscript>
-<!-- End Piwik Tag -->
-EOF
- }
+ my $html = tt2_template_include( 'footer.html', $q, \%params );
- return <<EOF;
-</div></div>
-<h2 class="v">$params{navigation}</h2>
-<ul id="navigation">
-<li><a href="/">$params{report}</a></li>
-<li><a href="/reports">$params{reports}</a></li>
-<li><a href="/alert$params{pc}">$params{alerts}</a></li>
-<li><a href="/faq">$params{help}</a></li>
-<li><a href="/contact">$params{contact}</a></li>
-</ul>
-
-$params{orglogo}
-
-<p id="footer">$params{creditline}</p>
-
-$piwik
-
-</body>
-</html>
-EOF
+ return $html;
}
=item error_page Q MESSAGE