Bug 17248 - Koha::AuthorisedValues - Remove GetKohaAuthorisedValueLib
[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::Koha;
26 use C4::Output;
27 use C4::Suggestions;
28 use C4::Koha;
29 use C4::Scrubber;
30
31 use Koha::AuthorisedValues;
32 use Koha::Libraries;
33
34 use Koha::DateUtils qw( dt_from_string );
35
36 my $input           = new CGI;
37 my $op              = $input->param('op');
38 my $suggestion      = $input->Vars;
39 my $negcaptcha      = $input->param('negcap');
40 my $suggested_by_anyone = $input->param('suggested_by_anyone') || 0;
41
42 # If a spambot accidentally populates the 'negcap' field in the sugesstions form, then silently skip and return.
43 if ($negcaptcha ) {
44     print $input->redirect("/cgi-bin/koha/opac-suggestions.pl");
45     exit;
46 } else {
47     # don't pass 'negcap' column to DB, else DBI::Class will error
48     # DBIx::Class::Row::store_column(): No such column 'negcap' on Koha::Schema::Result::Suggestion at  Koha/C4/Suggestions.pm
49     delete $suggestion->{negcap};
50 }
51
52 #If suggestions are turned off we redirect to 404 error. This will also redirect guest suggestions
53 if ( ! C4::Context->preference('suggestion') ) {
54     print $input->redirect("/cgi-bin/koha/errors/404.pl");
55     exit;
56 }
57
58 delete $suggestion->{$_} foreach qw<op suggested_by_anyone>;
59 $op = 'else' unless $op;
60
61 my ( $template, $borrowernumber, $cookie, @messages );
62 my $deleted = $input->param('deleted');
63 my $submitted = $input->param('submitted');
64
65 if ( C4::Context->preference("AnonSuggestions") or ( C4::Context->preference("OPACViewOthersSuggestions") and $op eq 'else' ) ) {
66     ( $template, $borrowernumber, $cookie ) = get_template_and_user(
67         {
68             template_name   => "opac-suggestions.tt",
69             query           => $input,
70             type            => "opac",
71             authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
72         }
73     );
74 }
75 else {
76     ( $template, $borrowernumber, $cookie ) = get_template_and_user(
77         {
78             template_name   => "opac-suggestions.tt",
79             query           => $input,
80             type            => "opac",
81             authnotrequired => 0,
82         }
83     );
84 }
85
86 if ( $op eq 'else' ) {
87     if ( C4::Context->preference("OPACViewOthersSuggestions") ) {
88         if ( $borrowernumber ) {
89             # A logged in user is able to see suggestions from others
90             $suggestion->{suggestedby} = $suggested_by_anyone
91                 ? undef
92                 : $borrowernumber;
93         }
94         else {
95             # Non logged in user is able to see all suggestions
96             $suggestion->{suggestedby} = undef;
97         }
98     }
99     else {
100         if ( $borrowernumber ) {
101             $suggestion->{suggestedby} = $borrowernumber;
102         }
103         else {
104             $suggestion->{suggestedby} = -1;
105         }
106     }
107 } else {
108     if ( $borrowernumber ) {
109         $suggestion->{suggestedby} = $borrowernumber;
110     }
111     else {
112         $suggestion->{suggestedby} = C4::Context->preference("AnonymousPatron");
113     }
114 }
115
116 my $patrons_pending_suggestions_count = 0;
117 if ( $borrowernumber && C4::Context->preference("MaxOpenSuggestions") ne '' ) {
118     $patrons_pending_suggestions_count = scalar @{ SearchSuggestion( { suggestedby => $borrowernumber, STATUS => 'ASKED' } ) } ;
119 }
120
121 my $suggestions_loop = &SearchSuggestion($suggestion);
122 if ( $op eq "add_confirm" ) {
123     if ( C4::Context->preference("MaxOpenSuggestions") ne '' && $patrons_pending_suggestions_count >= C4::Context->preference("MaxOpenSuggestions") ) #only check limit for signed in borrowers
124     {
125         push @messages, { type => 'error', code => 'too_many' };
126     }
127     elsif ( @$suggestions_loop >= 1 ) {
128
129         #some suggestion are answering the request Donot Add
130         for my $suggestion (@$suggestions_loop) {
131             push @messages,
132               {
133                 type => 'error',
134                 code => 'already_exists',
135                 id   => $suggestion->{suggestionid}
136               };
137             last;
138         }
139     }
140     else {
141         my $scrubber = C4::Scrubber->new();
142         foreach my $suggest ( keys %$suggestion ) {
143
144             # Don't know why the encode is needed for Perl v5.10 here
145             $suggestion->{$suggest} = Encode::encode( "utf8",
146                 $scrubber->scrub( $suggestion->{$suggest} ) );
147         }
148         $suggestion->{suggesteddate} = dt_from_string;
149         $suggestion->{branchcode} = $input->param('branchcode') || C4::Context->userenv->{"branch"};
150
151         &NewSuggestion($suggestion);
152         $patrons_pending_suggestions_count++;
153
154         # delete empty fields, to avoid filter in "SearchSuggestion"
155         foreach my $field ( qw( title author publishercode copyrightdate place collectiontitle isbn STATUS ) ) {
156             delete $suggestion->{$field}; #clear search filters (except borrower related) to show all suggestions after placing a new one
157         }
158         $suggestions_loop = &SearchSuggestion($suggestion);
159
160         push @messages, { type => 'info', code => 'success_on_inserted' };
161
162     }
163     $op = 'else';
164 }
165
166 if ( $op eq "delete_confirm" ) {
167     my @delete_field = $input->multi_param("delete_field");
168     foreach my $delete_field (@delete_field) {
169         &DelSuggestion( $borrowernumber, $delete_field );
170     }
171     $op = 'else';
172     print $input->redirect("/cgi-bin/koha/opac-suggestions.pl?op=else");
173     exit;
174 }
175
176 map{
177     my $s = $_;
178     my $library = Koha::Libraries->find($s->{branchcodesuggestedby});
179     $library ? $s->{branchcodesuggestedby} = $library->branchname : ()
180 } @$suggestions_loop;
181
182 foreach my $suggestion(@$suggestions_loop) {
183     if($suggestion->{'suggestedby'} == $borrowernumber) {
184         $suggestion->{'showcheckbox'} = $borrowernumber;
185     } else {
186         $suggestion->{'showcheckbox'} = 0;
187     }
188     if($suggestion->{'patronreason'}){
189         my $av = Koha::AuthorisedValues->search({ category => 'OPAC_SUG', authorised_value => $suggestion->{patronreason} });
190         $suggestion->{'patronreason'} = $av->count ? $av->next->opac_description : '';
191     }
192 }
193
194 my $patron_reason_loop = GetAuthorisedValues("OPAC_SUG");
195
196 # Is the person allowed to choose their branch
197 if ( C4::Context->preference("AllowPurchaseSuggestionBranchChoice") ) {
198     my ( $borr ) = GetMemberDetails( $borrowernumber );
199
200 # pass the pickup branch along....
201     my $userbranch = '';
202     if (C4::Context->userenv && C4::Context->userenv->{'branch'}) {
203         $userbranch = C4::Context->userenv->{'branch'};
204     }
205     my $branchcode = $input->param('branchcode') || $borr->{'branchcode'} || $userbranch || '' ;
206
207     $template->param( branchcode => $branchcode );
208 }
209
210 my $mandatoryfields = '';
211 {
212     last unless ($op eq 'add');
213     my $fldsreq_sp = C4::Context->preference("OPACSuggestionMandatoryFields") || 'title';
214     $mandatoryfields = join(', ', (map { '"'.$_.'"'; } sort split(/\s*\,\s*/, $fldsreq_sp)));
215 }
216
217 $template->param(
218     %$suggestion,
219     suggestions_loop      => $suggestions_loop,
220     patron_reason_loop    => $patron_reason_loop,
221     "op_$op"              => 1,
222     $op                   => 1,
223     messages              => \@messages,
224     suggestionsview       => 1,
225     suggested_by_anyone   => $suggested_by_anyone,
226     mandatoryfields       => $mandatoryfields,
227     patrons_pending_suggestions_count => $patrons_pending_suggestions_count,
228 );
229
230 output_html_with_http_headers $input, $cookie, $template->output;
231