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 under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA 02111-1307 USA
23 TODO :: Description here
25 C4::Scrubber is used to remove all markup content from the sumitted text.
32 use CGI::Cookie; # need to check cookies before having CGI parse the POST request
37 use C4::Output 3.02 qw(:html :ajax pagination_bar);
38 use C4::Dates qw(format_date);
41 use C4::Tags qw(add_tag get_tags get_tag_rows remove_tag);
48 # The trick here is to support multiple tags added to multiple bilbios in one POST.
49 # The HTML might not use this, but it makes it more web-servicey from the start.
50 # So the name of param has to have biblionumber built in.
51 # For lack of anything more compelling, we just use "newtag[biblionumber]"
52 # We split the value into tags at comma and semicolon
54 my $openadds = C4::Context->preference('TagsModeration') ? 0 : 1;
56 unless (C4::Context->preference('TagsEnabled')) {
57 push @errors, {+ tagsdisabled=>1 };
59 foreach ($query->param) {
61 my $biblionumber = $1;
62 unless ($biblionumber =~ /^\d+$/) {
63 $debug and warn "$_ references non numerical biblionumber '$biblionumber'";
64 push @errors, {+'badparam' => $_ };
67 $newtags{$biblionumber} = $query->param($_);
68 } elsif (/^del(\d+)$/) {
74 my $add_op = (scalar(keys %newtags) + scalar(@deltags)) ? 1 : 0;
75 my ($template, $loggedinuser, $cookie) = get_template_and_user({
76 template_name => "opac-tags.tmpl",
79 authnotrequired => ($add_op ? 0 : 1), # auth required to add tags
84 unless ($loggedinuser) {
85 push @errors, {+'login' => 1 };
86 %newtags=(); # zero out any attempted additions
87 @deltags=(); # zero out any attempted deletions
92 my @newtags_keys = (keys %newtags);
93 if (scalar @newtags_keys) {
94 $scrubber = C4::Scrubber->new();
95 foreach my $biblionumber (@newtags_keys) {
96 my @values = split /[;,]/, $newtags{$biblionumber};
99 my $clean_tag = $scrubber->scrub($_);
100 unless ($clean_tag eq $_) {
101 if ($clean_tag =~ /\S/) {
102 push @errors, {scrubbed=>$clean_tag};
104 push @errors, {scrubbed_all_bad=>1};
105 next; # we don't add it if there's nothing left!
108 my $result = ($openadds) ?
109 add_tag($biblionumber,$clean_tag,$loggedinuser,0) : # pre-approved
110 add_tag($biblionumber,$clean_tag,$loggedinuser) ;
112 $counts{$biblionumber}++;
114 warn "add_tag($biblionumber,$clean_tag,$loggedinuser...) returned bad result ($result)";
121 if (remove_tag($_,$loggedinuser)) {
124 push @errors, {failed_delete=>$_};
132 $my_tags = get_tag_rows({borrowernumber=>$loggedinuser});
133 foreach (@$my_tags) {
134 my $biblio = GetBiblioData($_->{biblionumber});
135 $_->{bib_summary} = $biblio->{title};
136 ($biblio->{author}) and $_->{bib_summary} .= " by " . $biblio->{author};
137 my $date = $_->{date_created} || '';
138 $date =~ /\s+(\d{2}\:\d{2}\:\d{2})/;
139 $_->{time_created_display} = $1;
140 $_->{date_created_display} = format_date($_->{date_created});
143 $template->param(tagsview => 1,);
146 for (values %counts) {$adds += $_;}
149 added_count => $adds,
150 deleted_count => $dels,
153 my ($arg,$limit,$tmpresults);
154 my $hardmax = 100; # you might disagree what this value should be, but there definitely should be a max
155 $limit = $query->param('limit') || $hardmax;
156 ($limit =~ /^\d+$/ and $limit <= $hardmax) or $limit = $hardmax;
157 if ($arg = $query->param('tag')) {
158 $tmpresults = get_tags({term => $arg, limit=>$limit, 'sort'=>'-weight'});
159 } elsif ($arg = $query->param('biblionumber')) {
160 $tmpresults = get_tags({biblionumber => $arg, limit=>$limit, 'sort'=>'-weight'});
162 $tmpresults = get_tags({limit=>$limit, 'sort'=>'-weight'});
165 foreach (@$tmpresults) {
166 $uniq{$_->{term}}++ and next;
170 (scalar @errors ) and $template->param(ERRORS => \@errors);
171 (scalar @$results) and $template->param(TAGLOOP => $results);
172 (scalar @$my_tags) and $template->param(MY_TAGS => $my_tags);
174 output_html_with_http_headers $query, $cookie, $template->output;