← Index
Performance Profile   « block view • line view • sub view »
For opac/opac-main.pl
  Run on Thu Jul 17 22:22:09 2008
Reported on Thu Jul 17 22:22:21 2008

FileC4/Output.pm
Statements Executed51
Total Time0.004208 seconds

Subroutines — ordered by inclusive time then name
CallsInclusive
Time
Subroutine
10.09166C4::Output::gettemplate
10.00634C4::Output::output_html_with_http_headers
10.00338C4::Output::themelanguage
00C4::Output::BEGIN
00C4::Output::END
00C4::Output::is_ajax
00C4::Output::output_ajax_with_http_headers
00C4::Output::pagination_bar
00C4::Output::setlanguagecookie

LineStmts.Exclusive
Time
Avg.Code
1package C4::Output;
2
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
6
7# Copyright 2000-2002 Katipo Communications
8#
9# This file is part of Koha.
10#
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.
15#
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.
19#
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
23
24
25# NOTE: I'm pretty sure this module is deprecated in favor of
26# templates.
27
28use strict;
29
30use C4::Context;
31use C4::Languages qw(getTranslatedLanguages get_bidi regex_lang_subtags language_get_description accept_language );
32
33use HTML::Template::Pro;
34use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
35
36BEGIN {
37 # set the version for version checking
38 $VERSION = 3.03;
39 require Exporter;
40 @ISA = qw(Exporter);
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)]
46 );
47 push @EXPORT, qw(
48 &themelanguage &gettemplate setlanguagecookie pagination_bar
49 );
50 push @EXPORT, qw(
51 &output_html_with_http_headers
52 );
53}
54
55=head1 NAME
56
57C4::Output - Functions for managing templates
58
59=head1 FUNCTIONS
60
61=over 2
62
63=cut
64
65#FIXME: this is a quick fix to stop rc1 installing broken
66#Still trying to figure out the correct fix.
67my $path = C4::Context->config('intrahtdocs') . "/prog/en/includes/";
68
69#---------------------------------------------------------------------------------------------------------
70# FIXME - POD
71
# spent 0.09166s within C4::Output::gettemplate which was called: # 1 times (0.09166s) by C4::Auth::get_template_and_user at line 120 of C4/Auth.pm
sub gettemplate {
7212e-062e-06 my ( $tmplbase, $interface, $query ) = @_;
73100 ($query) or warn "no query in gettemplate";
74100 my $htdocs;
7519e-069e-06 if ( $interface ne "intranet" ) {
# spent 0.00002s making 1 calls to C4::Context::config
76 $htdocs = C4::Context->config('opachtdocs');
77 }
78 else {
79 $htdocs = C4::Context->config('intrahtdocs');
80 }
8119e-069e-06 my $path = C4::Context->preference('intranet_includes') || 'includes';
# spent 0.00029s making 1 calls to C4::Context::preference
8210.000010.00001 my ( $theme, $lang ) = themelanguage( $htdocs, $tmplbase, $interface, $query );
# spent 0.00338s making 1 calls to C4::Output::themelanguage
8318e-068e-06 my $opacstylesheet = C4::Context->preference('opacstylesheet');
# spent 0.00026s making 1 calls to C4::Context::preference
84
85 # if the template doesn't exist, load the English one as a last resort
8617e-067e-06 my $filename = "$htdocs/$theme/$lang/modules/$tmplbase";
8719e-069e-06 unless (-f $filename) {
88 $lang = 'en';
89 $filename = "$htdocs/$theme/$lang/modules/$tmplbase";
90 }
9110.000020.00002 my $template = HTML::Template::Pro->new(
# spent 0.00009s making 1 calls to HTML::Template::Pro::new
92 filename => $filename,
93 die_on_bad_params => 1,
94 global_vars => 1,
95 case_sensitive => 1,
96 loop_context_vars => 1, # enable: __first__, __last__, __inner__, __odd__, __counter__
97 path => ["$htdocs/$theme/$lang/$path"]
98 );
9913e-063e-06 my $themelang=( $interface ne 'intranet' ? '/opac-tmpl' : '/intranet-tmpl' )
100 . "/$theme/$lang";
10110.000030.00003 $template->param(
# spent 0.00106s making 4 calls to C4::Context::preference, avg 0.00026s/call # spent 0.00006s making 1 calls to HTML::Template::Pro::param
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' ),
105 theme => $theme,
106 opacstylesheet => $opacstylesheet,
107 opaccolorstylesheet => C4::Context->preference('opaccolorstylesheet'),
108 opacsmallimage => C4::Context->preference('opacsmallimage'),
109 lang => $lang
110 );
111
112 # Bidirectionality
11318e-068e-06 my $current_lang = regex_lang_subtags($lang);
# spent 0.00023s making 1 calls to C4::Languages::regex_lang_subtags
11411e-061e-06 my $bidi;
11511e-061e-06 $bidi = get_bidi($current_lang->{script}) if $current_lang->{script};
116 # Languages
11710.000010.00001 my $languages_loop = getTranslatedLanguages($interface,$theme,$lang);
# spent 0.08613s making 1 calls to C4::Languages::getTranslatedLanguages
11810.000010.00001 $template->param(
# spent 0.00003s making 1 calls to HTML::Template::Pro::param
119 languages_loop => $languages_loop,
120 bidi => $bidi
121 ) unless @$languages_loop<2;
122
12315e-065e-06 return $template;
124}
125
126#---------------------------------------------------------------------------------------------------------
127# FIXME - POD
128
# spent 0.00338s within C4::Output::themelanguage which was called: # 1 times (0.00338s) by C4::Output::gettemplate at line 82 of C4/Output.pm
sub themelanguage {
12912e-062e-06 my ( $htdocs, $tmpl, $interface, $query ) = @_;
13011e-061e-06 ($query) or warn "no query in themelanguage";
131
132 # Set some defaults for language and theme
133 # First, check the user's preferences
13411e-061e-06 my $lang;
13512e-062e-06 my $http_env = $ENV{HTTP_ACCEPT_LANGUAGE};
13611e-061e-06 $http_env =~ m/(\w+-*\w*),/;
13712e-062e-06 my $language_preference = $1;
13811e-051e-05 my $http_accept_language = regex_lang_subtags($language_preference)->{language};
# spent 0.00035s making 1 calls to C4::Languages::regex_lang_subtags
139100 if ($http_accept_language) {
140 $lang = accept_language($http_accept_language,getTranslatedLanguages($interface,'prog'));
141 }
142 # But, if there's a cookie set, obey it
14310.000010.00001 $lang = $query->cookie('KohaOpacLanguage') if $query->cookie('KohaOpacLanguage');
# spent 0.00027s making 1 calls to CGI::AUTOLOAD
144 # Fall back to English
14511e-061e-06 my @languages;
14614e-064e-06 if ($interface eq 'intranet') {
147 @languages = split ",", C4::Context->preference("language");
148 } else {
14910.000020.00002 @languages = split ",", C4::Context->preference("opaclanguages");
# spent 0.00036s making 1 calls to C4::Context::preference
150 }
15111e-061e-06 if ($lang){
152 @languages=($lang,@languages);
153 } else {
15411e-061e-06 $lang = $languages[0];
155 }
15611e-061e-06 my $theme = 'prog'; # in the event of theme failure default to 'prog' -fbcit
15718e-068e-06 my $dbh = C4::Context->dbh;
# spent 0.00014s making 1 calls to C4::Context::dbh
15811e-061e-06 my @themes;
15911e-061e-06 if ( $interface eq "intranet" ) {
160 @themes = split " ", C4::Context->preference("template");
161 }
162 else {
163 # we are in the opac here, what im trying to do is let the individual user
164 # set the theme they want to use.
165 # and perhaps the them as well.
166 #my $lang = $query->cookie('KohaOpacLanguage');
16719e-069e-06 @themes = split " ", C4::Context->preference("opacthemes");
# spent 0.00034s making 1 calls to C4::Context::preference
168 }
169
170 # searches through the themes and languages. First template it find it returns.
171 # Priority is for getting the theme right.
172 THEME:
17311e-061e-06 foreach my $th (@themes) {
174100 foreach my $la (@languages) {
175 #for ( my $pass = 1 ; $pass <= 2 ; $pass += 1 ) {
176 # warn "$htdocs/$th/$la/modules/$interface-"."tmpl";
177 #$la =~ s/([-_])/ $1 eq '-'? '_': '-' /eg if $pass == 2;
17810.000020.00002 if ( -e "$htdocs/$th/$la/modules/$tmpl") {
179 #".($interface eq 'intranet'?"modules":"")."/$tmpl" ) {
180100 $theme = $th;
181100 $lang = $la;
18213e-063e-06 last THEME;
183 }
184 last unless $la =~ /[-_]/;
185 #}
186 }
187 }
18814e-064e-06 return ( $theme, $lang );
189}
190
191sub setlanguagecookie {
192 my ( $query, $language, $uri ) = @_;
193 my $cookie = $query->cookie(
194 -name => 'KohaOpacLanguage',
195 -value => $language,
196 -expires => ''
197 );
198 print $query->redirect(
199 -uri => $uri,
200 -cookie => $cookie
201 );
202}
203
204=item pagination_bar
205
206 pagination_bar($base_url, $nb_pages, $current_page, $startfrom_name)
207
208Build an HTML pagination bar based on the number of page to display, the
209current page and the url to give to each page link.
210
211C<$base_url> is the URL for each page link. The
212C<$startfrom_name>=page_number is added at the end of the each URL.
213
214C<$nb_pages> is the total number of pages available.
215
216C<$current_page> is the current page number. This page number won't become a
217link.
218
219This function returns HTML, without any language dependency.
220
221=cut
222
223sub pagination_bar {
224 my $base_url = (@_ ? shift : $ENV{SCRIPT_NAME} . $ENV{QUERY_STRING}) or return undef;
225 my $nb_pages = (@_) ? shift : 1;
226 my $current_page = (@_) ? shift : undef; # delay default until later
227 my $startfrom_name = (@_) ? shift : 'page';
228
229 # how many pages to show before and after the current page?
230 my $pages_around = 2;
231
232 my $delim = qr/\&(?:amp;)?|;/; # "non memory" cluster: no backreference
233 $base_url =~ s/$delim*\b$startfrom_name=(\d+)//g; # remove previous pagination var
234 unless (defined $current_page and $current_page > 0 and $current_page <= $nb_pages) {
235 $current_page = ($1) ? $1 : 1; # pull current page from param in URL, else default to 1
236 # $debug and # FIXME: use C4::Debug;
237 # warn "with QUERY_STRING:" .$ENV{QUERY_STRING}. "\ncurrent_page:$current_page\n1:$1 2:$2 3:$3";
238 }
239 $base_url =~ s/($delim)+/$1/g; # compress duplicate delims
240 $base_url =~ s/$delim;//g; # remove empties
241 $base_url =~ s/$delim$//; # remove trailing delim
242
243 my $url = $base_url . (($base_url =~ m/$delim/ or $base_url =~ m/\?/) ? '&amp;' : '?' ) . $startfrom_name . '=';
244 my $pagination_bar = '';
245
246 # navigation bar useful only if more than one page to display !
247 if ( $nb_pages > 1 ) {
248
249 # link to first page?
250 if ( $current_page > 1 ) {
251 $pagination_bar .=
252 "\n" . '&nbsp;'
253 . '<a href="'
254 . $url
255 . '1" rel="start">'
256 . '&lt;&lt;' . '</a>';
257 }
258 else {
259 $pagination_bar .=
260 "\n" . '&nbsp;<span class="inactive">&lt;&lt;</span>';
261 }
262
263 # link on previous page ?
264 if ( $current_page > 1 ) {
265 my $previous = $current_page - 1;
266
267 $pagination_bar .=
268 "\n" . '&nbsp;'
269 . '<a href="'
270 . $url
271 . $previous
272 . '" rel="prev">' . '&lt;' . '</a>';
273 }
274 else {
275 $pagination_bar .=
276 "\n" . '&nbsp;<span class="inactive">&lt;</span>';
277 }
278
279 my $min_to_display = $current_page - $pages_around;
280 my $max_to_display = $current_page + $pages_around;
281 my $last_displayed_page = undef;
282
283 for my $page_number ( 1 .. $nb_pages ) {
284 if (
285 $page_number == 1
286 or $page_number == $nb_pages
287 or ( $page_number >= $min_to_display
288 and $page_number <= $max_to_display )
289 )
290 {
291 if ( defined $last_displayed_page
292 and $last_displayed_page != $page_number - 1 )
293 {
294 $pagination_bar .=
295 "\n" . '&nbsp;<span class="inactive">...</span>';
296 }
297
298 if ( $page_number == $current_page ) {
299 $pagination_bar .=
300 "\n" . '&nbsp;'
301 . '<span class="currentPage">'
302 . $page_number
303 . '</span>';
304 }
305 else {
306 $pagination_bar .=
307 "\n" . '&nbsp;'
308 . '<a href="'
309 . $url
310 . $page_number . '">'
311 . $page_number . '</a>';
312 }
313 $last_displayed_page = $page_number;
314 }
315 }
316
317 # link on next page?
318 if ( $current_page < $nb_pages ) {
319 my $next = $current_page + 1;
320
321 $pagination_bar .= "\n"
322 . '&nbsp;<a href="'
323 . $url
324 . $next
325 . '" rel="next">' . '&gt;' . '</a>';
326 }
327 else {
328 $pagination_bar .=
329 "\n" . '&nbsp;<span class="inactive">&gt;</span>';
330 }
331
332 # link to last page?
333 if ( $current_page != $nb_pages ) {
334 $pagination_bar .= "\n"
335 . '&nbsp;<a href="'
336 . $url
337 . $nb_pages
338 . '" rel="last">'
339 . '&gt;&gt;' . '</a>';
340 }
341 else {
342 $pagination_bar .=
343 "\n" . '&nbsp;<span class="inactive">&gt;&gt;</span>';
344 }
345 }
346
347 return $pagination_bar;
348}
349
350=item output_html_with_http_headers
351
352 &output_html_with_http_headers($query, $cookie, $html[, $content_type])
353
354Outputs the HTML page $html with the appropriate HTTP headers,
355with the authentication cookie $cookie and a Content-Type that
356corresponds to the HTML page $html.
357
358If the optional C<$content_type> parameter is called, set the
359response's Content-Type to that value instead of "text/html".
360
361=cut
362
363
# spent 0.00634s within C4::Output::output_html_with_http_headers which was called: # 1 times (0.00634s) at line 59 of opac/opac-main.pl
sub output_html_with_http_headers ($$$;$) {
36411e-061e-06 my $query = shift;
36511e-061e-06 my $cookie = shift;
36613e-063e-06 my $html = shift;
36711e-061e-06 my $content_type = @_ ? shift : "text/html";
36813e-063e-06 $content_type = "text/html" unless $content_type =~ m!/!; # very basic sanity check
36910.003940.00394 print $query->header(
# spent 0.00080s making 1 calls to CGI::AUTOLOAD
370 -type => $content_type,
371 -charset => 'UTF-8',
372 -cookie => $cookie,
373 -Pragma => 'no-cache',
374 -'Cache-Control' => 'no-cache',
375 ), $html;
376}
377
378sub output_ajax_with_http_headers ($$) {
379 my ($query, $js) = @_;
380 print $query->header(
381 -type => 'text/javascript',
382 -charset => 'UTF-8',
383 -Pragma => 'no-cache',
384 -'Cache-Control' => 'no-cache',
385 -expires =>'-1d',
386 ), $js;
387}
388
389sub is_ajax () {
390 my $x_req = $ENV{HTTP_X_REQUESTED_WITH};
391 return ($x_req and $x_req =~ /XMLHttpRequest/i) ? 1 : 0;
392}
393
39411e-061e-06END { } # module clean-up code here (global destructor)
395
3961;
397__END__
398
399=back
400
401=head1 AUTHOR
402
403Koha Developement team <info@koha.org>
404
405=cut