File: | C4/Output.pm |
Coverage: | 19.0% |
line | stmt | bran | cond | sub | time | code |
---|---|---|---|---|---|---|
1 | package C4::Output; | |||||
2 | ||||||
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 | |||||
6 | ||||||
7 | # Copyright 2000-2002 Katipo Communications | |||||
8 | # | |||||
9 | # This file is part of Koha. | |||||
10 | # | |||||
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 | |||||
14 | # version. | |||||
15 | # | |||||
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. | |||||
19 | # | |||||
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. | |||||
23 | ||||||
24 | ||||||
25 | # NOTE: I'm pretty sure this module is deprecated in favor of | |||||
26 | # templates. | |||||
27 | ||||||
28 | 2 2 2 | 16942 7 50 | use strict; | |||
29 | #use warnings; FIXME - Bug 2505 | |||||
30 | ||||||
31 | 2 2 2 | 196 3 30 | use C4::Context; | |||
32 | 2 2 2 | 162 18 133 | use C4::Dates qw(format_date); | |||
33 | 2 2 2 | 357 5 289 | use C4::Budgets qw(GetCurrency); | |||
34 | 2 2 2 | 372 4 20 | use C4::Templates; | |||
35 | ||||||
36 | 2 2 2 | 97 2 286 | use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS); | |||
37 | ||||||
38 | BEGIN { | |||||
39 | # set the version for version checking | |||||
40 | 2 | 4 | $VERSION = 3.03; | |||
41 | 2 | 10 | require Exporter; | |||
42 | 2 | 20 | @ISA = qw(Exporter); | |||
43 | 2 | 6 | @EXPORT_OK = qw(&is_ajax ajax_fail); # More stuff should go here instead | |||
44 | 2 | 12 | %EXPORT_TAGS = ( all =>[qw(&pagination_bar | |||
45 | &output_with_http_headers &output_html_with_http_headers)], | |||||
46 | ajax =>[qw(&output_with_http_headers is_ajax)], | |||||
47 | html =>[qw(&output_with_http_headers &output_html_with_http_headers)] | |||||
48 | ); | |||||
49 | 2 | 2931 | push @EXPORT, qw( | |||
50 | &output_html_with_http_headers &output_with_http_headers FormatData FormatNumber pagination_bar | |||||
51 | ); | |||||
52 | } | |||||
53 | ||||||
54 | ||||||
55 - 62 | =head1 NAME C4::Output - Functions for managing output, is slowly being deprecated =head1 FUNCTIONS =over 2 =cut | |||||
63 | ||||||
64 - 65 | =item FormatNumber =cut | |||||
66 | sub FormatNumber{ | |||||
67 | 0 | my $cur = GetCurrency; | ||||
68 | 0 | my $cur_format = C4::Context->preference("CurrencyFormat"); | ||||
69 | 0 | my $num; | ||||
70 | ||||||
71 | 0 | if ( $cur_format eq 'FR' ) { | ||||
72 | 0 | $num = new Number::Format( | ||||
73 | 'decimal_fill' => '2', | |||||
74 | 'decimal_point' => ',', | |||||
75 | 'int_curr_symbol' => $cur->{symbol}, | |||||
76 | 'mon_thousands_sep' => ' ', | |||||
77 | 'thousands_sep' => ' ', | |||||
78 | 'mon_decimal_point' => ',' | |||||
79 | ); | |||||
80 | } else { # US by default.. | |||||
81 | 0 | $num = new Number::Format( | ||||
82 | 'int_curr_symbol' => '', | |||||
83 | 'mon_thousands_sep' => ',', | |||||
84 | 'mon_decimal_point' => '.' | |||||
85 | ); | |||||
86 | } | |||||
87 | 0 | return $num; | ||||
88 | } | |||||
89 | ||||||
90 - 97 | =item FormatData FormatData($data_hashref) C<$data_hashref> is a ref to data to format Format dates of data those dates are assumed to contain date in their noun Could be used in order to centralize all the formatting for HTML output =cut | |||||
98 | ||||||
99 | sub FormatData{ | |||||
100 | 0 | my $data_hashref=shift; | ||||
101 | 0 0 0 | $$data_hashref{$_} = format_date( $$data_hashref{$_} ) for grep{/date/} keys (%$data_hashref); | ||||
102 | } | |||||
103 | ||||||
104 - 121 | =item pagination_bar pagination_bar($base_url, $nb_pages, $current_page, $startfrom_name) Build an HTML pagination bar based on the number of page to display, the current page and the url to give to each page link. C<$base_url> is the URL for each page link. The C<$startfrom_name>=page_number is added at the end of the each URL. C<$nb_pages> is the total number of pages available. C<$current_page> is the current page number. This page number won't become a link. This function returns HTML, without any language dependency. =cut | |||||
122 | ||||||
123 | sub pagination_bar { | |||||
124 | 0 | my $base_url = (@_ ? shift : $ENV{SCRIPT_NAME} . $ENV{QUERY_STRING}) or return undef; | ||||
125 | 0 | my $nb_pages = (@_) ? shift : 1; | ||||
126 | 0 | my $current_page = (@_) ? shift : undef; # delay default until later | ||||
127 | 0 | my $startfrom_name = (@_) ? shift : 'page'; | ||||
128 | ||||||
129 | # how many pages to show before and after the current page? | |||||
130 | 0 | my $pages_around = 2; | ||||
131 | ||||||
132 | 0 | my $delim = qr/\&(?:amp;)?|;/; # "non memory" cluster: no backreference | ||||
133 | 0 | $base_url =~ s/$delim*\b$startfrom_name=(\d+)//g; # remove previous pagination var | ||||
134 | 0 | unless (defined $current_page and $current_page > 0 and $current_page <= $nb_pages) { | ||||
135 | 0 | $current_page = ($1) ? $1 : 1; # pull current page from param in URL, else default to 1 | ||||
136 | # $debug and # FIXME: use C4::Debug; | |||||
137 | # warn "with QUERY_STRING:" .$ENV{QUERY_STRING}. "\ncurrent_page:$current_page\n1:$1 2:$2 3:$3"; | |||||
138 | } | |||||
139 | 0 | $base_url =~ s/($delim)+/$1/g; # compress duplicate delims | ||||
140 | 0 | $base_url =~ s/$delim;//g; # remove empties | ||||
141 | 0 | $base_url =~ s/$delim$//; # remove trailing delim | ||||
142 | ||||||
143 | 0 | my $url = $base_url . (($base_url =~ m/$delim/ or $base_url =~ m/\?/) ? '&' : '?' ) . $startfrom_name . '='; | ||||
144 | 0 | my $pagination_bar = ''; | ||||
145 | ||||||
146 | # navigation bar useful only if more than one page to display ! | |||||
147 | 0 | if ( $nb_pages > 1 ) { | ||||
148 | ||||||
149 | # link to first page? | |||||
150 | 0 | if ( $current_page > 1 ) { | ||||
151 | 0 | $pagination_bar .= | ||||
152 | "\n" . ' ' | |||||
153 | . '<a href="' | |||||
154 | . $url | |||||
155 | . '1" rel="start">' | |||||
156 | . '<<' . '</a>'; | |||||
157 | } | |||||
158 | else { | |||||
159 | 0 | $pagination_bar .= | ||||
160 | "\n" . ' <span class="inactive"><<</span>'; | |||||
161 | } | |||||
162 | ||||||
163 | # link on previous page ? | |||||
164 | 0 | if ( $current_page > 1 ) { | ||||
165 | 0 | my $previous = $current_page - 1; | ||||
166 | ||||||
167 | 0 | $pagination_bar .= | ||||
168 | "\n" . ' ' | |||||
169 | . '<a href="' | |||||
170 | . $url | |||||
171 | . $previous | |||||
172 | . '" rel="prev">' . '<' . '</a>'; | |||||
173 | } | |||||
174 | else { | |||||
175 | 0 | $pagination_bar .= | ||||
176 | "\n" . ' <span class="inactive"><</span>'; | |||||
177 | } | |||||
178 | ||||||
179 | 0 | my $min_to_display = $current_page - $pages_around; | ||||
180 | 0 | my $max_to_display = $current_page + $pages_around; | ||||
181 | 0 | my $last_displayed_page = undef; | ||||
182 | ||||||
183 | 0 | for my $page_number ( 1 .. $nb_pages ) { | ||||
184 | 0 | if ( | ||||
185 | $page_number == 1 | |||||
186 | or $page_number == $nb_pages | |||||
187 | or ( $page_number >= $min_to_display | |||||
188 | and $page_number <= $max_to_display ) | |||||
189 | ) | |||||
190 | { | |||||
191 | 0 | if ( defined $last_displayed_page | ||||
192 | and $last_displayed_page != $page_number - 1 ) | |||||
193 | { | |||||
194 | 0 | $pagination_bar .= | ||||
195 | "\n" . ' <span class="inactive">...</span>'; | |||||
196 | } | |||||
197 | ||||||
198 | 0 | if ( $page_number == $current_page ) { | ||||
199 | 0 | $pagination_bar .= | ||||
200 | "\n" . ' ' | |||||
201 | . '<span class="currentPage">' | |||||
202 | . $page_number | |||||
203 | . '</span>'; | |||||
204 | } | |||||
205 | else { | |||||
206 | 0 | $pagination_bar .= | ||||
207 | "\n" . ' ' | |||||
208 | . '<a href="' | |||||
209 | . $url | |||||
210 | . $page_number . '">' | |||||
211 | . $page_number . '</a>'; | |||||
212 | } | |||||
213 | 0 | $last_displayed_page = $page_number; | ||||
214 | } | |||||
215 | } | |||||
216 | ||||||
217 | # link on next page? | |||||
218 | 0 | if ( $current_page < $nb_pages ) { | ||||
219 | 0 | my $next = $current_page + 1; | ||||
220 | ||||||
221 | 0 | $pagination_bar .= "\n" | ||||
222 | . ' <a href="' | |||||
223 | . $url | |||||
224 | . $next | |||||
225 | . '" rel="next">' . '>' . '</a>'; | |||||
226 | } | |||||
227 | else { | |||||
228 | 0 | $pagination_bar .= | ||||
229 | "\n" . ' <span class="inactive">></span>'; | |||||
230 | } | |||||
231 | ||||||
232 | # link to last page? | |||||
233 | 0 | if ( $current_page != $nb_pages ) { | ||||
234 | 0 | $pagination_bar .= "\n" | ||||
235 | . ' <a href="' | |||||
236 | . $url | |||||
237 | . $nb_pages | |||||
238 | . '" rel="last">' | |||||
239 | . '>>' . '</a>'; | |||||
240 | } | |||||
241 | else { | |||||
242 | 0 | $pagination_bar .= | ||||
243 | "\n" . ' <span class="inactive">>></span>'; | |||||
244 | } | |||||
245 | } | |||||
246 | ||||||
247 | 0 | return $pagination_bar; | ||||
248 | } | |||||
249 | ||||||
250 - 264 | =item output_with_http_headers &output_with_http_headers($query, $cookie, $data, $content_type[, $status]) Outputs $data with the appropriate HTTP headers, the authentication cookie $cookie and a Content-Type specified in $content_type. If applicable, $cookie can be undef, and it will not be sent. $content_type is one of the following: 'html', 'js', 'json', 'xml', 'rss', or 'atom'. $status is an HTTP status message, like '403 Authentication Required'. It defaults to '200 OK'. =cut | |||||
265 | ||||||
266 | sub output_with_http_headers($$$$;$) { | |||||
267 | 0 | my ( $query, $cookie, $data, $content_type, $status ) = @_; | ||||
268 | 0 | $status ||= '200 OK'; | ||||
269 | ||||||
270 | 0 | my %content_type_map = ( | ||||
271 | 'html' => 'text/html', | |||||
272 | 'js' => 'text/javascript', | |||||
273 | 'json' => 'application/json', | |||||
274 | 'xml' => 'text/xml', | |||||
275 | # NOTE: not using application/atom+xml or application/rss+xml because of | |||||
276 | # Internet Explorer 6; see bug 2078. | |||||
277 | 'rss' => 'text/xml', | |||||
278 | 'atom' => 'text/xml' | |||||
279 | ); | |||||
280 | ||||||
281 | 0 | die "Unknown content type '$content_type'" if ( !defined( $content_type_map{$content_type} ) ); | ||||
282 | 0 | my $options = { | ||||
283 | type => $content_type_map{$content_type}, | |||||
284 | status => $status, | |||||
285 | charset => 'UTF-8', | |||||
286 | Pragma => 'no-cache', | |||||
287 | 'Cache-Control' => 'no-cache', | |||||
288 | }; | |||||
289 | 0 | $options->{cookie} = $cookie if $cookie; | ||||
290 | 0 | if ($content_type eq 'html') { # guaranteed to be one of the content_type_map keys, else we'd have died | ||||
291 | 0 | $options->{'Content-Style-Type' } = 'text/css'; | ||||
292 | 0 | $options->{'Content-Script-Type'} = 'text/javascript'; | ||||
293 | } | |||||
294 | ||||||
295 | # We can't encode here, that will double encode our templates, and xslt | |||||
296 | # We need to fix the encoding as it comes out of the database, or when we pass the variables to templates | |||||
297 | ||||||
298 | # utf8::encode($data) if utf8::is_utf8($data); | |||||
299 | ||||||
300 | 0 | $data =~ s/\&\;amp\; /\&\; /g; | ||||
301 | 0 | print $query->header($options), $data; | ||||
302 | } | |||||
303 | ||||||
304 | sub output_html_with_http_headers ($$$;$) { | |||||
305 | 0 | my ( $query, $cookie, $data, $status ) = @_; | ||||
306 | 0 | output_with_http_headers( $query, $cookie, $data, 'html', $status ); | ||||
307 | } | |||||
308 | ||||||
309 | sub is_ajax () { | |||||
310 | 0 | my $x_req = $ENV{HTTP_X_REQUESTED_WITH}; | ||||
311 | 0 | return ( $x_req and $x_req =~ /XMLHttpRequest/i ) ? 1 : 0; | ||||
312 | } | |||||
313 | ||||||
314 | 2 | 367156 | END { } # module clean-up code here (global destructor) | |||
315 | ||||||
316 | 1; |