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
31 use Scalar::Util qw( looks_like_number );
33 use C4::Auth qw( get_template_and_user );
37 our (@ISA, @EXPORT_OK);
46 setlanguagecookie getlanguagecookie pagination_bar parametrized_url
47 output_html_with_http_headers output_ajax_with_http_headers output_with_http_headers
48 output_and_exit_if_error output_and_exit output_error
54 C4::Output - Functions for managing output, is slowly being deprecated
62 pagination_bar($base_url, $nb_pages, $current_page, $startfrom_name)
64 Build an HTML pagination bar based on the number of page to display, the
65 current page and the url to give to each page link.
67 C<$base_url> is the URL for each page link. The
68 C<$startfrom_name>=page_number is added at the end of the each URL.
70 C<$nb_pages> is the total number of pages available.
72 C<$current_page> is the current page number. This page number won't become a
75 This function returns HTML, without any language dependency.
80 my $base_url = (@_ ? shift : return);
81 my $nb_pages = (@_) ? shift : 1;
82 my $current_page = (@_) ? shift : undef; # delay default until later
83 my $startfrom_name = (@_) ? shift : 'page';
84 my $additional_parameters = shift || {};
86 $current_page = looks_like_number($current_page) ? $current_page : undef;
87 $nb_pages = looks_like_number($nb_pages) ? $nb_pages : undef;
89 # how many pages to show before and after the current page?
92 my $delim = qr/\&(?:amp;)?|;/; # "non memory" cluster: no backreference
93 $base_url =~ s/$delim*\b$startfrom_name=(\d+)//g; # remove previous pagination var
94 unless (defined $current_page and $current_page > 0 and $current_page <= $nb_pages) {
95 $current_page = ($1) ? $1 : 1; # pull current page from param in URL, else default to 1
97 $base_url =~ s/($delim)+/$1/g; # compress duplicate delims
98 $base_url =~ s/$delim;//g; # remove empties
99 $base_url =~ s/$delim$//; # remove trailing delim
101 my $url = $base_url . (($base_url =~ m/$delim/ or $base_url =~ m/\?/) ? '&' : '?' ) . $startfrom_name . '=';
103 while ( my ( $k, $v ) = each %$additional_parameters ) {
104 $url_suffix .= '&' . URI::Escape::uri_escape_utf8($k) . '=' . URI::Escape::uri_escape_utf8($v);
106 my $pagination_bar = '';
108 # navigation bar useful only if more than one page to display !
109 if ( $nb_pages > 1 ) {
111 # link to first page?
112 if ( $current_page > 1 ) {
120 . '<<' . '</a>';
124 "\n" . ' <span class="inactive"><<</span>';
127 # link on previous page ?
128 if ( $current_page > 1 ) {
129 my $previous = $current_page - 1;
137 . '" rel="prev">' . '<' . '</a>';
141 "\n" . ' <span class="inactive"><</span>';
144 my $min_to_display = $current_page - $pages_around;
145 my $max_to_display = $current_page + $pages_around;
146 my $last_displayed_page = undef;
148 for my $page_number ( 1 .. $nb_pages ) {
151 or $page_number == $nb_pages
152 or ( $page_number >= $min_to_display
153 and $page_number <= $max_to_display )
156 if ( defined $last_displayed_page
157 and $last_displayed_page != $page_number - 1 )
160 "\n" . ' <span class="inactive">...</span>';
163 if ( $page_number == $current_page ) {
166 . '<span class="currentPage">'
178 . $page_number . '</a>';
180 $last_displayed_page = $page_number;
185 if ( $current_page < $nb_pages ) {
186 my $next = $current_page + 1;
188 $pagination_bar .= "\n"
193 . '" rel="next">' . '>' . '</a>';
197 "\n" . ' <span class="inactive">></span>';
201 if ( $current_page != $nb_pages ) {
202 $pagination_bar .= "\n"
208 . '>>' . '</a>';
212 "\n" . ' <span class="inactive">>></span>';
216 return $pagination_bar;
219 =item output_with_http_headers
221 &output_with_http_headers($query, $cookie, $data, $content_type[, $status[, $extra_options]])
223 Outputs $data with the appropriate HTTP headers,
224 the authentication cookie $cookie and a Content-Type specified in
227 If applicable, $cookie can be undef, and it will not be sent.
229 $content_type is one of the following: 'html', 'js', 'json', 'opensearchdescription', 'xml', 'rss', or 'atom'.
231 $status is an HTTP status message, like '403 Authentication Required'. It defaults to '200 OK'.
233 $extra_options is hashref. If the key 'force_no_caching' is present and has
234 a true value, the HTTP headers include directives to force there to be no
239 sub output_with_http_headers {
240 my ( $query, $cookie, $data, $content_type, $status, $extra_options ) = @_;
241 $status ||= '200 OK';
243 $extra_options //= {};
245 my %content_type_map = (
246 'html' => 'text/html',
247 'js' => 'text/javascript',
248 'json' => 'application/json',
250 # NOTE: not using application/atom+xml or application/rss+xml because of
251 # Internet Explorer 6; see bug 2078.
253 'atom' => 'text/xml',
254 'opensearchdescription' => 'application/opensearchdescription+xml',
257 die "Unknown content type '$content_type'" if ( !defined( $content_type_map{$content_type} ) );
258 my $cache_policy = 'no-cache';
259 $cache_policy .= ', no-store, max-age=0' if $extra_options->{force_no_caching};
261 type => $content_type_map{$content_type},
264 Pragma => 'no-cache',
265 'Cache-Control' => $cache_policy,
266 'X-Frame-Options' => 'SAMEORIGIN',
268 $options->{expires} = 'now' if $extra_options->{force_no_caching};
269 $options->{'Access-Control-Allow-Origin'} = C4::Context->preference('AccessControlAllowOrigin')
270 if C4::Context->preference('AccessControlAllowOrigin');
272 $options->{cookie} = $cookie if $cookie;
273 if ($content_type eq 'html') { # guaranteed to be one of the content_type_map keys, else we'd have died
274 $options->{'Content-Style-Type' } = 'text/css';
275 $options->{'Content-Script-Type'} = 'text/javascript';
278 # We can't encode here, that will double encode our templates, and xslt
279 # We need to fix the encoding as it comes out of the database, or when we pass the variables to templates
281 $data =~ s/\&\;amp\; /\&\; /g;
282 print $query->header($options), $data;
285 sub output_html_with_http_headers {
286 my ( $query, $cookie, $data, $status, $extra_options ) = @_;
287 output_with_http_headers( $query, $cookie, $data, 'html', $status, $extra_options );
291 sub output_ajax_with_http_headers {
292 my ( $query, $js ) = @_;
293 print $query->header(
294 -type => 'text/javascript',
296 -Pragma => 'no-cache',
297 -'Cache-Control' => 'no-cache',
303 my $x_req = $ENV{HTTP_X_REQUESTED_WITH};
304 return ( $x_req and $x_req =~ /XMLHttpRequest/i ) ? 1 : 0;
307 =item output_and_exit_if_error
309 output_and_exit_if_error( $query, $cookie, $template, $params );
311 To executed at the beginning of scripts to stop the script at this point if
312 some errors are found.
314 Tests for module 'members':
315 * patron is not defined (we are looking for a patron that does no longer exist/never existed)
316 * The logged in user cannot see patron's infos (feature 'cannot_see_patron_infos')
318 Others will be added here depending on the needs (for instance biblio does not exist will be useful).
322 sub output_and_exit_if_error {
323 my ( $query, $cookie, $template, $params ) = @_;
325 if ( $params and exists $params->{module} ) {
326 if ( $params->{module} eq 'members' ) {
327 my $logged_in_user = $params->{logged_in_user};
328 my $current_patron = $params->{current_patron};
329 if ( not $current_patron ) {
330 $error = 'unknown_patron';
332 elsif( not $logged_in_user->can_see_patron_infos( $current_patron ) ) {
333 $error = 'cannot_see_patron_infos';
335 } elsif ( $params->{module} eq 'cataloguing' ) {
336 # We are testing the record to avoid additem to fetch the Koha::Biblio
337 # But in the long term we will want to get a biblio in parameter
338 $error = 'unknown_biblio' unless $params->{record};
342 output_and_exit( $query, $cookie, $template, $error ) if $error;
346 =item output_and_exit
348 output_and_exit( $query, $cookie, $template, $error );
350 $error is a blocking error like biblionumber not found or so.
351 We should output the error and exit.
355 sub output_and_exit {
356 my ( $query, $cookie, $template, $error ) = @_;
357 $template->param( blocking_error => $error );
358 output_html_with_http_headers ( $query, $cookie, $template->output );
363 my ( $query, $error ) = @_;
364 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
366 template_name => 'errors/errorpage.tt',
369 authnotrequired => 1,
372 my $admin = C4::Context->preference('KohaAdminEmailAddress');
377 output_with_http_headers $query, $cookie, $template->output, 'html', '404 Not Found';
380 sub parametrized_url {
381 my $url = shift || ''; # ie page.pl?ln={LANG}
382 my $vars = shift || {}; # ie { LANG => en }
384 while ( my ($key,$val) = each %$vars) {
385 my $val_url = URI::Escape::uri_escape_utf8( $val // q{} );
386 $ret =~ s/\{$key\}/$val_url/g;
388 $ret =~ s/\{[^\{]*\}//g; # remove remaining vars
392 END { } # module clean-up code here (global destructor)
401 Koha Development Team <http://koha-community.org/>