new: authorities in prog/en template, only partial import from default/en
[koha.git] / C4 / Output.pm
1 package C4::Output;
2
3 # $Id$
4
5 #package to deal with marking up output
6 #You will need to edit parts of this pm
7 #set the value of path to be where your html lives
8
9
10 # Copyright 2000-2002 Katipo Communications
11 #
12 # This file is part of Koha.
13 #
14 # Koha is free software; you can redistribute it and/or modify it under the
15 # terms of the GNU General Public License as published by the Free Software
16 # Foundation; either version 2 of the License, or (at your option) any later
17 # version.
18 #
19 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
20 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
21 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
22 #
23 # You should have received a copy of the GNU General Public License along with
24 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
25 # Suite 330, Boston, MA  02111-1307 USA
26
27 # NOTE: I'm pretty sure this module is deprecated in favor of
28 # templates.
29
30 use strict;
31 require Exporter;
32
33 use C4::Context;
34 use C4::Database;
35 use HTML::Template;
36
37 use vars qw($VERSION @ISA @EXPORT);
38
39 # set the version for version checking
40 $VERSION = 0.01;
41
42 =head1 NAME
43
44 C4::Output - Functions for managing templates
45
46 =head1 FUNCTIONS
47
48 =over 2
49
50 =cut
51
52 @ISA = qw(Exporter);
53 @EXPORT = qw(
54                 &themelanguage &gettemplate setlanguagecookie pagination_bar
55                 );
56
57 #FIXME: this is a quick fix to stop rc1 installing broken
58 #Still trying to figure out the correct fix.
59 my $path = C4::Context->config('intrahtdocs')."/default/en/includes/";
60
61 #---------------------------------------------------------------------------------------------------------
62 # FIXME - POD
63 sub gettemplate {
64         my ($tmplbase, $opac, $query) = @_;
65 if (!$query){
66   warn "no query in gettemplate";
67   }
68         my $htdocs;
69         if ($opac ne "intranet") {
70                 $htdocs = C4::Context->config('opachtdocs');
71         } else {
72                 $htdocs = C4::Context->config('intrahtdocs');
73         }
74
75         my ($theme, $lang) = themelanguage($htdocs, $tmplbase, $opac, $query);
76         my $opacstylesheet = C4::Context->preference('opacstylesheet');
77         my $template = HTML::Template->new(filename      => "$htdocs/$theme/$lang/$tmplbase",
78                                    die_on_bad_params => 0,
79                                    global_vars       => 1,
80                                    path              => ["$htdocs/$theme/$lang/includes"]);
81
82         $template->param(themelang => ($opac ne 'intranet'? '/opac-tmpl': '/intranet-tmpl') . "/$theme/$lang",
83                                                         interface => ($opac ne 'intranet'? '/opac-tmpl': '/intranet-tmpl'),
84                                                         theme => $theme,
85                                                         opacstylesheet => $opacstylesheet,
86                                                         opacsmallimage => C4::Context->preference('opacsmallimage'),
87                                                         lang => $lang);
88
89         
90         return $template;
91 }
92
93 #---------------------------------------------------------------------------------------------------------
94 # FIXME - POD
95 sub themelanguage {
96   my ($htdocs, $tmpl, $section, $query) = @_;
97 #   if (!$query) {
98 #     warn "no query";
99 #   }
100   my $dbh = C4::Context->dbh;
101   my @languages;
102   my @themes;
103   if ( $section eq "intranet")
104   {
105     @languages = split " ", C4::Context->preference("opaclanguages");
106     @themes = split " ", C4::Context->preference("template");
107   }
108   else
109   {
110   # we are in the opac here, what im trying to do is let the individual user
111   # set the theme they want to use.
112   # and perhaps the them as well.
113   my $lang=$query->cookie('KohaOpacLanguage');
114   if ($lang){
115   
116     push @languages,$lang;
117     @themes = split " ", C4::Context->preference("opacthemes");
118   } 
119   else {
120     @languages = split " ", C4::Context->preference("opaclanguages");
121     @themes = split " ", C4::Context->preference("opacthemes");
122     }
123   }
124
125   my ($theme, $lang);
126 # searches through the themes and languages. First template it find it returns.
127 # Priority is for getting the theme right.
128   THEME:
129   foreach my $th (@themes) {
130     foreach my $la (@languages) {
131         for (my $pass = 1; $pass <= 2; $pass += 1) {
132           $la =~ s/([-_])/ $1 eq '-'? '_': '-' /eg if $pass == 2;
133           if (-e "$htdocs/$th/$la/$tmpl") {
134               $theme = $th;
135               $lang = $la;
136               last THEME;
137           }
138         last unless $la =~ /[-_]/;
139         }
140     }
141   }
142   if ($theme and $lang) {
143     return ($theme, $lang);
144   } else {
145     return ('default', 'en');
146   }
147 }
148
149
150 sub setlanguagecookie {
151    my ($query,$language,$uri)=@_;
152    my $cookie=$query->cookie(-name => 'KohaOpacLanguage',
153                                            -value => $language,
154                                            -expires => '');
155    print $query->redirect(-uri=>$uri,
156    -cookie=>$cookie);
157 }                                  
158
159 =item pagination_bar
160
161    pagination_bar($base_url, $nb_pages, $current_page, $startfrom_name)
162
163 Build an HTML pagination bar based on the number of page to display, the
164 current page and the url to give to each page link.
165
166 C<$base_url> is the URL for each page link. The
167 C<$startfrom_name>=page_number is added at the end of the each URL.
168
169 C<$nb_pages> is the total number of pages available.
170
171 C<$current_page> is the current page number. This page number won't become a
172 link.
173
174 This function returns HTML, without any language dependency.
175
176 =cut
177
178 sub pagination_bar {
179     my ($base_url, $nb_pages, $current_page, $startfrom_name) = @_;
180
181     # how many pages to show before and after the current page?
182     my $pages_around = 2;
183
184     my $url =
185         $base_url
186         .($base_url =~ m/&/ ? '&amp;' : '?')
187         .$startfrom_name.'='
188         ;
189
190     my $pagination_bar = '';
191
192     # current page detection
193     if (not defined $current_page) {
194         $current_page = 1;
195     }
196
197     # navigation bar useful only if more than one page to display !
198     if ($nb_pages > 1) {
199         # link to first page?
200         if ($current_page > 1) {
201             $pagination_bar.=
202                 "\n".'&nbsp;'
203                 .'<a href="'.$url.'1" rel="start">'
204                 .'&lt;&lt;'
205                 .'</a>'
206                 ;
207         }
208         else {
209             $pagination_bar.=
210                 "\n".'&nbsp;<span class="inactive">&lt;&lt;</span>';
211         }
212
213         # link on previous page ?
214         if ($current_page > 1) {
215             my $previous = $current_page - 1;
216
217             $pagination_bar.=
218                 "\n".'&nbsp;'
219                 .'<a href="'
220                 .$url.$previous
221                 .'" rel="prev">'
222                 .'&lt;'
223                 .'</a>'
224                 ;
225         }
226         else {
227             $pagination_bar.=
228                 "\n".'&nbsp;<span class="inactive">&lt;</span>';
229         }
230
231         my $min_to_display = $current_page - $pages_around;
232         my $max_to_display = $current_page + $pages_around;
233         my $last_displayed_page = undef;
234
235         for my $page_number (1..$nb_pages) {
236             if ($page_number == 1
237                 or $page_number == $nb_pages
238                 or ($page_number >= $min_to_display and $page_number <= $max_to_display)
239             ) {
240                 if (defined $last_displayed_page
241                     and $last_displayed_page != $page_number - 1
242                 ) {
243                     $pagination_bar.=
244                         "\n".'&nbsp;<span class="inactive">...</span>'
245                         ;
246                 }
247
248                 if ($page_number == $current_page) {
249                     $pagination_bar.=
250                         "\n".'&nbsp;'
251                         .'<span class="currentPage">'.$page_number.'</span>'
252                         ;
253                 }
254                 else {
255                     $pagination_bar.=
256                         "\n".'&nbsp;'
257                         .'<a href="'.$url.$page_number.'">'.$page_number.'</a>'
258                         ;
259                 }
260                 $last_displayed_page = $page_number;
261             }
262         }
263
264         # link on next page?
265         if ($current_page < $nb_pages) {
266             my $next = $current_page + 1;
267
268             $pagination_bar.=
269                 "\n".'&nbsp;<a href="'.$url.$next.'" rel="next">'
270                 .'&gt;'
271                 .'</a>'
272                 ;
273         }
274         else {
275             $pagination_bar.=
276                 "\n".'&nbsp;<span class="inactive">&gt;</span>'
277                 ;
278         }
279
280         # link to last page?
281         if ($current_page != $nb_pages) {
282             $pagination_bar.=
283                 "\n".'&nbsp;<a href="'.$url.$nb_pages.'" rel="last">'
284                 .'&gt;&gt;'
285                 .'</a>'
286                 ;
287         }
288         else {
289             $pagination_bar.=
290                 "\n".'&nbsp;<span class="inactive">&gt;&gt;</span>';
291         }
292     }
293
294     return $pagination_bar;
295 }
296
297
298 END { }       # module clean-up code here (global destructor)
299
300 1;
301 __END__
302
303 =back
304
305 =head1 AUTHOR
306
307 Koha Developement team <info@koha.org>
308
309 =cut