Bug 18289 - Fix t/Prices.t having a Test::DBIx::Class cache issue
[koha.git] / C4 / Output.pm
blobaf7dd5a22b5c611cd9fc97f592cbbe4b292a1633
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;
33 use C4::Context;
34 use C4::Templates;
36 use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
38 BEGIN {
39 require Exporter;
41 @ISA = qw(Exporter);
42 @EXPORT_OK = qw(&is_ajax ajax_fail); # More stuff should go here instead
43 %EXPORT_TAGS = ( all =>[qw(setlanguagecookie pagination_bar parametrized_url
44 &output_with_http_headers &output_ajax_with_http_headers &output_html_with_http_headers)],
45 ajax =>[qw(&output_with_http_headers &output_ajax_with_http_headers is_ajax)],
46 html =>[qw(&output_with_http_headers &output_html_with_http_headers)]
48 push @EXPORT, qw(
49 setlanguagecookie getlanguagecookie pagination_bar parametrized_url
51 push @EXPORT, qw(
52 &output_html_with_http_headers &output_ajax_with_http_headers &output_with_http_headers
57 =head1 NAME
59 C4::Output - Functions for managing output, is slowly being deprecated
61 =head1 FUNCTIONS
63 =over 2
64 =cut
66 =item pagination_bar
68 pagination_bar($base_url, $nb_pages, $current_page, $startfrom_name)
70 Build an HTML pagination bar based on the number of page to display, the
71 current page and the url to give to each page link.
73 C<$base_url> is the URL for each page link. The
74 C<$startfrom_name>=page_number is added at the end of the each URL.
76 C<$nb_pages> is the total number of pages available.
78 C<$current_page> is the current page number. This page number won't become a
79 link.
81 This function returns HTML, without any language dependency.
83 =cut
85 sub pagination_bar {
86 my $base_url = (@_ ? shift : return);
87 my $nb_pages = (@_) ? shift : 1;
88 my $current_page = (@_) ? shift : undef; # delay default until later
89 my $startfrom_name = (@_) ? shift : 'page';
90 my $additional_parameters = shift || {};
92 # how many pages to show before and after the current page?
93 my $pages_around = 2;
95 my $delim = qr/\&(?:amp;)?|;/; # "non memory" cluster: no backreference
96 $base_url =~ s/$delim*\b$startfrom_name=(\d+)//g; # remove previous pagination var
97 unless (defined $current_page and $current_page > 0 and $current_page <= $nb_pages) {
98 $current_page = ($1) ? $1 : 1; # pull current page from param in URL, else default to 1
99 # $debug and # FIXME: use C4::Debug;
100 # warn "with QUERY_STRING:" .$ENV{QUERY_STRING}. "\ncurrent_page:$current_page\n1:$1 2:$2 3:$3";
102 $base_url =~ s/($delim)+/$1/g; # compress duplicate delims
103 $base_url =~ s/$delim;//g; # remove empties
104 $base_url =~ s/$delim$//; # remove trailing delim
106 my $url = $base_url . (($base_url =~ m/$delim/ or $base_url =~ m/\?/) ? '&amp;' : '?' ) . $startfrom_name . '=';
107 my $url_suffix;
108 while ( my ( $k, $v ) = each %$additional_parameters ) {
109 $url_suffix .= '&amp;' . $k . '=' . $v;
111 my $pagination_bar = '';
113 # navigation bar useful only if more than one page to display !
114 if ( $nb_pages > 1 ) {
116 # link to first page?
117 if ( $current_page > 1 ) {
118 $pagination_bar .=
119 "\n" . '&nbsp;'
120 . '<a href="'
121 . $url
122 . '1'
123 . $url_suffix
124 . '"rel="start">'
125 . '&lt;&lt;' . '</a>';
127 else {
128 $pagination_bar .=
129 "\n" . '&nbsp;<span class="inactive">&lt;&lt;</span>';
132 # link on previous page ?
133 if ( $current_page > 1 ) {
134 my $previous = $current_page - 1;
136 $pagination_bar .=
137 "\n" . '&nbsp;'
138 . '<a href="'
139 . $url
140 . $previous
141 . $url_suffix
142 . '" rel="prev">' . '&lt;' . '</a>';
144 else {
145 $pagination_bar .=
146 "\n" . '&nbsp;<span class="inactive">&lt;</span>';
149 my $min_to_display = $current_page - $pages_around;
150 my $max_to_display = $current_page + $pages_around;
151 my $last_displayed_page = undef;
153 for my $page_number ( 1 .. $nb_pages ) {
154 if (
155 $page_number == 1
156 or $page_number == $nb_pages
157 or ( $page_number >= $min_to_display
158 and $page_number <= $max_to_display )
161 if ( defined $last_displayed_page
162 and $last_displayed_page != $page_number - 1 )
164 $pagination_bar .=
165 "\n" . '&nbsp;<span class="inactive">...</span>';
168 if ( $page_number == $current_page ) {
169 $pagination_bar .=
170 "\n" . '&nbsp;'
171 . '<span class="currentPage">'
172 . $page_number
173 . '</span>';
175 else {
176 $pagination_bar .=
177 "\n" . '&nbsp;'
178 . '<a href="'
179 . $url
180 . $page_number
181 . $url_suffix
182 . '">'
183 . $page_number . '</a>';
185 $last_displayed_page = $page_number;
189 # link on next page?
190 if ( $current_page < $nb_pages ) {
191 my $next = $current_page + 1;
193 $pagination_bar .= "\n"
194 . '&nbsp;<a href="'
195 . $url
196 . $next
197 . $url_suffix
198 . '" rel="next">' . '&gt;' . '</a>';
200 else {
201 $pagination_bar .=
202 "\n" . '&nbsp;<span class="inactive">&gt;</span>';
205 # link to last page?
206 if ( $current_page != $nb_pages ) {
207 $pagination_bar .= "\n"
208 . '&nbsp;<a href="'
209 . $url
210 . $nb_pages
211 . $url_suffix
212 . '" rel="last">'
213 . '&gt;&gt;' . '</a>';
215 else {
216 $pagination_bar .=
217 "\n" . '&nbsp;<span class="inactive">&gt;&gt;</span>';
221 return $pagination_bar;
224 =item output_with_http_headers
226 &output_with_http_headers($query, $cookie, $data, $content_type[, $status[, $extra_options]])
228 Outputs $data with the appropriate HTTP headers,
229 the authentication cookie $cookie and a Content-Type specified in
230 $content_type.
232 If applicable, $cookie can be undef, and it will not be sent.
234 $content_type is one of the following: 'html', 'js', 'json', 'xml', 'rss', or 'atom'.
236 $status is an HTTP status message, like '403 Authentication Required'. It defaults to '200 OK'.
238 $extra_options is hashref. If the key 'force_no_caching' is present and has
239 a true value, the HTTP headers include directives to force there to be no
240 caching whatsoever.
242 =cut
244 sub output_with_http_headers {
245 my ( $query, $cookie, $data, $content_type, $status, $extra_options ) = @_;
246 $status ||= '200 OK';
248 $extra_options //= {};
250 my %content_type_map = (
251 'html' => 'text/html',
252 'js' => 'text/javascript',
253 'json' => 'application/json',
254 'xml' => 'text/xml',
255 # NOTE: not using application/atom+xml or application/rss+xml because of
256 # Internet Explorer 6; see bug 2078.
257 'rss' => 'text/xml',
258 'atom' => 'text/xml'
261 die "Unknown content type '$content_type'" if ( !defined( $content_type_map{$content_type} ) );
262 my $cache_policy = 'no-cache';
263 $cache_policy .= ', no-store, max-age=0' if $extra_options->{force_no_caching};
264 my $options = {
265 type => $content_type_map{$content_type},
266 status => $status,
267 charset => 'UTF-8',
268 Pragma => 'no-cache',
269 'Cache-Control' => $cache_policy,
270 'X-Frame-Options' => 'SAMEORIGIN',
272 $options->{expires} = 'now' if $extra_options->{force_no_caching};
274 $options->{cookie} = $cookie if $cookie;
275 if ($content_type eq 'html') { # guaranteed to be one of the content_type_map keys, else we'd have died
276 $options->{'Content-Style-Type' } = 'text/css';
277 $options->{'Content-Script-Type'} = 'text/javascript';
280 # We can't encode here, that will double encode our templates, and xslt
281 # We need to fix the encoding as it comes out of the database, or when we pass the variables to templates
283 $data =~ s/\&amp\;amp\; /\&amp\; /g;
284 print $query->header($options), $data;
287 sub output_html_with_http_headers {
288 my ( $query, $cookie, $data, $status, $extra_options ) = @_;
289 output_with_http_headers( $query, $cookie, $data, 'html', $status, $extra_options );
293 sub output_ajax_with_http_headers {
294 my ( $query, $js ) = @_;
295 print $query->header(
296 -type => 'text/javascript',
297 -charset => 'UTF-8',
298 -Pragma => 'no-cache',
299 -'Cache-Control' => 'no-cache',
300 -expires => '-1d',
301 ), $js;
304 sub is_ajax {
305 my $x_req = $ENV{HTTP_X_REQUESTED_WITH};
306 return ( $x_req and $x_req =~ /XMLHttpRequest/i ) ? 1 : 0;
309 sub parametrized_url {
310 my $url = shift || ''; # ie page.pl?ln={LANG}
311 my $vars = shift || {}; # ie { LANG => en }
312 my $ret = $url;
313 while ( my ($key,$val) = each %$vars) {
314 my $val_url = URI::Escape::uri_escape_utf8($val);
315 $ret =~ s/\{$key\}/$val_url/g;
317 $ret =~ s/\{[^\{]*\}//g; # remove not defined vars
318 return $ret;
321 END { } # module clean-up code here (global destructor)
324 __END__
326 =back
328 =head1 AUTHOR
330 Koha Development Team <http://koha-community.org/>
332 =cut