3 # Copyright 2000-2002 Katipo Communications
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
27 TODO :: Description here
29 C4::Scrubber is used to remove all markup content from the sumitted text.
36 use CGI
::Cookie
; # need to check cookies before having CGI parse the POST request
38 use C4
::Auth
qw(:DEFAULT check_cookie_auth);
41 use C4
::Output
qw(:html :ajax pagination_bar);
44 use C4
::Tags
qw(add_tag get_approval_rows get_tag_rows remove_tag stratify_tags);
53 my $perBibResults = {};
55 # Indexes of @errors that do not apply to a particular biblionumber.
56 my @globalErrorIndexes = ();
58 sub ajax_auth_cgi
{ # returns CGI object
59 my $needed_flags = shift;
60 my %cookies = fetch CGI
::Cookie
;
62 my $sessid = $cookies{'CGISESSID'}->value;
63 my ($auth_status, $auth_sessid) = check_cookie_auth
($sessid, $needed_flags);
65 print STDERR
"($auth_status, $auth_sessid) = check_cookie_auth($sessid," . Dumper
($needed_flags) . ")\n";
66 if ($auth_status ne "ok") {
67 output_with_http_headers
$input, undef,
68 "window.alert('Your CGI session cookie ($sessid) is not current. " .
69 "Please refresh the page and try again.');\n", 'js';
72 $debug and print STDERR
"AJAX request: " . Dumper
($input),
73 "\n(\$auth_status,\$auth_sessid) = ($auth_status,$auth_sessid)\n";
77 # The trick here is to support multiple tags added to multiple bilbios in one POST.
78 # The HTML might not use this, but it makes it more web-servicey from the start.
79 # So the name of param has to have biblionumber built in.
80 # For lack of anything more compelling, we just use "newtag[biblionumber]"
81 # We split the value into tags at comma and semicolon
83 my $is_ajax = is_ajax
();
84 my $openadds = C4
::Context
->preference('TagsModeration') ?
0 : 1;
85 my $query = ($is_ajax) ?
&ajax_auth_cgi
({}) : CGI
->new();
86 unless (C4
::Context
->preference('TagsEnabled')) {
87 push @errors, {+ tagsdisabled
=>1 };
88 push @globalErrorIndexes, $#errors;
90 foreach ($query->param) {
92 my $biblionumber = $1;
93 unless ($biblionumber =~ /^\d+$/) {
94 $debug and warn "$_ references non numerical biblionumber '$biblionumber'";
95 push @errors, {+'badparam' => $_ };
96 push @globalErrorIndexes, $#errors;
99 $newtags{$biblionumber} = $query->param($_);
100 } elsif (/^del(\d+)$/) {
106 my $add_op = (scalar(keys %newtags) + scalar(@deltags)) ?
1 : 0;
107 my ($template, $loggedinuser, $cookie);
109 $loggedinuser = C4
::Context
->userenv->{'number'}; # must occur AFTER auth
110 $debug and print STDERR
"op: $loggedinuser\n";
112 ($template, $loggedinuser, $cookie) = get_template_and_user
({
113 template_name
=> "opac-tags.tt",
116 authnotrequired
=> ($add_op ?
0 : 1), # auth required to add tags
122 unless ($loggedinuser) {
123 push @errors, {+'login' => 1 };
124 push @globalErrorIndexes, $#errors;
125 %newtags=(); # zero out any attempted additions
126 @deltags=(); # zero out any attempted deletions
131 my @newtags_keys = (keys %newtags);
132 if (scalar @newtags_keys) {
133 $scrubber = C4
::Scrubber
->new();
134 foreach my $biblionumber (@newtags_keys) {
135 my $bibResults = {adds
=>0, errors
=>[]};
136 my @values = split /[;,]/, $newtags{$biblionumber};
139 my $clean_tag = $scrubber->scrub($_);
140 unless ($clean_tag eq $_) {
141 if ($clean_tag =~ /\S/) {
142 push @errors, {scrubbed
=>$clean_tag};
143 push @
{$bibResults->{errors
}}, {scrubbed
=>$clean_tag};
145 push @errors, {scrubbed_all_bad
=>1};
146 push @
{$bibResults->{errors
}}, {scrubbed_all_bad
=>1};
147 next; # we don't add it if there's nothing left!
150 my $result = ($openadds) ?
151 add_tag
($biblionumber,$clean_tag,$loggedinuser,$loggedinuser) : # pre-approved
152 add_tag
($biblionumber,$clean_tag,$loggedinuser) ;
154 $counts{$biblionumber}++;
155 $bibResults->{adds
}++;
157 push @errors, {failed_add_tag
=>$clean_tag};
158 push @
{$bibResults->{errors
}}, {failed_add_tag
=>$clean_tag};
159 $debug and warn "add_tag($biblionumber,$clean_tag,$loggedinuser...) returned bad result (" . (defined $result ?
$result : 'UNDEF') .")";
162 $perBibResults->{$biblionumber} = $bibResults;
167 if (remove_tag
($_,$loggedinuser)) {
170 push @errors, {failed_delete
=>$_};
176 foreach (values %counts) {$sum += $_;}
177 my $js_reply = sprintf("response = {\n\tadded: %d,\n\tdeleted: %d,\n\terrors: %d",$sum,$dels,scalar @errors);
179 # If no add attempts were made, flag global errors.
180 if (@globalErrorIndexes) {
181 $js_reply .= ",\n\tglobal_errors: [";
183 foreach (@globalErrorIndexes) {
184 $js_reply .= "," unless $first;
186 $js_reply .= "\n\t\t$_";
188 $js_reply .= "\n\t]";
192 if (scalar @errors) {
193 $err_string = ",\n\talerts: ["; # open response_function
196 my $key = (keys %$_)[0];
197 $err_string .= "\n\t\t KOHA.Tags.tag_message.$key(\"" . $_->{$key} . '")';
198 if($i < scalar @errors){ $err_string .= ","; }
201 $err_string .= "\n\t]\n"; # close response_function
204 # Add per-biblionumber results for use on results page
206 for my $bib (keys %$perBibResults) {
207 my $bibResult = $perBibResults->{$bib};
208 my $js_bibres = ",\n\t$bib: {\n\t\tadded: $bibResult->{adds}";
209 $js_bibres .= ",\n\t\terrors: [";
211 foreach (@
{$bibResult->{errors
}}) {
212 $js_bibres .= "," if ($i);
213 my $key = (keys %$_)[0];
214 $js_bibres .= "\n\t\t\t KOHA.Tags.tag_message.$key(\"" . $_->{$key} . '")';
217 $js_bibres .= "\n\t\t]\n\t}";
218 $js_perbib .= $js_bibres;
221 output_with_http_headers
($query, undef, "$js_reply\n$err_string\n$js_perbib\n};", 'js');
229 $my_tags = get_tag_rows
({borrowernumber
=>$loggedinuser});
230 foreach (@
$my_tags) {
231 my $biblio = GetBiblioData
($_->{biblionumber
});
232 my $record = &GetMarcBiblio
( $_->{biblionumber
} );
233 $_->{subtitle
} = GetRecordValue
( 'subtitle', $record, GetFrameworkCode
( $_->{biblionumber
} ) );
234 $_->{title
} = $biblio->{title
};
235 $_->{author
} = $biblio->{author
};
236 if (C4
::Context
->preference("OPACXSLTResultsDisplay")) {
237 $_->{XSLTBloc
} = XSLTParse4Display
($_->{biblionumber
}, $record, "OPACXSLTResultsDisplay");
239 my $date = $_->{date_created
} || '';
240 $date =~ /\s+(\d{2}\:\d{2}\:\d{2})/;
241 $_->{time_created_display
} = $1;
245 $template->param(tagsview
=> 1);
249 for (values %counts) {$adds += $_;}
252 added_count
=> $adds,
253 deleted_count
=> $dels,
256 my ($arg,$limit,$mine);
257 my $hardmax = 100; # you might disagree what this value should be, but there definitely should be a max
258 $limit = $query->param('limit') || $hardmax;
259 $mine = $query->param('mine') || 0; # set if the patron want to see only his own tags.
260 ($limit =~ /^\d+$/ and $limit <= $hardmax) or $limit = $hardmax;
261 $template->param(limit
=> $limit);
262 my $arghash = {approved
=>1, limit
=>$limit, 'sort'=>'-weight_total'};
263 $arghash->{'borrowernumber'} = $loggedinuser if $mine;
264 # ($openadds) or $arghash->{approved} = 1;
265 if ($arg = $query->param('tag')) {
266 $arghash->{term
} = $arg;
267 } elsif ($arg = $query->param('biblionumber')) {
268 $arghash->{biblionumber
} = $arg;
270 $results = get_approval_rows
($arghash);
271 stratify_tags
(10, $results); # work out the differents sizes for things
272 my $count = scalar @
$results;
273 $template->param(TAGLOOP_COUNT
=> $count, mine
=> $mine);
275 (scalar @errors ) and $template->param(ERRORS
=> \
@errors);
276 my @orderedresult = sort { uc($a->{'term'}) cmp uc($b->{'term'}) } @
$results;
277 (scalar @
$results) and $template->param(TAGLOOP
=> \
@orderedresult );
278 (scalar @
$my_tags) and $template->param(MY_TAGS
=> $my_tags);
280 output_html_with_http_headers
$query, $cookie, $template->output;
283 =head1 EXAMPLE AJAX POST PARAMETERS
285 CGISESSID 7c6288263107beb320f70f78fd767f56
286 newtag396 fire,+<a+href="foobar.html">foobar</a>,+<img+src="foo.jpg"+/>
288 So this request is trying to add 3 tags to biblio #396. The CGISESSID is the same as that the browser would
289 typically communicate using cookies. If it is valid, the server will split the value of "newtag396" and
290 process the components for addition. In this case the intended tags are:
292 <a+href="foobar.html">foobar</a>
293 <img src="foo.jpg" />
295 The first tag is acceptable. The second will be scrubbed of markup, resulting in the tag "foobar".
296 The third tag is all markup, and will be rejected.
298 =head1 EXAMPLE AJAX JSON response
305 KOHA.Tags.tag_message.scrubbed("foobar"),
306 KOHA.Tags.tag_message.scrubbed_all_bad("1"),