Merge remote branch 'kc/new/enh/bug_5692' into kcmaster
[wip/koha-chris_n.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 with
15 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
16 # Suite 330, Boston, MA  02111-1307 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
31 my $input           = new CGI;
32 my $allsuggestions  = $input->param('showall');
33 my $op              = $input->param('op');
34 my $suggestion      = $input->Vars;
35 delete $$suggestion{$_} foreach qw<op suggestedbyme>;
36 $op = 'else' unless $op;
37
38 my ( $template, $borrowernumber, $cookie );
39 my $deleted = $input->param('deleted');
40 my $submitted = $input->param('submitted');
41
42 if ( C4::Context->preference("AnonSuggestions") ) {
43     ( $template, $borrowernumber, $cookie ) = get_template_and_user(
44         {
45             template_name   => "opac-suggestions.tmpl",
46             query           => $input,
47             type            => "opac",
48             authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
49         }
50     );
51     if ( !$$suggestion{suggestedby} ) {
52         $$suggestion{suggestedby} = C4::Context->preference("AnonymousPatron");
53     }
54 }
55 else {
56     ( $template, $borrowernumber, $cookie ) = get_template_and_user(
57         {
58             template_name   => "opac-suggestions.tmpl",
59             query           => $input,
60             type            => "opac",
61             authnotrequired => 0,
62         }
63     );
64 }
65 if ($allsuggestions){
66         delete $$suggestion{suggestedby};
67 }
68 else {
69         $$suggestion{suggestedby} ||= $borrowernumber unless ($allsuggestions);
70 }
71 # warn "bornum:",$borrowernumber;
72
73 my $suggestions_loop =
74   &SearchSuggestion( $suggestion);
75 if ( $op eq "add_confirm" ) {
76         if (@$suggestions_loop>=1){
77                 #some suggestion are answering the request Donot Add
78         }
79         else {
80                 $$suggestion{'suggesteddate'}=C4::Dates->today;
81                 $$suggestion{'branchcode'}= $input->param('branch') || C4::Context->userenv->{"branch"};
82                 &NewSuggestion($suggestion);
83                 # empty fields, to avoid filter in "SearchSuggestion"
84                 $$suggestion{$_}='' foreach qw<title author publishercode copyrightdate place collectiontitle isbn STATUS>;
85                 $suggestions_loop =
86                    &SearchSuggestion( $suggestion );
87         }
88         $op              = 'else';
89     print $input->redirect("/cgi-bin/koha/opac-suggestions.pl?op=else&submitted=1");
90     exit;
91 }
92
93 if ( $op eq "delete_confirm" ) {
94     my @delete_field = $input->param("delete_field");
95     foreach my $delete_field (@delete_field) {
96         &DelSuggestion( $borrowernumber, $delete_field );
97     }
98     $op = 'else';
99     print $input->redirect("/cgi-bin/koha/opac-suggestions.pl?op=else&deleted=1");
100     exit;
101 }
102 map{ $_->{'branchcodesuggestedby'}=GetBranchInfo($_->{'branchcodesuggestedby'})->[0]->{'branchname'}} @$suggestions_loop;
103 my $supportlist=GetSupportList();
104 foreach my $support(@$supportlist){
105         if ($$support{'imageurl'}){
106                 $$support{'imageurl'}= getitemtypeimagelocation( 'opac', $$support{'imageurl'} );
107         }
108         else {
109            delete $$support{'imageurl'}
110         }
111 }
112
113 foreach my $suggestion(@$suggestions_loop) {
114     if($suggestion->{'suggestedby'} == $borrowernumber) {
115         $suggestion->{'showcheckbox'} = $borrowernumber;
116     } else {
117         $suggestion->{'showcheckbox'} = 0;
118     }
119     if($suggestion->{'patronreason'}){
120         $suggestion->{'patronreason'} = GetKohaAuthorisedValueLib("OPAC_SUG",$suggestion->{'patronreason'},1);
121     }
122 }
123
124 my $patron_reason_loop = GetAuthorisedValues("OPAC_SUG");
125
126 # Is the person allowed to choose their branch
127 if ( C4::Context->preference("AllowPurchaseSuggestionBranchChoice") ) {
128     my ( $borr ) = GetMemberDetails( $borrowernumber );
129
130 # pass the pickup branch along....
131     my $branch = $input->param('branch') || $borr->{'branchcode'} || C4::Context->userenv->{branch} || '' ;
132
133 # make branch selection options...
134     my $CGIbranchloop = GetBranchesLoop($branch);
135     $template->param( branch_loop => $CGIbranchloop );
136 }
137
138 $template->param(
139         %$suggestion,
140         itemtypeloop=> $supportlist,
141     suggestions_loop => $suggestions_loop,
142     patron_reason_loop => $patron_reason_loop,
143     showall    => $allsuggestions,
144     "op_$op"         => 1,
145     suggestionsview => 1,
146 );
147
148 output_html_with_http_headers $input, $cookie, $template->output;
149