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