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>.
22 use Encode qw( encode );
23 use C4::Auth; # get_template_and_user
33 use Koha::DateUtils qw( dt_from_string );
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;
41 # If a spambot accidentally populates the 'negcap' field in the sugesstions form, then silently skip and return.
43 print $input->redirect("/cgi-bin/koha/opac-suggestions.pl");
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};
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");
57 delete $suggestion->{$_} foreach qw<op suggested_by_anyone>;
58 $op = 'else' unless $op;
60 my ( $template, $borrowernumber, $cookie, @messages );
61 my $deleted = $input->param('deleted');
62 my $submitted = $input->param('submitted');
64 if ( C4::Context->preference("AnonSuggestions") or ( C4::Context->preference("OPACViewOthersSuggestions") and $op eq 'else' ) ) {
65 ( $template, $borrowernumber, $cookie ) = get_template_and_user(
67 template_name => "opac-suggestions.tt",
70 authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
75 ( $template, $borrowernumber, $cookie ) = get_template_and_user(
77 template_name => "opac-suggestions.tt",
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
94 # Non logged in user is able to see all suggestions
95 $suggestion->{suggestedby} = undef;
99 if ( $borrowernumber ) {
100 $suggestion->{suggestedby} = $borrowernumber;
103 $suggestion->{suggestedby} = -1;
107 if ( $borrowernumber ) {
108 $suggestion->{suggestedby} = $borrowernumber;
111 $suggestion->{suggestedby} = C4::Context->preference("AnonymousPatron");
115 my $patrons_pending_suggestions_count = 0;
116 if ( $borrowernumber ) {
117 $patrons_pending_suggestions_count = scalar @{ SearchSuggestion( { suggestedby => $borrowernumber, STATUS => 'ASKED' } ) } ;
120 my $suggestions_loop = &SearchSuggestion($suggestion);
121 if ( $op eq "add_confirm" ) {
122 if ( C4::Context->preference("MaxOpenSuggestions") ne '' && $patrons_pending_suggestions_count >= C4::Context->preference("MaxOpenSuggestions") ) #only check limit for signed in borrowers
124 push @messages, { type => 'error', code => 'too_many' };
126 elsif ( @$suggestions_loop >= 1 ) {
128 #some suggestion are answering the request Donot Add
129 for my $suggestion (@$suggestions_loop) {
133 code => 'already_exists',
134 id => $suggestion->{suggestionid}
140 my $scrubber = C4::Scrubber->new();
141 foreach my $suggest ( keys %$suggestion ) {
143 # Don't know why the encode is needed for Perl v5.10 here
144 $suggestion->{$suggest} = Encode::encode( "utf8",
145 $scrubber->scrub( $suggestion->{$suggest} ) );
147 $suggestion->{suggesteddate} = dt_from_string;
148 $suggestion->{branchcode} = $input->param('branchcode') || C4::Context->userenv->{"branch"};
150 &NewSuggestion($suggestion);
151 push @messages, { type => 'info', code => 'success_on_inserted' };
154 # delete empty fields, to avoid filter in "SearchSuggestion" and load all suggestions for display
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
158 $suggestions_loop = &SearchSuggestion($suggestion);
162 if ( $op eq "delete_confirm" ) {
163 my @delete_field = $input->multi_param("delete_field");
164 foreach my $delete_field (@delete_field) {
165 &DelSuggestion( $borrowernumber, $delete_field );
168 print $input->redirect("/cgi-bin/koha/opac-suggestions.pl?op=else");
174 my $library = Koha::Libraries->find($s->{branchcodesuggestedby});
175 $library ? $s->{branchcodesuggestedby} = $library->branchname : ()
176 } @$suggestions_loop;
178 foreach my $suggestion(@$suggestions_loop) {
179 if($suggestion->{'suggestedby'} == $borrowernumber) {
180 $suggestion->{'showcheckbox'} = $borrowernumber;
182 $suggestion->{'showcheckbox'} = 0;
184 if($suggestion->{'patronreason'}){
185 $suggestion->{'patronreason'} = GetKohaAuthorisedValueLib("OPAC_SUG",$suggestion->{'patronreason'},1);
189 my $patron_reason_loop = GetAuthorisedValues("OPAC_SUG");
191 # Is the person allowed to choose their branch
192 if ( C4::Context->preference("AllowPurchaseSuggestionBranchChoice") ) {
193 my ( $borr ) = GetMemberDetails( $borrowernumber );
195 # pass the pickup branch along....
197 if (C4::Context->userenv && C4::Context->userenv->{'branch'}) {
198 $userbranch = C4::Context->userenv->{'branch'};
200 my $branchcode = $input->param('branchcode') || $borr->{'branchcode'} || $userbranch || '' ;
202 # make branch selection options...
203 my $branchloop = GetBranchesLoop($branchcode);
204 $template->param( branchloop => $branchloop );
209 suggestions_loop => $suggestions_loop,
210 patron_reason_loop => $patron_reason_loop,
213 messages => \@messages,
214 suggestionsview => 1,
215 suggested_by_anyone => $suggested_by_anyone,
216 patrons_pending_suggestions_count => $patrons_pending_suggestions_count,
219 output_html_with_http_headers $input, $cookie, $template->output;