Adding TagsEnabled to list of known sysprefs, adding 'my tabs'
[koha.git] / opac / opac-tags.pl
1 #!/usr/bin/perl
2
3 # Copyright 2000-2002 Katipo Communications
4 #
5 # This file is part of Koha.
6 #
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
10 # version.
11 #
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.
15 #
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
19
20
21 =head1 
22
23 TODO :: Description here
24
25 =cut
26
27 use strict;
28 use warnings;
29 use C4::Auth;
30 use C4::Context;
31 use C4::Debug;
32 use C4::Output;
33 use C4::Dates qw(format_date);
34 use CGI;
35 use C4::Biblio;
36 use C4::Tags qw(add_tag get_tags get_tag_rows remove_tag);
37
38 my $query = new CGI;
39 my %newtags = ();
40 my @deltags = ();
41 my %counts  = ();
42 my @errors  = ();
43
44 # The trick here is to support multiple tags added to multiple bilbios in one POST.
45 # So the name of param has to have biblionumber built in.
46 # For lack of anything more compelling, we just use "newtag[biblionumber]"
47 # We split the value into tags at comma and semicolon
48
49 my $openadds = C4::Context->preference('TagsModeration') ? 0 : 1;
50
51 unless (C4::Context->preference('TagsEnabled')) {
52         push @errors, {+ tagsdisabled=>1 };
53 } else {
54         foreach ($query->param) {
55                 if (/^newtag(.*)/) {
56                         my $biblionumber = $1;
57                         unless ($biblionumber =~ /^\d+$/) {
58                                 $debug and warn "$_ references non numerical biblionumber '$biblionumber'";
59                                 push @errors, {+'badparam' => $_ };
60                                 next;
61                         }
62                         $newtags{$biblionumber} = $query->param($_);
63                 } elsif (/^del(\d+)$/) {
64                         push @deltags, $1;
65                 }
66         }
67 }
68
69 my $add_op = (scalar(keys %newtags) + scalar(@deltags)) ? 1 : 0;
70 my ($template, $loggedinuser, $cookie) = get_template_and_user({
71         template_name   => "opac-tags.tmpl",
72         query           => $query,
73         type            => "opac",
74         authnotrequired => ($add_op ? 0 : 1),   # auth required to add tags
75         debug           => 1,
76 });
77
78 if ($add_op) {
79         unless ($loggedinuser) {
80                 push @errors, {+'login' => $_ };
81                 %newtags=();    # zero out any attempted additions
82                 @deltags=();    # zero out any attempted deletions
83         }
84 }
85 foreach my $biblionumber (keys %newtags) {
86         my @values = split /[;,]/, $newtags{$biblionumber};
87         foreach (@values) {
88                 s/^\s*(.+)\s*$/$1/;
89                 my $result;
90                 if ($openadds) {
91                         $result = add_tag($biblionumber,$_,$loggedinuser,0); # pre-approved
92                 } else {
93                         $result = add_tag($biblionumber,$_,$loggedinuser);
94                 }
95                 if ($result) {
96                         $counts{$biblionumber}++;
97                 } else {
98                         warn "add_tag($biblionumber,$_,$loggedinuser...) returned $result";
99                 }
100         }
101 }
102 my $dels = 0;
103 foreach (@deltags) {
104         remove_tag($_) and $dels++;
105 }
106
107 my $results = [];
108 my $my_tags = [];
109
110 if ($loggedinuser) {
111         $my_tags = get_tag_rows({borrowernumber=>$loggedinuser});
112         foreach (@$my_tags) {
113                 my $biblio = GetBiblioData($_->{biblionumber});
114                 $_->{bib_summary} = $biblio->{title}; 
115                 ($biblio->{author}) and $_->{bib_summary} .= " by " . $biblio->{author};
116                 my $date = $_->{date_created} || '';
117                 $date =~ /\s+(\d{2}\:\d{2}\:\d{2})/;
118                 $_->{time_created_display} = $1;
119                 $_->{date_created_display} = format_date($_->{date_created});
120         }
121 }
122 $template->param(tagsview => 1,);
123 if ($add_op) {
124         my $adds = 0;
125         for (values %counts) {$adds += $_;}
126         $template->param(
127                 add_op => 1,
128                 added_count => $adds,
129                 deleted_count => $dels,
130         );
131 } else {
132         my ($arg,$limit,$tmpresults);
133         my $hardmax = 100;      # you might disagree what this value should be, but there definitely should be a max
134         $limit = $query->param('limit') || $hardmax;
135         ($limit =~ /^\d+$/ and $limit <= $hardmax) or $limit = $hardmax;
136         if ($arg = $query->param('tag')) {
137                 $tmpresults = get_tags({term => $arg, limit=>$limit, 'sort'=>'-weight'});
138         } elsif ($arg = $query->param('biblionumber')) {
139                 $tmpresults = get_tags({biblionumber => $arg, limit=>$limit, 'sort'=>'-weight'});
140         } else {
141                 $tmpresults = get_tags({limit=>$limit, 'sort'=>'-weight'});
142         }
143         my %uniq;
144         foreach (@$tmpresults) {
145                 $uniq{$_->{term}}++ and next;
146                 push @$results, $_;
147         }
148 }
149 (scalar @errors  ) and $template->param(ERRORS  => \@errors);
150 (scalar @$results) and $template->param(TAGLOOP => $results);
151 (scalar @$my_tags) and $template->param(MY_TAGS => $my_tags);
152
153 output_html_with_http_headers $query, $cookie, $template->output;
154