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