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>.
22 use Encode
qw( encode );
23 use C4
::Auth
; # get_template_and_user
31 use Koha
::AuthorisedValues
;
35 use Koha
::DateUtils
qw( dt_from_string );
38 my $op = $input->param('op');
39 my $suggestion = $input->Vars;
40 my $negcaptcha = $input->param('negcap');
41 my $suggested_by_anyone = $input->param('suggested_by_anyone') || 0;
43 # If a spambot accidentally populates the 'negcap' field in the sugesstions form, then silently skip and return.
45 print $input->redirect("/cgi-bin/koha/opac-suggestions.pl");
48 # don't pass 'negcap' column to DB, else DBI::Class will error
49 # DBIx::Class::Row::store_column(): No such column 'negcap' on Koha::Schema::Result::Suggestion at Koha/C4/Suggestions.pm
50 delete $suggestion->{negcap
};
53 #If suggestions are turned off we redirect to 404 error. This will also redirect guest suggestions
54 if ( ! C4
::Context
->preference('suggestion') ) {
55 print $input->redirect("/cgi-bin/koha/errors/404.pl");
59 delete $suggestion->{$_} foreach qw
<op suggested_by_anyone
>;
60 $op = 'else' unless $op;
62 my ( $template, $borrowernumber, $cookie, @messages );
63 my $deleted = $input->param('deleted');
64 my $submitted = $input->param('submitted');
66 if ( C4
::Context
->preference("AnonSuggestions") or ( C4
::Context
->preference("OPACViewOthersSuggestions") and $op eq 'else' ) ) {
67 ( $template, $borrowernumber, $cookie ) = get_template_and_user
(
69 template_name
=> "opac-suggestions.tt",
72 authnotrequired
=> ( C4
::Context
->preference("OpacPublic") ?
1 : 0 ),
77 ( $template, $borrowernumber, $cookie ) = get_template_and_user
(
79 template_name
=> "opac-suggestions.tt",
87 if ( $op eq 'else' ) {
88 if ( C4
::Context
->preference("OPACViewOthersSuggestions") ) {
89 if ( $borrowernumber ) {
90 # A logged in user is able to see suggestions from others
91 $suggestion->{suggestedby
} = $suggested_by_anyone
96 # Non logged in user is able to see all suggestions
97 $suggestion->{suggestedby
} = undef;
101 if ( $borrowernumber ) {
102 $suggestion->{suggestedby
} = $borrowernumber;
105 $suggestion->{suggestedby
} = -1;
109 if ( $borrowernumber ) {
110 $suggestion->{suggestedby
} = $borrowernumber;
113 $suggestion->{suggestedby
} = C4
::Context
->preference("AnonymousPatron");
117 my $patrons_pending_suggestions_count = 0;
118 if ( $borrowernumber && C4
::Context
->preference("MaxOpenSuggestions") ne '' ) {
119 $patrons_pending_suggestions_count = scalar @
{ SearchSuggestion
( { suggestedby
=> $borrowernumber, STATUS
=> 'ASKED' } ) } ;
122 my $suggestions_loop = &SearchSuggestion
($suggestion);
123 if ( $op eq "add_confirm" ) {
124 if ( C4
::Context
->preference("MaxOpenSuggestions") ne '' && $patrons_pending_suggestions_count >= C4
::Context
->preference("MaxOpenSuggestions") ) #only check limit for signed in borrowers
126 push @messages, { type
=> 'error', code
=> 'too_many' };
128 elsif ( @
$suggestions_loop >= 1 ) {
130 #some suggestion are answering the request Donot Add
131 for my $suggestion (@
$suggestions_loop) {
135 code
=> 'already_exists',
136 id
=> $suggestion->{suggestionid
}
142 my $scrubber = C4
::Scrubber
->new();
143 foreach my $suggest ( keys %$suggestion ) {
145 # Don't know why the encode is needed for Perl v5.10 here
146 $suggestion->{$suggest} = Encode
::encode
( "utf8",
147 $scrubber->scrub( $suggestion->{$suggest} ) );
149 $suggestion->{suggesteddate
} = dt_from_string
;
150 $suggestion->{branchcode
} = $input->param('branchcode') || C4
::Context
->userenv->{"branch"};
152 &NewSuggestion
($suggestion);
153 $patrons_pending_suggestions_count++;
155 # delete empty fields, to avoid filter in "SearchSuggestion"
156 foreach my $field ( qw( title author publishercode copyrightdate place collectiontitle isbn STATUS ) ) {
157 delete $suggestion->{$field}; #clear search filters (except borrower related) to show all suggestions after placing a new one
159 $suggestions_loop = &SearchSuggestion
($suggestion);
161 push @messages, { type
=> 'info', code
=> 'success_on_inserted' };
167 if ( $op eq "delete_confirm" ) {
168 my @delete_field = $input->multi_param("delete_field");
169 foreach my $delete_field (@delete_field) {
170 &DelSuggestion
( $borrowernumber, $delete_field );
173 print $input->redirect("/cgi-bin/koha/opac-suggestions.pl?op=else");
179 my $library = Koha
::Libraries
->find($s->{branchcodesuggestedby
});
180 $library ?
$s->{branchcodesuggestedby
} = $library->branchname : ()
181 } @
$suggestions_loop;
183 foreach my $suggestion(@
$suggestions_loop) {
184 if($suggestion->{'suggestedby'} == $borrowernumber) {
185 $suggestion->{'showcheckbox'} = $borrowernumber;
187 $suggestion->{'showcheckbox'} = 0;
189 if($suggestion->{'patronreason'}){
190 my $av = Koha
::AuthorisedValues
->search({ category
=> 'OPAC_SUG', authorised_value
=> $suggestion->{patronreason
} });
191 $suggestion->{'patronreason'} = $av->count ?
$av->next->opac_description : '';
195 my $patron_reason_loop = GetAuthorisedValues
("OPAC_SUG");
197 # Is the person allowed to choose their branch
198 if ( C4
::Context
->preference("AllowPurchaseSuggestionBranchChoice") ) {
200 # pass the pickup branch along....
202 if (C4
::Context
->userenv && C4
::Context
->userenv->{'branch'}) {
203 $userbranch = C4
::Context
->userenv->{'branch'};
205 my $branchcode = $input->param('branchcode');
206 unless ( $branchcode ) {
207 my $patron = Koha
::Patrons
->find( $borrowernumber );
208 $branchcode = $patron->branchcode || $userbranch || '' ;
211 $template->param( branchcode
=> $branchcode );
214 my $mandatoryfields = '';
216 last unless ($op eq 'add');
217 my $fldsreq_sp = C4
::Context
->preference("OPACSuggestionMandatoryFields") || 'title';
218 $mandatoryfields = join(', ', (map { '"'.$_.'"'; } sort split(/\s*\,\s*/, $fldsreq_sp)));
223 suggestions_loop
=> $suggestions_loop,
224 patron_reason_loop
=> $patron_reason_loop,
227 messages
=> \
@messages,
228 suggestionsview
=> 1,
229 suggested_by_anyone
=> $suggested_by_anyone,
230 mandatoryfields
=> $mandatoryfields,
231 patrons_pending_suggestions_count
=> $patrons_pending_suggestions_count,
234 output_html_with_http_headers
$input, $cookie, $template->output;