Problem with error message
[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 @EXPORT_OK %EXPORT_TAGS);
35
36 BEGIN {
37     # set the version for version checking
38     $VERSION = 3.03;
39     require Exporter;
40     @ISA    = qw(Exporter);
41         @EXPORT_OK = qw(&output_ajax_with_http_headers &is_ajax); # More stuff should go here instead
42         %EXPORT_TAGS = ( all =>[qw(&themelanguage &gettemplate setlanguagecookie pagination_bar
43                                                                 &output_ajax_with_http_headers &output_html_with_http_headers)],
44                                         ajax =>[qw(&output_ajax_with_http_headers is_ajax)],
45                                         html =>[qw(&output_html_with_http_headers)]
46                                 );
47     push @EXPORT, qw(
48         &themelanguage &gettemplate setlanguagecookie pagination_bar
49     );
50     push @EXPORT, qw(
51         &output_html_with_http_headers
52     );
53 }
54
55 =head1 NAME
56
57 C4::Output - Functions for managing templates
58
59 =head1 FUNCTIONS
60
61 =over 2
62
63 =cut
64
65 #FIXME: this is a quick fix to stop rc1 installing broken
66 #Still trying to figure out the correct fix.
67 my $path = C4::Context->config('intrahtdocs') . "/prog/en/includes/";
68
69 #---------------------------------------------------------------------------------------------------------
70 # FIXME - POD
71 sub gettemplate {
72     my ( $tmplbase, $interface, $query ) = @_;
73     ($query) or warn "no query in gettemplate";
74     my $htdocs;
75     if ( $interface ne "intranet" ) {
76         $htdocs = C4::Context->config('opachtdocs');
77     }
78     else {
79         $htdocs = C4::Context->config('intrahtdocs');
80     }
81     my $path = C4::Context->preference('intranet_includes') || 'includes';
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/modules/$tmplbase";
87     unless (-f $filename) {
88         $lang = 'en';
89         $filename = "$htdocs/$theme/$lang/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             loop_context_vars => 1,             # enable: __first__, __last__, __inner__, __odd__, __counter__ 
97         path              => ["$htdocs/$theme/$lang/$path"]
98     );
99     my $themelang=( $interface ne 'intranet' ? '/opac-tmpl' : '/intranet-tmpl' )
100           . "/$theme/$lang";
101     $template->param(
102         themelang => $themelang,
103         yuipath   => (C4::Context->preference("yuipath") eq "local"?"$themelang/lib/yui":C4::Context->preference("yuipath")),
104         interface => ( $interface ne 'intranet' ? '/opac-tmpl' : '/intranet-tmpl' ),
105         theme     => $theme,
106         lang      => $lang
107     );
108
109     # Bidirectionality
110     my $current_lang = regex_lang_subtags($lang);
111     my $bidi;
112     $bidi = get_bidi($current_lang->{script}) if $current_lang->{script};
113     # Languages
114     my $languages_loop = getTranslatedLanguages($interface,$theme,$lang);
115     my $num_languages_enabled = 0;
116     foreach my $lang (@$languages_loop) {
117         foreach my $sublang (@{ $lang->{'sublanguages_loop'} }) {
118             $num_languages_enabled++ if $sublang->{enabled};
119          }
120     }
121     $template->param(
122             languages_loop       => $languages_loop,
123             bidi                 => $bidi,
124             one_language_enabled => ($num_languages_enabled <= 1) ? 1 : 0, # deal with zero enabled langs as well
125     ) unless @$languages_loop<2;
126
127     return $template;
128 }
129
130 #---------------------------------------------------------------------------------------------------------
131 # FIXME - POD
132 sub themelanguage {
133     my ( $htdocs, $tmpl, $interface, $query ) = @_;
134     ($query) or warn "no query in themelanguage";
135
136     # Set some defaults for language and theme
137     # First, check the user's preferences
138     my $lang;
139     my $http_accept_language = $ENV{ HTTP_ACCEPT_LANGUAGE };
140     # But, if there's a cookie set, obey it
141     $lang = $query->cookie('KohaOpacLanguage') if $query->cookie('KohaOpacLanguage');
142     
143     # Fall back to English
144     my @languages;
145     if ($interface eq 'intranet') {
146         @languages = split ",", C4::Context->preference("language");
147     } else {
148         @languages = split ",", C4::Context->preference("opaclanguages");
149     }
150
151     # Ricardo Dias Marques
152     # 14-Nov-2009
153     # - If we have a language set in the Cookie, we'll accept it if it exists in the list of Translated Languages
154     # - If we don't have a language set in the Cookie, we'll try to use the one set in the browser (available
155     #      in $http_accept_language) if it also exists in the list of Translated Languages
156     if ($lang ne "")
157     {
158         $lang = accept_language( $lang,
159               getTranslatedLanguages($interface,'prog') );
160     }
161     else
162     {
163         $lang = accept_language( $http_accept_language,
164               getTranslatedLanguages($interface,'prog') )
165       if $http_accept_language;
166     }
167
168     if (grep(/^$lang$/, @languages)){
169         @languages=($lang,@languages);
170     } else {
171         $lang = $languages[0];
172     }
173
174     my $theme = 'prog'; # in the event of theme failure default to 'prog' -fbcit
175     my $dbh = C4::Context->dbh;
176     if ( $interface eq "intranet" ) {
177         $theme = C4::Context->preference("template");
178     }
179     else {
180       # we are in the opac here, what im trying to do is let the individual user
181       # set the theme they want to use.
182       # and perhaps the them as well.
183         #my $lang = $query->cookie('KohaOpacLanguage');
184         $theme = C4::Context->preference("opacthemes");
185     }
186
187  # searches through the themes and languages. First template it find it returns.
188  # Priority is for getting the theme right.
189     THEME:
190     foreach my $la (@languages) {
191                         if ( -e "$htdocs/$theme/$la/modules/$tmpl") {
192                 $lang  = $la;
193                 last THEME;
194             }
195             last unless $la =~ /[-_]/;
196     }
197     return ( $theme, $lang );
198 }
199
200 sub setlanguagecookie {
201     my ( $query, $language, $uri ) = @_;
202     my $cookie = $query->cookie(
203         -name    => 'KohaOpacLanguage',
204         -value   => $language,
205         -expires => ''
206     );
207     print $query->redirect(
208         -uri    => $uri,
209         -cookie => $cookie
210     );
211 }
212
213 =item pagination_bar
214
215    pagination_bar($base_url, $nb_pages, $current_page, $startfrom_name)
216
217 Build an HTML pagination bar based on the number of page to display, the
218 current page and the url to give to each page link.
219
220 C<$base_url> is the URL for each page link. The
221 C<$startfrom_name>=page_number is added at the end of the each URL.
222
223 C<$nb_pages> is the total number of pages available.
224
225 C<$current_page> is the current page number. This page number won't become a
226 link.
227
228 This function returns HTML, without any language dependency.
229
230 =cut
231
232 sub pagination_bar {
233         my $base_url = (@_ ? shift : $ENV{SCRIPT_NAME} . $ENV{QUERY_STRING}) or return undef;
234     my $nb_pages       = (@_) ? shift : 1;
235     my $current_page   = (@_) ? shift : undef;  # delay default until later
236     my $startfrom_name = (@_) ? shift : 'page';
237
238     # how many pages to show before and after the current page?
239     my $pages_around = 2;
240
241         my $delim = qr/\&(?:amp;)?|;/;          # "non memory" cluster: no backreference
242         $base_url =~ s/$delim*\b$startfrom_name=(\d+)//g; # remove previous pagination var
243     unless (defined $current_page and $current_page > 0 and $current_page <= $nb_pages) {
244         $current_page = ($1) ? $1 : 1;  # pull current page from param in URL, else default to 1
245                 # $debug and    # FIXME: use C4::Debug;
246                 # warn "with QUERY_STRING:" .$ENV{QUERY_STRING}. "\ncurrent_page:$current_page\n1:$1  2:$2  3:$3";
247     }
248         $base_url =~ s/($delim)+/$1/g;  # compress duplicate delims
249         $base_url =~ s/$delim;//g;              # remove empties
250         $base_url =~ s/$delim$//;               # remove trailing delim
251
252     my $url = $base_url . (($base_url =~ m/$delim/ or $base_url =~ m/\?/) ? '&amp;' : '?' ) . $startfrom_name . '=';
253     my $pagination_bar = '';
254
255     # navigation bar useful only if more than one page to display !
256     if ( $nb_pages > 1 ) {
257
258         # link to first page?
259         if ( $current_page > 1 ) {
260             $pagination_bar .=
261                 "\n" . '&nbsp;'
262               . '<a href="'
263               . $url
264               . '1" rel="start">'
265               . '&lt;&lt;' . '</a>';
266         }
267         else {
268             $pagination_bar .=
269               "\n" . '&nbsp;<span class="inactive">&lt;&lt;</span>';
270         }
271
272         # link on previous page ?
273         if ( $current_page > 1 ) {
274             my $previous = $current_page - 1;
275
276             $pagination_bar .=
277                 "\n" . '&nbsp;'
278               . '<a href="'
279               . $url
280               . $previous
281               . '" rel="prev">' . '&lt;' . '</a>';
282         }
283         else {
284             $pagination_bar .=
285               "\n" . '&nbsp;<span class="inactive">&lt;</span>';
286         }
287
288         my $min_to_display      = $current_page - $pages_around;
289         my $max_to_display      = $current_page + $pages_around;
290         my $last_displayed_page = undef;
291
292         for my $page_number ( 1 .. $nb_pages ) {
293             if (
294                    $page_number == 1
295                 or $page_number == $nb_pages
296                 or (    $page_number >= $min_to_display
297                     and $page_number <= $max_to_display )
298               )
299             {
300                 if ( defined $last_displayed_page
301                     and $last_displayed_page != $page_number - 1 )
302                 {
303                     $pagination_bar .=
304                       "\n" . '&nbsp;<span class="inactive">...</span>';
305                 }
306
307                 if ( $page_number == $current_page ) {
308                     $pagination_bar .=
309                         "\n" . '&nbsp;'
310                       . '<span class="currentPage">'
311                       . $page_number
312                       . '</span>';
313                 }
314                 else {
315                     $pagination_bar .=
316                         "\n" . '&nbsp;'
317                       . '<a href="'
318                       . $url
319                       . $page_number . '">'
320                       . $page_number . '</a>';
321                 }
322                 $last_displayed_page = $page_number;
323             }
324         }
325
326         # link on next page?
327         if ( $current_page < $nb_pages ) {
328             my $next = $current_page + 1;
329
330             $pagination_bar .= "\n"
331               . '&nbsp;<a href="'
332               . $url
333               . $next
334               . '" rel="next">' . '&gt;' . '</a>';
335         }
336         else {
337             $pagination_bar .=
338               "\n" . '&nbsp;<span class="inactive">&gt;</span>';
339         }
340
341         # link to last page?
342         if ( $current_page != $nb_pages ) {
343             $pagination_bar .= "\n"
344               . '&nbsp;<a href="'
345               . $url
346               . $nb_pages
347               . '" rel="last">'
348               . '&gt;&gt;' . '</a>';
349         }
350         else {
351             $pagination_bar .=
352               "\n" . '&nbsp;<span class="inactive">&gt;&gt;</span>';
353         }
354     }
355
356     return $pagination_bar;
357 }
358
359 =item output_html_with_http_headers
360
361    &output_html_with_http_headers($query, $cookie, $html[, $content_type][, status])
362
363 Outputs the HTML page $html with the appropriate HTTP headers,
364 with the authentication cookie $cookie and a Content-Type that
365 corresponds to the HTML page $html.
366
367 If the optional C<$content_type> parameter is called, set the
368 response's Content-Type to that value instead of "text/html".
369
370 =cut
371
372 sub output_html_with_http_headers ($$$;$$) {
373     my $query = shift;
374     my $cookie = shift;
375     my $html = shift; 
376     $html =~ s/ \x{C2}
377         (?: \x{88} # NSB
378         |   \x{89} # NSE 
379         # SUDOC shares the cataloguing of french universities
380         |   \x{98} # SUDOC NSB 
381         |   \x{9c} # SUDOC NSE
382         )
383     //gx;
384
385     my $content_type = @_ ? shift : "text/html";
386     my $status = shift; 
387     $content_type = "text/html" unless $content_type =~ m!/!; # very basic sanity check
388     print $query->header(
389         -status  => $status,
390         -type    => $content_type,
391         -charset => 'UTF-8',
392         -cookie  => $cookie,
393         -Pragma => 'no-cache',
394         -'Cache-Control' => 'no-cache',
395     ), $html;
396 }
397
398 sub output_ajax_with_http_headers ($$) {
399     my ($query, $js) = @_;
400     print $query->header(
401         -type    => 'text/javascript',
402         -charset => 'UTF-8',
403         -Pragma  => 'no-cache',
404         -'Cache-Control' => 'no-cache',
405                 -expires =>'-1d',
406     ), $js;
407 }
408
409 sub is_ajax () {
410         my $x_req = $ENV{HTTP_X_REQUESTED_WITH};
411         return ($x_req and $x_req =~ /XMLHttpRequest/i) ? 1 : 0;
412 }
413
414 END { }    # module clean-up code here (global destructor)
415
416 1;
417 __END__
418
419 =back
420
421 =head1 AUTHOR
422
423 Koha Developement team <info@koha.org>
424
425 =cut