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