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