Bug 15072: (followup) fix spaces and consistency
[koha.git] / C4 / Output.pm
blob90249db1759d7143cc6d1aa3d8439687a0a1cc7d
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($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
38 BEGIN {
39 # set the version for version checking
40 $VERSION = 3.07.00.049;
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
59 =head1 NAME
61 C4::Output - Functions for managing output, is slowly being deprecated
63 =head1 FUNCTIONS
65 =over 2
66 =cut
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';
93 # how many pages to show before and after the current page?
94 my $pages_around = 2;
96 my $delim = qr/\&(?:amp;)?|;/; # "non memory" cluster: no backreference
97 $base_url =~ s/$delim*\b$startfrom_name=(\d+)//g; # remove previous pagination var
98 unless (defined $current_page and $current_page > 0 and $current_page <= $nb_pages) {
99 $current_page = ($1) ? $1 : 1; # pull current page from param in URL, else default to 1
100 # $debug and # FIXME: use C4::Debug;
101 # warn "with QUERY_STRING:" .$ENV{QUERY_STRING}. "\ncurrent_page:$current_page\n1:$1 2:$2 3:$3";
103 $base_url =~ s/($delim)+/$1/g; # compress duplicate delims
104 $base_url =~ s/$delim;//g; # remove empties
105 $base_url =~ s/$delim$//; # remove trailing delim
107 my $url = $base_url . (($base_url =~ m/$delim/ or $base_url =~ m/\?/) ? '&amp;' : '?' ) . $startfrom_name . '=';
108 my $pagination_bar = '';
110 # navigation bar useful only if more than one page to display !
111 if ( $nb_pages > 1 ) {
113 # link to first page?
114 if ( $current_page > 1 ) {
115 $pagination_bar .=
116 "\n" . '&nbsp;'
117 . '<a href="'
118 . $url
119 . '1" rel="start">'
120 . '&lt;&lt;' . '</a>';
122 else {
123 $pagination_bar .=
124 "\n" . '&nbsp;<span class="inactive">&lt;&lt;</span>';
127 # link on previous page ?
128 if ( $current_page > 1 ) {
129 my $previous = $current_page - 1;
131 $pagination_bar .=
132 "\n" . '&nbsp;'
133 . '<a href="'
134 . $url
135 . $previous
136 . '" rel="prev">' . '&lt;' . '</a>';
138 else {
139 $pagination_bar .=
140 "\n" . '&nbsp;<span class="inactive">&lt;</span>';
143 my $min_to_display = $current_page - $pages_around;
144 my $max_to_display = $current_page + $pages_around;
145 my $last_displayed_page = undef;
147 for my $page_number ( 1 .. $nb_pages ) {
148 if (
149 $page_number == 1
150 or $page_number == $nb_pages
151 or ( $page_number >= $min_to_display
152 and $page_number <= $max_to_display )
155 if ( defined $last_displayed_page
156 and $last_displayed_page != $page_number - 1 )
158 $pagination_bar .=
159 "\n" . '&nbsp;<span class="inactive">...</span>';
162 if ( $page_number == $current_page ) {
163 $pagination_bar .=
164 "\n" . '&nbsp;'
165 . '<span class="currentPage">'
166 . $page_number
167 . '</span>';
169 else {
170 $pagination_bar .=
171 "\n" . '&nbsp;'
172 . '<a href="'
173 . $url
174 . $page_number . '">'
175 . $page_number . '</a>';
177 $last_displayed_page = $page_number;
181 # link on next page?
182 if ( $current_page < $nb_pages ) {
183 my $next = $current_page + 1;
185 $pagination_bar .= "\n"
186 . '&nbsp;<a href="'
187 . $url
188 . $next
189 . '" rel="next">' . '&gt;' . '</a>';
191 else {
192 $pagination_bar .=
193 "\n" . '&nbsp;<span class="inactive">&gt;</span>';
196 # link to last page?
197 if ( $current_page != $nb_pages ) {
198 $pagination_bar .= "\n"
199 . '&nbsp;<a href="'
200 . $url
201 . $nb_pages
202 . '" rel="last">'
203 . '&gt;&gt;' . '</a>';
205 else {
206 $pagination_bar .=
207 "\n" . '&nbsp;<span class="inactive">&gt;&gt;</span>';
211 return $pagination_bar;
214 =item output_with_http_headers
216 &output_with_http_headers($query, $cookie, $data, $content_type[, $status[, $extra_options]])
218 Outputs $data with the appropriate HTTP headers,
219 the authentication cookie $cookie and a Content-Type specified in
220 $content_type.
222 If applicable, $cookie can be undef, and it will not be sent.
224 $content_type is one of the following: 'html', 'js', 'json', 'xml', 'rss', or 'atom'.
226 $status is an HTTP status message, like '403 Authentication Required'. It defaults to '200 OK'.
228 $extra_options is hashref. If the key 'force_no_caching' is present and has
229 a true value, the HTTP headers include directives to force there to be no
230 caching whatsoever.
232 =cut
234 sub output_with_http_headers {
235 my ( $query, $cookie, $data, $content_type, $status, $extra_options ) = @_;
236 $status ||= '200 OK';
238 $extra_options //= {};
240 my %content_type_map = (
241 'html' => 'text/html',
242 'js' => 'text/javascript',
243 'json' => 'application/json',
244 'xml' => 'text/xml',
245 # NOTE: not using application/atom+xml or application/rss+xml because of
246 # Internet Explorer 6; see bug 2078.
247 'rss' => 'text/xml',
248 'atom' => 'text/xml'
251 die "Unknown content type '$content_type'" if ( !defined( $content_type_map{$content_type} ) );
252 my $cache_policy = 'no-cache';
253 $cache_policy .= ', no-store, max-age=0' if $extra_options->{force_no_caching};
254 my $options = {
255 type => $content_type_map{$content_type},
256 status => $status,
257 charset => 'UTF-8',
258 Pragma => 'no-cache',
259 'Cache-Control' => $cache_policy,
261 $options->{expires} = 'now' if $extra_options->{force_no_caching};
263 $options->{cookie} = $cookie if $cookie;
264 if ($content_type eq 'html') { # guaranteed to be one of the content_type_map keys, else we'd have died
265 $options->{'Content-Style-Type' } = 'text/css';
266 $options->{'Content-Script-Type'} = 'text/javascript';
269 # We can't encode here, that will double encode our templates, and xslt
270 # We need to fix the encoding as it comes out of the database, or when we pass the variables to templates
272 $data =~ s/\&amp\;amp\; /\&amp\; /g;
273 print $query->header($options), $data;
276 sub output_html_with_http_headers {
277 my ( $query, $cookie, $data, $status, $extra_options ) = @_;
278 output_with_http_headers( $query, $cookie, $data, 'html', $status, $extra_options );
282 sub output_ajax_with_http_headers {
283 my ( $query, $js ) = @_;
284 print $query->header(
285 -type => 'text/javascript',
286 -charset => 'UTF-8',
287 -Pragma => 'no-cache',
288 -'Cache-Control' => 'no-cache',
289 -expires => '-1d',
290 ), $js;
293 sub is_ajax {
294 my $x_req = $ENV{HTTP_X_REQUESTED_WITH};
295 return ( $x_req and $x_req =~ /XMLHttpRequest/i ) ? 1 : 0;
298 sub parametrized_url {
299 my $url = shift || ''; # ie page.pl?ln={LANG}
300 my $vars = shift || {}; # ie { LANG => en }
301 my $ret = $url;
302 while ( my ($key,$val) = each %$vars) {
303 my $val_url = URI::Escape::uri_escape_utf8($val);
304 $ret =~ s/\{$key\}/$val_url/g;
306 $ret =~ s/\{[^\{]*\}//g; # remove not defined vars
307 return $ret;
310 END { } # module clean-up code here (global destructor)
313 __END__
315 =back
317 =head1 AUTHOR
319 Koha Development Team <http://koha-community.org/>
321 =cut