Bug 4289: 'OpacPublic' feature
[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 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::Branch;
24 use C4::Koha;
25 use C4::Output;
26 use C4::Suggestions;
27 use C4::Koha;
28 use C4::Dates;
29
30 my $input           = new CGI;
31 my $allsuggestions  = $input->param('showall');
32 my $op              = $input->param('op');
33 my $suggestion      = $input->Vars;
34 delete $$suggestion{$_} foreach qw<op suggestedbyme>;
35 $op = 'else' unless $op;
36
37 my ( $template, $borrowernumber, $cookie );
38 my $deleted = $input->param('deleted');
39 my $submitted = $input->param('submitted');
40
41 if ( C4::Context->preference("AnonSuggestions") ) {
42     ( $template, $borrowernumber, $cookie ) = get_template_and_user(
43         {
44             template_name   => "opac-suggestions.tmpl",
45             query           => $input,
46             type            => "opac",
47             authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
48         }
49     );
50     if ( !$$suggestion{suggestedby} ) {
51         $$suggestion{suggestedby} = C4::Context->preference("AnonSuggestions");
52     }
53 }
54 else {
55     ( $template, $borrowernumber, $cookie ) = get_template_and_user(
56         {
57             template_name   => "opac-suggestions.tmpl",
58             query           => $input,
59             type            => "opac",
60             authnotrequired => 0,
61         }
62     );
63 }
64 if ($allsuggestions){
65         delete $$suggestion{suggestedby};
66 }
67 else {
68         $$suggestion{suggestedby} ||= $borrowernumber unless ($allsuggestions);
69 }
70 # warn "bornum:",$borrowernumber;
71
72 my $suggestions_loop =
73   &SearchSuggestion( $suggestion);
74 if ( $op eq "add_confirm" ) {
75         if (@$suggestions_loop>=1){
76                 #some suggestion are answering the request Donot Add
77         }
78         else {
79                 $$suggestion{'suggesteddate'}=C4::Dates->today;
80                 $$suggestion{'branchcode'}=C4::Context->userenv->{"branch"};
81                 &NewSuggestion($suggestion);
82                 # empty fields, to avoid filter in "SearchSuggestion"
83                 $$suggestion{$_}='' foreach qw<title author publishercode copyrightdate place collectiontitle isbn STATUS>;
84                 $suggestions_loop =
85                    &SearchSuggestion( $suggestion );
86         }
87         $op              = 'else';
88     print $input->redirect("/cgi-bin/koha/opac-suggestions.pl?op=else&submitted=1");
89     exit;
90 }
91
92 if ( $op eq "delete_confirm" ) {
93     my @delete_field = $input->param("delete_field");
94     foreach my $delete_field (@delete_field) {
95         &DelSuggestion( $borrowernumber, $delete_field );
96     }
97     $op = 'else';
98     print $input->redirect("/cgi-bin/koha/opac-suggestions.pl?op=else&deleted=1");
99     exit;
100 }
101 map{ $_->{'branchcodesuggestedby'}=GetBranchInfo($_->{'branchcodesuggestedby'})->[0]->{'branchname'}} @$suggestions_loop;
102 my $supportlist=GetSupportList();
103 foreach my $support(@$supportlist){
104         if ($$support{'imageurl'}){
105                 $$support{'imageurl'}= getitemtypeimagelocation( 'opac', $$support{'imageurl'} );
106         }
107         else {
108            delete $$support{'imageurl'}
109         }
110 }
111
112 foreach my $suggestion(@$suggestions_loop) {
113     if($suggestion->{'suggestedby'} == $borrowernumber) {
114         $suggestion->{'showcheckbox'} = $borrowernumber;
115     } else {
116         $suggestion->{'showcheckbox'} = 0;
117     }
118     if($suggestion->{'patronreason'}){
119         $suggestion->{'patronreason'} = GetKohaAuthorisedValueLib("OPAC_SUG",$suggestion->{'patronreason'},1);
120     }
121 }
122
123 my $patron_reason_loop = GetAuthorisedValues("OPAC_SUG");
124
125 $template->param(
126         %$suggestion,
127         itemtypeloop=> $supportlist,
128     suggestions_loop => $suggestions_loop,
129     patron_reason_loop => $patron_reason_loop,
130     showall    => $allsuggestions,
131     "op_$op"         => 1,
132     suggestionsview => 1,
133 );
134
135 output_html_with_http_headers $input, $cookie, $template->output;
136