Output.pm - BEGIN block VERSION and vars related to export.
[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);
35
36 BEGIN {
37         # set the version for version checking
38         $VERSION = 3.01;
39         require Exporter;
40         @ISA    = qw(Exporter);
41         push @EXPORT, qw(
42                 &themelanguage &gettemplate setlanguagecookie pagination_bar
43         );
44         push @EXPORT, qw(
45                 &output_html_with_http_headers
46         );
47 }
48
49 =head1 NAME
50
51 C4::Output - Functions for managing templates
52
53 =head1 FUNCTIONS
54
55 =over 2
56
57 =cut
58
59 #FIXME: this is a quick fix to stop rc1 installing broken
60 #Still trying to figure out the correct fix.
61 my $path = C4::Context->config('intrahtdocs') . "/prog/en/includes/";
62
63 #---------------------------------------------------------------------------------------------------------
64 # FIXME - POD
65 sub gettemplate {
66     my ( $tmplbase, $interface, $query ) = @_;
67     if ( !$query ) {
68         warn "no query in gettemplate";
69     }
70     my $htdocs;
71     if ( $interface ne "intranet" ) {
72         $htdocs = C4::Context->config('opachtdocs');
73     }
74     else {
75         $htdocs = C4::Context->config('intrahtdocs');
76     }
77     my $path = C4::Context->preference('intranet_includes') || 'includes';
78
79     my ( $theme, $lang ) = themelanguage( $htdocs, $tmplbase, $interface, $query );
80     my $opacstylesheet = C4::Context->preference('opacstylesheet');
81
82         # if the template doesn't exist, load the English one as a last resort
83         my $filename = "$htdocs/$theme/$lang/modules/$tmplbase";
84         unless (-f $filename) {
85                 $lang = 'en';
86                 $filename = "$htdocs/$theme/$lang/".($interface eq 'intranet'?"modules":"")."/$tmplbase";
87         }
88     my $template       = HTML::Template::Pro->new(
89                 filename          => $filename,
90         die_on_bad_params => 1,
91         global_vars       => 1,
92         case_sensitive    => 1,
93         path              => ["$htdocs/$theme/$lang/$path"]
94     );
95
96     $template->param(
97         themelang => ( $interface ne 'intranet' ? '/opac-tmpl' : '/intranet-tmpl' )
98           . "/$theme/$lang",
99         interface => ( $interface ne 'intranet' ? '/opac-tmpl' : '/intranet-tmpl' ),
100         theme => $theme,
101         opacstylesheet      => $opacstylesheet,
102         opaccolorstylesheet => C4::Context->preference('opaccolorstylesheet'),
103         opacsmallimage      => C4::Context->preference('opacsmallimage'),
104         lang                => $lang
105     );
106
107         # Bidirectionality
108         my $current_lang = regex_lang_subtags($lang);
109         my $bidi;
110         $bidi = get_bidi($current_lang->{script}) if $current_lang->{script};
111
112         # Languages
113         my @template_languages;
114         my $languages_loop = getTranslatedLanguages($interface,$theme);
115
116         for my $language_hashref (@$languages_loop) {
117                         $language_hashref->{'current_lang'} = $current_lang->{'language'};
118                         $language_hashref->{'native_description'} = language_get_description($language_hashref->{'language_code'},$language_hashref->{'language_code'},'language');
119                         #warn "($language_hashref->{'language_code'},$language_hashref->{'current_lang'},$language_hashref->{'script_code'}";
120                         $language_hashref->{'locale_description'} = language_get_description($language_hashref->{'language_code'},$language_hashref->{'current_lang'},'language');
121                         $language_hashref->{'language_description'} = language_get_description($language_hashref->{'language_code'},$language_hashref->{'current_lang'},'language');
122                         $language_hashref->{'script_description'} = language_get_description($language_hashref->{'script_code'},$language_hashref->{'current_lang'},'script');
123                         $language_hashref->{'region_description'} = language_get_description($language_hashref->{'region_code'},$language_hashref->{'current_lang'},'region');
124                         $language_hashref->{'variant_description'} = language_get_description($language_hashref->{'variant_code'},$language_hashref->{'current_lang'},'variant');
125
126                 if ($language_hashref->{'language_lang'} eq $lang) {
127                         $language_hashref->{current}++;
128                 }
129                 push @template_languages, $language_hashref;
130         }
131         # load the languages ( for switching from one template to another )
132         $template->param(       languages_loop => \@template_languages,
133                                                 bidi => $bidi
134         );
135
136     return $template;
137 }
138
139 #---------------------------------------------------------------------------------------------------------
140 # FIXME - POD
141 sub themelanguage {
142     my ( $htdocs, $tmpl, $interface, $query ) = @_;
143
144         # Set some defaults for language and theme
145         # First, check the user's preferences
146         my $lang;
147         my $http_accept_language = regex_lang_subtags($ENV{HTTP_ACCEPT_LANGUAGE})->{language};
148         $lang = accept_language($http_accept_language,getTranslatedLanguages($interface,'prog'));
149
150         # But, if there's a cookie set, obey it
151         $lang = $query->cookie('KohaOpacLanguage') if $query->cookie('KohaOpacLanguage');
152
153         # Fall back to English
154         $lang = 'en' unless $lang;
155         my $theme = 'prog';
156
157     my $dbh = C4::Context->dbh;
158     my @languages;
159     my @themes;
160     if ( $interface eq "intranet" ) {
161         @languages = split " ", C4::Context->preference("opaclanguages");
162         @themes    = split " ", C4::Context->preference("template");
163         pop @languages, $lang if $lang;
164     }
165     else {
166
167       # we are in the opac here, what im trying to do is let the individual user
168       # set the theme they want to use.
169       # and perhaps the them as well.
170         #my $lang = $query->cookie('KohaOpacLanguage');
171         if ($lang) {
172
173             push @languages, $lang;
174             @themes = split " ", C4::Context->preference("opacthemes");
175         }
176         else {
177             @languages = split " ", C4::Context->preference("opaclanguages");
178             @themes    = split " ", C4::Context->preference("opacthemes");
179         }
180     }
181
182  # searches through the themes and languages. First template it find it returns.
183  # Priority is for getting the theme right.
184   THEME:
185     foreach my $th (@themes) {
186         foreach my $la (@languages) {
187             for ( my $pass = 1 ; $pass <= 2 ; $pass += 1 ) {
188                 $la =~ s/([-_])/ $1 eq '-'? '_': '-' /eg if $pass == 2;
189                 if ( -e "$htdocs/$th/$la/".($interface eq 'intranet'?"modules":"")."/$tmpl" ) {
190                     $theme = $th;
191                     $lang  = $la;
192                     last THEME;
193                 }
194                 last unless $la =~ /[-_]/;
195             }
196         }
197     }
198     return ( $theme, $lang );
199 }
200
201 sub setlanguagecookie {
202     my ( $query, $language, $uri ) = @_;
203     my $cookie = $query->cookie(
204         -name    => 'KohaOpacLanguage',
205         -value   => $language,
206         -expires => ''
207     );
208     print $query->redirect(
209         -uri    => $uri,
210         -cookie => $cookie
211     );
212 }
213
214 =item pagination_bar
215
216    pagination_bar($base_url, $nb_pages, $current_page, $startfrom_name)
217
218 Build an HTML pagination bar based on the number of page to display, the
219 current page and the url to give to each page link.
220
221 C<$base_url> is the URL for each page link. The
222 C<$startfrom_name>=page_number is added at the end of the each URL.
223
224 C<$nb_pages> is the total number of pages available.
225
226 C<$current_page> is the current page number. This page number won't become a
227 link.
228
229 This function returns HTML, without any language dependency.
230
231 =cut
232
233 sub pagination_bar {
234     my ( $base_url, $nb_pages, $current_page, $startfrom_name ) = @_;
235
236     # how many pages to show before and after the current page?
237     my $pages_around = 2;
238
239     my $url =
240       $base_url . ( $base_url =~ m/&/ ? '&amp;' : '?' ) . $startfrom_name . '=';
241
242     my $pagination_bar = '';
243
244     # current page detection
245     if ( not defined $current_page ) {
246         $current_page = 1;
247     }
248
249     # navigation bar useful only if more than one page to display !
250     if ( $nb_pages > 1 ) {
251
252         # link to first page?
253         if ( $current_page > 1 ) {
254             $pagination_bar .=
255                 "\n" . '&nbsp;'
256               . '<a href="'
257               . $url
258               . '1" rel="start">'
259               . '&lt;&lt;' . '</a>';
260         }
261         else {
262             $pagination_bar .=
263               "\n" . '&nbsp;<span class="inactive">&lt;&lt;</span>';
264         }
265
266         # link on previous page ?
267         if ( $current_page > 1 ) {
268             my $previous = $current_page - 1;
269
270             $pagination_bar .=
271                 "\n" . '&nbsp;'
272               . '<a href="'
273               . $url
274               . $previous
275               . '" rel="prev">' . '&lt;' . '</a>';
276         }
277         else {
278             $pagination_bar .=
279               "\n" . '&nbsp;<span class="inactive">&lt;</span>';
280         }
281
282         my $min_to_display      = $current_page - $pages_around;
283         my $max_to_display      = $current_page + $pages_around;
284         my $last_displayed_page = undef;
285
286         for my $page_number ( 1 .. $nb_pages ) {
287             if (
288                    $page_number == 1
289                 or $page_number == $nb_pages
290                 or (    $page_number >= $min_to_display
291                     and $page_number <= $max_to_display )
292               )
293             {
294                 if ( defined $last_displayed_page
295                     and $last_displayed_page != $page_number - 1 )
296                 {
297                     $pagination_bar .=
298                       "\n" . '&nbsp;<span class="inactive">...</span>';
299                 }
300
301                 if ( $page_number == $current_page ) {
302                     $pagination_bar .=
303                         "\n" . '&nbsp;'
304                       . '<span class="currentPage">'
305                       . $page_number
306                       . '</span>';
307                 }
308                 else {
309                     $pagination_bar .=
310                         "\n" . '&nbsp;'
311                       . '<a href="'
312                       . $url
313                       . $page_number . '">'
314                       . $page_number . '</a>';
315                 }
316                 $last_displayed_page = $page_number;
317             }
318         }
319
320         # link on next page?
321         if ( $current_page < $nb_pages ) {
322             my $next = $current_page + 1;
323
324             $pagination_bar .= "\n"
325               . '&nbsp;<a href="'
326               . $url
327               . $next
328               . '" rel="next">' . '&gt;' . '</a>';
329         }
330         else {
331             $pagination_bar .=
332               "\n" . '&nbsp;<span class="inactive">&gt;</span>';
333         }
334
335         # link to last page?
336         if ( $current_page != $nb_pages ) {
337             $pagination_bar .= "\n"
338               . '&nbsp;<a href="'
339               . $url
340               . $nb_pages
341               . '" rel="last">'
342               . '&gt;&gt;' . '</a>';
343         }
344         else {
345             $pagination_bar .=
346               "\n" . '&nbsp;<span class="inactive">&gt;&gt;</span>';
347         }
348     }
349
350     return $pagination_bar;
351 }
352
353 =item output_html_with_http_headers
354
355    &output_html_with_http_headers($query, $cookie, $html)
356
357 Outputs the HTML page $html with the appropriate HTTP headers,
358 with the authentication cookie $cookie and a Content-Type that
359 corresponds to the HTML page $html.
360
361 =cut
362
363 sub output_html_with_http_headers ($$$) {
364     my($query, $cookie, $html) = @_;
365     print $query->header(
366         -type    => 'text/html',
367         -charset => 'UTF-8',
368         -cookie  => $cookie,
369                 -Pragma => 'no-cache',
370                 -'Cache-Control' => 'no-cache',
371     ), $html;
372 }
373
374 END { }    # module clean-up code here (global destructor)
375
376 1;
377 __END__
378
379 =back
380
381 =head1 AUTHOR
382
383 Koha Developement team <info@koha.org>
384
385 =cut