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