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>.
23 use C4::Auth qw( get_template_and_user );
25 use C4::Koha qw( GetAuthorisedValues );
26 use C4::Output qw( output_html_with_http_headers );
27 use C4::Suggestions qw(
29 MarcRecordFromNewSuggestion
33 use C4::Koha qw( GetAuthorisedValues );
35 use C4::Search qw( FindDuplicate );
37 use Koha::AuthorisedValues;
41 use Koha::DateUtils qw( dt_from_string output_pref );
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');
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'),
67 # If a spambot accidentally populates the 'negcap' field in the sugesstions form, then silently skip and return.
69 print $input->redirect("/cgi-bin/koha/opac-suggestions.pl");
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");
79 my ( $template, $borrowernumber, $cookie, @messages );
80 my $deleted = $input->param('deleted');
81 my $submitted = $input->param('submitted');
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(
86 template_name => "opac-suggestions.tt",
89 authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
94 ( $template, $borrowernumber, $cookie ) = get_template_and_user(
96 template_name => "opac-suggestions.tt",
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
112 # Non logged in user is able to see all suggestions
113 $suggestion->{suggestedby} = undef;
117 if ( $borrowernumber ) {
118 $suggestion->{suggestedby} = $borrowernumber;
121 $suggestion->{suggestedby} = -1;
125 if ( $borrowernumber ) {
126 $suggestion->{suggestedby} = $borrowernumber;
129 $suggestion->{suggestedby} = C4::Context->preference("AnonymousPatron");
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
135 my $biblio = MarcRecordFromNewSuggestion($suggestion);
136 if ( my ($duplicatebiblionumber, $duplicatetitle) = FindDuplicate($biblio) ) {
137 push @messages, { type => 'error', code => 'biblio_exists', id => $duplicatebiblionumber, title => $duplicatetitle };
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;
152 if ( C4::Context->preference("MaxOpenSuggestions") ne '' ) {
153 $patrons_pending_suggestions_count = Koha::Suggestions->search({ suggestedby => $borrowernumber, STATUS => 'ASKED' } )->count ;
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") )
161 push @messages, { type => 'error', code => 'total_suggestions' };
163 elsif ( C4::Context->preference("MaxOpenSuggestions") ne '' && $patrons_pending_suggestions_count >= C4::Context->preference("MaxOpenSuggestions") ) #only check limit for signed in borrowers
165 push @messages, { type => 'error', code => 'too_many' };
167 elsif ( @$suggestions_loop >= 1 ) {
169 #some suggestion are answering the request Donot Add
170 for my $s (@$suggestions_loop) {
174 code => 'already_exists',
175 id => $s->{suggestionid}
181 for my $f ( split(/\s*\,\s*/, C4::Context->preference("OPACSuggestionUnwantedFields") ) ) {
182 delete $suggestion->{$f};
185 my $scrubber = C4::Scrubber->new();
186 foreach my $suggest ( keys %$suggestion ) {
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} ) );
192 $suggestion->{suggesteddate} = dt_from_string;
193 $suggestion->{branchcode} = $input->param('branchcode') || C4::Context->userenv->{"branch"};
194 $suggestion->{STATUS} = 'ASKED';
196 &NewSuggestion($suggestion);
197 $patrons_pending_suggestions_count++;
198 $patrons_total_suggestions_count++;
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
204 $suggestions_loop = &SearchSuggestion($suggestion);
206 push @messages, { type => 'info', code => 'success_on_inserted' };
212 my $suggestions_loop = &SearchSuggestion(
214 suggestedby => $suggestion->{suggestedby},
215 title => $title_filter,
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 );
224 print $input->redirect("/cgi-bin/koha/opac-suggestions.pl?op=else");
230 my $library = Koha::Libraries->find($s->{branchcodesuggestedby});
231 $library ? $s->{branchcodesuggestedby} = $library->branchname : ()
232 } @$suggestions_loop;
234 foreach my $suggestion(@$suggestions_loop) {
235 if($suggestion->{'suggestedby'} == $borrowernumber) {
236 $suggestion->{'showcheckbox'} = $borrowernumber;
238 $suggestion->{'showcheckbox'} = 0;
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 : '';
246 my $patron_reason_loop = GetAuthorisedValues("OPAC_SUG", "opac");
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);
255 if ( $biblionumber ) {
256 my $biblio = Koha::Biblios->find($biblionumber);
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,
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);
282 suggestions_loop => $suggestions_loop,
283 patron_reason_loop => $patron_reason_loop,
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,
295 output_html_with_http_headers $input, $cookie, $template->output, undef, { force_no_caching => 1 };