Bug 17301 - Add callnumber to label-edit-batch.pl
[koha.git] / opac / opac-suggestions.pl
blob388e04aa7e26f485f844d646a1f9ec5adbd1b2dc
1 #!/usr/bin/perl
3 # This file is part of Koha.
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.
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.
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>.
18 use strict;
19 use warnings;
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 use Koha::Libraries;
32 use Koha::DateUtils qw( dt_from_string );
34 my $input = new CGI;
35 my $op = $input->param('op');
36 my $suggestion = $input->Vars;
37 my $negcaptcha = $input->param('negcap');
38 my $suggested_by_anyone = $input->param('suggested_by_anyone') || 0;
40 # If a spambot accidentally populates the 'negcap' field in the sugesstions form, then silently skip and return.
41 if ($negcaptcha ) {
42 print $input->redirect("/cgi-bin/koha/opac-suggestions.pl");
43 exit;
44 } else {
45 # don't pass 'negcap' column to DB, else DBI::Class will error
46 # DBIx::Class::Row::store_column(): No such column 'negcap' on Koha::Schema::Result::Suggestion at Koha/C4/Suggestions.pm
47 delete $suggestion->{negcap};
50 #If suggestions are turned off we redirect to 404 error. This will also redirect guest suggestions
51 if ( ! C4::Context->preference('suggestion') ) {
52 print $input->redirect("/cgi-bin/koha/errors/404.pl");
53 exit;
56 delete $suggestion->{$_} foreach qw<op suggested_by_anyone>;
57 $op = 'else' unless $op;
59 my ( $template, $borrowernumber, $cookie, @messages );
60 my $deleted = $input->param('deleted');
61 my $submitted = $input->param('submitted');
63 if ( C4::Context->preference("AnonSuggestions") or ( C4::Context->preference("OPACViewOthersSuggestions") and $op eq 'else' ) ) {
64 ( $template, $borrowernumber, $cookie ) = get_template_and_user(
66 template_name => "opac-suggestions.tt",
67 query => $input,
68 type => "opac",
69 authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
73 else {
74 ( $template, $borrowernumber, $cookie ) = get_template_and_user(
76 template_name => "opac-suggestions.tt",
77 query => $input,
78 type => "opac",
79 authnotrequired => 0,
84 if ( $op eq 'else' ) {
85 if ( C4::Context->preference("OPACViewOthersSuggestions") ) {
86 if ( $borrowernumber ) {
87 # A logged in user is able to see suggestions from others
88 $suggestion->{suggestedby} = $suggested_by_anyone
89 ? undef
90 : $borrowernumber;
92 else {
93 # Non logged in user is able to see all suggestions
94 $suggestion->{suggestedby} = undef;
97 else {
98 if ( $borrowernumber ) {
99 $suggestion->{suggestedby} = $borrowernumber;
101 else {
102 $suggestion->{suggestedby} = -1;
105 } else {
106 if ( $borrowernumber ) {
107 $suggestion->{suggestedby} = $borrowernumber;
109 else {
110 $suggestion->{suggestedby} = C4::Context->preference("AnonymousPatron");
114 my $patrons_pending_suggestions_count = 0;
115 if ( $borrowernumber && C4::Context->preference("MaxOpenSuggestions") ne '' ) {
116 $patrons_pending_suggestions_count = scalar @{ SearchSuggestion( { suggestedby => $borrowernumber, STATUS => 'ASKED' } ) } ;
119 my $suggestions_loop = &SearchSuggestion($suggestion);
120 if ( $op eq "add_confirm" ) {
121 if ( C4::Context->preference("MaxOpenSuggestions") ne '' && $patrons_pending_suggestions_count >= C4::Context->preference("MaxOpenSuggestions") ) #only check limit for signed in borrowers
123 push @messages, { type => 'error', code => 'too_many' };
125 elsif ( @$suggestions_loop >= 1 ) {
127 #some suggestion are answering the request Donot Add
128 for my $suggestion (@$suggestions_loop) {
129 push @messages,
131 type => 'error',
132 code => 'already_exists',
133 id => $suggestion->{suggestionid}
135 last;
138 else {
139 my $scrubber = C4::Scrubber->new();
140 foreach my $suggest ( keys %$suggestion ) {
142 # Don't know why the encode is needed for Perl v5.10 here
143 $suggestion->{$suggest} = Encode::encode( "utf8",
144 $scrubber->scrub( $suggestion->{$suggest} ) );
146 $suggestion->{suggesteddate} = dt_from_string;
147 $suggestion->{branchcode} = $input->param('branchcode') || C4::Context->userenv->{"branch"};
149 &NewSuggestion($suggestion);
150 $patrons_pending_suggestions_count++;
152 # delete empty fields, to avoid filter in "SearchSuggestion"
153 foreach my $field ( qw( title author publishercode copyrightdate place collectiontitle isbn STATUS ) ) {
154 delete $suggestion->{$field}; #clear search filters (except borrower related) to show all suggestions after placing a new one
156 $suggestions_loop = &SearchSuggestion($suggestion);
158 push @messages, { type => 'info', code => 'success_on_inserted' };
161 $op = 'else';
164 if ( $op eq "delete_confirm" ) {
165 my @delete_field = $input->multi_param("delete_field");
166 foreach my $delete_field (@delete_field) {
167 &DelSuggestion( $borrowernumber, $delete_field );
169 $op = 'else';
170 print $input->redirect("/cgi-bin/koha/opac-suggestions.pl?op=else");
171 exit;
174 map{
175 my $s = $_;
176 my $library = Koha::Libraries->find($s->{branchcodesuggestedby});
177 $library ? $s->{branchcodesuggestedby} = $library->branchname : ()
178 } @$suggestions_loop;
180 foreach my $suggestion(@$suggestions_loop) {
181 if($suggestion->{'suggestedby'} == $borrowernumber) {
182 $suggestion->{'showcheckbox'} = $borrowernumber;
183 } else {
184 $suggestion->{'showcheckbox'} = 0;
186 if($suggestion->{'patronreason'}){
187 $suggestion->{'patronreason'} = GetKohaAuthorisedValueLib("OPAC_SUG",$suggestion->{'patronreason'},1);
191 my $patron_reason_loop = GetAuthorisedValues("OPAC_SUG");
193 # Is the person allowed to choose their branch
194 if ( C4::Context->preference("AllowPurchaseSuggestionBranchChoice") ) {
195 my ( $borr ) = GetMemberDetails( $borrowernumber );
197 # pass the pickup branch along....
198 my $userbranch = '';
199 if (C4::Context->userenv && C4::Context->userenv->{'branch'}) {
200 $userbranch = C4::Context->userenv->{'branch'};
202 my $branchcode = $input->param('branchcode') || $borr->{'branchcode'} || $userbranch || '' ;
204 $template->param( branchcode => $branchcode );
207 my $mandatoryfields = '';
209 last unless ($op eq 'add');
210 my $fldsreq_sp = C4::Context->preference("OPACSuggestionMandatoryFields") || 'title';
211 $mandatoryfields = join(', ', (map { '"'.$_.'"'; } sort split(/\s*\,\s*/, $fldsreq_sp)));
214 $template->param(
215 %$suggestion,
216 suggestions_loop => $suggestions_loop,
217 patron_reason_loop => $patron_reason_loop,
218 "op_$op" => 1,
219 $op => 1,
220 messages => \@messages,
221 suggestionsview => 1,
222 suggested_by_anyone => $suggested_by_anyone,
223 mandatoryfields => $mandatoryfields,
224 patrons_pending_suggestions_count => $patrons_pending_suggestions_count,
227 output_html_with_http_headers $input, $cookie, $template->output;