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