c8404dc05bcc88764efd0e30faf2acd8a40c8c9a
[koha.git] / C4 / Output.pm
1 package C4::Output;
2
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
6
7 # Copyright 2000-2002 Katipo Communications
8 #
9 # This file is part of Koha.
10 #
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.
15 #
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.
19 #
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
23
24
25 # NOTE: I'm pretty sure this module is deprecated in favor of
26 # templates.
27
28 use strict;
29
30 use C4::Context;
31 use C4::Languages qw(getTranslatedLanguages get_bidi regex_lang_subtags language_get_description accept_language );
32
33 use HTML::Template::Pro;
34 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
35
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)]
46                                 );
47     push @EXPORT, qw(
48         &themelanguage &gettemplate setlanguagecookie pagination_bar
49     );
50     push @EXPORT, qw(
51         &output_html_with_http_headers
52     );
53 }
54
55 =head1 NAME
56
57 C4::Output - Functions for managing templates
58
59 =head1 FUNCTIONS
60
61 =over 2
62
63 =cut
64
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/";
68
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');
77     }
78     else {
79         $htdocs = C4::Context->config('intrahtdocs');
80     }
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');
84
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";
90     }
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"]
98     );
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
110     );
111
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     my $num_languages_enabled = 0;
119     foreach my $lang (@$languages_loop) {
120         foreach my $sublang (@{ $lang->{'sublanguages_loop'} }) {
121             $num_languages_enabled++ if $sublang->{enabled};
122          }
123     }
124     $template->param(
125             languages_loop       => $languages_loop,
126             bidi                 => $bidi,
127             one_language_enabled => ($num_languages_enabled <= 1) ? 1 : 0, # deal with zero enabled langs as well
128     ) unless @$languages_loop<2;
129
130     return $template;
131 }
132
133 #---------------------------------------------------------------------------------------------------------
134 # FIXME - POD
135 sub themelanguage {
136     my ( $htdocs, $tmpl, $interface, $query ) = @_;
137     ($query) or warn "no query in themelanguage";
138
139     # Set some defaults for language and theme
140     # First, check the user's preferences
141     my $lang;
142         my $http_env = $ENV{HTTP_ACCEPT_LANGUAGE};
143         $http_env =~ m/(\w+-*\w*),/;
144         my $language_preference = $1;
145     my $http_accept_language = regex_lang_subtags($language_preference)->{language};
146     if ($http_accept_language) {
147         $lang = accept_language($http_accept_language,getTranslatedLanguages($interface,'prog'));
148     } 
149     # But, if there's a cookie set, obey it
150     $lang = $query->cookie('KohaOpacLanguage') if $query->cookie('KohaOpacLanguage');
151     # Fall back to English
152     my @languages;
153     if ($interface eq 'intranet') {
154         @languages = split ",", C4::Context->preference("language");
155     } else {
156         @languages = split ",", C4::Context->preference("opaclanguages");
157     }
158     if ($lang){  
159         @languages=($lang,@languages);
160     } else {
161         $lang = $languages[0];
162     }      
163     my $theme = 'prog'; # in the event of theme failure default to 'prog' -fbcit
164     my $dbh = C4::Context->dbh;
165     my @themes;
166     if ( $interface eq "intranet" ) {
167         @themes    = split " ", C4::Context->preference("template");
168     }
169     else {
170       # we are in the opac here, what im trying to do is let the individual user
171       # set the theme they want to use.
172       # and perhaps the them as well.
173         #my $lang = $query->cookie('KohaOpacLanguage');
174         @themes = split " ", C4::Context->preference("opacthemes");
175     }
176
177  # searches through the themes and languages. First template it find it returns.
178  # Priority is for getting the theme right.
179     THEME:
180     foreach my $th (@themes) {
181         foreach my $la (@languages) {
182             #for ( my $pass = 1 ; $pass <= 2 ; $pass += 1 ) {
183                 # warn "$htdocs/$th/$la/modules/$interface-"."tmpl";
184                 #$la =~ s/([-_])/ $1 eq '-'? '_': '-' /eg if $pass == 2;
185                                 if ( -e "$htdocs/$th/$la/modules/$tmpl") {
186                 #".($interface eq 'intranet'?"modules":"")."/$tmpl" ) {
187                     $theme = $th;
188                     $lang  = $la;
189                     last THEME;
190                 }
191                 last unless $la =~ /[-_]/;
192             #}
193         }
194     }
195     return ( $theme, $lang );
196 }
197
198 sub setlanguagecookie {
199     my ( $query, $language, $uri ) = @_;
200     my $cookie = $query->cookie(
201         -name    => 'KohaOpacLanguage',
202         -value   => $language,
203         -expires => ''
204     );
205     print $query->redirect(
206         -uri    => $uri,
207         -cookie => $cookie
208     );
209 }
210
211 =item pagination_bar
212
213    pagination_bar($base_url, $nb_pages, $current_page, $startfrom_name)
214
215 Build an HTML pagination bar based on the number of page to display, the
216 current page and the url to give to each page link.
217
218 C<$base_url> is the URL for each page link. The
219 C<$startfrom_name>=page_number is added at the end of the each URL.
220
221 C<$nb_pages> is the total number of pages available.
222
223 C<$current_page> is the current page number. This page number won't become a
224 link.
225
226 This function returns HTML, without any language dependency.
227
228 =cut
229
230 sub pagination_bar {
231         my $base_url = (@_ ? shift : $ENV{SCRIPT_NAME} . $ENV{QUERY_STRING}) or return undef;
232     my $nb_pages       = (@_) ? shift : 1;
233     my $current_page   = (@_) ? shift : undef;  # delay default until later
234     my $startfrom_name = (@_) ? shift : 'page';
235
236     # how many pages to show before and after the current page?
237     my $pages_around = 2;
238
239         my $delim = qr/\&(?:amp;)?|;/;          # "non memory" cluster: no backreference
240         $base_url =~ s/$delim*\b$startfrom_name=(\d+)//g; # remove previous pagination var
241     unless (defined $current_page and $current_page > 0 and $current_page <= $nb_pages) {
242         $current_page = ($1) ? $1 : 1;  # pull current page from param in URL, else default to 1
243                 # $debug and    # FIXME: use C4::Debug;
244                 # warn "with QUERY_STRING:" .$ENV{QUERY_STRING}. "\ncurrent_page:$current_page\n1:$1  2:$2  3:$3";
245     }
246         $base_url =~ s/($delim)+/$1/g;  # compress duplicate delims
247         $base_url =~ s/$delim;//g;              # remove empties
248         $base_url =~ s/$delim$//;               # remove trailing delim
249
250     my $url = $base_url . (($base_url =~ m/$delim/ or $base_url =~ m/\?/) ? '&amp;' : '?' ) . $startfrom_name . '=';
251     my $pagination_bar = '';
252
253     # navigation bar useful only if more than one page to display !
254     if ( $nb_pages > 1 ) {
255
256         # link to first page?
257         if ( $current_page > 1 ) {
258             $pagination_bar .=
259                 "\n" . '&nbsp;'
260               . '<a href="'
261               . $url
262               . '1" rel="start">'
263               . '&lt;&lt;' . '</a>';
264         }
265         else {
266             $pagination_bar .=
267               "\n" . '&nbsp;<span class="inactive">&lt;&lt;</span>';
268         }
269
270         # link on previous page ?
271         if ( $current_page > 1 ) {
272             my $previous = $current_page - 1;
273
274             $pagination_bar .=
275                 "\n" . '&nbsp;'
276               . '<a href="'
277               . $url
278               . $previous
279               . '" rel="prev">' . '&lt;' . '</a>';
280         }
281         else {
282             $pagination_bar .=
283               "\n" . '&nbsp;<span class="inactive">&lt;</span>';
284         }
285
286         my $min_to_display      = $current_page - $pages_around;
287         my $max_to_display      = $current_page + $pages_around;
288         my $last_displayed_page = undef;
289
290         for my $page_number ( 1 .. $nb_pages ) {
291             if (
292                    $page_number == 1
293                 or $page_number == $nb_pages
294                 or (    $page_number >= $min_to_display
295                     and $page_number <= $max_to_display )
296               )
297             {
298                 if ( defined $last_displayed_page
299                     and $last_displayed_page != $page_number - 1 )
300                 {
301                     $pagination_bar .=
302                       "\n" . '&nbsp;<span class="inactive">...</span>';
303                 }
304
305                 if ( $page_number == $current_page ) {
306                     $pagination_bar .=
307                         "\n" . '&nbsp;'
308                       . '<span class="currentPage">'
309                       . $page_number
310                       . '</span>';
311                 }
312                 else {
313                     $pagination_bar .=
314                         "\n" . '&nbsp;'
315                       . '<a href="'
316                       . $url
317                       . $page_number . '">'
318                       . $page_number . '</a>';
319                 }
320                 $last_displayed_page = $page_number;
321             }
322         }
323
324         # link on next page?
325         if ( $current_page < $nb_pages ) {
326             my $next = $current_page + 1;
327
328             $pagination_bar .= "\n"
329               . '&nbsp;<a href="'
330               . $url
331               . $next
332               . '" rel="next">' . '&gt;' . '</a>';
333         }
334         else {
335             $pagination_bar .=
336               "\n" . '&nbsp;<span class="inactive">&gt;</span>';
337         }
338
339         # link to last page?
340         if ( $current_page != $nb_pages ) {
341             $pagination_bar .= "\n"
342               . '&nbsp;<a href="'
343               . $url
344               . $nb_pages
345               . '" rel="last">'
346               . '&gt;&gt;' . '</a>';
347         }
348         else {
349             $pagination_bar .=
350               "\n" . '&nbsp;<span class="inactive">&gt;&gt;</span>';
351         }
352     }
353
354     return $pagination_bar;
355 }
356
357 =item output_html_with_http_headers
358
359    &output_html_with_http_headers($query, $cookie, $html[, $content_type])
360
361 Outputs the HTML page $html with the appropriate HTTP headers,
362 with the authentication cookie $cookie and a Content-Type that
363 corresponds to the HTML page $html.
364
365 If the optional C<$content_type> parameter is called, set the
366 response's Content-Type to that value instead of "text/html".
367
368 =cut
369
370 sub output_html_with_http_headers ($$$;$) {
371     my $query = shift;
372     my $cookie = shift;
373     my $html = shift;
374     my $content_type = @_ ? shift : "text/html";
375     $content_type = "text/html" unless $content_type =~ m!/!; # very basic sanity check
376     print $query->header(
377         -type    => $content_type,
378         -charset => 'UTF-8',
379         -cookie  => $cookie,
380         -Pragma => 'no-cache',
381         -'Cache-Control' => 'no-cache',
382     ), $html;
383 }
384
385 sub output_ajax_with_http_headers ($$) {
386     my ($query, $js) = @_;
387     print $query->header(
388         -type    => 'text/javascript',
389         -charset => 'UTF-8',
390         -Pragma  => 'no-cache',
391         -'Cache-Control' => 'no-cache',
392                 -expires =>'-1d',
393     ), $js;
394 }
395
396 sub is_ajax () {
397         my $x_req = $ENV{HTTP_X_REQUESTED_WITH};
398         return ($x_req and $x_req =~ /XMLHttpRequest/i) ? 1 : 0;
399 }
400
401 END { }    # module clean-up code here (global destructor)
402
403 1;
404 __END__
405
406 =back
407
408 =head1 AUTHOR
409
410 Koha Developement team <info@koha.org>
411
412 =cut