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