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