Bug 14422: Typo in updatedatabase.pl
[koha.git] / opac / opac-suggestions.pl
blobcbdada24dd118900362cd3ec5a53559651852130
1 #!/usr/bin/perl
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>.
18 use strict;
19 use warnings;
21 use CGI;
22 use C4::Auth; # get_template_and_user
23 use C4::Members;
24 use C4::Branch;
25 use C4::Koha;
26 use C4::Output;
27 use C4::Suggestions;
28 use C4::Koha;
29 use C4::Dates;
30 use C4::Scrubber;
32 use Koha::DateUtils qw( dt_from_string );
34 my $input = new CGI;
35 my $op = $input->param('op');
36 my $suggestion = $input->Vars;
37 delete $suggestion->{negcap};
38 my $negcaptcha = $input->param('negcap');
39 my $suggested_by_anyone = $input->param('suggested_by_anyone') || 0;
41 # If a spambot accidentally populates the 'negcap' field in the sugesstions form, then silently skip and return.
42 if ($negcaptcha ) {
43 print $input->redirect("/cgi-bin/koha/opac-suggestions.pl");
44 exit;
47 #If suggestions are turned off we redirect to 404 error. This will also redirect guest suggestions
48 if ( ! C4::Context->preference('suggestion') ) {
49 print $input->redirect("/cgi-bin/koha/errors/404.pl");
50 exit;
53 delete $suggestion->{$_} foreach qw<op suggested_by_anyone>;
54 $op = 'else' unless $op;
56 my ( $template, $borrowernumber, $cookie, @messages );
57 my $deleted = $input->param('deleted');
58 my $submitted = $input->param('submitted');
60 if ( C4::Context->preference("AnonSuggestions") or ( C4::Context->preference("OPACViewOthersSuggestions") and $op eq 'else' ) ) {
61 ( $template, $borrowernumber, $cookie ) = get_template_and_user(
63 template_name => "opac-suggestions.tt",
64 query => $input,
65 type => "opac",
66 authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
70 else {
71 ( $template, $borrowernumber, $cookie ) = get_template_and_user(
73 template_name => "opac-suggestions.tt",
74 query => $input,
75 type => "opac",
76 authnotrequired => 0,
81 if ( $op eq 'else' ) {
82 if ( C4::Context->preference("OPACViewOthersSuggestions") ) {
83 if ( $borrowernumber ) {
84 # A logged in user is able to see suggestions from others
85 $suggestion->{suggestedby} = $suggested_by_anyone
86 ? undef
87 : $borrowernumber;
89 else {
90 # Non logged in user is able to see all suggestions
91 $suggestion->{suggestedby} = undef;
94 else {
95 if ( $borrowernumber ) {
96 $suggestion->{suggestedby} = $borrowernumber;
98 else {
99 $suggestion->{suggestedby} = -1;
102 } else {
103 if ( $borrowernumber ) {
104 $suggestion->{suggestedby} = $borrowernumber;
106 else {
107 $suggestion->{suggestedby} = C4::Context->preference("AnonymousPatron");
111 my $suggestions_loop =
112 &SearchSuggestion( $suggestion);
113 if ( $op eq "add_confirm" ) {
114 if (@$suggestions_loop>=1){
115 #some suggestion are answering the request Donot Add
116 for my $suggestion ( @$suggestions_loop ) {
117 push @messages, { type => 'error', code => 'already_exists', id => $suggestion->{suggestionid} };
118 last;
121 else {
122 my $scrubber = C4::Scrubber->new();
123 foreach my $suggest (keys %$suggestion){
124 $suggestion->{$suggest} = $scrubber->scrub($suggestion->{$suggest});
126 $suggestion->{suggesteddate} = dt_from_string;
127 $suggestion->{branchcode} = $input->param('branchcode') || C4::Context->userenv->{"branch"};
129 &NewSuggestion($suggestion);
130 # empty fields, to avoid filter in "SearchSuggestion"
131 $$suggestion{$_}='' foreach qw<title author publishercode copyrightdate place collectiontitle isbn STATUS>;
132 $suggestions_loop =
133 &SearchSuggestion( $suggestion );
134 push @messages, { type => 'info', code => 'success_on_inserted' };
136 $op = 'else';
139 if ( $op eq "delete_confirm" ) {
140 my @delete_field = $input->param("delete_field");
141 foreach my $delete_field (@delete_field) {
142 &DelSuggestion( $borrowernumber, $delete_field );
144 $op = 'else';
145 print $input->redirect("/cgi-bin/koha/opac-suggestions.pl?op=else");
146 exit;
148 map{ $_->{'branchcodesuggestedby'}=GetBranchInfo($_->{'branchcodesuggestedby'})->[0]->{'branchname'}} @$suggestions_loop;
149 my $supportlist=GetSupportList();
150 foreach my $support(@$supportlist){
151 if ($$support{'imageurl'}){
152 $$support{'imageurl'}= getitemtypeimagelocation( 'opac', $$support{'imageurl'} );
154 else {
155 delete $$support{'imageurl'}
159 foreach my $suggestion(@$suggestions_loop) {
160 if($suggestion->{'suggestedby'} == $borrowernumber) {
161 $suggestion->{'showcheckbox'} = $borrowernumber;
162 } else {
163 $suggestion->{'showcheckbox'} = 0;
165 if($suggestion->{'patronreason'}){
166 $suggestion->{'patronreason'} = GetKohaAuthorisedValueLib("OPAC_SUG",$suggestion->{'patronreason'},1);
170 my $patron_reason_loop = GetAuthorisedValues("OPAC_SUG");
172 # Is the person allowed to choose their branch
173 if ( C4::Context->preference("AllowPurchaseSuggestionBranchChoice") ) {
174 my ( $borr ) = GetMemberDetails( $borrowernumber );
176 # pass the pickup branch along....
177 my $userbranch = '';
178 if (C4::Context->userenv && C4::Context->userenv->{'branch'}) {
179 $userbranch = C4::Context->userenv->{'branch'};
181 my $branchcode = $input->param('branchcode') || $borr->{'branchcode'} || $userbranch || '' ;
183 # make branch selection options...
184 my $branchloop = GetBranchesLoop($branchcode);
185 $template->param( branchloop => $branchloop );
188 $template->param(
189 %$suggestion,
190 itemtypeloop=> $supportlist,
191 suggestions_loop => $suggestions_loop,
192 patron_reason_loop => $patron_reason_loop,
193 "op_$op" => 1,
194 $op => 1,
195 messages => \@messages,
196 suggestionsview => 1,
197 suggested_by_anyone => $suggested_by_anyone,
200 output_html_with_http_headers $input, $cookie, $template->output;