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