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