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