Bug 24316: (follow up) Fix es-ES web installer
[koha.git] / C4 / Output.pm
blobf8f8e13583a3d3cc3d135ada38326e7fc29b7af6
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
12 # under the terms of the GNU General Public License as published by
13 # the Free Software Foundation; either version 3 of the License, or
14 # (at your option) any later version.
16 # Koha is distributed in the hope that it will be useful, but
17 # WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 # GNU General Public License for more details.
21 # You should have received a copy of the GNU General Public License
22 # along with Koha; if not, see <http://www.gnu.org/licenses>.
25 # NOTE: I'm pretty sure this module is deprecated in favor of
26 # templates.
28 use strict;
29 #use warnings; FIXME - Bug 2505
31 use URI::Escape;
32 use Scalar::Util qw( looks_like_number );
34 use C4::Auth qw(get_template_and_user);
35 use C4::Context;
36 use C4::Templates;
38 use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
40 BEGIN {
41 require Exporter;
43 @ISA = qw(Exporter);
44 @EXPORT_OK = qw(&is_ajax ajax_fail); # More stuff should go here instead
45 %EXPORT_TAGS = ( all =>[qw(setlanguagecookie pagination_bar parametrized_url
46 &output_with_http_headers &output_ajax_with_http_headers &output_html_with_http_headers)],
47 ajax =>[qw(&output_with_http_headers &output_ajax_with_http_headers is_ajax)],
48 html =>[qw(&output_with_http_headers &output_html_with_http_headers)]
50 push @EXPORT, qw(
51 setlanguagecookie getlanguagecookie pagination_bar parametrized_url
53 push @EXPORT, qw(
54 &output_html_with_http_headers &output_ajax_with_http_headers &output_with_http_headers
55 &output_and_exit_if_error &output_and_exit &output_error
60 =head1 NAME
62 C4::Output - Functions for managing output, is slowly being deprecated
64 =head1 FUNCTIONS
66 =over 2
68 =item pagination_bar
70 pagination_bar($base_url, $nb_pages, $current_page, $startfrom_name)
72 Build an HTML pagination bar based on the number of page to display, the
73 current page and the url to give to each page link.
75 C<$base_url> is the URL for each page link. The
76 C<$startfrom_name>=page_number is added at the end of the each URL.
78 C<$nb_pages> is the total number of pages available.
80 C<$current_page> is the current page number. This page number won't become a
81 link.
83 This function returns HTML, without any language dependency.
85 =cut
87 sub pagination_bar {
88 my $base_url = (@_ ? shift : return);
89 my $nb_pages = (@_) ? shift : 1;
90 my $current_page = (@_) ? shift : undef; # delay default until later
91 my $startfrom_name = (@_) ? shift : 'page';
92 my $additional_parameters = shift || {};
94 $current_page = looks_like_number($current_page) ? $current_page : undef;
95 $nb_pages = looks_like_number($nb_pages) ? $nb_pages : undef;
97 # how many pages to show before and after the current page?
98 my $pages_around = 2;
100 my $delim = qr/\&(?:amp;)?|;/; # "non memory" cluster: no backreference
101 $base_url =~ s/$delim*\b$startfrom_name=(\d+)//g; # remove previous pagination var
102 unless (defined $current_page and $current_page > 0 and $current_page <= $nb_pages) {
103 $current_page = ($1) ? $1 : 1; # pull current page from param in URL, else default to 1
104 # $debug and # FIXME: use C4::Debug;
105 # warn "with QUERY_STRING:" .$ENV{QUERY_STRING}. "\ncurrent_page:$current_page\n1:$1 2:$2 3:$3";
107 $base_url =~ s/($delim)+/$1/g; # compress duplicate delims
108 $base_url =~ s/$delim;//g; # remove empties
109 $base_url =~ s/$delim$//; # remove trailing delim
111 my $url = $base_url . (($base_url =~ m/$delim/ or $base_url =~ m/\?/) ? '&amp;' : '?' ) . $startfrom_name . '=';
112 my $url_suffix;
113 while ( my ( $k, $v ) = each %$additional_parameters ) {
114 $url_suffix .= '&amp;' . URI::Escape::uri_escape_utf8($k) . '=' . URI::Escape::uri_escape_utf8($v);
116 my $pagination_bar = '';
118 # navigation bar useful only if more than one page to display !
119 if ( $nb_pages > 1 ) {
121 # link to first page?
122 if ( $current_page > 1 ) {
123 $pagination_bar .=
124 "\n" . '&nbsp;'
125 . '<a href="'
126 . $url
127 . '1'
128 . $url_suffix
129 . '"rel="start">'
130 . '&lt;&lt;' . '</a>';
132 else {
133 $pagination_bar .=
134 "\n" . '&nbsp;<span class="inactive">&lt;&lt;</span>';
137 # link on previous page ?
138 if ( $current_page > 1 ) {
139 my $previous = $current_page - 1;
141 $pagination_bar .=
142 "\n" . '&nbsp;'
143 . '<a href="'
144 . $url
145 . $previous
146 . $url_suffix
147 . '" rel="prev">' . '&lt;' . '</a>';
149 else {
150 $pagination_bar .=
151 "\n" . '&nbsp;<span class="inactive">&lt;</span>';
154 my $min_to_display = $current_page - $pages_around;
155 my $max_to_display = $current_page + $pages_around;
156 my $last_displayed_page = undef;
158 for my $page_number ( 1 .. $nb_pages ) {
159 if (
160 $page_number == 1
161 or $page_number == $nb_pages
162 or ( $page_number >= $min_to_display
163 and $page_number <= $max_to_display )
166 if ( defined $last_displayed_page
167 and $last_displayed_page != $page_number - 1 )
169 $pagination_bar .=
170 "\n" . '&nbsp;<span class="inactive">...</span>';
173 if ( $page_number == $current_page ) {
174 $pagination_bar .=
175 "\n" . '&nbsp;'
176 . '<span class="currentPage">'
177 . $page_number
178 . '</span>';
180 else {
181 $pagination_bar .=
182 "\n" . '&nbsp;'
183 . '<a href="'
184 . $url
185 . $page_number
186 . $url_suffix
187 . '">'
188 . $page_number . '</a>';
190 $last_displayed_page = $page_number;
194 # link on next page?
195 if ( $current_page < $nb_pages ) {
196 my $next = $current_page + 1;
198 $pagination_bar .= "\n"
199 . '&nbsp;<a href="'
200 . $url
201 . $next
202 . $url_suffix
203 . '" rel="next">' . '&gt;' . '</a>';
205 else {
206 $pagination_bar .=
207 "\n" . '&nbsp;<span class="inactive">&gt;</span>';
210 # link to last page?
211 if ( $current_page != $nb_pages ) {
212 $pagination_bar .= "\n"
213 . '&nbsp;<a href="'
214 . $url
215 . $nb_pages
216 . $url_suffix
217 . '" rel="last">'
218 . '&gt;&gt;' . '</a>';
220 else {
221 $pagination_bar .=
222 "\n" . '&nbsp;<span class="inactive">&gt;&gt;</span>';
226 return $pagination_bar;
229 =item output_with_http_headers
231 &output_with_http_headers($query, $cookie, $data, $content_type[, $status[, $extra_options]])
233 Outputs $data with the appropriate HTTP headers,
234 the authentication cookie $cookie and a Content-Type specified in
235 $content_type.
237 If applicable, $cookie can be undef, and it will not be sent.
239 $content_type is one of the following: 'html', 'js', 'json', 'xml', 'rss', or 'atom'.
241 $status is an HTTP status message, like '403 Authentication Required'. It defaults to '200 OK'.
243 $extra_options is hashref. If the key 'force_no_caching' is present and has
244 a true value, the HTTP headers include directives to force there to be no
245 caching whatsoever.
247 =cut
249 sub output_with_http_headers {
250 my ( $query, $cookie, $data, $content_type, $status, $extra_options ) = @_;
251 $status ||= '200 OK';
253 $extra_options //= {};
255 my %content_type_map = (
256 'html' => 'text/html',
257 'js' => 'text/javascript',
258 'json' => 'application/json',
259 'xml' => 'text/xml',
260 # NOTE: not using application/atom+xml or application/rss+xml because of
261 # Internet Explorer 6; see bug 2078.
262 'rss' => 'text/xml',
263 'atom' => 'text/xml'
266 die "Unknown content type '$content_type'" if ( !defined( $content_type_map{$content_type} ) );
267 my $cache_policy = 'no-cache';
268 $cache_policy .= ', no-store, max-age=0' if $extra_options->{force_no_caching};
269 my $options = {
270 type => $content_type_map{$content_type},
271 status => $status,
272 charset => 'UTF-8',
273 Pragma => 'no-cache',
274 'Cache-Control' => $cache_policy,
275 'X-Frame-Options' => 'SAMEORIGIN',
277 $options->{expires} = 'now' if $extra_options->{force_no_caching};
279 $options->{cookie} = $cookie if $cookie;
280 if ($content_type eq 'html') { # guaranteed to be one of the content_type_map keys, else we'd have died
281 $options->{'Content-Style-Type' } = 'text/css';
282 $options->{'Content-Script-Type'} = 'text/javascript';
285 # We can't encode here, that will double encode our templates, and xslt
286 # We need to fix the encoding as it comes out of the database, or when we pass the variables to templates
288 $data =~ s/\&amp\;amp\; /\&amp\; /g;
289 print $query->header($options), $data;
292 sub output_html_with_http_headers {
293 my ( $query, $cookie, $data, $status, $extra_options ) = @_;
294 output_with_http_headers( $query, $cookie, $data, 'html', $status, $extra_options );
298 sub output_ajax_with_http_headers {
299 my ( $query, $js ) = @_;
300 print $query->header(
301 -type => 'text/javascript',
302 -charset => 'UTF-8',
303 -Pragma => 'no-cache',
304 -'Cache-Control' => 'no-cache',
305 -expires => '-1d',
306 ), $js;
309 sub is_ajax {
310 my $x_req = $ENV{HTTP_X_REQUESTED_WITH};
311 return ( $x_req and $x_req =~ /XMLHttpRequest/i ) ? 1 : 0;
314 =item output_and_exit_if_error
316 output_and_exit_if_error( $query, $cookie, $template, $params );
318 To executed at the beginning of scripts to stop the script at this point if
319 some errors are found.
321 Tests for module 'members':
322 * patron is not defined (we are looking for a patron that does no longer exist/never existed)
323 * The logged in user cannot see patron's infos (feature 'cannot_see_patron_infos')
325 Others will be added here depending on the needs (for instance biblio does not exist will be useful).
327 =cut
329 sub output_and_exit_if_error {
330 my ( $query, $cookie, $template, $params ) = @_;
331 my $error;
332 if ( $params and exists $params->{module} ) {
333 if ( $params->{module} eq 'members' ) {
334 my $logged_in_user = $params->{logged_in_user};
335 my $current_patron = $params->{current_patron};
336 if ( not $current_patron ) {
337 $error = 'unknown_patron';
339 elsif( not $logged_in_user->can_see_patron_infos( $current_patron ) ) {
340 $error = 'cannot_see_patron_infos';
342 } elsif ( $params->{module} eq 'cataloguing' ) {
343 # We are testing the record to avoid additem to fetch the Koha::Biblio
344 # But in the long term we will want to get a biblio in parameter
345 $error = 'unknown_biblio' unless $params->{record};
349 output_and_exit( $query, $cookie, $template, $error ) if $error;
350 return;
353 =item output_and_exit
355 output_and_exit( $query, $cookie, $template, $error );
357 $error is a blocking error like biblionumber not found or so.
358 We should output the error and exit.
360 =cut
362 sub output_and_exit {
363 my ( $query, $cookie, $template, $error ) = @_;
364 $template->param( blocking_error => $error );
365 output_html_with_http_headers ( $query, $cookie, $template->output );
366 exit;
369 sub output_error {
370 my ( $query, $error ) = @_;
371 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
373 template_name => 'errors/errorpage.tt',
374 query => $query,
375 type => 'intranet',
376 authnotrequired => 1,
379 my $admin = C4::Context->preference('KohaAdminEmailAddress');
380 $template->param (
381 admin => $admin,
382 errno => $error,
384 output_with_http_headers $query, $cookie, $template->output, 'html', '404 Not Found';
387 sub parametrized_url {
388 my $url = shift || ''; # ie page.pl?ln={LANG}
389 my $vars = shift || {}; # ie { LANG => en }
390 my $ret = $url;
391 while ( my ($key,$val) = each %$vars) {
392 my $val_url = URI::Escape::uri_escape_utf8( $val // q{} );
393 $ret =~ s/\{$key\}/$val_url/g;
395 $ret =~ s/\{[^\{]*\}//g; # remove remaining vars
396 return $ret;
399 END { } # module clean-up code here (global destructor)
402 __END__
404 =back
406 =head1 AUTHOR
408 Koha Development Team <http://koha-community.org/>
410 =cut