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
31 use C4::Languages qw(getTranslatedLanguages get_bidi regex_lang_subtags language_get_description accept_language );
33 use HTML::Template::Pro;
34 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
37 # set the version for version checking
41 @EXPORT_OK = qw(&output_ajax_with_http_headers &is_ajax); # More stuff should go here instead
42 %EXPORT_TAGS = ( all =>[qw(&themelanguage &gettemplate setlanguagecookie pagination_bar
43 &output_ajax_with_http_headers &output_html_with_http_headers)],
44 ajax =>[qw(&output_ajax_with_http_headers is_ajax)],
45 html =>[qw(&output_html_with_http_headers)]
48 &themelanguage &gettemplate setlanguagecookie getlanguagecookie pagination_bar
51 &output_html_with_http_headers
57 C4::Output - Functions for managing templates
65 #FIXME: this is a quick fix to stop rc1 installing broken
66 #Still trying to figure out the correct fix.
67 my $path = C4::Context->config('intrahtdocs') . "/prog/en/includes/";
69 #---------------------------------------------------------------------------------------------------------
72 my ( $tmplbase, $interface, $query ) = @_;
73 ($query) or warn "no query in gettemplate";
75 if ( $interface ne "intranet" ) {
76 $htdocs = C4::Context->config('opachtdocs');
79 $htdocs = C4::Context->config('intrahtdocs');
81 my $path = C4::Context->preference('intranet_includes') || 'includes';
82 my ( $theme, $lang ) = themelanguage( $htdocs, $tmplbase, $interface, $query );
83 my $opacstylesheet = C4::Context->preference('opacstylesheet');
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 # But, if there's a cookie set, obey it
142 $lang = $query->cookie('KohaOpacLanguage') if (defined $query and $query->cookie('KohaOpacLanguage'));
143 # Fall back to English
145 if ($interface eq 'intranet') {
146 @languages = split ",", C4::Context->preference("language");
148 @languages = split ",", C4::Context->preference("opaclanguages");
151 # Ricardo Dias Marques
153 # - If we have a language set in the Cookie, we'll accept it if it exists in the list of Translated Languages
154 # - If we don't have a language set in the Cookie, we'll try to use the one set in the browser (available
155 # in $http_accept_language) if it also exists in the list of Translated Languages
158 $lang = accept_language( $lang,
159 getTranslatedLanguages($interface,'prog') );
163 $lang = accept_language( $http_accept_language,
164 getTranslatedLanguages($interface,'prog') )
165 if $http_accept_language;
168 if (grep(/^$lang$/, @languages)){
169 @languages=($lang,@languages);
171 $lang = $languages[0];
174 my $theme = 'prog'; # in the event of theme failure default to 'prog' -fbcit
175 my $dbh = C4::Context->dbh;
176 if ( $interface eq "intranet" ) {
177 $theme = C4::Context->preference("template");
180 # we are in the opac here, what im trying to do is let the individual user
181 # set the theme they want to use.
182 # and perhaps the them as well.
183 #my $lang = $query->cookie('KohaOpacLanguage');
184 $theme = C4::Context->preference("opacthemes");
187 # searches through the themes and languages. First template it find it returns.
188 # Priority is for getting the theme right.
190 foreach my $la (@languages) {
191 if ( -e "$htdocs/$theme/$la/modules/$tmpl") {
195 last unless $la =~ /[-_]/;
197 return ( $theme, $lang );
200 sub setlanguagecookie {
201 my ( $query, $language, $uri ) = @_;
202 my $cookie = $query->cookie(
203 -name => 'KohaOpacLanguage',
207 print $query->redirect(
213 sub getlanguagecookie {
216 if ($query->cookie('KohaOpacLanguage')){
217 $lang = $query->cookie('KohaOpacLanguage') ;
219 $lang = $ENV{HTTP_ACCEPT_LANGUAGE};
222 $lang = substr($lang, 0, 2);
229 pagination_bar($base_url, $nb_pages, $current_page, $startfrom_name)
231 Build an HTML pagination bar based on the number of page to display, the
232 current page and the url to give to each page link.
234 C<$base_url> is the URL for each page link. The
235 C<$startfrom_name>=page_number is added at the end of the each URL.
237 C<$nb_pages> is the total number of pages available.
239 C<$current_page> is the current page number. This page number won't become a
242 This function returns HTML, without any language dependency.
247 my $base_url = (@_ ? shift : $ENV{SCRIPT_NAME} . $ENV{QUERY_STRING}) or return undef;
248 my $nb_pages = (@_) ? shift : 1;
249 my $current_page = (@_) ? shift : undef; # delay default until later
250 my $startfrom_name = (@_) ? shift : 'page';
252 # how many pages to show before and after the current page?
253 my $pages_around = 2;
255 my $delim = qr/\&(?:amp;)?|;/; # "non memory" cluster: no backreference
256 $base_url =~ s/$delim*\b$startfrom_name=(\d+)//g; # remove previous pagination var
257 unless (defined $current_page and $current_page > 0 and $current_page <= $nb_pages) {
258 $current_page = ($1) ? $1 : 1; # pull current page from param in URL, else default to 1
259 # $debug and # FIXME: use C4::Debug;
260 # warn "with QUERY_STRING:" .$ENV{QUERY_STRING}. "\ncurrent_page:$current_page\n1:$1 2:$2 3:$3";
262 $base_url =~ s/($delim)+/$1/g; # compress duplicate delims
263 $base_url =~ s/$delim;//g; # remove empties
264 $base_url =~ s/$delim$//; # remove trailing delim
266 my $url = $base_url . (($base_url =~ m/$delim/ or $base_url =~ m/\?/) ? '&' : '?' ) . $startfrom_name . '=';
267 my $pagination_bar = '';
269 # navigation bar useful only if more than one page to display !
270 if ( $nb_pages > 1 ) {
272 # link to first page?
273 if ( $current_page > 1 ) {
279 . '<<' . '</a>';
283 "\n" . ' <span class="inactive"><<</span>';
286 # link on previous page ?
287 if ( $current_page > 1 ) {
288 my $previous = $current_page - 1;
295 . '" rel="prev">' . '<' . '</a>';
299 "\n" . ' <span class="inactive"><</span>';
302 my $min_to_display = $current_page - $pages_around;
303 my $max_to_display = $current_page + $pages_around;
304 my $last_displayed_page = undef;
306 for my $page_number ( 1 .. $nb_pages ) {
309 or $page_number == $nb_pages
310 or ( $page_number >= $min_to_display
311 and $page_number <= $max_to_display )
314 if ( defined $last_displayed_page
315 and $last_displayed_page != $page_number - 1 )
318 "\n" . ' <span class="inactive">...</span>';
321 if ( $page_number == $current_page ) {
324 . '<span class="currentPage">'
333 . $page_number . '">'
334 . $page_number . '</a>';
336 $last_displayed_page = $page_number;
341 if ( $current_page < $nb_pages ) {
342 my $next = $current_page + 1;
344 $pagination_bar .= "\n"
348 . '" rel="next">' . '>' . '</a>';
352 "\n" . ' <span class="inactive">></span>';
356 if ( $current_page != $nb_pages ) {
357 $pagination_bar .= "\n"
362 . '>>' . '</a>';
366 "\n" . ' <span class="inactive">>></span>';
370 return $pagination_bar;
373 =item output_html_with_http_headers
375 &output_html_with_http_headers($query, $cookie, $html[, $content_type])
377 Outputs the HTML page $html with the appropriate HTTP headers,
378 with the authentication cookie $cookie and a Content-Type that
379 corresponds to the HTML page $html.
381 If the optional C<$content_type> parameter is called, set the
382 response's Content-Type to that value instead of "text/html".
386 sub output_with_http_headers($$$$;$) {
387 my ( $query, $cookie, $data, $content_type, $status ) = @_;
388 $status ||= '200 OK';
390 my %content_type_map = (
391 'html' => 'text/html',
392 'js' => 'text/javascript',
393 'json' => 'application/json',
395 # NOTE: not using application/atom+xml or application/rss+xml because of
396 # Internet Explorer 6; see bug 2078.
401 die "Unknown content type '$content_type'" if ( !defined( $content_type_map{$content_type} ) );
403 type => $content_type_map{$content_type},
406 Pragma => 'no-cache',
407 'Cache-Control' => 'no-cache',
409 $options->{cookie} = $cookie if $cookie;
410 if ($content_type eq 'html') { # guaranteed to be one of the content_type_map keys, else we'd have died
411 $options->{'Content-Style-Type' } = 'text/css';
412 $options->{'Content-Script-Type'} = 'text/javascript';
414 # remove SUDOC specific NSB NSE
415 $data =~ s/\x{C2}\x{98}|\x{C2}\x{9C}/ /g;
416 $data =~ s/\x{C2}\x{88}|\x{C2}\x{89}/ /g;
417 print $query->header($options), $data;
420 sub output_ajax_with_http_headers ($$) {
421 my ($query, $js) = @_;
422 print $query->header(
423 -type => 'text/javascript',
425 -Pragma => 'no-cache',
426 -'Cache-Control' => 'no-cache',
432 my $x_req = $ENV{HTTP_X_REQUESTED_WITH};
433 return ($x_req and $x_req =~ /XMLHttpRequest/i) ? 1 : 0;
436 END { } # module clean-up code here (global destructor)
445 Koha Developement team <info@koha.org>