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