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 under the
12 # terms of the GNU General Public License as published by the Free Software
13 # Foundation; either version 2 of the License, or (at your option) any later
16 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
17 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
18 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
20 # You should have received a copy of the GNU General Public License along
21 # with Koha; if not, write to the Free Software Foundation, Inc.,
22 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 # NOTE: I'm pretty sure this module is deprecated in favor of
29 #use warnings; FIXME - Bug 2505
34 use C4::Dates qw(format_date);
35 use C4::Budgets qw(GetCurrency);
38 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
41 # set the version for version checking
42 $VERSION = 3.07.00.049;
46 @EXPORT_OK = qw(&is_ajax ajax_fail); # More stuff should go here instead
47 %EXPORT_TAGS = ( all =>[qw(setlanguagecookie pagination_bar parametrized_url
48 &output_with_http_headers &output_ajax_with_http_headers &output_html_with_http_headers)],
49 ajax =>[qw(&output_with_http_headers &output_ajax_with_http_headers is_ajax)],
50 html =>[qw(&output_with_http_headers &output_html_with_http_headers)]
53 setlanguagecookie getlanguagecookie pagination_bar parametrized_url
56 &output_html_with_http_headers &output_ajax_with_http_headers &output_with_http_headers
63 C4::Output - Functions for managing output, is slowly being deprecated
72 pagination_bar($base_url, $nb_pages, $current_page, $startfrom_name)
74 Build an HTML pagination bar based on the number of page to display, the
75 current page and the url to give to each page link.
77 C<$base_url> is the URL for each page link. The
78 C<$startfrom_name>=page_number is added at the end of the each URL.
80 C<$nb_pages> is the total number of pages available.
82 C<$current_page> is the current page number. This page number won't become a
85 This function returns HTML, without any language dependency.
90 my $base_url = (@_ ? shift : $ENV{SCRIPT_NAME} . $ENV{QUERY_STRING}) or return;
91 my $nb_pages = (@_) ? shift : 1;
92 my $current_page = (@_) ? shift : undef; # delay default until later
93 my $startfrom_name = (@_) ? shift : 'page';
95 # how many pages to show before and after the current page?
98 my $delim = qr/\&(?:amp;)?|;/; # "non memory" cluster: no backreference
99 $base_url =~ s/$delim*\b$startfrom_name=(\d+)//g; # remove previous pagination var
100 unless (defined $current_page and $current_page > 0 and $current_page <= $nb_pages) {
101 $current_page = ($1) ? $1 : 1; # pull current page from param in URL, else default to 1
102 # $debug and # FIXME: use C4::Debug;
103 # warn "with QUERY_STRING:" .$ENV{QUERY_STRING}. "\ncurrent_page:$current_page\n1:$1 2:$2 3:$3";
105 $base_url =~ s/($delim)+/$1/g; # compress duplicate delims
106 $base_url =~ s/$delim;//g; # remove empties
107 $base_url =~ s/$delim$//; # remove trailing delim
109 my $url = $base_url . (($base_url =~ m/$delim/ or $base_url =~ m/\?/) ? '&' : '?' ) . $startfrom_name . '=';
110 my $pagination_bar = '';
112 # navigation bar useful only if more than one page to display !
113 if ( $nb_pages > 1 ) {
115 # link to first page?
116 if ( $current_page > 1 ) {
122 . '<<' . '</a>';
126 "\n" . ' <span class="inactive"><<</span>';
129 # link on previous page ?
130 if ( $current_page > 1 ) {
131 my $previous = $current_page - 1;
138 . '" rel="prev">' . '<' . '</a>';
142 "\n" . ' <span class="inactive"><</span>';
145 my $min_to_display = $current_page - $pages_around;
146 my $max_to_display = $current_page + $pages_around;
147 my $last_displayed_page = undef;
149 for my $page_number ( 1 .. $nb_pages ) {
152 or $page_number == $nb_pages
153 or ( $page_number >= $min_to_display
154 and $page_number <= $max_to_display )
157 if ( defined $last_displayed_page
158 and $last_displayed_page != $page_number - 1 )
161 "\n" . ' <span class="inactive">...</span>';
164 if ( $page_number == $current_page ) {
167 . '<span class="currentPage">'
176 . $page_number . '">'
177 . $page_number . '</a>';
179 $last_displayed_page = $page_number;
184 if ( $current_page < $nb_pages ) {
185 my $next = $current_page + 1;
187 $pagination_bar .= "\n"
191 . '" rel="next">' . '>' . '</a>';
195 "\n" . ' <span class="inactive">></span>';
199 if ( $current_page != $nb_pages ) {
200 $pagination_bar .= "\n"
205 . '>>' . '</a>';
209 "\n" . ' <span class="inactive">>></span>';
213 return $pagination_bar;
216 =item output_with_http_headers
218 &output_with_http_headers($query, $cookie, $data, $content_type[, $status[, $extra_options]])
220 Outputs $data with the appropriate HTTP headers,
221 the authentication cookie $cookie and a Content-Type specified in
224 If applicable, $cookie can be undef, and it will not be sent.
226 $content_type is one of the following: 'html', 'js', 'json', 'xml', 'rss', or 'atom'.
228 $status is an HTTP status message, like '403 Authentication Required'. It defaults to '200 OK'.
230 $extra_options is hashref. If the key 'force_no_caching' is present and has
231 a true value, the HTTP headers include directives to force there to be no
236 sub output_with_http_headers {
237 my ( $query, $cookie, $data, $content_type, $status, $extra_options ) = @_;
238 $status ||= '200 OK';
240 $extra_options //= {};
242 my %content_type_map = (
243 'html' => 'text/html',
244 'js' => 'text/javascript',
245 'json' => 'application/json',
247 # NOTE: not using application/atom+xml or application/rss+xml because of
248 # Internet Explorer 6; see bug 2078.
253 die "Unknown content type '$content_type'" if ( !defined( $content_type_map{$content_type} ) );
254 my $cache_policy = 'no-cache';
255 $cache_policy .= ', no-store, max-age=0' if $extra_options->{force_no_caching};
257 type => $content_type_map{$content_type},
260 Pragma => 'no-cache',
261 'Cache-Control' => $cache_policy,
263 $options->{expires} = 'now' if $extra_options->{force_no_caching};
265 $options->{cookie} = $cookie if $cookie;
266 if ($content_type eq 'html') { # guaranteed to be one of the content_type_map keys, else we'd have died
267 $options->{'Content-Style-Type' } = 'text/css';
268 $options->{'Content-Script-Type'} = 'text/javascript';
271 # We can't encode here, that will double encode our templates, and xslt
272 # We need to fix the encoding as it comes out of the database, or when we pass the variables to templates
274 $data =~ s/\&\;amp\; /\&\; /g;
275 print $query->header($options), $data;
278 sub output_html_with_http_headers {
279 my ( $query, $cookie, $data, $status, $extra_options ) = @_;
280 output_with_http_headers( $query, $cookie, $data, 'html', $status, $extra_options );
284 sub output_ajax_with_http_headers {
285 my ( $query, $js ) = @_;
286 print $query->header(
287 -type => 'text/javascript',
289 -Pragma => 'no-cache',
290 -'Cache-Control' => 'no-cache',
296 my $x_req = $ENV{HTTP_X_REQUESTED_WITH};
297 return ( $x_req and $x_req =~ /XMLHttpRequest/i ) ? 1 : 0;
300 sub parametrized_url {
301 my $url = shift || ''; # ie page.pl?ln={LANG}
302 my $vars = shift || {}; # ie { LANG => en }
304 while ( my ($key,$val) = each %$vars) {
305 my $val_url = URI::Escape::uri_escape_utf8($val);
306 $ret =~ s/\{$key\}/$val_url/g;
308 $ret =~ s/\{[^\{]*\}//g; # remove not defined vars
312 END { } # module clean-up code here (global destructor)
321 Koha Development Team <http://koha-community.org/>