Avoid XSLT stylesheet building for each biblio record to transform
[koha.git] / C4 / Output.pm
blobfd32541a02f52b12091445a41dd1d46b9ced3d13
1 package C4::Output;
3 #package to deal with marking up output
4 #You will need to edit parts of this pm
5 #set the value of path to be where your html lives
7 # Copyright 2000-2002 Katipo Communications
9 # This file is part of Koha.
11 # Koha is free software; you can redistribute it and/or modify it under the
12 # terms of the GNU General Public License as published by the Free Software
13 # Foundation; either version 2 of the License, or (at your option) any later
14 # version.
16 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
17 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
18 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
20 # You should have received a copy of the GNU General Public License along with
21 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
22 # Suite 330, Boston, MA 02111-1307 USA
25 # NOTE: I'm pretty sure this module is deprecated in favor of
26 # templates.
28 use strict;
30 use C4::Context;
31 use C4::Languages qw(getTranslatedLanguages get_bidi regex_lang_subtags language_get_description accept_language );
33 use HTML::Template::Pro;
34 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
36 BEGIN {
37 # set the version for version checking
38 $VERSION = 3.03;
39 require Exporter;
40 @ISA = qw(Exporter);
41 @EXPORT_OK = qw(&output_ajax_with_http_headers &is_ajax); # More stuff should go here instead
42 %EXPORT_TAGS = ( all =>[qw(&themelanguage &gettemplate setlanguagecookie pagination_bar
43 &output_ajax_with_http_headers &output_html_with_http_headers)],
44 ajax =>[qw(&output_ajax_with_http_headers is_ajax)],
45 html =>[qw(&output_html_with_http_headers)]
47 push @EXPORT, qw(
48 &themelanguage &gettemplate setlanguagecookie pagination_bar
50 push @EXPORT, qw(
51 &output_html_with_http_headers
55 =head1 NAME
57 C4::Output - Functions for managing templates
59 =head1 FUNCTIONS
61 =over 2
63 =cut
65 #FIXME: this is a quick fix to stop rc1 installing broken
66 #Still trying to figure out the correct fix.
67 my $path = C4::Context->config('intrahtdocs') . "/prog/en/includes/";
69 #---------------------------------------------------------------------------------------------------------
70 # FIXME - POD
71 sub gettemplate {
72 my ( $tmplbase, $interface, $query ) = @_;
73 ($query) or warn "no query in gettemplate";
74 my $htdocs;
75 if ( $interface ne "intranet" ) {
76 $htdocs = C4::Context->config('opachtdocs');
78 else {
79 $htdocs = C4::Context->config('intrahtdocs');
81 my $path = C4::Context->preference('intranet_includes') || 'includes';
82 my ( $theme, $lang ) = themelanguage( $htdocs, $tmplbase, $interface, $query );
83 my $opacstylesheet = C4::Context->preference('opacstylesheet');
85 # if the template doesn't exist, load the English one as a last resort
86 my $filename = "$htdocs/$theme/$lang/modules/$tmplbase";
87 unless (-f $filename) {
88 $lang = 'en';
89 $filename = "$htdocs/$theme/$lang/modules/$tmplbase";
91 my $template = HTML::Template::Pro->new(
92 filename => $filename,
93 die_on_bad_params => 1,
94 global_vars => 1,
95 case_sensitive => 1,
96 loop_context_vars => 1, # enable: __first__, __last__, __inner__, __odd__, __counter__
97 path => ["$htdocs/$theme/$lang/$path"]
99 my $themelang=( $interface ne 'intranet' ? '/opac-tmpl' : '/intranet-tmpl' )
100 . "/$theme/$lang";
101 $template->param(
102 themelang => $themelang,
103 yuipath => (C4::Context->preference("yuipath") eq "local"?"$themelang/lib/yui":C4::Context->preference("yuipath")),
104 interface => ( $interface ne 'intranet' ? '/opac-tmpl' : '/intranet-tmpl' ),
105 theme => $theme,
106 opacstylesheet => $opacstylesheet,
107 opaccolorstylesheet => C4::Context->preference('opaccolorstylesheet'),
108 opacsmallimage => C4::Context->preference('opacsmallimage'),
109 lang => $lang
112 # Bidirectionality
113 my $current_lang = regex_lang_subtags($lang);
114 my $bidi;
115 $bidi = get_bidi($current_lang->{script}) if $current_lang->{script};
116 # Languages
117 my $languages_loop = getTranslatedLanguages($interface,$theme,$lang);
118 $template->param(
119 languages_loop => $languages_loop,
120 bidi => $bidi
121 ) unless @$languages_loop<2;
123 return $template;
126 #---------------------------------------------------------------------------------------------------------
127 # FIXME - POD
128 sub themelanguage {
129 my ( $htdocs, $tmpl, $interface, $query ) = @_;
130 ($query) or warn "no query in themelanguage";
132 # Set some defaults for language and theme
133 # First, check the user's preferences
134 my $lang;
135 my $http_env = $ENV{HTTP_ACCEPT_LANGUAGE};
136 $http_env =~ m/(\w+-*\w*),/;
137 my $language_preference = $1;
138 my $http_accept_language = regex_lang_subtags($language_preference)->{language};
139 if ($http_accept_language) {
140 $lang = accept_language($http_accept_language,getTranslatedLanguages($interface,'prog'));
142 # But, if there's a cookie set, obey it
143 $lang = $query->cookie('KohaOpacLanguage') if $query->cookie('KohaOpacLanguage');
144 # Fall back to English
145 my @languages;
146 if ($interface eq 'intranet') {
147 @languages = split ",", C4::Context->preference("language");
148 } else {
149 @languages = split ",", C4::Context->preference("opaclanguages");
151 if ($lang){
152 @languages=($lang,@languages);
153 } else {
154 $lang = $languages[0];
156 my $theme = 'prog'; # in the event of theme failure default to 'prog' -fbcit
157 my $dbh = C4::Context->dbh;
158 my @themes;
159 if ( $interface eq "intranet" ) {
160 @themes = split " ", C4::Context->preference("template");
162 else {
163 # we are in the opac here, what im trying to do is let the individual user
164 # set the theme they want to use.
165 # and perhaps the them as well.
166 #my $lang = $query->cookie('KohaOpacLanguage');
167 @themes = split " ", C4::Context->preference("opacthemes");
170 # searches through the themes and languages. First template it find it returns.
171 # Priority is for getting the theme right.
172 THEME:
173 foreach my $th (@themes) {
174 foreach my $la (@languages) {
175 #for ( my $pass = 1 ; $pass <= 2 ; $pass += 1 ) {
176 # warn "$htdocs/$th/$la/modules/$interface-"."tmpl";
177 #$la =~ s/([-_])/ $1 eq '-'? '_': '-' /eg if $pass == 2;
178 if ( -e "$htdocs/$th/$la/modules/$tmpl") {
179 #".($interface eq 'intranet'?"modules":"")."/$tmpl" ) {
180 $theme = $th;
181 $lang = $la;
182 last THEME;
184 last unless $la =~ /[-_]/;
188 return ( $theme, $lang );
191 sub setlanguagecookie {
192 my ( $query, $language, $uri ) = @_;
193 my $cookie = $query->cookie(
194 -name => 'KohaOpacLanguage',
195 -value => $language,
196 -expires => ''
198 print $query->redirect(
199 -uri => $uri,
200 -cookie => $cookie
204 =item pagination_bar
206 pagination_bar($base_url, $nb_pages, $current_page, $startfrom_name)
208 Build an HTML pagination bar based on the number of page to display, the
209 current page and the url to give to each page link.
211 C<$base_url> is the URL for each page link. The
212 C<$startfrom_name>=page_number is added at the end of the each URL.
214 C<$nb_pages> is the total number of pages available.
216 C<$current_page> is the current page number. This page number won't become a
217 link.
219 This function returns HTML, without any language dependency.
221 =cut
223 sub pagination_bar {
224 my $base_url = (@_ ? shift : $ENV{SCRIPT_NAME} . $ENV{QUERY_STRING}) or return undef;
225 my $nb_pages = (@_) ? shift : 1;
226 my $current_page = (@_) ? shift : undef; # delay default until later
227 my $startfrom_name = (@_) ? shift : 'page';
229 # how many pages to show before and after the current page?
230 my $pages_around = 2;
232 my $delim = qr/\&(?:amp;)?|;/; # "non memory" cluster: no backreference
233 $base_url =~ s/$delim*\b$startfrom_name=(\d+)//g; # remove previous pagination var
234 unless (defined $current_page and $current_page > 0 and $current_page <= $nb_pages) {
235 $current_page = ($1) ? $1 : 1; # pull current page from param in URL, else default to 1
236 # $debug and # FIXME: use C4::Debug;
237 # warn "with QUERY_STRING:" .$ENV{QUERY_STRING}. "\ncurrent_page:$current_page\n1:$1 2:$2 3:$3";
239 $base_url =~ s/($delim)+/$1/g; # compress duplicate delims
240 $base_url =~ s/$delim;//g; # remove empties
241 $base_url =~ s/$delim$//; # remove trailing delim
243 my $url = $base_url . (($base_url =~ m/$delim/ or $base_url =~ m/\?/) ? '&amp;' : '?' ) . $startfrom_name . '=';
244 my $pagination_bar = '';
246 # navigation bar useful only if more than one page to display !
247 if ( $nb_pages > 1 ) {
249 # link to first page?
250 if ( $current_page > 1 ) {
251 $pagination_bar .=
252 "\n" . '&nbsp;'
253 . '<a href="'
254 . $url
255 . '1" rel="start">'
256 . '&lt;&lt;' . '</a>';
258 else {
259 $pagination_bar .=
260 "\n" . '&nbsp;<span class="inactive">&lt;&lt;</span>';
263 # link on previous page ?
264 if ( $current_page > 1 ) {
265 my $previous = $current_page - 1;
267 $pagination_bar .=
268 "\n" . '&nbsp;'
269 . '<a href="'
270 . $url
271 . $previous
272 . '" rel="prev">' . '&lt;' . '</a>';
274 else {
275 $pagination_bar .=
276 "\n" . '&nbsp;<span class="inactive">&lt;</span>';
279 my $min_to_display = $current_page - $pages_around;
280 my $max_to_display = $current_page + $pages_around;
281 my $last_displayed_page = undef;
283 for my $page_number ( 1 .. $nb_pages ) {
284 if (
285 $page_number == 1
286 or $page_number == $nb_pages
287 or ( $page_number >= $min_to_display
288 and $page_number <= $max_to_display )
291 if ( defined $last_displayed_page
292 and $last_displayed_page != $page_number - 1 )
294 $pagination_bar .=
295 "\n" . '&nbsp;<span class="inactive">...</span>';
298 if ( $page_number == $current_page ) {
299 $pagination_bar .=
300 "\n" . '&nbsp;'
301 . '<span class="currentPage">'
302 . $page_number
303 . '</span>';
305 else {
306 $pagination_bar .=
307 "\n" . '&nbsp;'
308 . '<a href="'
309 . $url
310 . $page_number . '">'
311 . $page_number . '</a>';
313 $last_displayed_page = $page_number;
317 # link on next page?
318 if ( $current_page < $nb_pages ) {
319 my $next = $current_page + 1;
321 $pagination_bar .= "\n"
322 . '&nbsp;<a href="'
323 . $url
324 . $next
325 . '" rel="next">' . '&gt;' . '</a>';
327 else {
328 $pagination_bar .=
329 "\n" . '&nbsp;<span class="inactive">&gt;</span>';
332 # link to last page?
333 if ( $current_page != $nb_pages ) {
334 $pagination_bar .= "\n"
335 . '&nbsp;<a href="'
336 . $url
337 . $nb_pages
338 . '" rel="last">'
339 . '&gt;&gt;' . '</a>';
341 else {
342 $pagination_bar .=
343 "\n" . '&nbsp;<span class="inactive">&gt;&gt;</span>';
347 return $pagination_bar;
350 =item output_html_with_http_headers
352 &output_html_with_http_headers($query, $cookie, $html[, $content_type])
354 Outputs the HTML page $html with the appropriate HTTP headers,
355 with the authentication cookie $cookie and a Content-Type that
356 corresponds to the HTML page $html.
358 If the optional C<$content_type> parameter is called, set the
359 response's Content-Type to that value instead of "text/html".
361 =cut
363 sub output_html_with_http_headers ($$$;$) {
364 my $query = shift;
365 my $cookie = shift;
366 my $html = shift;
367 my $content_type = @_ ? shift : "text/html";
368 $content_type = "text/html" unless $content_type =~ m!/!; # very basic sanity check
369 print $query->header(
370 -type => $content_type,
371 -charset => 'UTF-8',
372 -cookie => $cookie,
373 -Pragma => 'no-cache',
374 -'Cache-Control' => 'no-cache',
375 ), $html;
378 sub output_ajax_with_http_headers ($$) {
379 my ($query, $js) = @_;
380 print $query->header(
381 -type => 'text/javascript',
382 -charset => 'UTF-8',
383 -Pragma => 'no-cache',
384 -'Cache-Control' => 'no-cache',
385 -expires =>'-1d',
386 ), $js;
389 sub is_ajax () {
390 my $x_req = $ENV{HTTP_X_REQUESTED_WITH};
391 return ($x_req and $x_req =~ /XMLHttpRequest/i) ? 1 : 0;
394 END { } # module clean-up code here (global destructor)
397 __END__
399 =back
401 =head1 AUTHOR
403 Koha Developement team <info@koha.org>
405 =cut