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
59 my $cur = GetCurrency;
60 my $cur_format = C4::Context->preference("CurrencyFormat");
63 if ( $cur_format eq 'FR' ) {
64 $num = new Number::Format(
65 'decimal_fill' => '2',
66 'decimal_point' => ',',
67 'int_curr_symbol' => $cur->{symbol},
68 'mon_thousands_sep' => ' ',
69 'thousands_sep' => ' ',
70 'mon_decimal_point' => ','
72 } else { # US by default..
73 $num = new Number::Format(
74 'int_curr_symbol' => '',
75 'mon_thousands_sep' => ',',
76 'mon_decimal_point' => '.'
84 FormatData($data_hashref)
85 C<$data_hashref> is a ref to data to format
87 Format dates of data those dates are assumed to contain date in their noun
88 Could be used in order to centralize all the formatting for HTML output
92 my $data_hashref=shift;
93 $$data_hashref{$_} = format_date( $$data_hashref{$_} ) for grep{/date/} keys (%$data_hashref);
98 pagination_bar($base_url, $nb_pages, $current_page, $startfrom_name)
100 Build an HTML pagination bar based on the number of page to display, the
101 current page and the url to give to each page link.
103 C<$base_url> is the URL for each page link. The
104 C<$startfrom_name>=page_number is added at the end of the each URL.
106 C<$nb_pages> is the total number of pages available.
108 C<$current_page> is the current page number. This page number won't become a
111 This function returns HTML, without any language dependency.
116 my $base_url = (@_ ? shift : $ENV{SCRIPT_NAME} . $ENV{QUERY_STRING}) or return undef;
117 my $nb_pages = (@_) ? shift : 1;
118 my $current_page = (@_) ? shift : undef; # delay default until later
119 my $startfrom_name = (@_) ? shift : 'page';
121 # how many pages to show before and after the current page?
122 my $pages_around = 2;
124 my $delim = qr/\&(?:amp;)?|;/; # "non memory" cluster: no backreference
125 $base_url =~ s/$delim*\b$startfrom_name=(\d+)//g; # remove previous pagination var
126 unless (defined $current_page and $current_page > 0 and $current_page <= $nb_pages) {
127 $current_page = ($1) ? $1 : 1; # pull current page from param in URL, else default to 1
128 # $debug and # FIXME: use C4::Debug;
129 # warn "with QUERY_STRING:" .$ENV{QUERY_STRING}. "\ncurrent_page:$current_page\n1:$1 2:$2 3:$3";
131 $base_url =~ s/($delim)+/$1/g; # compress duplicate delims
132 $base_url =~ s/$delim;//g; # remove empties
133 $base_url =~ s/$delim$//; # remove trailing delim
135 my $url = $base_url . (($base_url =~ m/$delim/ or $base_url =~ m/\?/) ? '&' : '?' ) . $startfrom_name . '=';
136 my $pagination_bar = '';
138 # navigation bar useful only if more than one page to display !
139 if ( $nb_pages > 1 ) {
141 # link to first page?
142 if ( $current_page > 1 ) {
148 . '<<' . '</a>';
152 "\n" . ' <span class="inactive"><<</span>';
155 # link on previous page ?
156 if ( $current_page > 1 ) {
157 my $previous = $current_page - 1;
164 . '" rel="prev">' . '<' . '</a>';
168 "\n" . ' <span class="inactive"><</span>';
171 my $min_to_display = $current_page - $pages_around;
172 my $max_to_display = $current_page + $pages_around;
173 my $last_displayed_page = undef;
175 for my $page_number ( 1 .. $nb_pages ) {
178 or $page_number == $nb_pages
179 or ( $page_number >= $min_to_display
180 and $page_number <= $max_to_display )
183 if ( defined $last_displayed_page
184 and $last_displayed_page != $page_number - 1 )
187 "\n" . ' <span class="inactive">...</span>';
190 if ( $page_number == $current_page ) {
193 . '<span class="currentPage">'
202 . $page_number . '">'
203 . $page_number . '</a>';
205 $last_displayed_page = $page_number;
210 if ( $current_page < $nb_pages ) {
211 my $next = $current_page + 1;
213 $pagination_bar .= "\n"
217 . '" rel="next">' . '>' . '</a>';
221 "\n" . ' <span class="inactive">></span>';
225 if ( $current_page != $nb_pages ) {
226 $pagination_bar .= "\n"
231 . '>>' . '</a>';
235 "\n" . ' <span class="inactive">>></span>';
239 return $pagination_bar;
242 =item output_with_http_headers
244 &output_with_http_headers($query, $cookie, $data, $content_type[, $status])
246 Outputs $data with the appropriate HTTP headers,
247 the authentication cookie $cookie and a Content-Type specified in
250 If applicable, $cookie can be undef, and it will not be sent.
252 $content_type is one of the following: 'html', 'js', 'json', 'xml', 'rss', or 'atom'.
254 $status is an HTTP status message, like '403 Authentication Required'. It defaults to '200 OK'.
258 sub output_with_http_headers($$$$;$) {
259 my ( $query, $cookie, $data, $content_type, $status ) = @_;
260 $status ||= '200 OK';
262 my %content_type_map = (
263 'html' => 'text/html',
264 'js' => 'text/javascript',
265 'json' => 'application/json',
267 # NOTE: not using application/atom+xml or application/rss+xml because of
268 # Internet Explorer 6; see bug 2078.
273 die "Unknown content type '$content_type'" if ( !defined( $content_type_map{$content_type} ) );
275 type => $content_type_map{$content_type},
278 Pragma => 'no-cache',
279 'Cache-Control' => 'no-cache',
281 $options->{cookie} = $cookie if $cookie;
282 if ($content_type eq 'html') { # guaranteed to be one of the content_type_map keys, else we'd have died
283 $options->{'Content-Style-Type' } = 'text/css';
284 $options->{'Content-Script-Type'} = 'text/javascript';
286 # remove SUDOC specific NSB NSE
287 $data =~ s/\x{C2}\x{98}|\x{C2}\x{9C}/ /g;
288 $data =~ s/\x{C2}\x{88}|\x{C2}\x{89}/ /g;
290 # We can't encode here, that will double encode our templates, and xslt
291 # We need to fix the encoding as it comes out of the database, or when we pass the variables to templates
293 # utf8::encode($data) if utf8::is_utf8($data);
295 print $query->header($options), $data;
298 sub output_html_with_http_headers ($$$;$) {
299 my ( $query, $cookie, $data, $status ) = @_;
300 $data =~ s/\&\;amp\; /\&\; /g;
301 output_with_http_headers( $query, $cookie, $data, 'html', $status );
305 my $x_req = $ENV{HTTP_X_REQUESTED_WITH};
306 return ( $x_req and $x_req =~ /XMLHttpRequest/i ) ? 1 : 0;
309 END { } # module clean-up code here (global destructor)
318 Koha Development Team <http://koha-community.org/>