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