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