Bug 30430: UNIMARC XSLT add field B214 display
[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
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.
9 #
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.
14 #
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>.
17
18 use Modern::Perl;
19
20
21 use CGI qw ( -utf8 );
22 use Encode;
23 use C4::Auth qw( get_template_and_user );
24 use C4::Members;
25 use C4::Koha qw( GetAuthorisedValues );
26 use C4::Output qw( output_html_with_http_headers );
27 use C4::Suggestions qw(
28     DelSuggestion
29     MarcRecordFromNewSuggestion
30     NewSuggestion
31 );
32 use C4::Koha qw( GetAuthorisedValues );
33 use C4::Scrubber;
34 use C4::Search qw( FindDuplicate );
35
36 use Koha::AuthorisedValues;
37 use Koha::Libraries;
38 use Koha::Patrons;
39
40 use Koha::DateUtils qw( dt_from_string output_pref );
41
42 my $input           = CGI->new;
43 my $op              = $input->param('op') || 'else';
44 my $biblionumber    = $input->param('biblionumber');
45 my $negcaptcha      = $input->param('negcap');
46 my $suggested_by_anyone = $input->param('suggested_by_anyone') || 0;
47 my $title_filter    = $input->param('title_filter');
48 my $need_confirm    = 0;
49
50 my $suggestion = {
51     biblionumber    => scalar $input->param('biblionumber'),
52     title           => scalar $input->param('title'),
53     author          => scalar $input->param('author'),
54     copyrightdate   => scalar $input->param('copyrightdate'),
55     isbn            => scalar $input->param('isbn'),
56     publishercode   => scalar $input->param('publishercode'),
57     collectiontitle => scalar $input->param('collectiontitle'),
58     place           => scalar $input->param('place'),
59     quantity        => scalar $input->param('quantity'),
60     itemtype        => scalar $input->param('itemtype'),
61     branchcode      => scalar $input->param('branchcode'),
62     patronreason    => scalar $input->param('patronreason'),
63     note            => scalar $input->param('note'),
64 };
65
66 # If a spambot accidentally populates the 'negcap' field in the sugesstions form, then silently skip and return.
67 if ($negcaptcha ) {
68     print $input->redirect("/cgi-bin/koha/opac-suggestions.pl");
69     exit;
70 }
71
72 #If suggestions are turned off we redirect to 404 error. This will also redirect guest suggestions
73 if ( ! C4::Context->preference('suggestion') ) {
74     print $input->redirect("/cgi-bin/koha/errors/404.pl");
75     exit;
76 }
77
78 my ( $template, $borrowernumber, $cookie, @messages );
79 my $deleted = $input->param('deleted');
80 my $submitted = $input->param('submitted');
81
82 if ( ( C4::Context->preference("AnonSuggestions") and Koha::Patrons->find( C4::Context->preference("AnonymousPatron") ) ) or ( C4::Context->preference("OPACViewOthersSuggestions") and $op eq 'else' ) ) {
83     ( $template, $borrowernumber, $cookie ) = get_template_and_user(
84         {
85             template_name   => "opac-suggestions.tt",
86             query           => $input,
87             type            => "opac",
88             authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
89         }
90     );
91 }
92 else {
93     ( $template, $borrowernumber, $cookie ) = get_template_and_user(
94         {
95             template_name   => "opac-suggestions.tt",
96             query           => $input,
97             type            => "opac",
98         }
99     );
100 }
101
102 my $suggested_by;
103 if ( $op eq 'else' ) {
104     if ( C4::Context->preference("OPACViewOthersSuggestions") ) {
105         if ( $borrowernumber ) {
106             # A logged in user is able to see suggestions from others
107             $suggested_by = $suggested_by_anyone
108                 ? undef
109                 : $borrowernumber;
110         }
111         # else: Non logged in user is able to see all suggestions
112     }
113     else {
114         if ( $borrowernumber ) {
115             $suggested_by = $borrowernumber;
116         }
117         else {
118             $suggested_by = -1;
119         }
120     }
121 } else {
122     if ( $borrowernumber ) {
123         $suggested_by = $borrowernumber;
124     }
125     else {
126         $suggested_by = C4::Context->preference("AnonymousPatron");
127     }
128 }
129
130 $suggestion = {
131     map {
132         my $p = $suggestion->{$_};
133         # Keep parameters that are not an empty string
134         ( defined $p && $p ne '' ? ( $_ => $p ) : () )
135     } keys %$suggestion
136 };
137 $suggestion->{suggestedby} = $borrowernumber;
138
139 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
140     $op = 'add_confirm';
141     my $biblio = MarcRecordFromNewSuggestion($suggestion);
142     if ( my ($duplicatebiblionumber, $duplicatetitle) = FindDuplicate($biblio) ) {
143         push @messages, { type => 'error', code => 'biblio_exists', id => $duplicatebiblionumber, title => $duplicatetitle };
144         $need_confirm = 1;
145         $op = 'add';
146     }
147 }
148
149 my $patrons_pending_suggestions_count = 0;
150 my $patrons_total_suggestions_count = 0;
151 if ( $borrowernumber ){
152     if ( C4::Context->preference("MaxTotalSuggestions") ne '' && C4::Context->preference("NumberOfSuggestionDays") ne '' ) {
153         my $suggesteddate_from = dt_from_string()->subtract(days=>C4::Context->preference("NumberOfSuggestionDays"));
154         $suggesteddate_from = output_pref({ dt => $suggesteddate_from, dateformat => 'iso', dateonly => 1 });
155         $patrons_total_suggestions_count = Koha::Suggestions->search({ suggestedby => $borrowernumber, suggesteddate => { '>=' => $suggesteddate_from } })->count;
156
157     }
158     if ( C4::Context->preference("MaxOpenSuggestions") ne '' ) {
159         $patrons_pending_suggestions_count = Koha::Suggestions->search({ suggestedby => $borrowernumber, STATUS => 'ASKED' } )->count ;
160     }
161 }
162
163 if ( $op eq "add_confirm" ) {
164     my $suggestions = Koha::Suggestions->search($suggestion);
165     if ( C4::Context->preference("MaxTotalSuggestions") ne '' && $patrons_total_suggestions_count >= C4::Context->preference("MaxTotalSuggestions") )
166     {
167         push @messages, { type => 'error', code => 'total_suggestions' };
168     }
169     elsif ( C4::Context->preference("MaxOpenSuggestions") ne '' && $patrons_pending_suggestions_count >= C4::Context->preference("MaxOpenSuggestions") ) #only check limit for signed in borrowers
170     {
171         push @messages, { type => 'error', code => 'too_many' };
172     }
173     elsif ( $suggestions->count >= 1 ) {
174
175         #some suggestion are answering the request Donot Add
176         while ( my $suggestion = $suggestions->next ) {
177             push @messages,
178               {
179                 type => 'error',
180                 code => 'already_exists',
181                 id   => $suggestion->suggestionid
182               };
183             last;
184         }
185     }
186     else {
187         for my $f ( split(/\s*\,\s*/, C4::Context->preference("OPACSuggestionUnwantedFields") ) ) {
188             delete $suggestion->{$f};
189         }
190
191         my $scrubber = C4::Scrubber->new();
192         foreach my $suggest ( keys %$suggestion ) {
193
194             # Don't know why the encode is needed for Perl v5.10 here
195             $suggestion->{$suggest} = Encode::encode( "utf8",
196                 $scrubber->scrub( $suggestion->{$suggest} ) );
197         }
198         $suggestion->{suggesteddate} = dt_from_string;
199         $suggestion->{branchcode} = $input->param('branchcode') || C4::Context->userenv->{"branch"};
200         $suggestion->{STATUS} = 'ASKED';
201
202         &NewSuggestion($suggestion);
203         $patrons_pending_suggestions_count++;
204         $patrons_total_suggestions_count++;
205
206         push @messages, { type => 'info', code => 'success_on_inserted' };
207
208     }
209     $op = 'else';
210 }
211
212 my $suggestions = [ Koha::Suggestions->search_limited(
213     {
214         $suggestion->{suggestedby}
215         ? ( suggestedby => $suggestion->{suggestedby} )
216         : (),
217         $title_filter
218         ? ( title       => $title_filter )
219         : (),
220     }
221 )->as_list ];
222
223 if ( $op eq "delete_confirm" ) {
224     my @delete_field = $input->multi_param("delete_field");
225     foreach my $delete_field (@delete_field) {
226         &DelSuggestion( $borrowernumber, $delete_field );
227     }
228     $op = 'else';
229     print $input->redirect("/cgi-bin/koha/opac-suggestions.pl?op=else");
230     exit;
231 }
232
233 my $patron_reason_loop = GetAuthorisedValues("OPAC_SUG", "opac");
234
235 my @mandatoryfields;
236 if ( $op eq 'add' ) {
237     my $fldsreq_sp = C4::Context->preference("OPACSuggestionMandatoryFields") || 'title';
238     @mandatoryfields = sort split(/\s*\|\s*/, $fldsreq_sp);
239     foreach (@mandatoryfields) {
240         $template->param( $_."_required" => 1);
241     }
242     if ( $biblionumber ) {
243         my $biblio = Koha::Biblios->find($biblionumber);
244         $suggestion = {
245             biblionumber    => $biblio->biblionumber,
246             title           => $biblio->title,
247             author          => $biblio->author,
248             copyrightdate   => $biblio->copyrightdate,
249             isbn            => $biblio->biblioitem->isbn,
250             publishercode   => $biblio->biblioitem->publishercode,
251             collectiontitle => $biblio->biblioitem->collectiontitle,
252             place           => $biblio->biblioitem->place,
253         };
254     }
255 }
256
257 my @unwantedfields;
258 {
259     last unless ($op eq 'add');
260     my $fldsreq_sp = C4::Context->preference("OPACSuggestionUnwantedFields");
261     @unwantedfields = sort split(/\s*\|\s*/, $fldsreq_sp);
262     foreach (@unwantedfields) {
263         $template->param( $_."_hidden" => 1);
264     }
265 }
266
267 $template->param(
268     %$suggestion,
269     suggestions           => $suggestions,
270     patron_reason_loop    => $patron_reason_loop,
271     "op_$op"              => 1,
272     $op                   => 1,
273     messages              => \@messages,
274     suggestionsview       => 1,
275     suggested_by_anyone   => $suggested_by_anyone,
276     title_filter          => $title_filter,
277     patrons_pending_suggestions_count => $patrons_pending_suggestions_count,
278     need_confirm => $need_confirm,
279     patrons_total_suggestions_count => $patrons_total_suggestions_count,
280 );
281
282 output_html_with_http_headers $input, $cookie, $template->output, undef, { force_no_caching => 1 };
283