Bug 11944: Fix suggestion at the OPAC for perl v5.10
[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 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 $allsuggestions  = $input->param('showall');
37 my $op              = $input->param('op');
38 my $suggestion      = $input->Vars;
39 delete $suggestion->{negcap};
40 my $negcaptcha      = $input->param('negcap');
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 }
47
48 delete $$suggestion{$_} foreach qw<op suggestedbyme>;
49 $op = 'else' unless $op;
50
51 my ( $template, $borrowernumber, $cookie );
52 my $deleted = $input->param('deleted');
53 my $submitted = $input->param('submitted');
54
55 if ( C4::Context->preference("AnonSuggestions") ) {
56     ( $template, $borrowernumber, $cookie ) = get_template_and_user(
57         {
58             template_name   => "opac-suggestions.tt",
59             query           => $input,
60             type            => "opac",
61             authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
62         }
63     );
64     if ( !$$suggestion{suggestedby} ) {
65         $$suggestion{suggestedby} = C4::Context->preference("AnonymousPatron");
66     }
67 }
68 else {
69     ( $template, $borrowernumber, $cookie ) = get_template_and_user(
70         {
71             template_name   => "opac-suggestions.tt",
72             query           => $input,
73             type            => "opac",
74             authnotrequired => 0,
75         }
76     );
77 }
78 if ($allsuggestions){
79         delete $$suggestion{suggestedby};
80 }
81 else {
82         $$suggestion{suggestedby} ||= $borrowernumber unless ($allsuggestions);
83 }
84 # warn "bornum:",$borrowernumber;
85
86 my $suggestions_loop =
87   &SearchSuggestion( $suggestion);
88 if ( $op eq "add_confirm" ) {
89         if (@$suggestions_loop>=1){
90                 #some suggestion are answering the request Donot Add
91         }
92         else {
93                 my $scrubber = C4::Scrubber->new();
94                 foreach my $suggest (keys %$suggestion){
95             # Don't know why the encode is needed for Perl v5.10 here
96             $suggestion->{$suggest} = Encode::encode("utf8", $scrubber->scrub($suggestion->{$suggest}) );
97                 }
98         $suggestion->{suggesteddate} = dt_from_string;
99         $suggestion->{branchcode} = $input->param('branchcode') || C4::Context->userenv->{"branch"};
100
101                 &NewSuggestion($suggestion);
102                 # empty fields, to avoid filter in "SearchSuggestion"
103                 $$suggestion{$_}='' foreach qw<title author publishercode copyrightdate place collectiontitle isbn STATUS>;
104                 $suggestions_loop =
105                    &SearchSuggestion( $suggestion );
106         }
107         $op              = 'else';
108     print $input->redirect("/cgi-bin/koha/opac-suggestions.pl?op=else&submitted=1");
109     exit;
110 }
111
112 if ( $op eq "delete_confirm" ) {
113     my @delete_field = $input->param("delete_field");
114     foreach my $delete_field (@delete_field) {
115         &DelSuggestion( $borrowernumber, $delete_field );
116     }
117     $op = 'else';
118     print $input->redirect("/cgi-bin/koha/opac-suggestions.pl?op=else&deleted=1");
119     exit;
120 }
121 map{ $_->{'branchcodesuggestedby'}=GetBranchInfo($_->{'branchcodesuggestedby'})->[0]->{'branchname'}} @$suggestions_loop;
122 my $supportlist=GetSupportList();
123 foreach my $support(@$supportlist){
124         if ($$support{'imageurl'}){
125                 $$support{'imageurl'}= getitemtypeimagelocation( 'opac', $$support{'imageurl'} );
126         }
127         else {
128            delete $$support{'imageurl'}
129         }
130 }
131
132 foreach my $suggestion(@$suggestions_loop) {
133     if($suggestion->{'suggestedby'} == $borrowernumber) {
134         $suggestion->{'showcheckbox'} = $borrowernumber;
135     } else {
136         $suggestion->{'showcheckbox'} = 0;
137     }
138     if($suggestion->{'patronreason'}){
139         $suggestion->{'patronreason'} = GetKohaAuthorisedValueLib("OPAC_SUG",$suggestion->{'patronreason'},1);
140     }
141 }
142
143 my $patron_reason_loop = GetAuthorisedValues("OPAC_SUG");
144
145 # Is the person allowed to choose their branch
146 if ( C4::Context->preference("AllowPurchaseSuggestionBranchChoice") ) {
147     my ( $borr ) = GetMemberDetails( $borrowernumber );
148
149 # pass the pickup branch along....
150     my $userbranch = '';
151     if (C4::Context->userenv && C4::Context->userenv->{'branch'}) {
152         $userbranch = C4::Context->userenv->{'branch'};
153     }
154     my $branchcode = $input->param('branchcode') || $borr->{'branchcode'} || $userbranch || '' ;
155
156 # make branch selection options...
157     my $branchloop = GetBranchesLoop($branchcode);
158     $template->param( branchloop => $branchloop );
159 }
160
161 $template->param(
162         %$suggestion,
163         itemtypeloop=> $supportlist,
164     suggestions_loop => $suggestions_loop,
165     patron_reason_loop => $patron_reason_loop,
166     showall    => $allsuggestions,
167     "op_$op"         => 1,
168     suggestionsview => 1,
169 );
170
171 output_html_with_http_headers $input, $cookie, $template->output;
172