Bug 15035: Anti-spam for opac-suggestions - FOLLOW-UP
[koha.git] / opac / opac-suggestions.pl
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use strict;
19 use warnings;
20
21 use CGI qw ( -utf8 );
22 use Encode qw( encode );
23 use C4::Auth;    # get_template_and_user
24 use C4::Members;
25 use C4::Branch;
26 use C4::Koha;
27 use C4::Output;
28 use C4::Suggestions;
29 use C4::Koha;
30 use C4::Dates;
31 use C4::Scrubber;
32
33 use Koha::DateUtils qw( dt_from_string );
34
35 my $input           = new CGI;
36 my $op              = $input->param('op');
37 my $suggestion      = $input->Vars;
38 my $negcaptcha      = $input->param('negcap');
39 my $suggested_by_anyone = $input->param('suggested_by_anyone') || 0;
40
41 # If a spambot accidentally populates the 'negcap' field in the sugesstions form, then silently skip and return.
42 if ($negcaptcha ) {
43     print $input->redirect("/cgi-bin/koha/opac-suggestions.pl");
44     exit;
45 } else {
46     # don't pass 'negcap' column to DB, else DBI::Class will error
47     # DBIx::Class::Row::store_column(): No such column 'negcap' on Koha::Schema::Result::Suggestion at  Koha/C4/Suggestions.pm
48     delete $suggestion->{negcap};
49 }
50
51 #If suggestions are turned off we redirect to 404 error. This will also redirect guest suggestions
52 if ( ! C4::Context->preference('suggestion') ) {
53     print $input->redirect("/cgi-bin/koha/errors/404.pl");
54     exit;
55 }
56
57 delete $suggestion->{$_} foreach qw<op suggested_by_anyone>;
58 $op = 'else' unless $op;
59
60 my ( $template, $borrowernumber, $cookie, @messages );
61 my $deleted = $input->param('deleted');
62 my $submitted = $input->param('submitted');
63
64 if ( C4::Context->preference("AnonSuggestions") or ( C4::Context->preference("OPACViewOthersSuggestions") and $op eq 'else' ) ) {
65     ( $template, $borrowernumber, $cookie ) = get_template_and_user(
66         {
67             template_name   => "opac-suggestions.tt",
68             query           => $input,
69             type            => "opac",
70             authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
71         }
72     );
73 }
74 else {
75     ( $template, $borrowernumber, $cookie ) = get_template_and_user(
76         {
77             template_name   => "opac-suggestions.tt",
78             query           => $input,
79             type            => "opac",
80             authnotrequired => 0,
81         }
82     );
83 }
84
85 if ( $op eq 'else' ) {
86     if ( C4::Context->preference("OPACViewOthersSuggestions") ) {
87         if ( $borrowernumber ) {
88             # A logged in user is able to see suggestions from others
89             $suggestion->{suggestedby} = $suggested_by_anyone
90                 ? undef
91                 : $borrowernumber;
92         }
93         else {
94             # Non logged in user is able to see all suggestions
95             $suggestion->{suggestedby} = undef;
96         }
97     }
98     else {
99         if ( $borrowernumber ) {
100             $suggestion->{suggestedby} = $borrowernumber;
101         }
102         else {
103             $suggestion->{suggestedby} = -1;
104         }
105     }
106 } else {
107     if ( $borrowernumber ) {
108         $suggestion->{suggestedby} = $borrowernumber;
109     }
110     else {
111         $suggestion->{suggestedby} = C4::Context->preference("AnonymousPatron");
112     }
113 }
114
115 my $suggestions_loop =
116   &SearchSuggestion( $suggestion);
117 if ( $op eq "add_confirm" ) {
118         if (@$suggestions_loop>=1){
119                 #some suggestion are answering the request Donot Add
120         for my $suggestion ( @$suggestions_loop ) {
121             push @messages, { type => 'error', code => 'already_exists', id => $suggestion->{suggestionid} };
122             last;
123         }
124         }
125         else {
126                 my $scrubber = C4::Scrubber->new();
127                 foreach my $suggest (keys %$suggestion){
128             # Don't know why the encode is needed for Perl v5.10 here
129             $suggestion->{$suggest} = Encode::encode("utf8", $scrubber->scrub($suggestion->{$suggest}) );
130                 }
131         $suggestion->{suggesteddate} = dt_from_string;
132         $suggestion->{branchcode} = $input->param('branchcode') || C4::Context->userenv->{"branch"};
133
134                 &NewSuggestion($suggestion);
135                 # empty fields, to avoid filter in "SearchSuggestion"
136                 $$suggestion{$_}='' foreach qw<title author publishercode copyrightdate place collectiontitle isbn STATUS>;
137                 $suggestions_loop =
138                    &SearchSuggestion( $suggestion );
139         push @messages, { type => 'info', code => 'success_on_inserted' };
140         }
141     $op = 'else';
142 }
143
144 if ( $op eq "delete_confirm" ) {
145     my @delete_field = $input->param("delete_field");
146     foreach my $delete_field (@delete_field) {
147         &DelSuggestion( $borrowernumber, $delete_field );
148     }
149     $op = 'else';
150     print $input->redirect("/cgi-bin/koha/opac-suggestions.pl?op=else");
151     exit;
152 }
153 map{ $_->{'branchcodesuggestedby'}=GetBranchInfo($_->{'branchcodesuggestedby'})->[0]->{'branchname'}} @$suggestions_loop;
154 my $supportlist=GetSupportList();
155 foreach my $support(@$supportlist){
156         if ($$support{'imageurl'}){
157                 $$support{'imageurl'}= getitemtypeimagelocation( 'opac', $$support{'imageurl'} );
158         }
159         else {
160            delete $$support{'imageurl'}
161         }
162 }
163
164 foreach my $suggestion(@$suggestions_loop) {
165     if($suggestion->{'suggestedby'} == $borrowernumber) {
166         $suggestion->{'showcheckbox'} = $borrowernumber;
167     } else {
168         $suggestion->{'showcheckbox'} = 0;
169     }
170     if($suggestion->{'patronreason'}){
171         $suggestion->{'patronreason'} = GetKohaAuthorisedValueLib("OPAC_SUG",$suggestion->{'patronreason'},1);
172     }
173 }
174
175 my $patron_reason_loop = GetAuthorisedValues("OPAC_SUG");
176
177 # Is the person allowed to choose their branch
178 if ( C4::Context->preference("AllowPurchaseSuggestionBranchChoice") ) {
179     my ( $borr ) = GetMemberDetails( $borrowernumber );
180
181 # pass the pickup branch along....
182     my $userbranch = '';
183     if (C4::Context->userenv && C4::Context->userenv->{'branch'}) {
184         $userbranch = C4::Context->userenv->{'branch'};
185     }
186     my $branchcode = $input->param('branchcode') || $borr->{'branchcode'} || $userbranch || '' ;
187
188 # make branch selection options...
189     my $branchloop = GetBranchesLoop($branchcode);
190     $template->param( branchloop => $branchloop );
191 }
192
193 $template->param(
194         %$suggestion,
195         itemtypeloop=> $supportlist,
196     suggestions_loop => $suggestions_loop,
197     patron_reason_loop => $patron_reason_loop,
198     "op_$op"         => 1,
199     $op => 1,
200     messages => \@messages,
201     suggestionsview => 1,
202     suggested_by_anyone => $suggested_by_anyone,
203 );
204
205 output_html_with_http_headers $input, $cookie, $template->output;
206