header-and-footer-templates.pl 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. # Copyright (C) 2004 Alex Schroeder <alex@emacswiki.org>
  2. #
  3. # This program is free software; you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation; either version 3 of the License, or
  6. # (at your option) any later version.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. use strict;
  16. use v5.10;
  17. AddModuleDescription('header-and-footer-templates.pl', 'Comments on HTML Templates');
  18. our ($q, $Now, $Message, $DataDir, %SpecialDays);
  19. our ($HtmlTemplateDir);
  20. use HTML::Template;
  21. $HtmlTemplateDir = "$DataDir/templates";
  22. sub HtmlTemplateLanguage {
  23. my $requested_language = $q->http('Accept-language');
  24. my @languages = split(/ *, */, $requested_language);
  25. my %Lang = ();
  26. foreach (@languages) {
  27. my $qual = 1;
  28. $qual = $1 if (/q=([0-9.]+)/);
  29. $Lang{$qual} = $1 if (/^([-a-z]+)/);
  30. }
  31. return map { $Lang{$_} } sort { $b <=> $a } keys %Lang;
  32. }
  33. sub HtmlTemplate {
  34. my $type = shift;
  35. # return header.de.html, or header.html, or error.html, or report an error...
  36. foreach my $f ((map { "$type.$_" } HtmlTemplateLanguage()), $type, "error") {
  37. return "$HtmlTemplateDir/$f.html" if IsFile("$HtmlTemplateDir/$f.html");
  38. }
  39. ReportError(Tss('Could not find %1.html template in %2', $type, $HtmlTemplateDir),
  40. '500 INTERNAL SERVER ERROR');
  41. }
  42. sub GetSpecialDays {
  43. if (%SpecialDays) {
  44. my ($sec, $min, $hour, $mday, $mon, $year) = gmtime($Now);
  45. return $SpecialDays{($mon + 1) . '-' . $mday};
  46. }
  47. }
  48. *GetHeader = \&HeaderTemplate;
  49. sub HeaderTemplate {
  50. my ($id, $title, $oldId, $nocache, $status) = @_;
  51. if ($oldId ne '') {
  52. $Message .= $q->p('(' . Ts('redirected from %s', GetEditLink($oldId, $oldId)) . ')');
  53. }
  54. my $template = HTML::Template->new(filename => HtmlTemplate('header'),
  55. die_on_bad_params => 0);
  56. $template->param(GOTO_BAR => GetGotoBar($id));
  57. $template->param(SPECIAL_DAYS => GetSpecialDays());
  58. $template->param(MESSAGE => $Message);
  59. $template->param(ID => $id);
  60. $template->param(TITLE => $title);
  61. $template->param(SEARCH => GetSearchForm());
  62. return GetHttpHeader('text/html', $nocache ? $Now : 0, $status)
  63. . $template->output;
  64. }
  65. *PrintFooter = \&PrintFooterTemplate;
  66. sub PrintFooterTemplate {
  67. my ($id, $rev, $comment) = @_;
  68. my $template = HTML::Template->new(filename => HtmlTemplate('footer'),
  69. die_on_bad_params => 0);
  70. $template->param(GOTO_BAR => GetGotoBar($id));
  71. $template->param(SPECIAL_DAYS => GetSpecialDays());
  72. $template->param(ID => $id);
  73. $template->param(COMMENT_FORM => GetCommentForm($id, $rev, $comment));
  74. $template->param(FOOTER_LINKS => GetFooterLinks($id, $rev));
  75. $template->param(ADMIN_LINKS => GetAdminBar($id, $rev)) if UserIsAdmin();
  76. $template->param(TIMESTAMP => GetFooterTimestamp($id, $rev));
  77. $template->param(SEARCH => GetSearchForm());
  78. $template->param(SISTER_SITES => GetSisterSites($id));
  79. $template->param(NEAR_LINKS_USED => GetNearLinksUsed($id));
  80. print $template->output;
  81. }