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