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