improving NOzebra search :
[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 # $Id$
25
26 # NOTE: I'm pretty sure this module is deprecated in favor of
27 # templates.
28
29 use strict;
30 require Exporter;
31
32 use C4::Context;
33 use HTML::Template::Pro;
34
35 use vars qw($VERSION @ISA @EXPORT);
36
37 # set the version for version checking
38 $VERSION = do { my @v = '$Revision$' =~ /\d+/g; shift(@v) . "." . join( "_", map { sprintf "%03d", $_ } @v ); };
39
40 =head1 NAME
41
42 C4::Output - Functions for managing templates
43
44 =head1 FUNCTIONS
45
46 =over 2
47
48 =cut
49
50 @ISA    = qw(Exporter);
51 push @EXPORT, qw(
52   &themelanguage &gettemplate setlanguagecookie pagination_bar
53 );
54
55 #Output
56 push @EXPORT, qw(
57                 &guesscharset
58                 &guesstype
59                 &output_html_with_http_headers
60                 );
61
62
63 #FIXME: this is a quick fix to stop rc1 installing broken
64 #Still trying to figure out the correct fix.
65 my $path = C4::Context->config('intrahtdocs') . "/default/en/includes/";
66
67 #---------------------------------------------------------------------------------------------------------
68 # FIXME - POD
69 sub gettemplate {
70     my ( $tmplbase, $opac, $query ) = @_;
71     if ( !$query ) {
72         warn "no query in gettemplate";
73     }
74     my $htdocs;
75     if ( $opac 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     #    warn "PATH : $path";
84     my ( $theme, $lang ) = themelanguage( $htdocs, $tmplbase, $opac, $query );
85     my $opacstylesheet = C4::Context->preference('opacstylesheet');
86     my $template       = HTML::Template::Pro->new(
87         filename          => "$htdocs/$theme/$lang/$tmplbase",
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 => ( $opac ne 'intranet' ? '/opac-tmpl' : '/intranet-tmpl' )
96           . "/$theme/$lang",
97         interface => ( $opac 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     return $template;
106 }
107
108 #---------------------------------------------------------------------------------------------------------
109 # FIXME - POD
110 sub themelanguage {
111     my ( $htdocs, $tmpl, $section, $query ) = @_;
112
113     #   if (!$query) {
114     #     warn "no query";
115     #   }
116     my $dbh = C4::Context->dbh;
117     my @languages;
118     my @themes;
119     if ( $section eq "intranet" ) {
120         @languages = split " ", C4::Context->preference("opaclanguages");
121         @themes    = split " ", C4::Context->preference("template");
122     }
123     else {
124
125       # we are in the opac here, what im trying to do is let the individual user
126       # set the theme they want to use.
127       # and perhaps the them as well.
128         my $lang = $query->cookie('KohaOpacLanguage');
129         if ($lang) {
130
131             push @languages, $lang;
132             @themes = split " ", C4::Context->preference("opacthemes");
133         }
134         else {
135             @languages = split " ", C4::Context->preference("opaclanguages");
136             @themes    = split " ", C4::Context->preference("opacthemes");
137         }
138     }
139
140     my ( $theme, $lang );
141
142  # searches through the themes and languages. First template it find it returns.
143  # Priority is for getting the theme right.
144   THEME:
145     foreach my $th (@themes) {
146         foreach my $la (@languages) {
147             for ( my $pass = 1 ; $pass <= 2 ; $pass += 1 ) {
148                 $la =~ s/([-_])/ $1 eq '-'? '_': '-' /eg if $pass == 2;
149                 if ( -e "$htdocs/$th/$la/$tmpl" ) {
150                     $theme = $th;
151                     $lang  = $la;
152                     last THEME;
153                 }
154                 last unless $la =~ /[-_]/;
155             }
156         }
157     }
158     if ( $theme and $lang ) {
159         return ( $theme, $lang );
160     }
161     else {
162         return ( 'prog', 'en' );
163     }
164 }
165
166 sub setlanguagecookie {
167     my ( $query, $language, $uri ) = @_;
168     my $cookie = $query->cookie(
169         -name    => 'KohaOpacLanguage',
170         -value   => $language,
171         -expires => ''
172     );
173     print $query->redirect(
174         -uri    => $uri,
175         -cookie => $cookie
176     );
177 }
178
179 =item pagination_bar
180
181    pagination_bar($base_url, $nb_pages, $current_page, $startfrom_name)
182
183 Build an HTML pagination bar based on the number of page to display, the
184 current page and the url to give to each page link.
185
186 C<$base_url> is the URL for each page link. The
187 C<$startfrom_name>=page_number is added at the end of the each URL.
188
189 C<$nb_pages> is the total number of pages available.
190
191 C<$current_page> is the current page number. This page number won't become a
192 link.
193
194 This function returns HTML, without any language dependency.
195
196 =cut
197
198 sub pagination_bar {
199     my ( $base_url, $nb_pages, $current_page, $startfrom_name ) = @_;
200
201     # how many pages to show before and after the current page?
202     my $pages_around = 2;
203
204     my $url =
205       $base_url . ( $base_url =~ m/&/ ? '&amp;' : '?' ) . $startfrom_name . '=';
206
207     my $pagination_bar = '';
208
209     # current page detection
210     if ( not defined $current_page ) {
211         $current_page = 1;
212     }
213
214     # navigation bar useful only if more than one page to display !
215     if ( $nb_pages > 1 ) {
216
217         # link to first page?
218         if ( $current_page > 1 ) {
219             $pagination_bar .=
220                 "\n" . '&nbsp;'
221               . '<a href="'
222               . $url
223               . '1" rel="start">'
224               . '&lt;&lt;' . '</a>';
225         }
226         else {
227             $pagination_bar .=
228               "\n" . '&nbsp;<span class="inactive">&lt;&lt;</span>';
229         }
230
231         # link on previous page ?
232         if ( $current_page > 1 ) {
233             my $previous = $current_page - 1;
234
235             $pagination_bar .=
236                 "\n" . '&nbsp;'
237               . '<a href="'
238               . $url
239               . $previous
240               . '" rel="prev">' . '&lt;' . '</a>';
241         }
242         else {
243             $pagination_bar .=
244               "\n" . '&nbsp;<span class="inactive">&lt;</span>';
245         }
246
247         my $min_to_display      = $current_page - $pages_around;
248         my $max_to_display      = $current_page + $pages_around;
249         my $last_displayed_page = undef;
250
251         for my $page_number ( 1 .. $nb_pages ) {
252             if (
253                    $page_number == 1
254                 or $page_number == $nb_pages
255                 or (    $page_number >= $min_to_display
256                     and $page_number <= $max_to_display )
257               )
258             {
259                 if ( defined $last_displayed_page
260                     and $last_displayed_page != $page_number - 1 )
261                 {
262                     $pagination_bar .=
263                       "\n" . '&nbsp;<span class="inactive">...</span>';
264                 }
265
266                 if ( $page_number == $current_page ) {
267                     $pagination_bar .=
268                         "\n" . '&nbsp;'
269                       . '<span class="currentPage">'
270                       . $page_number
271                       . '</span>';
272                 }
273                 else {
274                     $pagination_bar .=
275                         "\n" . '&nbsp;'
276                       . '<a href="'
277                       . $url
278                       . $page_number . '">'
279                       . $page_number . '</a>';
280                 }
281                 $last_displayed_page = $page_number;
282             }
283         }
284
285         # link on next page?
286         if ( $current_page < $nb_pages ) {
287             my $next = $current_page + 1;
288
289             $pagination_bar .= "\n"
290               . '&nbsp;<a href="'
291               . $url
292               . $next
293               . '" rel="next">' . '&gt;' . '</a>';
294         }
295         else {
296             $pagination_bar .=
297               "\n" . '&nbsp;<span class="inactive">&gt;</span>';
298         }
299
300         # link to last page?
301         if ( $current_page != $nb_pages ) {
302             $pagination_bar .= "\n"
303               . '&nbsp;<a href="'
304               . $url
305               . $nb_pages
306               . '" rel="last">'
307               . '&gt;&gt;' . '</a>';
308         }
309         else {
310             $pagination_bar .=
311               "\n" . '&nbsp;<span class="inactive">&gt;&gt;</span>';
312         }
313     }
314
315     return $pagination_bar;
316 }
317
318
319 =item guesscharset
320
321    &guesscharset($output)
322
323 "Guesses" the charset from the some HTML that would be output.
324
325 C<$output> is the HTML page to be output. If it contains a META tag
326 with a Content-Type, the tag will be scanned for a language code.
327 This code is returned if it is found; undef is returned otherwise.
328
329 This function only does sloppy guessing; it will be confused by
330 unexpected things like SGML comments. What it basically does is to
331 grab something that looks like a META tag and scan it.
332
333 =cut
334
335 sub guesscharset ($) {
336     my($html) = @_;
337     my $charset = undef;
338     local($`, $&, $', $1, $2, $3);
339     # FIXME... These regular expressions will miss a lot of valid tags!
340     if ($html =~ /<meta\s+http-equiv=(["']?)Content-Type\1\s+content=(["'])text\/html\s*;\s*charset=([^\2\s\r\n]+)\2\s*(?:\/?)>/is) {
341         $charset = $3;
342     } elsif ($html =~ /<meta\s+content=(["'])text\/html\s*;\s*charset=([^\1\s\r\n]+)\1\s+http-equiv=(["']?)Content-Type\3\s*(?:\/?)>/is) {
343         $charset = $2;
344     }
345     return $charset;
346 } # guess
347
348 sub guesstype ($) {
349     my($html) = @_;
350     my $charset = guesscharset($html);
351     return defined $charset? "text/html; charset=$charset": "text/html";
352 }
353
354 =item output_html_with_http_headers
355
356    &output_html_with_http_headers($query, $cookie, $html)
357
358 Outputs the HTML page $html with the appropriate HTTP headers,
359 with the authentication cookie $cookie and a Content-Type that
360 corresponds to the HTML page $html.
361
362 =cut
363
364 sub output_html_with_http_headers ($$$) {
365     my($query, $cookie, $html) = @_;
366     print $query->header(
367         -type   => guesstype($html),
368         -cookie => $cookie,
369     ), $html;
370 }
371
372 END { }    # module clean-up code here (global destructor)
373
374 1;
375 __END__
376
377 =back
378
379 =head1 AUTHOR
380
381 Koha Developement team <info@koha.org>
382
383 =cut