Bug 24131: (QA follow-up) Formatting in web client
[koha.git] / opac / opac-suggestions.pl
blob1ac168c0f2a87abba6441cd0c2f5ef49e0fc86f1
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') || 'else';
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;
52 #If suggestions are turned off we redirect to 404 error. This will also redirect guest suggestions
53 if ( ! C4::Context->preference('suggestion') ) {
54 print $input->redirect("/cgi-bin/koha/errors/404.pl");
55 exit;
58 my ( $template, $borrowernumber, $cookie, @messages );
59 my $deleted = $input->param('deleted');
60 my $submitted = $input->param('submitted');
62 if ( ( C4::Context->preference("AnonSuggestions") and Koha::Patrons->find( C4::Context->preference("AnonymousPatron") ) ) or ( C4::Context->preference("OPACViewOthersSuggestions") and $op eq 'else' ) ) {
63 ( $template, $borrowernumber, $cookie ) = get_template_and_user(
65 template_name => "opac-suggestions.tt",
66 query => $input,
67 type => "opac",
68 authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
72 else {
73 ( $template, $borrowernumber, $cookie ) = get_template_and_user(
75 template_name => "opac-suggestions.tt",
76 query => $input,
77 type => "opac",
78 authnotrequired => 0,
83 # don't pass 'negcap' column to DB, else DBI::Class will error
84 # DBIx::Class::Row::store_column(): No such column 'negcap' on Koha::Schema::Result::Suggestion at Koha/C4/Suggestions.pm
85 delete $suggestion->{negcap};
86 delete $suggestion->{$_} foreach qw<op suggested_by_anyone confirm>;
88 if ( $op eq 'else' ) {
89 if ( C4::Context->preference("OPACViewOthersSuggestions") ) {
90 if ( $borrowernumber ) {
91 # A logged in user is able to see suggestions from others
92 $suggestion->{suggestedby} = $suggested_by_anyone
93 ? undef
94 : $borrowernumber;
96 else {
97 # Non logged in user is able to see all suggestions
98 $suggestion->{suggestedby} = undef;
101 else {
102 if ( $borrowernumber ) {
103 $suggestion->{suggestedby} = $borrowernumber;
105 else {
106 $suggestion->{suggestedby} = -1;
109 } else {
110 if ( $borrowernumber ) {
111 $suggestion->{suggestedby} = $borrowernumber;
113 else {
114 $suggestion->{suggestedby} = C4::Context->preference("AnonymousPatron");
118 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
119 $op = 'add_confirm';
120 my $biblio = MarcRecordFromNewSuggestion($suggestion);
121 if ( my ($duplicatebiblionumber, $duplicatetitle) = FindDuplicate($biblio) ) {
122 push @messages, { type => 'error', code => 'biblio_exists', id => $duplicatebiblionumber, title => $duplicatetitle };
123 $need_confirm = 1;
124 $op = 'add';
128 my $patrons_pending_suggestions_count = 0;
129 if ( $borrowernumber && C4::Context->preference("MaxOpenSuggestions") ne '' ) {
130 $patrons_pending_suggestions_count = scalar @{ SearchSuggestion( { suggestedby => $borrowernumber, STATUS => 'ASKED' } ) } ;
133 if ( $op eq "add_confirm" ) {
134 my $suggestions_loop = &SearchSuggestion($suggestion);
135 if ( C4::Context->preference("MaxOpenSuggestions") ne '' && $patrons_pending_suggestions_count >= C4::Context->preference("MaxOpenSuggestions") ) #only check limit for signed in borrowers
137 push @messages, { type => 'error', code => 'too_many' };
139 elsif ( @$suggestions_loop >= 1 ) {
141 #some suggestion are answering the request Donot Add
142 for my $suggestion (@$suggestions_loop) {
143 push @messages,
145 type => 'error',
146 code => 'already_exists',
147 id => $suggestion->{suggestionid}
149 last;
152 else {
153 my $scrubber = C4::Scrubber->new();
154 foreach my $suggest ( keys %$suggestion ) {
156 # Don't know why the encode is needed for Perl v5.10 here
157 $suggestion->{$suggest} = Encode::encode( "utf8",
158 $scrubber->scrub( $suggestion->{$suggest} ) );
160 $suggestion->{suggesteddate} = dt_from_string;
161 $suggestion->{branchcode} = $input->param('branchcode') || C4::Context->userenv->{"branch"};
163 &NewSuggestion($suggestion);
164 $patrons_pending_suggestions_count++;
166 # delete empty fields, to avoid filter in "SearchSuggestion"
167 foreach my $field ( qw( title author publishercode copyrightdate place collectiontitle isbn STATUS ) ) {
168 delete $suggestion->{$field}; #clear search filters (except borrower related) to show all suggestions after placing a new one
170 $suggestions_loop = &SearchSuggestion($suggestion);
172 push @messages, { type => 'info', code => 'success_on_inserted' };
175 $op = 'else';
178 my $suggestions_loop = &SearchSuggestion({suggestedby => $suggestion->{suggestedby}});
179 if ( $op eq "delete_confirm" ) {
180 my @delete_field = $input->multi_param("delete_field");
181 foreach my $delete_field (@delete_field) {
182 &DelSuggestion( $borrowernumber, $delete_field );
184 $op = 'else';
185 print $input->redirect("/cgi-bin/koha/opac-suggestions.pl?op=else");
186 exit;
189 map{
190 my $s = $_;
191 my $library = Koha::Libraries->find($s->{branchcodesuggestedby});
192 $library ? $s->{branchcodesuggestedby} = $library->branchname : ()
193 } @$suggestions_loop;
195 foreach my $suggestion(@$suggestions_loop) {
196 if($suggestion->{'suggestedby'} == $borrowernumber) {
197 $suggestion->{'showcheckbox'} = $borrowernumber;
198 } else {
199 $suggestion->{'showcheckbox'} = 0;
201 if($suggestion->{'patronreason'}){
202 my $av = Koha::AuthorisedValues->search({ category => 'OPAC_SUG', authorised_value => $suggestion->{patronreason} });
203 $suggestion->{'patronreason'} = $av->count ? $av->next->opac_description : '';
207 my $patron_reason_loop = GetAuthorisedValues("OPAC_SUG");
209 # Is the person allowed to choose their branch
210 if ( C4::Context->preference("AllowPurchaseSuggestionBranchChoice") ) {
211 my $branchcode = $input->param('branchcode') || q{};
213 if ( !$branchcode
214 && C4::Context->userenv
215 && C4::Context->userenv->{branch} )
217 $branchcode = C4::Context->userenv->{branch};
220 $template->param( branchcode => $branchcode );
223 my @mandatoryfields;
225 last unless ($op eq 'add');
226 my $fldsreq_sp = C4::Context->preference("OPACSuggestionMandatoryFields") || 'title';
227 @mandatoryfields = sort split(/\s*\,\s*/, $fldsreq_sp);
228 foreach (@mandatoryfields) {
229 $template->param( $_."_required" => 1);
231 if ( $biblionumber ) {
232 my $biblio = Koha::Biblios->find($biblionumber);
233 $template->param(
234 biblionumber => $biblio->biblionumber,
235 title => $biblio->title,
236 author => $biblio->author,
237 copyrightdate => $biblio->copyrightdate,
238 isbn => $biblio->biblioitem->isbn,
239 publishercode => $biblio->biblioitem->publishercode,
240 collectiontitle => $biblio->biblioitem->collectiontitle,
241 place => $biblio->biblioitem->place,
246 $template->param(
247 %$suggestion,
248 suggestions_loop => $suggestions_loop,
249 patron_reason_loop => $patron_reason_loop,
250 "op_$op" => 1,
251 $op => 1,
252 messages => \@messages,
253 suggestionsview => 1,
254 suggested_by_anyone => $suggested_by_anyone,
255 patrons_pending_suggestions_count => $patrons_pending_suggestions_count,
256 need_confirm => $need_confirm,
259 output_html_with_http_headers $input, $cookie, $template->output, undef, { force_no_caching => 1 };