fix the 'current language' change language display
[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
85         # if the template doesn't exist, load the English one as a last resort
86         my $filename = "$htdocs/$theme/$lang/".($interface eq 'intranet'?"modules":"")."/$tmplbase";
87         unless (-f $filename) {
88                 $lang = 'en';
89                 $filename = "$htdocs/$theme/$lang/".($interface eq 'intranet'?"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         path              => ["$htdocs/$theme/$lang/$path"]
97     );
98
99     $template->param(
100         themelang => ( $interface ne 'intranet' ? '/opac-tmpl' : '/intranet-tmpl' )
101           . "/$theme/$lang",
102         interface => ( $interface ne 'intranet' ? '/opac-tmpl' : '/intranet-tmpl' ),
103         theme => $theme,
104         opacstylesheet      => $opacstylesheet,
105         opaccolorstylesheet => C4::Context->preference('opaccolorstylesheet'),
106         opacsmallimage      => C4::Context->preference('opacsmallimage'),
107         lang                => $lang
108     );
109
110         # Language, Script, and Locale
111         my $language_subtags_hashref = regex_lang_subtags($lang);
112         my $bidi;
113         $bidi = get_bidi($language_subtags_hashref->{script}) if $language_subtags_hashref->{script};
114         my @template_languages;
115         my $languages_loop = getTranslatedLanguages($interface,$theme);
116         for my $language_hashref (@$languages_loop) {
117                         $language_hashref->{'language_script_description'} = language_get_description($language_hashref->{'language_script'},$lang);
118                         $language_hashref->{'language_region_description'} = language_get_description($language_hashref->{'language_region'},$lang);
119                         $language_hashref->{'language_variant_description'} = language_get_description($language_hashref->{'language_variant'},$lang);
120
121                 if ($language_hashref->{'language_code'} eq $lang) {
122                         warn "LANG: $lang";
123                         warn "Code:".$language_hashref->{'language_code'};
124                 #language_hashref->{language_code} eq $language_subtags_hashref->{language}) {
125                         $language_hashref->{current}++;
126                 }
127                 push @template_languages, $language_hashref;
128         }
129         # load the languages ( for switching from one template to another )
130         $template->param(       languages_loop => \@template_languages,
131                                                 bidi => $bidi
132         );
133
134     return $template;
135 }
136
137 #---------------------------------------------------------------------------------------------------------
138 # FIXME - POD
139 sub themelanguage {
140     my ( $htdocs, $tmpl, $section, $query ) = @_;
141
142     #   if (!$query) {
143     #     warn "no query";
144     #   }
145
146         # set some defaults for language and theme
147         my $lang = $query->cookie('KohaOpacLanguage');
148         $lang = 'en' unless $lang;
149         my $theme = 'prog';
150
151     my $dbh = C4::Context->dbh;
152     my @languages;
153     my @themes;
154     if ( $section eq "intranet" ) {
155         @languages = split " ", C4::Context->preference("opaclanguages");
156         @themes    = split " ", C4::Context->preference("template");
157         pop @languages, $lang if $lang;
158     }
159     else {
160
161       # we are in the opac here, what im trying to do is let the individual user
162       # set the theme they want to use.
163       # and perhaps the them as well.
164         #my $lang = $query->cookie('KohaOpacLanguage');
165         if ($lang) {
166
167             push @languages, $lang;
168             @themes = split " ", C4::Context->preference("opacthemes");
169         }
170         else {
171             @languages = split " ", C4::Context->preference("opaclanguages");
172             @themes    = split " ", C4::Context->preference("opacthemes");
173         }
174     }
175
176  # searches through the themes and languages. First template it find it returns.
177  # Priority is for getting the theme right.
178   THEME:
179     foreach my $th (@themes) {
180         foreach my $la (@languages) {
181             for ( my $pass = 1 ; $pass <= 2 ; $pass += 1 ) {
182                 $la =~ s/([-_])/ $1 eq '-'? '_': '-' /eg if $pass == 2;
183                 if ( -e "$htdocs/$th/$la/modules/$tmpl" ) {
184                     $theme = $th;
185                     $lang  = $la;
186                     last THEME;
187                 }
188                 last unless $la =~ /[-_]/;
189             }
190         }
191     }
192     return ( $theme, $lang );
193 }
194
195 sub setlanguagecookie {
196     my ( $query, $language, $uri ) = @_;
197     my $cookie = $query->cookie(
198         -name    => 'KohaOpacLanguage',
199         -value   => $language,
200         -expires => ''
201     );
202     print $query->redirect(
203         -uri    => $uri,
204         -cookie => $cookie
205     );
206 }
207
208 =item pagination_bar
209
210    pagination_bar($base_url, $nb_pages, $current_page, $startfrom_name)
211
212 Build an HTML pagination bar based on the number of page to display, the
213 current page and the url to give to each page link.
214
215 C<$base_url> is the URL for each page link. The
216 C<$startfrom_name>=page_number is added at the end of the each URL.
217
218 C<$nb_pages> is the total number of pages available.
219
220 C<$current_page> is the current page number. This page number won't become a
221 link.
222
223 This function returns HTML, without any language dependency.
224
225 =cut
226
227 sub pagination_bar {
228     my ( $base_url, $nb_pages, $current_page, $startfrom_name ) = @_;
229
230     # how many pages to show before and after the current page?
231     my $pages_around = 2;
232
233     my $url =
234       $base_url . ( $base_url =~ m/&/ ? '&amp;' : '?' ) . $startfrom_name . '=';
235
236     my $pagination_bar = '';
237
238     # current page detection
239     if ( not defined $current_page ) {
240         $current_page = 1;
241     }
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)
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 =cut
356
357 sub output_html_with_http_headers ($$$) {
358     my($query, $cookie, $html) = @_;
359     print $query->header(
360         -type    => 'text/html',
361         -charset => 'UTF-8',
362         -cookie  => $cookie,
363                 -Pragma => 'no-cache',
364                 -'Cache-Control' => 'no-cache',
365     ), $html;
366 }
367
368 END { }    # module clean-up code here (global destructor)
369
370 1;
371 __END__
372
373 =back
374
375 =head1 AUTHOR
376
377 Koha Developement team <info@koha.org>
378
379 =cut