Bug 11986: fix searching by tags in OPAC when DOM mode is in use
[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 under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 2 of the License, or (at your option) any later
8 # version.
9 #
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License along
15 # with Koha; if not, write to the Free Software Foundation, Inc.,
16 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
18 use strict;
19 use warnings;
20
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;
31
32 my $input           = new CGI;
33 my $allsuggestions  = $input->param('showall');
34 my $op              = $input->param('op');
35 my $suggestion      = $input->Vars;
36 delete $$suggestion{$_} foreach qw<op suggestedbyme>;
37 $op = 'else' unless $op;
38
39 my ( $template, $borrowernumber, $cookie );
40 my $deleted = $input->param('deleted');
41 my $submitted = $input->param('submitted');
42
43 if ( C4::Context->preference("AnonSuggestions") ) {
44     ( $template, $borrowernumber, $cookie ) = get_template_and_user(
45         {
46             template_name   => "opac-suggestions.tmpl",
47             query           => $input,
48             type            => "opac",
49             authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
50         }
51     );
52     if ( !$$suggestion{suggestedby} ) {
53         $$suggestion{suggestedby} = C4::Context->preference("AnonymousPatron");
54     }
55 }
56 else {
57     ( $template, $borrowernumber, $cookie ) = get_template_and_user(
58         {
59             template_name   => "opac-suggestions.tmpl",
60             query           => $input,
61             type            => "opac",
62             authnotrequired => 0,
63         }
64     );
65 }
66 if ($allsuggestions){
67         delete $$suggestion{suggestedby};
68 }
69 else {
70         $$suggestion{suggestedby} ||= $borrowernumber unless ($allsuggestions);
71 }
72 # warn "bornum:",$borrowernumber;
73
74 my $suggestions_loop =
75   &SearchSuggestion( $suggestion);
76 if ( $op eq "add_confirm" ) {
77         if (@$suggestions_loop>=1){
78                 #some suggestion are answering the request Donot Add
79         }
80         else {
81                 my $scrubber = C4::Scrubber->new();
82                 foreach my $suggest (keys %$suggestion){
83                     $suggestion->{$suggest} = $scrubber->scrub($suggestion->{$suggest});
84                 }
85                 $$suggestion{'suggesteddate'}=C4::Dates->today;
86                 $$suggestion{'branchcode'}= $input->param('branch') || C4::Context->userenv->{"branch"};
87
88                 &NewSuggestion($suggestion);
89                 # empty fields, to avoid filter in "SearchSuggestion"
90                 $$suggestion{$_}='' foreach qw<title author publishercode copyrightdate place collectiontitle isbn STATUS>;
91                 $suggestions_loop =
92                    &SearchSuggestion( $suggestion );
93         }
94         $op              = 'else';
95     print $input->redirect("/cgi-bin/koha/opac-suggestions.pl?op=else&submitted=1");
96     exit;
97 }
98
99 if ( $op eq "delete_confirm" ) {
100     my @delete_field = $input->param("delete_field");
101     foreach my $delete_field (@delete_field) {
102         &DelSuggestion( $borrowernumber, $delete_field );
103     }
104     $op = 'else';
105     print $input->redirect("/cgi-bin/koha/opac-suggestions.pl?op=else&deleted=1");
106     exit;
107 }
108 map{ $_->{'branchcodesuggestedby'}=GetBranchInfo($_->{'branchcodesuggestedby'})->[0]->{'branchname'}} @$suggestions_loop;
109 my $supportlist=GetSupportList();
110 foreach my $support(@$supportlist){
111         if ($$support{'imageurl'}){
112                 $$support{'imageurl'}= getitemtypeimagelocation( 'opac', $$support{'imageurl'} );
113         }
114         else {
115            delete $$support{'imageurl'}
116         }
117 }
118
119 foreach my $suggestion(@$suggestions_loop) {
120     if($suggestion->{'suggestedby'} == $borrowernumber) {
121         $suggestion->{'showcheckbox'} = $borrowernumber;
122     } else {
123         $suggestion->{'showcheckbox'} = 0;
124     }
125     if($suggestion->{'patronreason'}){
126         $suggestion->{'patronreason'} = GetKohaAuthorisedValueLib("OPAC_SUG",$suggestion->{'patronreason'},1);
127     }
128 }
129
130 my $patron_reason_loop = GetAuthorisedValues("OPAC_SUG");
131
132 # Is the person allowed to choose their branch
133 if ( C4::Context->preference("AllowPurchaseSuggestionBranchChoice") ) {
134     my ( $borr ) = GetMemberDetails( $borrowernumber );
135
136 # pass the pickup branch along....
137     my $userbranch = '';
138     if (C4::Context->userenv && C4::Context->userenv->{'branch'}) {
139         $userbranch = C4::Context->userenv->{'branch'};
140     }
141     my $branch = $input->param('branch') || $borr->{'branchcode'} || $userbranch || '' ;
142
143 # make branch selection options...
144     my $branchloop = GetBranchesLoop($branch);
145     $template->param( branchloop => $branchloop );
146 }
147
148 $template->param(
149         %$suggestion,
150         itemtypeloop=> $supportlist,
151     suggestions_loop => $suggestions_loop,
152     patron_reason_loop => $patron_reason_loop,
153     showall    => $allsuggestions,
154     "op_$op"         => 1,
155     suggestionsview => 1,
156 );
157
158 output_html_with_http_headers $input, $cookie, $template->output;
159