Bug 14888: Revert "Bug 14888: (QA followup) remove unused lib/var"
[koha.git] / opac / opac-suggestions.pl
blob6b7f8d4770e0db6f47ddb1f390923c819bb987dc
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::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;
33 use Koha::DateUtils qw( dt_from_string );
35 my $input = new CGI;
36 my $op = $input->param('op');
37 my $suggestion = $input->Vars;
38 delete $suggestion->{negcap};
39 my $negcaptcha = $input->param('negcap');
40 my $suggested_by_anyone = $input->param('suggested_by_anyone') || 0;
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;
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;
54 delete $suggestion->{$_} foreach qw<op suggested_by_anyone>;
55 $op = 'else' unless $op;
57 my ( $template, $borrowernumber, $cookie, @messages );
58 my $deleted = $input->param('deleted');
59 my $submitted = $input->param('submitted');
61 if ( C4::Context->preference("AnonSuggestions") or ( C4::Context->preference("OPACViewOthersSuggestions") and $op eq 'else' ) ) {
62 ( $template, $borrowernumber, $cookie ) = get_template_and_user(
64 template_name => "opac-suggestions.tt",
65 query => $input,
66 type => "opac",
67 authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
71 else {
72 ( $template, $borrowernumber, $cookie ) = get_template_and_user(
74 template_name => "opac-suggestions.tt",
75 query => $input,
76 type => "opac",
77 authnotrequired => 0,
82 if ( $op eq 'else' ) {
83 if ( C4::Context->preference("OPACViewOthersSuggestions") ) {
84 if ( $borrowernumber ) {
85 # A logged in user is able to see suggestions from others
86 $suggestion->{suggestedby} = $suggested_by_anyone
87 ? undef
88 : $borrowernumber;
90 else {
91 # Non logged in user is able to see all suggestions
92 $suggestion->{suggestedby} = undef;
95 else {
96 if ( $borrowernumber ) {
97 $suggestion->{suggestedby} = $borrowernumber;
99 else {
100 $suggestion->{suggestedby} = -1;
103 } else {
104 if ( $borrowernumber ) {
105 $suggestion->{suggestedby} = $borrowernumber;
107 else {
108 $suggestion->{suggestedby} = C4::Context->preference("AnonymousPatron");
112 my $suggestions_loop =
113 &SearchSuggestion( $suggestion);
114 if ( $op eq "add_confirm" ) {
115 if (@$suggestions_loop>=1){
116 #some suggestion are answering the request Donot Add
117 for my $suggestion ( @$suggestions_loop ) {
118 push @messages, { type => 'error', code => 'already_exists', id => $suggestion->{suggestionid} };
119 last;
122 else {
123 my $scrubber = C4::Scrubber->new();
124 foreach my $suggest (keys %$suggestion){
125 # Don't know why the encode is needed for Perl v5.10 here
126 $suggestion->{$suggest} = Encode::encode("utf8", $scrubber->scrub($suggestion->{$suggest}) );
128 $suggestion->{suggesteddate} = dt_from_string;
129 $suggestion->{branchcode} = $input->param('branchcode') || C4::Context->userenv->{"branch"};
131 &NewSuggestion($suggestion);
132 # empty fields, to avoid filter in "SearchSuggestion"
133 $$suggestion{$_}='' foreach qw<title author publishercode copyrightdate place collectiontitle isbn STATUS>;
134 $suggestions_loop =
135 &SearchSuggestion( $suggestion );
136 push @messages, { type => 'info', code => 'success_on_inserted' };
138 $op = 'else';
141 if ( $op eq "delete_confirm" ) {
142 my @delete_field = $input->param("delete_field");
143 foreach my $delete_field (@delete_field) {
144 &DelSuggestion( $borrowernumber, $delete_field );
146 $op = 'else';
147 print $input->redirect("/cgi-bin/koha/opac-suggestions.pl?op=else");
148 exit;
150 map{ $_->{'branchcodesuggestedby'}=GetBranchInfo($_->{'branchcodesuggestedby'})->[0]->{'branchname'}} @$suggestions_loop;
151 my $supportlist=GetSupportList();
152 foreach my $support(@$supportlist){
153 if ($$support{'imageurl'}){
154 $$support{'imageurl'}= getitemtypeimagelocation( 'opac', $$support{'imageurl'} );
156 else {
157 delete $$support{'imageurl'}
161 foreach my $suggestion(@$suggestions_loop) {
162 if($suggestion->{'suggestedby'} == $borrowernumber) {
163 $suggestion->{'showcheckbox'} = $borrowernumber;
164 } else {
165 $suggestion->{'showcheckbox'} = 0;
167 if($suggestion->{'patronreason'}){
168 $suggestion->{'patronreason'} = GetKohaAuthorisedValueLib("OPAC_SUG",$suggestion->{'patronreason'},1);
172 my $patron_reason_loop = GetAuthorisedValues("OPAC_SUG");
174 # Is the person allowed to choose their branch
175 if ( C4::Context->preference("AllowPurchaseSuggestionBranchChoice") ) {
176 my ( $borr ) = GetMemberDetails( $borrowernumber );
178 # pass the pickup branch along....
179 my $userbranch = '';
180 if (C4::Context->userenv && C4::Context->userenv->{'branch'}) {
181 $userbranch = C4::Context->userenv->{'branch'};
183 my $branchcode = $input->param('branchcode') || $borr->{'branchcode'} || $userbranch || '' ;
185 # make branch selection options...
186 my $branchloop = GetBranchesLoop($branchcode);
187 $template->param( branchloop => $branchloop );
190 $template->param(
191 %$suggestion,
192 itemtypeloop=> $supportlist,
193 suggestions_loop => $suggestions_loop,
194 patron_reason_loop => $patron_reason_loop,
195 "op_$op" => 1,
196 $op => 1,
197 messages => \@messages,
198 suggestionsview => 1,
199 suggested_by_anyone => $suggested_by_anyone,
202 output_html_with_http_headers $input, $cookie, $template->output;