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 = CGI
::Cookie
->fetch;
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 my $my_approved_tags = get_approval_rows
({ approved
=> 1 });
231 foreach my $tag (@
$my_tags) {
232 my $biblio = GetBiblioData
($tag->{biblionumber
});
233 my $record = &GetMarcBiblio
( $tag->{biblionumber
} );
234 $tag->{subtitle
} = GetRecordValue
( 'subtitle', $record, GetFrameworkCode
( $tag->{biblionumber
} ) );
235 $tag->{title
} = $biblio->{title
};
236 $tag->{author
} = $biblio->{author
};
238 my $xslfile = C4
::Context
->preference('OPACXSLTResultsDisplay');
239 my $lang = $xslfile ? C4
::Languages
::getlanguage
() : undef;
240 my $sysxml = $xslfile ? C4
::XSLT
::get_xslt_sysprefs
() : undef;
243 $tag->{XSLTBloc
} = XSLTParse4Display
(
244 $tag->{ biblionumber
}, $record, "OPACXSLTResultsDisplay",
245 1, undef, $sysxml, $xslfile, $lang
249 my $date = $tag->{date_created
} || '';
250 $date =~ /\s+(\d{2}\:\d{2}\:\d{2})/;
251 $tag->{time_created_display
} = $1;
252 $tag->{approved
} = ( grep { $_->{term
} eq $tag->{term
} and $_->{approved
} } @
$my_approved_tags );
256 $template->param(tagsview
=> 1);
260 for (values %counts) {$adds += $_;}
263 added_count
=> $adds,
264 deleted_count
=> $dels,
267 my ($arg,$limit,$mine);
268 my $hardmax = 100; # you might disagree what this value should be, but there definitely should be a max
269 $limit = $query->param('limit') || $hardmax;
270 $mine = $query->param('mine') || 0; # set if the patron want to see only his own tags.
271 ($limit =~ /^\d+$/ and $limit <= $hardmax) or $limit = $hardmax;
272 $template->param(limit
=> $limit);
273 my $arghash = {approved
=>1, limit
=>$limit, 'sort'=>'-weight_total'};
274 $arghash->{'borrowernumber'} = $loggedinuser if $mine;
275 # ($openadds) or $arghash->{approved} = 1;
276 if ($arg = $query->param('tag')) {
277 $arghash->{term
} = $arg;
278 } elsif ($arg = $query->param('biblionumber')) {
279 $arghash->{biblionumber
} = $arg;
281 $results = get_approval_rows
($arghash);
282 stratify_tags
(10, $results); # work out the differents sizes for things
283 my $count = scalar @
$results;
284 $template->param(TAGLOOP_COUNT
=> $count, mine
=> $mine);
286 (scalar @errors ) and $template->param(ERRORS
=> \
@errors);
287 my @orderedresult = sort { uc($a->{'term'}) cmp uc($b->{'term'}) } @
$results;
288 (scalar @
$results) and $template->param(TAGLOOP
=> \
@orderedresult );
289 (scalar @
$my_tags) and $template->param(MY_TAGS
=> $my_tags);
291 output_html_with_http_headers
$query, $cookie, $template->output;
294 =head1 EXAMPLE AJAX POST PARAMETERS
296 CGISESSID 7c6288263107beb320f70f78fd767f56
297 newtag396 fire,+<a+href="foobar.html">foobar</a>,+<img+src="foo.jpg"+/>
299 So this request is trying to add 3 tags to biblio #396. The CGISESSID is the same as that the browser would
300 typically communicate using cookies. If it is valid, the server will split the value of "newtag396" and
301 process the components for addition. In this case the intended tags are:
303 <a+href="foobar.html">foobar</a>
304 <img src="foo.jpg" />
306 The first tag is acceptable. The second will be scrubbed of markup, resulting in the tag "foobar".
307 The third tag is all markup, and will be rejected.
309 =head1 EXAMPLE AJAX JSON response
316 KOHA.Tags.tag_message.scrubbed("foobar"),
317 KOHA.Tags.tag_message.scrubbed_all_bad("1"),