kohabug 2078 - send correct Content-type for search feeds
[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
83     my ( $theme, $lang ) = themelanguage( $htdocs, $tmplbase, $interface, $query );
84     my $opacstylesheet = C4::Context->preference('opacstylesheet');
85
86     # if the template doesn't exist, load the English one as a last resort
87     my $filename = "$htdocs/$theme/$lang/modules/$tmplbase";
88     unless (-f $filename) {
89         $lang = 'en';
90         $filename = "$htdocs/$theme/$lang/modules/$tmplbase";
91     }
92     my $template       = HTML::Template::Pro->new(
93         filename          => $filename,
94         die_on_bad_params => 1,
95         global_vars       => 1,
96         case_sensitive    => 1,
97             loop_context_vars => 1,             # enable: __first__, __last__, __inner__, __odd__, __counter__ 
98         path              => ["$htdocs/$theme/$lang/$path"]
99     );
100     my $themelang=( $interface ne 'intranet' ? '/opac-tmpl' : '/intranet-tmpl' )
101           . "/$theme/$lang";
102     $template->param(
103         themelang => $themelang,
104         yuipath => (C4::Context->preference("yuipath") eq "local"?"$themelang/lib/yui":C4::Context->preference("yuipath")),
105         interface => ( $interface ne 'intranet' ? '/opac-tmpl' : '/intranet-tmpl' ),
106         theme => $theme,
107         opacstylesheet      => $opacstylesheet,
108         opaccolorstylesheet => C4::Context->preference('opaccolorstylesheet'),
109         opacsmallimage      => C4::Context->preference('opacsmallimage'),
110         lang                => $lang
111     );
112
113     # Bidirectionality
114     my $current_lang = regex_lang_subtags($lang);
115     my $bidi;
116     $bidi = get_bidi($current_lang->{script}) if $current_lang->{script};
117     # Languages
118     my $languages_loop = getTranslatedLanguages($interface,$theme,$lang);
119     $template->param(
120             languages_loop => $languages_loop,
121             bidi => $bidi
122     ) unless @$languages_loop<2;
123
124     return $template;
125 }
126
127 #---------------------------------------------------------------------------------------------------------
128 # FIXME - POD
129 sub themelanguage {
130     my ( $htdocs, $tmpl, $interface, $query ) = @_;
131     ($query) or warn "no query in themelanguage";
132
133     # Set some defaults for language and theme
134     # First, check the user's preferences
135     my $lang;
136     my $http_accept_language = regex_lang_subtags($ENV{HTTP_ACCEPT_LANGUAGE})->{language};
137     if ($http_accept_language) {
138         $lang = accept_language($http_accept_language,getTranslatedLanguages($interface,'prog'));
139     } 
140     # But, if there's a cookie set, obey it
141     $lang = $query->cookie('KohaOpacLanguage') if $query->cookie('KohaOpacLanguage');
142     # Fall back to English
143     my @languages = split " ", C4::Context->preference("opaclanguages");
144     if ($lang){  
145         @languages=($lang,@languages);
146     } else {
147         $lang = $languages[0];
148     }      
149     my $theme = 'prog'; # in the event of theme failure default to 'prog' -fbcit
150     my $dbh = C4::Context->dbh;
151     my @themes;
152     if ( $interface eq "intranet" ) {
153         @themes    = split " ", C4::Context->preference("template");
154     }
155     else {
156       # we are in the opac here, what im trying to do is let the individual user
157       # set the theme they want to use.
158       # and perhaps the them as well.
159         #my $lang = $query->cookie('KohaOpacLanguage');
160         @themes = split " ", C4::Context->preference("opacthemes");
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                 # warn "$htdocs/$th/$la/modules/$interface-"."tmpl";
170                 #$la =~ s/([-_])/ $1 eq '-'? '_': '-' /eg if $pass == 2;
171                                 if ( -e "$htdocs/$th/$la/modules/$tmpl") {
172                 #".($interface eq 'intranet'?"modules":"")."/$tmpl" ) {
173                     $theme = $th;
174                     $lang  = $la;
175                     last THEME;
176                 }
177                 last unless $la =~ /[-_]/;
178             #}
179         }
180     }
181     return ( $theme, $lang );
182 }
183
184 sub setlanguagecookie {
185     my ( $query, $language, $uri ) = @_;
186     my $cookie = $query->cookie(
187         -name    => 'KohaOpacLanguage',
188         -value   => $language,
189         -expires => ''
190     );
191     print $query->redirect(
192         -uri    => $uri,
193         -cookie => $cookie
194     );
195 }
196
197 =item pagination_bar
198
199    pagination_bar($base_url, $nb_pages, $current_page, $startfrom_name)
200
201 Build an HTML pagination bar based on the number of page to display, the
202 current page and the url to give to each page link.
203
204 C<$base_url> is the URL for each page link. The
205 C<$startfrom_name>=page_number is added at the end of the each URL.
206
207 C<$nb_pages> is the total number of pages available.
208
209 C<$current_page> is the current page number. This page number won't become a
210 link.
211
212 This function returns HTML, without any language dependency.
213
214 =cut
215
216 sub pagination_bar {
217         my $base_url = (@_ ? shift : $ENV{SCRIPT_NAME} . $ENV{QUERY_STRING}) or return undef;
218     my $nb_pages       = (@_) ? shift : 1;
219     my $current_page   = (@_) ? shift : undef;  # delay default until later
220     my $startfrom_name = (@_) ? shift : 'page';
221
222     # how many pages to show before and after the current page?
223     my $pages_around = 2;
224
225         my $delim = qr/\&(?:amp;)?|;/;          # "non memory" cluster: no backreference
226         $base_url =~ s/$delim*\b$startfrom_name=(\d+)//g; # remove previous pagination var
227     unless (defined $current_page and $current_page > 0 and $current_page <= $nb_pages) {
228         $current_page = ($1) ? $1 : 1;  # pull current page from param in URL, else default to 1
229                 # $debug and    # FIXME: use C4::Debug;
230                 # warn "with QUERY_STRING:" .$ENV{QUERY_STRING}. "\ncurrent_page:$current_page\n1:$1  2:$2  3:$3";
231     }
232         $base_url =~ s/($delim)+/$1/g;  # compress duplicate delims
233         $base_url =~ s/$delim;//g;              # remove empties
234         $base_url =~ s/$delim$//;               # remove trailing delim
235
236     my $url = $base_url . ( $base_url =~ m/$delim/ ? '&amp;' : '?' ) . $startfrom_name . '=';
237     my $pagination_bar = '';
238
239     # navigation bar useful only if more than one page to display !
240     if ( $nb_pages > 1 ) {
241
242         # link to first page?
243         if ( $current_page > 1 ) {
244             $pagination_bar .=
245                 "\n" . '&nbsp;'
246               . '<a href="'
247               . $url
248               . '1" rel="start">'
249               . '&lt;&lt;' . '</a>';
250         }
251         else {
252             $pagination_bar .=
253               "\n" . '&nbsp;<span class="inactive">&lt;&lt;</span>';
254         }
255
256         # link on previous page ?
257         if ( $current_page > 1 ) {
258             my $previous = $current_page - 1;
259
260             $pagination_bar .=
261                 "\n" . '&nbsp;'
262               . '<a href="'
263               . $url
264               . $previous
265               . '" rel="prev">' . '&lt;' . '</a>';
266         }
267         else {
268             $pagination_bar .=
269               "\n" . '&nbsp;<span class="inactive">&lt;</span>';
270         }
271
272         my $min_to_display      = $current_page - $pages_around;
273         my $max_to_display      = $current_page + $pages_around;
274         my $last_displayed_page = undef;
275
276         for my $page_number ( 1 .. $nb_pages ) {
277             if (
278                    $page_number == 1
279                 or $page_number == $nb_pages
280                 or (    $page_number >= $min_to_display
281                     and $page_number <= $max_to_display )
282               )
283             {
284                 if ( defined $last_displayed_page
285                     and $last_displayed_page != $page_number - 1 )
286                 {
287                     $pagination_bar .=
288                       "\n" . '&nbsp;<span class="inactive">...</span>';
289                 }
290
291                 if ( $page_number == $current_page ) {
292                     $pagination_bar .=
293                         "\n" . '&nbsp;'
294                       . '<span class="currentPage">'
295                       . $page_number
296                       . '</span>';
297                 }
298                 else {
299                     $pagination_bar .=
300                         "\n" . '&nbsp;'
301                       . '<a href="'
302                       . $url
303                       . $page_number . '">'
304                       . $page_number . '</a>';
305                 }
306                 $last_displayed_page = $page_number;
307             }
308         }
309
310         # link on next page?
311         if ( $current_page < $nb_pages ) {
312             my $next = $current_page + 1;
313
314             $pagination_bar .= "\n"
315               . '&nbsp;<a href="'
316               . $url
317               . $next
318               . '" rel="next">' . '&gt;' . '</a>';
319         }
320         else {
321             $pagination_bar .=
322               "\n" . '&nbsp;<span class="inactive">&gt;</span>';
323         }
324
325         # link to last page?
326         if ( $current_page != $nb_pages ) {
327             $pagination_bar .= "\n"
328               . '&nbsp;<a href="'
329               . $url
330               . $nb_pages
331               . '" rel="last">'
332               . '&gt;&gt;' . '</a>';
333         }
334         else {
335             $pagination_bar .=
336               "\n" . '&nbsp;<span class="inactive">&gt;&gt;</span>';
337         }
338     }
339
340     return $pagination_bar;
341 }
342
343 =item output_html_with_http_headers
344
345    &output_html_with_http_headers($query, $cookie, $html[, $content_type])
346
347 Outputs the HTML page $html with the appropriate HTTP headers,
348 with the authentication cookie $cookie and a Content-Type that
349 corresponds to the HTML page $html.
350
351 If the optional C<$content_type> parameter is called, set the
352 response's Content-Type to that value instead of "text/html".
353
354 =cut
355
356 sub output_html_with_http_headers ($$$;$) {
357     my $query = shift;
358     my $cookie = shift;
359     my $html = shift;
360     my $content_type = @_ ? shift : "text/html";
361     $content_type = "text/html" unless $content_type =~ m!/!; # very basic sanity check
362     print $query->header(
363         -type    => $content_type,
364         -charset => 'UTF-8',
365         -cookie  => $cookie,
366         -Pragma => 'no-cache',
367         -'Cache-Control' => 'no-cache',
368     ), $html;
369 }
370
371 sub output_ajax_with_http_headers ($$) {
372     my ($query, $js) = @_;
373     print $query->header(
374         -type    => 'text/javascript',
375         -charset => 'UTF-8',
376         -Pragma  => 'no-cache',
377         -'Cache-Control' => 'no-cache',
378                 -expires =>'-1d',
379     ), $js;
380 }
381
382 sub is_ajax () {
383         my $x_req = $ENV{HTTP_X_REQUESTED_WITH};
384         return ($x_req and $x_req =~ /XMLHttpRequest/i) ? 1 : 0;
385 }
386
387 END { }    # module clean-up code here (global destructor)
388
389 1;
390 __END__
391
392 =back
393
394 =head1 AUTHOR
395
396 Koha Developement team <info@koha.org>
397
398 =cut