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