Bug 12176: Remove HTML from additem.pl
[koha.git] / C4 / Output.pm
blob1dbdbee97353d76ad81ac4e3095fb15f001edfe0
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::Dates qw(format_date);
35 use C4::Templates;
37 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
39 BEGIN {
40 # set the version for version checking
41 $VERSION = 3.07.00.049;
42 require Exporter;
44 @ISA = qw(Exporter);
45 @EXPORT_OK = qw(&is_ajax ajax_fail); # More stuff should go here instead
46 %EXPORT_TAGS = ( all =>[qw(setlanguagecookie pagination_bar parametrized_url
47 &output_with_http_headers &output_ajax_with_http_headers &output_html_with_http_headers)],
48 ajax =>[qw(&output_with_http_headers &output_ajax_with_http_headers is_ajax)],
49 html =>[qw(&output_with_http_headers &output_html_with_http_headers)]
51 push @EXPORT, qw(
52 setlanguagecookie getlanguagecookie pagination_bar parametrized_url
54 push @EXPORT, qw(
55 &output_html_with_http_headers &output_ajax_with_http_headers &output_with_http_headers
60 =head1 NAME
62 C4::Output - Functions for managing output, is slowly being deprecated
64 =head1 FUNCTIONS
66 =over 2
67 =cut
69 =item pagination_bar
71 pagination_bar($base_url, $nb_pages, $current_page, $startfrom_name)
73 Build an HTML pagination bar based on the number of page to display, the
74 current page and the url to give to each page link.
76 C<$base_url> is the URL for each page link. The
77 C<$startfrom_name>=page_number is added at the end of the each URL.
79 C<$nb_pages> is the total number of pages available.
81 C<$current_page> is the current page number. This page number won't become a
82 link.
84 This function returns HTML, without any language dependency.
86 =cut
88 sub pagination_bar {
89 my $base_url = (@_ ? shift : $ENV{SCRIPT_NAME} . $ENV{QUERY_STRING}) or return;
90 my $nb_pages = (@_) ? shift : 1;
91 my $current_page = (@_) ? shift : undef; # delay default until later
92 my $startfrom_name = (@_) ? shift : 'page';
94 # how many pages to show before and after the current page?
95 my $pages_around = 2;
97 my $delim = qr/\&(?:amp;)?|;/; # "non memory" cluster: no backreference
98 $base_url =~ s/$delim*\b$startfrom_name=(\d+)//g; # remove previous pagination var
99 unless (defined $current_page and $current_page > 0 and $current_page <= $nb_pages) {
100 $current_page = ($1) ? $1 : 1; # pull current page from param in URL, else default to 1
101 # $debug and # FIXME: use C4::Debug;
102 # warn "with QUERY_STRING:" .$ENV{QUERY_STRING}. "\ncurrent_page:$current_page\n1:$1 2:$2 3:$3";
104 $base_url =~ s/($delim)+/$1/g; # compress duplicate delims
105 $base_url =~ s/$delim;//g; # remove empties
106 $base_url =~ s/$delim$//; # remove trailing delim
108 my $url = $base_url . (($base_url =~ m/$delim/ or $base_url =~ m/\?/) ? '&amp;' : '?' ) . $startfrom_name . '=';
109 my $pagination_bar = '';
111 # navigation bar useful only if more than one page to display !
112 if ( $nb_pages > 1 ) {
114 # link to first page?
115 if ( $current_page > 1 ) {
116 $pagination_bar .=
117 "\n" . '&nbsp;'
118 . '<a href="'
119 . $url
120 . '1" rel="start">'
121 . '&lt;&lt;' . '</a>';
123 else {
124 $pagination_bar .=
125 "\n" . '&nbsp;<span class="inactive">&lt;&lt;</span>';
128 # link on previous page ?
129 if ( $current_page > 1 ) {
130 my $previous = $current_page - 1;
132 $pagination_bar .=
133 "\n" . '&nbsp;'
134 . '<a href="'
135 . $url
136 . $previous
137 . '" rel="prev">' . '&lt;' . '</a>';
139 else {
140 $pagination_bar .=
141 "\n" . '&nbsp;<span class="inactive">&lt;</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 ) {
149 if (
150 $page_number == 1
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 )
159 $pagination_bar .=
160 "\n" . '&nbsp;<span class="inactive">...</span>';
163 if ( $page_number == $current_page ) {
164 $pagination_bar .=
165 "\n" . '&nbsp;'
166 . '<span class="currentPage">'
167 . $page_number
168 . '</span>';
170 else {
171 $pagination_bar .=
172 "\n" . '&nbsp;'
173 . '<a href="'
174 . $url
175 . $page_number . '">'
176 . $page_number . '</a>';
178 $last_displayed_page = $page_number;
182 # link on next page?
183 if ( $current_page < $nb_pages ) {
184 my $next = $current_page + 1;
186 $pagination_bar .= "\n"
187 . '&nbsp;<a href="'
188 . $url
189 . $next
190 . '" rel="next">' . '&gt;' . '</a>';
192 else {
193 $pagination_bar .=
194 "\n" . '&nbsp;<span class="inactive">&gt;</span>';
197 # link to last page?
198 if ( $current_page != $nb_pages ) {
199 $pagination_bar .= "\n"
200 . '&nbsp;<a href="'
201 . $url
202 . $nb_pages
203 . '" rel="last">'
204 . '&gt;&gt;' . '</a>';
206 else {
207 $pagination_bar .=
208 "\n" . '&nbsp;<span class="inactive">&gt;&gt;</span>';
212 return $pagination_bar;
215 =item output_with_http_headers
217 &output_with_http_headers($query, $cookie, $data, $content_type[, $status[, $extra_options]])
219 Outputs $data with the appropriate HTTP headers,
220 the authentication cookie $cookie and a Content-Type specified in
221 $content_type.
223 If applicable, $cookie can be undef, and it will not be sent.
225 $content_type is one of the following: 'html', 'js', 'json', 'xml', 'rss', or 'atom'.
227 $status is an HTTP status message, like '403 Authentication Required'. It defaults to '200 OK'.
229 $extra_options is hashref. If the key 'force_no_caching' is present and has
230 a true value, the HTTP headers include directives to force there to be no
231 caching whatsoever.
233 =cut
235 sub output_with_http_headers {
236 my ( $query, $cookie, $data, $content_type, $status, $extra_options ) = @_;
237 $status ||= '200 OK';
239 $extra_options //= {};
241 my %content_type_map = (
242 'html' => 'text/html',
243 'js' => 'text/javascript',
244 'json' => 'application/json',
245 'xml' => 'text/xml',
246 # NOTE: not using application/atom+xml or application/rss+xml because of
247 # Internet Explorer 6; see bug 2078.
248 'rss' => 'text/xml',
249 'atom' => 'text/xml'
252 die "Unknown content type '$content_type'" if ( !defined( $content_type_map{$content_type} ) );
253 my $cache_policy = 'no-cache';
254 $cache_policy .= ', no-store, max-age=0' if $extra_options->{force_no_caching};
255 my $options = {
256 type => $content_type_map{$content_type},
257 status => $status,
258 charset => 'UTF-8',
259 Pragma => 'no-cache',
260 'Cache-Control' => $cache_policy,
262 $options->{expires} = 'now' if $extra_options->{force_no_caching};
264 $options->{cookie} = $cookie if $cookie;
265 if ($content_type eq 'html') { # guaranteed to be one of the content_type_map keys, else we'd have died
266 $options->{'Content-Style-Type' } = 'text/css';
267 $options->{'Content-Script-Type'} = 'text/javascript';
270 # We can't encode here, that will double encode our templates, and xslt
271 # We need to fix the encoding as it comes out of the database, or when we pass the variables to templates
273 $data =~ s/\&amp\;amp\; /\&amp\; /g;
274 print $query->header($options), $data;
277 sub output_html_with_http_headers {
278 my ( $query, $cookie, $data, $status, $extra_options ) = @_;
279 output_with_http_headers( $query, $cookie, $data, 'html', $status, $extra_options );
283 sub output_ajax_with_http_headers {
284 my ( $query, $js ) = @_;
285 print $query->header(
286 -type => 'text/javascript',
287 -charset => 'UTF-8',
288 -Pragma => 'no-cache',
289 -'Cache-Control' => 'no-cache',
290 -expires => '-1d',
291 ), $js;
294 sub is_ajax {
295 my $x_req = $ENV{HTTP_X_REQUESTED_WITH};
296 return ( $x_req and $x_req =~ /XMLHttpRequest/i ) ? 1 : 0;
299 sub parametrized_url {
300 my $url = shift || ''; # ie page.pl?ln={LANG}
301 my $vars = shift || {}; # ie { LANG => en }
302 my $ret = $url;
303 while ( my ($key,$val) = each %$vars) {
304 my $val_url = URI::Escape::uri_escape_utf8($val);
305 $ret =~ s/\{$key\}/$val_url/g;
307 $ret =~ s/\{[^\{]*\}//g; # remove not defined vars
308 return $ret;
311 END { } # module clean-up code here (global destructor)
314 __END__
316 =back
318 =head1 AUTHOR
320 Koha Development Team <http://koha-community.org/>
322 =cut