Fix for variable scoping problem
[koha.git] / C4 / Output.pm
blobd4ae30fe007b6437cddcc1181987193c1bf79a1e
1 package C4::Output;
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
7 # Copyright 2000-2002 Katipo Communications
9 # This file is part of Koha.
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.
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.
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
25 # NOTE: I'm pretty sure this module is deprecated in favor of
26 # templates.
28 use strict;
29 require Exporter;
31 use C4::Context;
32 use HTML::Template::Pro;
34 use vars qw($VERSION @ISA @EXPORT);
36 # set the version for version checking
37 $VERSION = 3.00;
39 =head1 NAME
41 C4::Output - Functions for managing templates
43 =head1 FUNCTIONS
45 =over 2
47 =cut
49 @ISA = qw(Exporter);
50 push @EXPORT, qw(
51 &themelanguage &gettemplate setlanguagecookie pagination_bar
54 #Output
55 push @EXPORT, qw(
56 &output_html_with_http_headers
60 #FIXME: this is a quick fix to stop rc1 installing broken
61 #Still trying to figure out the correct fix.
62 my $path = C4::Context->config('intrahtdocs') . "/prog/en/includes/";
64 #---------------------------------------------------------------------------------------------------------
65 # FIXME - POD
66 sub gettemplate {
67 my ( $tmplbase, $interface, $query ) = @_;
68 if ( !$query ) {
69 warn "no query in gettemplate";
71 my $htdocs;
72 if ( $interface ne "intranet" ) {
73 $htdocs = C4::Context->config('opachtdocs');
75 else {
76 $htdocs = C4::Context->config('intrahtdocs');
78 my $path = C4::Context->preference('intranet_includes') || 'includes';
80 # warn "PATH : $path";
81 my ( $theme, $lang ) = themelanguage( $htdocs, $tmplbase, $interface, $query );
82 my $opacstylesheet = C4::Context->preference('opacstylesheet');
83 my $template = HTML::Template::Pro->new(
84 filename => "$htdocs/$theme/$lang/".($interface eq 'intranet'?"modules":"")."/$tmplbase",
85 die_on_bad_params => 1,
86 global_vars => 1,
87 case_sensitive => 1,
88 path => ["$htdocs/$theme/$lang/$path"]
91 $template->param(
92 themelang => ( $interface ne 'intranet' ? '/opac-tmpl' : '/intranet-tmpl' )
93 . "/$theme/$lang",
94 interface => ( $interface ne 'intranet' ? '/opac-tmpl' : '/intranet-tmpl' ),
95 theme => $theme,
96 opacstylesheet => $opacstylesheet,
97 opaccolorstylesheet => C4::Context->preference('opaccolorstylesheet'),
98 opacsmallimage => C4::Context->preference('opacsmallimage'),
99 lang => $lang
102 return $template;
105 #---------------------------------------------------------------------------------------------------------
106 # FIXME - POD
107 sub themelanguage {
108 my ( $htdocs, $tmpl, $section, $query ) = @_;
110 # if (!$query) {
111 # warn "no query";
113 my $dbh = C4::Context->dbh;
114 my @languages;
115 my @themes;
116 if ( $section eq "intranet" ) {
117 @languages = split " ", C4::Context->preference("opaclanguages");
118 @themes = split " ", C4::Context->preference("template");
120 else {
122 # we are in the opac here, what im trying to do is let the individual user
123 # set the theme they want to use.
124 # and perhaps the them as well.
125 my $lang = $query->cookie('KohaOpacLanguage');
126 if ($lang) {
128 push @languages, $lang;
129 @themes = split " ", C4::Context->preference("opacthemes");
131 else {
132 @languages = split " ", C4::Context->preference("opaclanguages");
133 @themes = split " ", C4::Context->preference("opacthemes");
137 my ( $theme, $lang );
139 # searches through the themes and languages. First template it find it returns.
140 # Priority is for getting the theme right.
141 THEME:
142 foreach my $th (@themes) {
143 foreach my $la (@languages) {
144 for ( my $pass = 1 ; $pass <= 2 ; $pass += 1 ) {
145 $la =~ s/([-_])/ $1 eq '-'? '_': '-' /eg if $pass == 2;
146 if ( -e "$htdocs/$th/$la/".($section eq 'intranet'?"modules":"")."/$tmpl" ) {
147 $theme = $th;
148 $lang = $la;
149 last THEME;
151 last unless $la =~ /[-_]/;
155 if ( $theme and $lang ) {
156 return ( $theme, $lang );
158 else {
159 return ( 'prog', 'en' );
163 sub setlanguagecookie {
164 my ( $query, $language, $uri ) = @_;
165 my $cookie = $query->cookie(
166 -name => 'KohaOpacLanguage',
167 -value => $language,
168 -expires => ''
170 print $query->redirect(
171 -uri => $uri,
172 -cookie => $cookie
176 =item pagination_bar
178 pagination_bar($base_url, $nb_pages, $current_page, $startfrom_name)
180 Build an HTML pagination bar based on the number of page to display, the
181 current page and the url to give to each page link.
183 C<$base_url> is the URL for each page link. The
184 C<$startfrom_name>=page_number is added at the end of the each URL.
186 C<$nb_pages> is the total number of pages available.
188 C<$current_page> is the current page number. This page number won't become a
189 link.
191 This function returns HTML, without any language dependency.
193 =cut
195 sub pagination_bar {
196 my ( $base_url, $nb_pages, $current_page, $startfrom_name ) = @_;
198 # how many pages to show before and after the current page?
199 my $pages_around = 2;
201 my $url =
202 $base_url . ( $base_url =~ m/&/ ? '&amp;' : '?' ) . $startfrom_name . '=';
204 my $pagination_bar = '';
206 # current page detection
207 if ( not defined $current_page ) {
208 $current_page = 1;
211 # navigation bar useful only if more than one page to display !
212 if ( $nb_pages > 1 ) {
214 # link to first page?
215 if ( $current_page > 1 ) {
216 $pagination_bar .=
217 "\n" . '&nbsp;'
218 . '<a href="'
219 . $url
220 . '1" rel="start">'
221 . '&lt;&lt;' . '</a>';
223 else {
224 $pagination_bar .=
225 "\n" . '&nbsp;<span class="inactive">&lt;&lt;</span>';
228 # link on previous page ?
229 if ( $current_page > 1 ) {
230 my $previous = $current_page - 1;
232 $pagination_bar .=
233 "\n" . '&nbsp;'
234 . '<a href="'
235 . $url
236 . $previous
237 . '" rel="prev">' . '&lt;' . '</a>';
239 else {
240 $pagination_bar .=
241 "\n" . '&nbsp;<span class="inactive">&lt;</span>';
244 my $min_to_display = $current_page - $pages_around;
245 my $max_to_display = $current_page + $pages_around;
246 my $last_displayed_page = undef;
248 for my $page_number ( 1 .. $nb_pages ) {
249 if (
250 $page_number == 1
251 or $page_number == $nb_pages
252 or ( $page_number >= $min_to_display
253 and $page_number <= $max_to_display )
256 if ( defined $last_displayed_page
257 and $last_displayed_page != $page_number - 1 )
259 $pagination_bar .=
260 "\n" . '&nbsp;<span class="inactive">...</span>';
263 if ( $page_number == $current_page ) {
264 $pagination_bar .=
265 "\n" . '&nbsp;'
266 . '<span class="currentPage">'
267 . $page_number
268 . '</span>';
270 else {
271 $pagination_bar .=
272 "\n" . '&nbsp;'
273 . '<a href="'
274 . $url
275 . $page_number . '">'
276 . $page_number . '</a>';
278 $last_displayed_page = $page_number;
282 # link on next page?
283 if ( $current_page < $nb_pages ) {
284 my $next = $current_page + 1;
286 $pagination_bar .= "\n"
287 . '&nbsp;<a href="'
288 . $url
289 . $next
290 . '" rel="next">' . '&gt;' . '</a>';
292 else {
293 $pagination_bar .=
294 "\n" . '&nbsp;<span class="inactive">&gt;</span>';
297 # link to last page?
298 if ( $current_page != $nb_pages ) {
299 $pagination_bar .= "\n"
300 . '&nbsp;<a href="'
301 . $url
302 . $nb_pages
303 . '" rel="last">'
304 . '&gt;&gt;' . '</a>';
306 else {
307 $pagination_bar .=
308 "\n" . '&nbsp;<span class="inactive">&gt;&gt;</span>';
312 return $pagination_bar;
315 =item output_html_with_http_headers
317 &output_html_with_http_headers($query, $cookie, $html)
319 Outputs the HTML page $html with the appropriate HTTP headers,
320 with the authentication cookie $cookie and a Content-Type that
321 corresponds to the HTML page $html.
323 =cut
325 sub output_html_with_http_headers ($$$) {
326 my($query, $cookie, $html) = @_;
327 print $query->header(
328 -type => 'text/html',
329 -charset => 'UTF-8',
330 -cookie => $cookie,
331 ), $html;
334 END { } # module clean-up code here (global destructor)
337 __END__
339 =back
341 =head1 AUTHOR
343 Koha Developement team <info@koha.org>
345 =cut