Bug 12627: SQLHelper replacement - C4::Suggestions
[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;
22 use C4::Auth;    # get_template_and_user
23 use C4::Members;
24 use C4::Branch;
25 use C4::Koha;
26 use C4::Output;
27 use C4::Suggestions;
28 use C4::Koha;
29 use C4::Dates;
30 use C4::Scrubber;
31
32 my $input           = new CGI;
33 my $allsuggestions  = $input->param('showall');
34 my $op              = $input->param('op');
35 my $suggestion      = $input->Vars;
36 delete $suggestion->{negcap};
37 my $negcaptcha      = $input->param('negcap');
38
39 # If a spambot accidentally populates the 'negcap' field in the sugesstions form, then silently skip and return.
40 if ($negcaptcha ) {
41     print $input->redirect("/cgi-bin/koha/opac-suggestions.pl");
42     exit;
43 }
44
45 delete $$suggestion{$_} foreach qw<op suggestedbyme>;
46 $op = 'else' unless $op;
47
48 my ( $template, $borrowernumber, $cookie );
49 my $deleted = $input->param('deleted');
50 my $submitted = $input->param('submitted');
51
52 if ( C4::Context->preference("AnonSuggestions") ) {
53     ( $template, $borrowernumber, $cookie ) = get_template_and_user(
54         {
55             template_name   => "opac-suggestions.tt",
56             query           => $input,
57             type            => "opac",
58             authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
59         }
60     );
61     if ( !$$suggestion{suggestedby} ) {
62         $$suggestion{suggestedby} = C4::Context->preference("AnonymousPatron");
63     }
64 }
65 else {
66     ( $template, $borrowernumber, $cookie ) = get_template_and_user(
67         {
68             template_name   => "opac-suggestions.tt",
69             query           => $input,
70             type            => "opac",
71             authnotrequired => 0,
72         }
73     );
74 }
75 if ($allsuggestions){
76         delete $$suggestion{suggestedby};
77 }
78 else {
79         $$suggestion{suggestedby} ||= $borrowernumber unless ($allsuggestions);
80 }
81 # warn "bornum:",$borrowernumber;
82
83 my $suggestions_loop =
84   &SearchSuggestion( $suggestion);
85 if ( $op eq "add_confirm" ) {
86         if (@$suggestions_loop>=1){
87                 #some suggestion are answering the request Donot Add
88         }
89         else {
90                 my $scrubber = C4::Scrubber->new();
91                 foreach my $suggest (keys %$suggestion){
92                     $suggestion->{$suggest} = $scrubber->scrub($suggestion->{$suggest});
93                 }
94                 $$suggestion{'suggesteddate'}=C4::Dates->today;
95                 $$suggestion{'branchcode'}= $input->param('branch') || C4::Context->userenv->{"branch"};
96
97                 &NewSuggestion($suggestion);
98                 # empty fields, to avoid filter in "SearchSuggestion"
99                 $$suggestion{$_}='' foreach qw<title author publishercode copyrightdate place collectiontitle isbn STATUS>;
100                 $suggestions_loop =
101                    &SearchSuggestion( $suggestion );
102         }
103         $op              = 'else';
104     print $input->redirect("/cgi-bin/koha/opac-suggestions.pl?op=else&submitted=1");
105     exit;
106 }
107
108 if ( $op eq "delete_confirm" ) {
109     my @delete_field = $input->param("delete_field");
110     foreach my $delete_field (@delete_field) {
111         &DelSuggestion( $borrowernumber, $delete_field );
112     }
113     $op = 'else';
114     print $input->redirect("/cgi-bin/koha/opac-suggestions.pl?op=else&deleted=1");
115     exit;
116 }
117 map{ $_->{'branchcodesuggestedby'}=GetBranchInfo($_->{'branchcodesuggestedby'})->[0]->{'branchname'}} @$suggestions_loop;
118 my $supportlist=GetSupportList();
119 foreach my $support(@$supportlist){
120         if ($$support{'imageurl'}){
121                 $$support{'imageurl'}= getitemtypeimagelocation( 'opac', $$support{'imageurl'} );
122         }
123         else {
124            delete $$support{'imageurl'}
125         }
126 }
127
128 foreach my $suggestion(@$suggestions_loop) {
129     if($suggestion->{'suggestedby'} == $borrowernumber) {
130         $suggestion->{'showcheckbox'} = $borrowernumber;
131     } else {
132         $suggestion->{'showcheckbox'} = 0;
133     }
134     if($suggestion->{'patronreason'}){
135         $suggestion->{'patronreason'} = GetKohaAuthorisedValueLib("OPAC_SUG",$suggestion->{'patronreason'},1);
136     }
137 }
138
139 my $patron_reason_loop = GetAuthorisedValues("OPAC_SUG");
140
141 # Is the person allowed to choose their branch
142 if ( C4::Context->preference("AllowPurchaseSuggestionBranchChoice") ) {
143     my ( $borr ) = GetMemberDetails( $borrowernumber );
144
145 # pass the pickup branch along....
146     my $userbranch = '';
147     if (C4::Context->userenv && C4::Context->userenv->{'branch'}) {
148         $userbranch = C4::Context->userenv->{'branch'};
149     }
150     my $branch = $input->param('branch') || $borr->{'branchcode'} || $userbranch || '' ;
151
152 # make branch selection options...
153     my $branchloop = GetBranchesLoop($branch);
154     $template->param( branchloop => $branchloop );
155 }
156
157 $template->param(
158         %$suggestion,
159         itemtypeloop=> $supportlist,
160     suggestions_loop => $suggestions_loop,
161     patron_reason_loop => $patron_reason_loop,
162     showall    => $allsuggestions,
163     "op_$op"         => 1,
164     suggestionsview => 1,
165 );
166
167 output_html_with_http_headers $input, $cookie, $template->output;
168