removing warn compilation.
[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                 &output_html_with_http_headers
58                 );
59
60
61 #FIXME: this is a quick fix to stop rc1 installing broken
62 #Still trying to figure out the correct fix.
63 my $path = C4::Context->config('intrahtdocs') . "/default/en/includes/";
64
65 #---------------------------------------------------------------------------------------------------------
66 # FIXME - POD
67 sub gettemplate {
68     my ( $tmplbase, $opac, $query ) = @_;
69     if ( !$query ) {
70         warn "no query in gettemplate";
71     }
72     my $htdocs;
73     if ( $opac ne "intranet" ) {
74         $htdocs = C4::Context->config('opachtdocs');
75     }
76     else {
77         $htdocs = C4::Context->config('intrahtdocs');
78     }
79     my $path = C4::Context->preference('intranet_includes') || 'includes';
80
81     #    warn "PATH : $path";
82     my ( $theme, $lang ) = themelanguage( $htdocs, $tmplbase, $opac, $query );
83     my $opacstylesheet = C4::Context->preference('opacstylesheet');
84     my $template       = HTML::Template::Pro->new(
85         filename          => "$htdocs/$theme/$lang/$tmplbase",
86         die_on_bad_params => 1,
87         global_vars       => 1,
88         case_sensitive    => 1,
89         path              => ["$htdocs/$theme/$lang/$path"]
90     );
91
92     $template->param(
93         themelang => ( $opac ne 'intranet' ? '/opac-tmpl' : '/intranet-tmpl' )
94           . "/$theme/$lang",
95         interface => ( $opac ne 'intranet' ? '/opac-tmpl' : '/intranet-tmpl' ),
96         theme => $theme,
97         opacstylesheet      => $opacstylesheet,
98         opaccolorstylesheet => C4::Context->preference('opaccolorstylesheet'),
99         opacsmallimage      => C4::Context->preference('opacsmallimage'),
100         lang                => $lang
101     );
102
103     return $template;
104 }
105
106 #---------------------------------------------------------------------------------------------------------
107 # FIXME - POD
108 sub themelanguage {
109     my ( $htdocs, $tmpl, $section, $query ) = @_;
110
111     #   if (!$query) {
112     #     warn "no query";
113     #   }
114     my $dbh = C4::Context->dbh;
115     my @languages;
116     my @themes;
117     if ( $section eq "intranet" ) {
118         @languages = split " ", C4::Context->preference("opaclanguages");
119         @themes    = split " ", C4::Context->preference("template");
120     }
121     else {
122
123       # we are in the opac here, what im trying to do is let the individual user
124       # set the theme they want to use.
125       # and perhaps the them as well.
126         my $lang = $query->cookie('KohaOpacLanguage');
127         if ($lang) {
128
129             push @languages, $lang;
130             @themes = split " ", C4::Context->preference("opacthemes");
131         }
132         else {
133             @languages = split " ", C4::Context->preference("opaclanguages");
134             @themes    = split " ", C4::Context->preference("opacthemes");
135         }
136     }
137
138     my ( $theme, $lang );
139
140  # searches through the themes and languages. First template it find it returns.
141  # Priority is for getting the theme right.
142   THEME:
143     foreach my $th (@themes) {
144         foreach my $la (@languages) {
145             for ( my $pass = 1 ; $pass <= 2 ; $pass += 1 ) {
146                 $la =~ s/([-_])/ $1 eq '-'? '_': '-' /eg if $pass == 2;
147                 if ( -e "$htdocs/$th/$la/$tmpl" ) {
148                     $theme = $th;
149                     $lang  = $la;
150                     last THEME;
151                 }
152                 last unless $la =~ /[-_]/;
153             }
154         }
155     }
156     if ( $theme and $lang ) {
157         return ( $theme, $lang );
158     }
159     else {
160         return ( 'prog', 'en' );
161     }
162 }
163
164 sub setlanguagecookie {
165     my ( $query, $language, $uri ) = @_;
166     my $cookie = $query->cookie(
167         -name    => 'KohaOpacLanguage',
168         -value   => $language,
169         -expires => ''
170     );
171     print $query->redirect(
172         -uri    => $uri,
173         -cookie => $cookie
174     );
175 }
176
177 =item pagination_bar
178
179    pagination_bar($base_url, $nb_pages, $current_page, $startfrom_name)
180
181 Build an HTML pagination bar based on the number of page to display, the
182 current page and the url to give to each page link.
183
184 C<$base_url> is the URL for each page link. The
185 C<$startfrom_name>=page_number is added at the end of the each URL.
186
187 C<$nb_pages> is the total number of pages available.
188
189 C<$current_page> is the current page number. This page number won't become a
190 link.
191
192 This function returns HTML, without any language dependency.
193
194 =cut
195
196 sub pagination_bar {
197     my ( $base_url, $nb_pages, $current_page, $startfrom_name ) = @_;
198
199     # how many pages to show before and after the current page?
200     my $pages_around = 2;
201
202     my $url =
203       $base_url . ( $base_url =~ m/&/ ? '&amp;' : '?' ) . $startfrom_name . '=';
204
205     my $pagination_bar = '';
206
207     # current page detection
208     if ( not defined $current_page ) {
209         $current_page = 1;
210     }
211
212     # navigation bar useful only if more than one page to display !
213     if ( $nb_pages > 1 ) {
214
215         # link to first page?
216         if ( $current_page > 1 ) {
217             $pagination_bar .=
218                 "\n" . '&nbsp;'
219               . '<a href="'
220               . $url
221               . '1" rel="start">'
222               . '&lt;&lt;' . '</a>';
223         }
224         else {
225             $pagination_bar .=
226               "\n" . '&nbsp;<span class="inactive">&lt;&lt;</span>';
227         }
228
229         # link on previous page ?
230         if ( $current_page > 1 ) {
231             my $previous = $current_page - 1;
232
233             $pagination_bar .=
234                 "\n" . '&nbsp;'
235               . '<a href="'
236               . $url
237               . $previous
238               . '" rel="prev">' . '&lt;' . '</a>';
239         }
240         else {
241             $pagination_bar .=
242               "\n" . '&nbsp;<span class="inactive">&lt;</span>';
243         }
244
245         my $min_to_display      = $current_page - $pages_around;
246         my $max_to_display      = $current_page + $pages_around;
247         my $last_displayed_page = undef;
248
249         for my $page_number ( 1 .. $nb_pages ) {
250             if (
251                    $page_number == 1
252                 or $page_number == $nb_pages
253                 or (    $page_number >= $min_to_display
254                     and $page_number <= $max_to_display )
255               )
256             {
257                 if ( defined $last_displayed_page
258                     and $last_displayed_page != $page_number - 1 )
259                 {
260                     $pagination_bar .=
261                       "\n" . '&nbsp;<span class="inactive">...</span>';
262                 }
263
264                 if ( $page_number == $current_page ) {
265                     $pagination_bar .=
266                         "\n" . '&nbsp;'
267                       . '<span class="currentPage">'
268                       . $page_number
269                       . '</span>';
270                 }
271                 else {
272                     $pagination_bar .=
273                         "\n" . '&nbsp;'
274                       . '<a href="'
275                       . $url
276                       . $page_number . '">'
277                       . $page_number . '</a>';
278                 }
279                 $last_displayed_page = $page_number;
280             }
281         }
282
283         # link on next page?
284         if ( $current_page < $nb_pages ) {
285             my $next = $current_page + 1;
286
287             $pagination_bar .= "\n"
288               . '&nbsp;<a href="'
289               . $url
290               . $next
291               . '" rel="next">' . '&gt;' . '</a>';
292         }
293         else {
294             $pagination_bar .=
295               "\n" . '&nbsp;<span class="inactive">&gt;</span>';
296         }
297
298         # link to last page?
299         if ( $current_page != $nb_pages ) {
300             $pagination_bar .= "\n"
301               . '&nbsp;<a href="'
302               . $url
303               . $nb_pages
304               . '" rel="last">'
305               . '&gt;&gt;' . '</a>';
306         }
307         else {
308             $pagination_bar .=
309               "\n" . '&nbsp;<span class="inactive">&gt;&gt;</span>';
310         }
311     }
312
313     return $pagination_bar;
314 }
315
316 =item output_html_with_http_headers
317
318    &output_html_with_http_headers($query, $cookie, $html)
319
320 Outputs the HTML page $html with the appropriate HTTP headers,
321 with the authentication cookie $cookie and a Content-Type that
322 corresponds to the HTML page $html.
323
324 =cut
325
326 sub output_html_with_http_headers ($$$) {
327     my($query, $cookie, $html) = @_;
328     print $query->header(
329         -type   => 'utf-8',
330         -cookie => $cookie,
331     ), $html;
332 }
333
334 END { }    # module clean-up code here (global destructor)
335
336 1;
337 __END__
338
339 =back
340
341 =head1 AUTHOR
342
343 Koha Developement team <info@koha.org>
344
345 =cut