Bug 10752: Alert if the suggestion has not been added - OPAC
[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::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, @messages );
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         for my $suggestion ( @$suggestions_loop ) {
98             push @messages, { type => 'error', code => 'already_exists', id => $suggestion->{suggestionid} };
99             last;
100         }
101         }
102         else {
103                 my $scrubber = C4::Scrubber->new();
104                 foreach my $suggest (keys %$suggestion){
105             # Don't know why the encode is needed for Perl v5.10 here
106             $suggestion->{$suggest} = Encode::encode("utf8", $scrubber->scrub($suggestion->{$suggest}) );
107                 }
108         $suggestion->{suggesteddate} = dt_from_string;
109         $suggestion->{branchcode} = $input->param('branchcode') || C4::Context->userenv->{"branch"};
110
111                 &NewSuggestion($suggestion);
112                 # empty fields, to avoid filter in "SearchSuggestion"
113                 $$suggestion{$_}='' foreach qw<title author publishercode copyrightdate place collectiontitle isbn STATUS>;
114                 $suggestions_loop =
115                    &SearchSuggestion( $suggestion );
116         push @messages, { type => 'info', code => 'success_on_inserted' };
117         }
118     $op = 'else';
119 }
120
121 if ( $op eq "delete_confirm" ) {
122     my @delete_field = $input->param("delete_field");
123     foreach my $delete_field (@delete_field) {
124         &DelSuggestion( $borrowernumber, $delete_field );
125     }
126     $op = 'else';
127     print $input->redirect("/cgi-bin/koha/opac-suggestions.pl?op=else");
128     exit;
129 }
130 map{ $_->{'branchcodesuggestedby'}=GetBranchInfo($_->{'branchcodesuggestedby'})->[0]->{'branchname'}} @$suggestions_loop;
131 my $supportlist=GetSupportList();
132 foreach my $support(@$supportlist){
133         if ($$support{'imageurl'}){
134                 $$support{'imageurl'}= getitemtypeimagelocation( 'opac', $$support{'imageurl'} );
135         }
136         else {
137            delete $$support{'imageurl'}
138         }
139 }
140
141 foreach my $suggestion(@$suggestions_loop) {
142     if($suggestion->{'suggestedby'} == $borrowernumber) {
143         $suggestion->{'showcheckbox'} = $borrowernumber;
144     } else {
145         $suggestion->{'showcheckbox'} = 0;
146     }
147     if($suggestion->{'patronreason'}){
148         $suggestion->{'patronreason'} = GetKohaAuthorisedValueLib("OPAC_SUG",$suggestion->{'patronreason'},1);
149     }
150 }
151
152 my $patron_reason_loop = GetAuthorisedValues("OPAC_SUG");
153
154 # Is the person allowed to choose their branch
155 if ( C4::Context->preference("AllowPurchaseSuggestionBranchChoice") ) {
156     my ( $borr ) = GetMemberDetails( $borrowernumber );
157
158 # pass the pickup branch along....
159     my $userbranch = '';
160     if (C4::Context->userenv && C4::Context->userenv->{'branch'}) {
161         $userbranch = C4::Context->userenv->{'branch'};
162     }
163     my $branchcode = $input->param('branchcode') || $borr->{'branchcode'} || $userbranch || '' ;
164
165 # make branch selection options...
166     my $branchloop = GetBranchesLoop($branchcode);
167     $template->param( branchloop => $branchloop );
168 }
169
170 $template->param(
171         %$suggestion,
172         itemtypeloop=> $supportlist,
173     suggestions_loop => $suggestions_loop,
174     patron_reason_loop => $patron_reason_loop,
175     showall    => $allsuggestions,
176     "op_$op"         => 1,
177     $op => 1,
178     messages => \@messages,
179     suggestionsview => 1,
180 );
181
182 output_html_with_http_headers $input, $cookie, $template->output;
183