Bug 24214: Fix due date formatting when sticky
[koha.git] / opac / opac-suggestions.pl
blob9f7337be626ba06f5fefc7c6fc19126c869dc337
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 Modern::Perl;
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::Koha;
26 use C4::Output;
27 use C4::Suggestions;
28 use C4::Koha;
29 use C4::Scrubber;
30 use C4::Search qw( FindDuplicate );
32 use Koha::AuthorisedValues;
33 use Koha::Libraries;
34 use Koha::Patrons;
36 use Koha::DateUtils qw( dt_from_string );
38 my $input = new CGI;
39 my $op = $input->param('op');
40 my $biblionumber = $input->param('biblionumber');
41 my $suggestion = $input->Vars;
42 my $negcaptcha = $input->param('negcap');
43 my $suggested_by_anyone = $input->param('suggested_by_anyone') || 0;
44 my $need_confirm = 0;
46 # If a spambot accidentally populates the 'negcap' field in the sugesstions form, then silently skip and return.
47 if ($negcaptcha ) {
48 print $input->redirect("/cgi-bin/koha/opac-suggestions.pl");
49 exit;
50 } else {
51 # don't pass 'negcap' column to DB, else DBI::Class will error
52 # DBIx::Class::Row::store_column(): No such column 'negcap' on Koha::Schema::Result::Suggestion at Koha/C4/Suggestions.pm
53 delete $suggestion->{negcap};
56 #If suggestions are turned off we redirect to 404 error. This will also redirect guest suggestions
57 if ( ! C4::Context->preference('suggestion') ) {
58 print $input->redirect("/cgi-bin/koha/errors/404.pl");
59 exit;
62 delete $suggestion->{$_} foreach qw<op suggested_by_anyone confirm>;
63 $op = 'else' unless $op;
65 my ( $template, $borrowernumber, $cookie, @messages );
66 my $deleted = $input->param('deleted');
67 my $submitted = $input->param('submitted');
69 if ( ( C4::Context->preference("AnonSuggestions") and Koha::Patrons->find( C4::Context->preference("AnonymousPatron") ) ) or ( C4::Context->preference("OPACViewOthersSuggestions") and $op eq 'else' ) ) {
70 ( $template, $borrowernumber, $cookie ) = get_template_and_user(
72 template_name => "opac-suggestions.tt",
73 query => $input,
74 type => "opac",
75 authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
79 else {
80 ( $template, $borrowernumber, $cookie ) = get_template_and_user(
82 template_name => "opac-suggestions.tt",
83 query => $input,
84 type => "opac",
85 authnotrequired => 0,
90 if ( $op eq 'else' ) {
91 if ( C4::Context->preference("OPACViewOthersSuggestions") ) {
92 if ( $borrowernumber ) {
93 # A logged in user is able to see suggestions from others
94 $suggestion->{suggestedby} = $suggested_by_anyone
95 ? undef
96 : $borrowernumber;
98 else {
99 # Non logged in user is able to see all suggestions
100 $suggestion->{suggestedby} = undef;
103 else {
104 if ( $borrowernumber ) {
105 $suggestion->{suggestedby} = $borrowernumber;
107 else {
108 $suggestion->{suggestedby} = -1;
111 } else {
112 if ( $borrowernumber ) {
113 $suggestion->{suggestedby} = $borrowernumber;
115 else {
116 $suggestion->{suggestedby} = C4::Context->preference("AnonymousPatron");
120 if ( $op eq "add_validate" && not $biblionumber ) { # If we are creating the suggestion from an existing record we do not want to search for duplicates
121 $op = 'add_confirm';
122 my $biblio = MarcRecordFromNewSuggestion($suggestion);
123 if ( my ($duplicatebiblionumber, $duplicatetitle) = FindDuplicate($biblio) ) {
124 push @messages, { type => 'error', code => 'biblio_exists', id => $duplicatebiblionumber, title => $duplicatetitle };
125 $need_confirm = 1;
126 $op = 'add';
130 my $patrons_pending_suggestions_count = 0;
131 if ( $borrowernumber && C4::Context->preference("MaxOpenSuggestions") ne '' ) {
132 $patrons_pending_suggestions_count = scalar @{ SearchSuggestion( { suggestedby => $borrowernumber, STATUS => 'ASKED' } ) } ;
135 if ( $op eq "add_confirm" ) {
136 my $suggestions_loop = &SearchSuggestion($suggestion);
137 if ( C4::Context->preference("MaxOpenSuggestions") ne '' && $patrons_pending_suggestions_count >= C4::Context->preference("MaxOpenSuggestions") ) #only check limit for signed in borrowers
139 push @messages, { type => 'error', code => 'too_many' };
141 elsif ( @$suggestions_loop >= 1 ) {
143 #some suggestion are answering the request Donot Add
144 for my $suggestion (@$suggestions_loop) {
145 push @messages,
147 type => 'error',
148 code => 'already_exists',
149 id => $suggestion->{suggestionid}
151 last;
154 else {
155 my $scrubber = C4::Scrubber->new();
156 foreach my $suggest ( keys %$suggestion ) {
158 # Don't know why the encode is needed for Perl v5.10 here
159 $suggestion->{$suggest} = Encode::encode( "utf8",
160 $scrubber->scrub( $suggestion->{$suggest} ) );
162 $suggestion->{suggesteddate} = dt_from_string;
163 $suggestion->{branchcode} = $input->param('branchcode') || C4::Context->userenv->{"branch"};
165 &NewSuggestion($suggestion);
166 $patrons_pending_suggestions_count++;
168 # delete empty fields, to avoid filter in "SearchSuggestion"
169 foreach my $field ( qw( title author publishercode copyrightdate place collectiontitle isbn STATUS ) ) {
170 delete $suggestion->{$field}; #clear search filters (except borrower related) to show all suggestions after placing a new one
172 $suggestions_loop = &SearchSuggestion($suggestion);
174 push @messages, { type => 'info', code => 'success_on_inserted' };
177 $op = 'else';
180 my $suggestions_loop = &SearchSuggestion({suggestedby => $suggestion->{suggestedby}});
181 if ( $op eq "delete_confirm" ) {
182 my @delete_field = $input->multi_param("delete_field");
183 foreach my $delete_field (@delete_field) {
184 &DelSuggestion( $borrowernumber, $delete_field );
186 $op = 'else';
187 print $input->redirect("/cgi-bin/koha/opac-suggestions.pl?op=else");
188 exit;
191 map{
192 my $s = $_;
193 my $library = Koha::Libraries->find($s->{branchcodesuggestedby});
194 $library ? $s->{branchcodesuggestedby} = $library->branchname : ()
195 } @$suggestions_loop;
197 foreach my $suggestion(@$suggestions_loop) {
198 if($suggestion->{'suggestedby'} == $borrowernumber) {
199 $suggestion->{'showcheckbox'} = $borrowernumber;
200 } else {
201 $suggestion->{'showcheckbox'} = 0;
203 if($suggestion->{'patronreason'}){
204 my $av = Koha::AuthorisedValues->search({ category => 'OPAC_SUG', authorised_value => $suggestion->{patronreason} });
205 $suggestion->{'patronreason'} = $av->count ? $av->next->opac_description : '';
209 my $patron_reason_loop = GetAuthorisedValues("OPAC_SUG");
211 # Is the person allowed to choose their branch
212 if ( C4::Context->preference("AllowPurchaseSuggestionBranchChoice") ) {
213 my $branchcode = $input->param('branchcode') || q{};
215 if ( !$branchcode
216 && C4::Context->userenv
217 && C4::Context->userenv->{branch} )
219 $branchcode = C4::Context->userenv->{branch};
222 $template->param( branchcode => $branchcode );
225 my @mandatoryfields;
227 last unless ($op eq 'add');
228 my $fldsreq_sp = C4::Context->preference("OPACSuggestionMandatoryFields") || 'title';
229 @mandatoryfields = sort split(/\s*\,\s*/, $fldsreq_sp);
230 foreach (@mandatoryfields) {
231 $template->param( $_."_required" => 1);
233 if ( $biblionumber ) {
234 my $biblio = Koha::Biblios->find($biblionumber);
235 $template->param(
236 biblionumber => $biblio->biblionumber,
237 title => $biblio->title,
238 author => $biblio->author,
239 copyrightdate => $biblio->copyrightdate,
240 isbn => $biblio->biblioitem->isbn,
241 publishercode => $biblio->biblioitem->publishercode,
242 collectiontitle => $biblio->biblioitem->collectiontitle,
243 place => $biblio->biblioitem->place,
248 $template->param(
249 %$suggestion,
250 suggestions_loop => $suggestions_loop,
251 patron_reason_loop => $patron_reason_loop,
252 "op_$op" => 1,
253 $op => 1,
254 messages => \@messages,
255 suggestionsview => 1,
256 suggested_by_anyone => $suggested_by_anyone,
257 patrons_pending_suggestions_count => $patrons_pending_suggestions_count,
258 need_confirm => $need_confirm,
261 output_html_with_http_headers $input, $cookie, $template->output, undef, { force_no_caching => 1 };