Cleanup selectbranchprinter.pl and .tmpl
[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(&is_ajax ajax_fail); # More stuff should go here instead
42         %EXPORT_TAGS = ( all =>[qw(&themelanguage &gettemplate setlanguagecookie pagination_bar
43                                                                 &output_with_http_headers &output_html_with_http_headers)],
44                                         ajax =>[qw(&output_with_http_headers is_ajax)],
45                                         html =>[qw(&output_with_http_headers &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 &output_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
84     # if the template doesn't exist, load the English one as a last resort
85     my $filename = "$htdocs/$theme/$lang/modules/$tmplbase";
86     unless (-f $filename) {
87         $lang = 'en';
88         $filename = "$htdocs/$theme/$lang/modules/$tmplbase";
89     }
90     my $template       = HTML::Template::Pro->new(
91         filename          => $filename,
92         die_on_bad_params => 1,
93         global_vars       => 1,
94         case_sensitive    => 1,
95         loop_context_vars => 1, # enable: __first__, __last__, __inner__, __odd__, __counter__ 
96         path              => ["$htdocs/$theme/$lang/$path"]
97     );
98     my $themelang=( $interface ne 'intranet' ? '/opac-tmpl' : '/intranet-tmpl' )
99           . "/$theme/$lang";
100     $template->param(
101         themelang => $themelang,
102         yuipath   => (C4::Context->preference("yuipath") eq "local"?"$themelang/lib/yui":C4::Context->preference("yuipath")),
103         interface => ( $interface ne 'intranet' ? '/opac-tmpl' : '/intranet-tmpl' ),
104         theme     => $theme,
105         lang      => $lang
106     );
107
108     # Bidirectionality
109     my $current_lang = regex_lang_subtags($lang);
110     my $bidi;
111     $bidi = get_bidi($current_lang->{script}) if $current_lang->{script};
112     # Languages
113     my $languages_loop = getTranslatedLanguages($interface,$theme,$lang);
114     my $num_languages_enabled = 0;
115     foreach my $lang (@$languages_loop) {
116         foreach my $sublang (@{ $lang->{'sublanguages_loop'} }) {
117             $num_languages_enabled++ if $sublang->{enabled};
118          }
119     }
120     $template->param(
121             languages_loop       => $languages_loop,
122             bidi                 => $bidi,
123             one_language_enabled => ($num_languages_enabled <= 1) ? 1 : 0, # deal with zero enabled langs as well
124     ) unless @$languages_loop<2;
125
126     return $template;
127 }
128
129 #---------------------------------------------------------------------------------------------------------
130 # FIXME - POD
131 sub themelanguage {
132     my ( $htdocs, $tmpl, $interface, $query ) = @_;
133     ($query) or warn "no query in themelanguage";
134
135     # Set some defaults for language and theme
136     # First, check the user's preferences
137     my $lang;
138     my $http_accept_language = $ENV{ HTTP_ACCEPT_LANGUAGE };
139     $lang = accept_language( $http_accept_language, 
140               getTranslatedLanguages($interface,'prog') )
141       if $http_accept_language;
142     # But, if there's a cookie set, obey it
143     $lang = $query->cookie('KohaOpacLanguage') if $query->cookie('KohaOpacLanguage');
144     # Fall back to English
145     my @languages;
146     if ($interface eq 'intranet') {
147         @languages = split ",", C4::Context->preference("language");
148     } else {
149         @languages = split ",", C4::Context->preference("opaclanguages");
150     }
151     if ($lang){  
152         @languages=($lang,@languages);
153     } else {
154         $lang = $languages[0];
155     }      
156     my $theme = 'prog'; # in the event of theme failure default to 'prog' -fbcit
157     my $dbh = C4::Context->dbh;
158     my @themes;
159     if ( $interface eq "intranet" ) {
160         @themes    = split " ", C4::Context->preference("template");
161     }
162     else {
163       # we are in the opac here, what im trying to do is let the individual user
164       # set the theme they want to use.
165       # and perhaps the them as well.
166         #my $lang = $query->cookie('KohaOpacLanguage');
167         @themes = split " ", C4::Context->preference("opacthemes");
168     }
169
170  # searches through the themes and languages. First template it find it returns.
171  # Priority is for getting the theme right.
172     THEME:
173     foreach my $th (@themes) {
174         foreach my $la (@languages) {
175             #for ( my $pass = 1 ; $pass <= 2 ; $pass += 1 ) {
176                 # warn "$htdocs/$th/$la/modules/$interface-"."tmpl";
177                 #$la =~ s/([-_])/ $1 eq '-'? '_': '-' /eg if $pass == 2;
178                                 if ( -e "$htdocs/$th/$la/modules/$tmpl") {
179                 #".($interface eq 'intranet'?"modules":"")."/$tmpl" ) {
180                     $theme = $th;
181                     $lang  = $la;
182                     last THEME;
183                 }
184                 last unless $la =~ /[-_]/;
185             #}
186         }
187     }
188     return ( $theme, $lang );
189 }
190
191 sub setlanguagecookie {
192     my ( $query, $language, $uri ) = @_;
193     my $cookie = $query->cookie(
194         -name    => 'KohaOpacLanguage',
195         -value   => $language,
196         -expires => ''
197     );
198     print $query->redirect(
199         -uri    => $uri,
200         -cookie => $cookie
201     );
202 }
203
204 =item pagination_bar
205
206    pagination_bar($base_url, $nb_pages, $current_page, $startfrom_name)
207
208 Build an HTML pagination bar based on the number of page to display, the
209 current page and the url to give to each page link.
210
211 C<$base_url> is the URL for each page link. The
212 C<$startfrom_name>=page_number is added at the end of the each URL.
213
214 C<$nb_pages> is the total number of pages available.
215
216 C<$current_page> is the current page number. This page number won't become a
217 link.
218
219 This function returns HTML, without any language dependency.
220
221 =cut
222
223 sub pagination_bar {
224         my $base_url = (@_ ? shift : $ENV{SCRIPT_NAME} . $ENV{QUERY_STRING}) or return undef;
225     my $nb_pages       = (@_) ? shift : 1;
226     my $current_page   = (@_) ? shift : undef;  # delay default until later
227     my $startfrom_name = (@_) ? shift : 'page';
228
229     # how many pages to show before and after the current page?
230     my $pages_around = 2;
231
232         my $delim = qr/\&(?:amp;)?|;/;          # "non memory" cluster: no backreference
233         $base_url =~ s/$delim*\b$startfrom_name=(\d+)//g; # remove previous pagination var
234     unless (defined $current_page and $current_page > 0 and $current_page <= $nb_pages) {
235         $current_page = ($1) ? $1 : 1;  # pull current page from param in URL, else default to 1
236                 # $debug and    # FIXME: use C4::Debug;
237                 # warn "with QUERY_STRING:" .$ENV{QUERY_STRING}. "\ncurrent_page:$current_page\n1:$1  2:$2  3:$3";
238     }
239         $base_url =~ s/($delim)+/$1/g;  # compress duplicate delims
240         $base_url =~ s/$delim;//g;              # remove empties
241         $base_url =~ s/$delim$//;               # remove trailing delim
242
243     my $url = $base_url . (($base_url =~ m/$delim/ or $base_url =~ m/\?/) ? '&amp;' : '?' ) . $startfrom_name . '=';
244     my $pagination_bar = '';
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_with_http_headers
351
352    &output_with_http_headers($query, $cookie, $data, $content_type[, $status])
353
354 Outputs $data with the appropriate HTTP headers,
355 the authentication cookie $cookie and a Content-Type specified in
356 $content_type.
357
358 If applicable, $cookie can be undef, and it will not be sent.
359
360 $content_type is one of the following: 'html', 'js', 'json', 'xml', 'rss', or 'atom'.
361
362 $status is an HTTP status message, like '403 Authentication Required'. It defaults to '200 OK'.
363
364 =cut
365
366 sub output_with_http_headers($$$$;$) {
367     my ( $query, $cookie, $data, $content_type, $status ) = @_;
368     $status ||= '200 OK';
369
370     my %content_type_map = (
371         'html' => 'text/html',
372         'js'   => 'text/javascript',
373         'json' => 'application/json',
374         'xml'  => 'text/xml',
375         # NOTE: not using application/atom+xml or application/rss+xml because of
376         # Internet Explorer 6; see bug 2078.
377         'rss'  => 'text/xml',
378         'atom' => 'text/xml'
379     );
380
381     die "Unknown content type '$content_type'" if ( !defined( $content_type_map{$content_type} ) );
382     my $options = {
383         type    => $content_type_map{$content_type},
384         status  => $status,
385         charset => 'UTF-8',
386         Pragma          => 'no-cache',
387         'Cache-Control' => 'no-cache',
388     };
389     $options->{cookie} = $cookie if $cookie;
390     if ($content_type eq 'html') {  # guaranteed to be one of the content_type_map keys, else we'd have died
391         $options->{'Content-Style-Type' } = 'text/css';
392         $options->{'Content-Script-Type'} = 'text/javascript';
393     }
394     print $query->header($options), $data;
395 }
396
397 sub output_html_with_http_headers ($$$) {
398     my ( $query, $cookie, $data ) = @_;
399     output_with_http_headers( $query, $cookie, $data, 'html' );
400 }
401
402 sub is_ajax () {
403     my $x_req = $ENV{HTTP_X_REQUESTED_WITH};
404     return ( $x_req and $x_req =~ /XMLHttpRequest/i ) ? 1 : 0;
405 }
406
407 END { }    # module clean-up code here (global destructor)
408
409 1;
410 __END__
411
412 =back
413
414 =head1 AUTHOR
415
416 Koha Developement team <info@koha.org>
417
418 =cut