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
32 use C4::Dates qw(format_date);
33 use C4::Budgets qw(GetCurrency);
36 #use HTML::Template::Pro;
37 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
40 # set the version for version checking
44 @EXPORT_OK = qw(&is_ajax ajax_fail); # More stuff should go here instead
45 %EXPORT_TAGS = ( all =>[qw(&pagination_bar
46 &output_with_http_headers &output_html_with_http_headers)],
47 ajax =>[qw(&output_with_http_headers is_ajax)],
48 html =>[qw(&output_with_http_headers &output_html_with_http_headers)]
51 &output_html_with_http_headers &output_with_http_headers FormatData FormatNumber pagination_bar
58 C4::Output - Functions for managing output, is slowly being deprecated
68 my $cur = GetCurrency;
69 my $cur_format = C4::Context->preference("CurrencyFormat");
72 if ( $cur_format eq 'FR' ) {
73 $num = new Number::Format(
74 'decimal_fill' => '2',
75 'decimal_point' => ',',
76 'int_curr_symbol' => $cur->{symbol},
77 'mon_thousands_sep' => ' ',
78 'thousands_sep' => ' ',
79 'mon_decimal_point' => ','
81 } else { # US by default..
82 $num = new Number::Format(
83 'int_curr_symbol' => '',
84 'mon_thousands_sep' => ',',
85 'mon_decimal_point' => '.'
93 FormatData($data_hashref)
94 C<$data_hashref> is a ref to data to format
96 Format dates of data those dates are assumed to contain date in their noun
97 Could be used in order to centralize all the formatting for HTML output
101 my $data_hashref=shift;
102 $$data_hashref{$_} = format_date( $$data_hashref{$_} ) for grep{/date/} keys (%$data_hashref);
107 pagination_bar($base_url, $nb_pages, $current_page, $startfrom_name)
109 Build an HTML pagination bar based on the number of page to display, the
110 current page and the url to give to each page link.
112 C<$base_url> is the URL for each page link. The
113 C<$startfrom_name>=page_number is added at the end of the each URL.
115 C<$nb_pages> is the total number of pages available.
117 C<$current_page> is the current page number. This page number won't become a
120 This function returns HTML, without any language dependency.
125 my $base_url = (@_ ? shift : $ENV{SCRIPT_NAME} . $ENV{QUERY_STRING}) or return undef;
126 my $nb_pages = (@_) ? shift : 1;
127 my $current_page = (@_) ? shift : undef; # delay default until later
128 my $startfrom_name = (@_) ? shift : 'page';
130 # how many pages to show before and after the current page?
131 my $pages_around = 2;
133 my $delim = qr/\&(?:amp;)?|;/; # "non memory" cluster: no backreference
134 $base_url =~ s/$delim*\b$startfrom_name=(\d+)//g; # remove previous pagination var
135 unless (defined $current_page and $current_page > 0 and $current_page <= $nb_pages) {
136 $current_page = ($1) ? $1 : 1; # pull current page from param in URL, else default to 1
137 # $debug and # FIXME: use C4::Debug;
138 # warn "with QUERY_STRING:" .$ENV{QUERY_STRING}. "\ncurrent_page:$current_page\n1:$1 2:$2 3:$3";
140 $base_url =~ s/($delim)+/$1/g; # compress duplicate delims
141 $base_url =~ s/$delim;//g; # remove empties
142 $base_url =~ s/$delim$//; # remove trailing delim
144 my $url = $base_url . (($base_url =~ m/$delim/ or $base_url =~ m/\?/) ? '&' : '?' ) . $startfrom_name . '=';
145 my $pagination_bar = '';
147 # navigation bar useful only if more than one page to display !
148 if ( $nb_pages > 1 ) {
150 # link to first page?
151 if ( $current_page > 1 ) {
157 . '<<' . '</a>';
161 "\n" . ' <span class="inactive"><<</span>';
164 # link on previous page ?
165 if ( $current_page > 1 ) {
166 my $previous = $current_page - 1;
173 . '" rel="prev">' . '<' . '</a>';
177 "\n" . ' <span class="inactive"><</span>';
180 my $min_to_display = $current_page - $pages_around;
181 my $max_to_display = $current_page + $pages_around;
182 my $last_displayed_page = undef;
184 for my $page_number ( 1 .. $nb_pages ) {
187 or $page_number == $nb_pages
188 or ( $page_number >= $min_to_display
189 and $page_number <= $max_to_display )
192 if ( defined $last_displayed_page
193 and $last_displayed_page != $page_number - 1 )
196 "\n" . ' <span class="inactive">...</span>';
199 if ( $page_number == $current_page ) {
202 . '<span class="currentPage">'
211 . $page_number . '">'
212 . $page_number . '</a>';
214 $last_displayed_page = $page_number;
219 if ( $current_page < $nb_pages ) {
220 my $next = $current_page + 1;
222 $pagination_bar .= "\n"
226 . '" rel="next">' . '>' . '</a>';
230 "\n" . ' <span class="inactive">></span>';
234 if ( $current_page != $nb_pages ) {
235 $pagination_bar .= "\n"
240 . '>>' . '</a>';
244 "\n" . ' <span class="inactive">>></span>';
248 return $pagination_bar;
251 =item output_with_http_headers
253 &output_with_http_headers($query, $cookie, $data, $content_type[, $status])
255 Outputs $data with the appropriate HTTP headers,
256 the authentication cookie $cookie and a Content-Type specified in
259 If applicable, $cookie can be undef, and it will not be sent.
261 $content_type is one of the following: 'html', 'js', 'json', 'xml', 'rss', or 'atom'.
263 $status is an HTTP status message, like '403 Authentication Required'. It defaults to '200 OK'.
267 sub output_with_http_headers($$$$;$) {
268 my ( $query, $cookie, $data, $content_type, $status ) = @_;
269 $status ||= '200 OK';
271 my %content_type_map = (
272 'html' => 'text/html',
273 'js' => 'text/javascript',
274 'json' => 'application/json',
276 # NOTE: not using application/atom+xml or application/rss+xml because of
277 # Internet Explorer 6; see bug 2078.
282 die "Unknown content type '$content_type'" if ( !defined( $content_type_map{$content_type} ) );
284 type => $content_type_map{$content_type},
287 Pragma => 'no-cache',
288 'Cache-Control' => 'no-cache',
290 $options->{cookie} = $cookie if $cookie;
291 if ($content_type eq 'html') { # guaranteed to be one of the content_type_map keys, else we'd have died
292 $options->{'Content-Style-Type' } = 'text/css';
293 $options->{'Content-Script-Type'} = 'text/javascript';
295 # remove SUDOC specific NSB NSE
296 $data =~ s/\x{C2}\x{98}|\x{C2}\x{9C}/ /g;
297 $data =~ s/\x{C2}\x{88}|\x{C2}\x{89}/ /g;
299 # We can't encode here, that will double encode our templates, and xslt
300 # We need to fix the encoding as it comes out of the database, or when we pass the variables to templates
302 # utf8::encode($data) if utf8::is_utf8($data);
304 print $query->header($options), $data;
307 sub output_html_with_http_headers ($$$;$) {
308 my ( $query, $cookie, $data, $status ) = @_;
309 $data =~ s/\&\;amp\; /\&\; /g;
310 output_with_http_headers( $query, $cookie, $data, 'html', $status );
314 my $x_req = $ENV{HTTP_X_REQUESTED_WITH};
315 return ( $x_req and $x_req =~ /XMLHttpRequest/i ) ? 1 : 0;
318 END { } # module clean-up code here (global destructor)
327 Koha Development Team <http://koha-community.org/>