Bug 3144 - anti-spam for opac-suggestions
[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 under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 2 of the License, or (at your option) any later
8 # version.
9 #
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License along
15 # with Koha; if not, write to the Free Software Foundation, Inc.,
16 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
18 use strict;
19 use warnings;
20
21 use CGI;
22 use C4::Auth;    # get_template_and_user
23 use C4::Members;
24 use C4::Branch;
25 use C4::Koha;
26 use C4::Output;
27 use C4::Suggestions;
28 use C4::Koha;
29 use C4::Dates;
30 use C4::Scrubber;
31
32 my $input           = new CGI;
33 my $allsuggestions  = $input->param('showall');
34 my $op              = $input->param('op');
35 my $suggestion      = $input->Vars;
36 my $negcaptcha      = $input->param('negcap');
37
38 # If a spambot accidentally populates the 'negcap' field in the sugesstions form, then silently skip and return.
39 if ($negcaptcha ) {
40     print $input->redirect("/cgi-bin/koha/opac-suggestions.pl");
41     exit;
42 }
43
44 delete $$suggestion{$_} foreach qw<op suggestedbyme>;
45 $op = 'else' unless $op;
46
47 my ( $template, $borrowernumber, $cookie );
48 my $deleted = $input->param('deleted');
49 my $submitted = $input->param('submitted');
50
51 if ( C4::Context->preference("AnonSuggestions") ) {
52     ( $template, $borrowernumber, $cookie ) = get_template_and_user(
53         {
54             template_name   => "opac-suggestions.tmpl",
55             query           => $input,
56             type            => "opac",
57             authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
58         }
59     );
60     if ( !$$suggestion{suggestedby} ) {
61         $$suggestion{suggestedby} = C4::Context->preference("AnonymousPatron");
62     }
63 }
64 else {
65     ( $template, $borrowernumber, $cookie ) = get_template_and_user(
66         {
67             template_name   => "opac-suggestions.tmpl",
68             query           => $input,
69             type            => "opac",
70             authnotrequired => 0,
71         }
72     );
73 }
74 if ($allsuggestions){
75         delete $$suggestion{suggestedby};
76 }
77 else {
78         $$suggestion{suggestedby} ||= $borrowernumber unless ($allsuggestions);
79 }
80 # warn "bornum:",$borrowernumber;
81
82 my $suggestions_loop =
83   &SearchSuggestion( $suggestion);
84 if ( $op eq "add_confirm" ) {
85         if (@$suggestions_loop>=1){
86                 #some suggestion are answering the request Donot Add
87         }
88         else {
89                 my $scrubber = C4::Scrubber->new();
90                 foreach my $suggest (keys %$suggestion){
91                     $suggestion->{$suggest} = $scrubber->scrub($suggestion->{$suggest});
92                 }
93                 $$suggestion{'suggesteddate'}=C4::Dates->today;
94                 $$suggestion{'branchcode'}= $input->param('branch') || C4::Context->userenv->{"branch"};
95
96                 &NewSuggestion($suggestion);
97                 # empty fields, to avoid filter in "SearchSuggestion"
98                 $$suggestion{$_}='' foreach qw<title author publishercode copyrightdate place collectiontitle isbn STATUS>;
99                 $suggestions_loop =
100                    &SearchSuggestion( $suggestion );
101         }
102         $op              = 'else';
103     print $input->redirect("/cgi-bin/koha/opac-suggestions.pl?op=else&submitted=1");
104     exit;
105 }
106
107 if ( $op eq "delete_confirm" ) {
108     my @delete_field = $input->param("delete_field");
109     foreach my $delete_field (@delete_field) {
110         &DelSuggestion( $borrowernumber, $delete_field );
111     }
112     $op = 'else';
113     print $input->redirect("/cgi-bin/koha/opac-suggestions.pl?op=else&deleted=1");
114     exit;
115 }
116 map{ $_->{'branchcodesuggestedby'}=GetBranchInfo($_->{'branchcodesuggestedby'})->[0]->{'branchname'}} @$suggestions_loop;
117 my $supportlist=GetSupportList();
118 foreach my $support(@$supportlist){
119         if ($$support{'imageurl'}){
120                 $$support{'imageurl'}= getitemtypeimagelocation( 'opac', $$support{'imageurl'} );
121         }
122         else {
123            delete $$support{'imageurl'}
124         }
125 }
126
127 foreach my $suggestion(@$suggestions_loop) {
128     if($suggestion->{'suggestedby'} == $borrowernumber) {
129         $suggestion->{'showcheckbox'} = $borrowernumber;
130     } else {
131         $suggestion->{'showcheckbox'} = 0;
132     }
133     if($suggestion->{'patronreason'}){
134         $suggestion->{'patronreason'} = GetKohaAuthorisedValueLib("OPAC_SUG",$suggestion->{'patronreason'},1);
135     }
136 }
137
138 my $patron_reason_loop = GetAuthorisedValues("OPAC_SUG");
139
140 # Is the person allowed to choose their branch
141 if ( C4::Context->preference("AllowPurchaseSuggestionBranchChoice") ) {
142     my ( $borr ) = GetMemberDetails( $borrowernumber );
143
144 # pass the pickup branch along....
145     my $userbranch = '';
146     if (C4::Context->userenv && C4::Context->userenv->{'branch'}) {
147         $userbranch = C4::Context->userenv->{'branch'};
148     }
149     my $branch = $input->param('branch') || $borr->{'branchcode'} || $userbranch || '' ;
150
151 # make branch selection options...
152     my $branchloop = GetBranchesLoop($branch);
153     $template->param( branchloop => $branchloop );
154 }
155
156 $template->param(
157         %$suggestion,
158         itemtypeloop=> $supportlist,
159     suggestions_loop => $suggestions_loop,
160     patron_reason_loop => $patron_reason_loop,
161     showall    => $allsuggestions,
162     "op_$op"         => 1,
163     suggestionsview => 1,
164 );
165
166 output_html_with_http_headers $input, $cookie, $template->output;
167