merged from upstream master
[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 => 1,
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 }
119
120 $template->param(
121         %$suggestion,
122         itemtypeloop=> $supportlist,
123     suggestions_loop => $suggestions_loop,
124     showall    => $allsuggestions,
125     "op_$op"         => 1,
126     suggestionsview => 1,
127 );
128
129
130 output_html_with_http_headers $input, $cookie, $template->output;
131