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
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
32 use C4::Languages qw(getTranslatedLanguages get_bidi regex_lang_subtags language_get_description accept_language );
34 use HTML::Template::Pro;
35 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
38 # set the version for version checking
42 @EXPORT_OK = qw(&is_ajax ajax_fail); # More stuff should go here instead
43 %EXPORT_TAGS = ( all =>[qw(&themelanguage &gettemplate setlanguagecookie pagination_bar
44 &output_with_http_headers &output_html_with_http_headers)],
45 ajax =>[qw(&output_with_http_headers is_ajax)],
46 html =>[qw(&output_with_http_headers &output_html_with_http_headers)]
49 &themelanguage &gettemplate setlanguagecookie pagination_bar
52 &output_html_with_http_headers &output_with_http_headers
58 C4::Output - Functions for managing templates
66 #FIXME: this is a quick fix to stop rc1 installing broken
67 #Still trying to figure out the correct fix.
68 my $path = C4::Context->config('intrahtdocs') . "/prog/en/includes/";
70 #---------------------------------------------------------------------------------------------------------
73 my ( $tmplbase, $interface, $query ) = @_;
74 ($query) or warn "no query in gettemplate";
76 if ( $interface ne "intranet" ) {
77 $htdocs = C4::Context->config('opachtdocs');
80 $htdocs = C4::Context->config('intrahtdocs');
82 my $path = C4::Context->preference('intranet_includes') || 'includes';
83 my ( $theme, $lang ) = themelanguage( $htdocs, $tmplbase, $interface, $query );
85 # if the template doesn't exist, load the English one as a last resort
86 my $filename = "$htdocs/$theme/$lang/modules/$tmplbase";
87 unless (-f $filename) {
89 $filename = "$htdocs/$theme/$lang/modules/$tmplbase";
91 my $template = HTML::Template::Pro->new(
92 filename => $filename,
93 die_on_bad_params => 1,
96 loop_context_vars => 1, # enable: __first__, __last__, __inner__, __odd__, __counter__
97 path => ["$htdocs/$theme/$lang/$path"]
99 my $themelang=( $interface ne 'intranet' ? '/opac-tmpl' : '/intranet-tmpl' )
102 themelang => $themelang,
103 yuipath => (C4::Context->preference("yuipath") eq "local"?"$themelang/lib/yui":C4::Context->preference("yuipath")),
104 interface => ( $interface ne 'intranet' ? '/opac-tmpl' : '/intranet-tmpl' ),
110 my $current_lang = regex_lang_subtags($lang);
112 $bidi = get_bidi($current_lang->{script}) if $current_lang->{script};
114 my $languages_loop = getTranslatedLanguages($interface,$theme,$lang);
115 my $num_languages_enabled = 0;
116 foreach my $lang (@$languages_loop) {
117 foreach my $sublang (@{ $lang->{'sublanguages_loop'} }) {
118 $num_languages_enabled++ if $sublang->{enabled};
122 languages_loop => $languages_loop,
124 one_language_enabled => ($num_languages_enabled <= 1) ? 1 : 0, # deal with zero enabled langs as well
125 ) unless @$languages_loop<2;
130 #---------------------------------------------------------------------------------------------------------
133 my ( $htdocs, $tmpl, $interface, $query ) = @_;
134 ($query) or warn "no query in themelanguage";
136 # Set some defaults for language and theme
137 # First, check the user's preferences
139 my $http_accept_language = $ENV{ HTTP_ACCEPT_LANGUAGE };
140 $lang = accept_language( $http_accept_language,
141 getTranslatedLanguages($interface,'prog') )
142 if $http_accept_language;
143 # But, if there's a cookie set, obey it
144 $lang = $query->cookie('KohaOpacLanguage') if $query->cookie('KohaOpacLanguage');
145 # Fall back to English
147 if ($interface eq 'intranet') {
148 @languages = split ",", C4::Context->preference("language");
150 @languages = split ",", C4::Context->preference("opaclanguages");
153 @languages=($lang,@languages);
155 $lang = $languages[0];
157 my $theme = 'prog'; # in the event of theme failure default to 'prog' -fbcit
158 my $dbh = C4::Context->dbh;
160 if ( $interface eq "intranet" ) {
161 @themes = split " ", C4::Context->preference("template");
164 # we are in the opac here, what im trying to do is let the individual user
165 # set the theme they want to use.
166 # and perhaps the them as well.
167 #my $lang = $query->cookie('KohaOpacLanguage');
168 @themes = split " ", C4::Context->preference("opacthemes");
171 # searches through the themes and languages. First template it find it returns.
172 # Priority is for getting the theme right.
174 foreach my $th (@themes) {
175 foreach my $la (@languages) {
176 #for ( my $pass = 1 ; $pass <= 2 ; $pass += 1 ) {
177 # warn "$htdocs/$th/$la/modules/$interface-"."tmpl";
178 #$la =~ s/([-_])/ $1 eq '-'? '_': '-' /eg if $pass == 2;
179 if ( -e "$htdocs/$th/$la/modules/$tmpl") {
180 #".($interface eq 'intranet'?"modules":"")."/$tmpl" ) {
185 last unless $la =~ /[-_]/;
189 return ( $theme, $lang );
192 sub setlanguagecookie {
193 my ( $query, $language, $uri ) = @_;
194 my $cookie = $query->cookie(
195 -name => 'KohaOpacLanguage',
199 print $query->redirect(
207 pagination_bar($base_url, $nb_pages, $current_page, $startfrom_name)
209 Build an HTML pagination bar based on the number of page to display, the
210 current page and the url to give to each page link.
212 C<$base_url> is the URL for each page link. The
213 C<$startfrom_name>=page_number is added at the end of the each URL.
215 C<$nb_pages> is the total number of pages available.
217 C<$current_page> is the current page number. This page number won't become a
220 This function returns HTML, without any language dependency.
225 my $base_url = (@_ ? shift : $ENV{SCRIPT_NAME} . $ENV{QUERY_STRING}) or return undef;
226 my $nb_pages = (@_) ? shift : 1;
227 my $current_page = (@_) ? shift : undef; # delay default until later
228 my $startfrom_name = (@_) ? shift : 'page';
230 # how many pages to show before and after the current page?
231 my $pages_around = 2;
233 my $delim = qr/\&(?:amp;)?|;/; # "non memory" cluster: no backreference
234 $base_url =~ s/$delim*\b$startfrom_name=(\d+)//g; # remove previous pagination var
235 unless (defined $current_page and $current_page > 0 and $current_page <= $nb_pages) {
236 $current_page = ($1) ? $1 : 1; # pull current page from param in URL, else default to 1
237 # $debug and # FIXME: use C4::Debug;
238 # warn "with QUERY_STRING:" .$ENV{QUERY_STRING}. "\ncurrent_page:$current_page\n1:$1 2:$2 3:$3";
240 $base_url =~ s/($delim)+/$1/g; # compress duplicate delims
241 $base_url =~ s/$delim;//g; # remove empties
242 $base_url =~ s/$delim$//; # remove trailing delim
244 my $url = $base_url . (($base_url =~ m/$delim/ or $base_url =~ m/\?/) ? '&' : '?' ) . $startfrom_name . '=';
245 my $pagination_bar = '';
247 # navigation bar useful only if more than one page to display !
248 if ( $nb_pages > 1 ) {
250 # link to first page?
251 if ( $current_page > 1 ) {
257 . '<<' . '</a>';
261 "\n" . ' <span class="inactive"><<</span>';
264 # link on previous page ?
265 if ( $current_page > 1 ) {
266 my $previous = $current_page - 1;
273 . '" rel="prev">' . '<' . '</a>';
277 "\n" . ' <span class="inactive"><</span>';
280 my $min_to_display = $current_page - $pages_around;
281 my $max_to_display = $current_page + $pages_around;
282 my $last_displayed_page = undef;
284 for my $page_number ( 1 .. $nb_pages ) {
287 or $page_number == $nb_pages
288 or ( $page_number >= $min_to_display
289 and $page_number <= $max_to_display )
292 if ( defined $last_displayed_page
293 and $last_displayed_page != $page_number - 1 )
296 "\n" . ' <span class="inactive">...</span>';
299 if ( $page_number == $current_page ) {
302 . '<span class="currentPage">'
311 . $page_number . '">'
312 . $page_number . '</a>';
314 $last_displayed_page = $page_number;
319 if ( $current_page < $nb_pages ) {
320 my $next = $current_page + 1;
322 $pagination_bar .= "\n"
326 . '" rel="next">' . '>' . '</a>';
330 "\n" . ' <span class="inactive">></span>';
334 if ( $current_page != $nb_pages ) {
335 $pagination_bar .= "\n"
340 . '>>' . '</a>';
344 "\n" . ' <span class="inactive">>></span>';
348 return $pagination_bar;
351 =item output_with_http_headers
353 &output_with_http_headers($query, $cookie, $data, $content_type[, $status])
355 Outputs $data with the appropriate HTTP headers,
356 the authentication cookie $cookie and a Content-Type specified in
359 If applicable, $cookie can be undef, and it will not be sent.
361 $content_type is one of the following: 'html', 'js', 'json', 'xml', 'rss', or 'atom'.
363 $status is an HTTP status message, like '403 Authentication Required'. It defaults to '200 OK'.
367 sub output_with_http_headers($$$$;$) {
368 my ( $query, $cookie, $data, $content_type, $status ) = @_;
369 $status ||= '200 OK';
371 my %content_type_map = (
372 'html' => 'text/html',
373 'js' => 'text/javascript',
374 'json' => 'application/json',
376 # NOTE: not using application/atom+xml or application/rss+xml because of
377 # Internet Explorer 6; see bug 2078.
382 die "Unknown content type '$content_type'" if ( !defined( $content_type_map{$content_type} ) );
384 type => $content_type_map{$content_type},
387 Pragma => 'no-cache',
388 'Cache-Control' => 'no-cache',
390 $options->{cookie} = $cookie if $cookie;
391 if ($content_type eq 'html') { # guaranteed to be one of the content_type_map keys, else we'd have died
392 $options->{'Content-Style-Type' } = 'text/css';
393 $options->{'Content-Script-Type'} = 'text/javascript';
395 print $query->header($options), $data;
398 sub output_html_with_http_headers ($$$) {
399 my ( $query, $cookie, $data ) = @_;
400 output_with_http_headers( $query, $cookie, $data, 'html' );
404 my $x_req = $ENV{HTTP_X_REQUESTED_WITH};
405 return ( $x_req and $x_req =~ /XMLHttpRequest/i ) ? 1 : 0;
408 END { } # module clean-up code here (global destructor)
417 Koha Developement team <info@koha.org>