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