Bug 33036: Use Koha::Objects
[koha.git] / opac / opac-review.pl
1 #!/usr/bin/perl
2
3 # Copyright 2006 Katipo Communications
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use Modern::Perl;
21 use CGI qw ( -utf8 );
22 use C4::Auth qw( get_template_and_user );
23 use C4::Output qw( output_html_with_http_headers );
24 use C4::Scrubber;
25
26 use Koha::Biblios;
27 use Koha::DateUtils qw( dt_from_string );
28 use Koha::Review;
29 use Koha::Reviews;
30
31 my $query        = CGI->new;
32 my $biblionumber = $query->param('biblionumber');
33 my $review       = $query->param('review');
34 my $reviewid     = $query->param('reviewid');
35 my $op           = $query->param('op') // q{};
36
37 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
38     {
39         template_name   => "opac-review.tt",
40         query           => $query,
41         type            => "opac",
42         authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
43     }
44 );
45
46 # FIXME: need to allow user to delete their own comment(s)
47
48 my ( $clean, @errors, $savedreview );
49 my $biblio = Koha::Biblios->find( $biblionumber );
50
51 if( !$biblio ) {
52     push @errors, { nobiblio => 1 };
53 } elsif( $reviewid ) { # edit existing one, check on creator
54     $savedreview = Koha::Reviews->search({ reviewid => $reviewid, borrowernumber => $borrowernumber })->next;
55     push @errors, { unauthorized => 1 } if !$savedreview;
56 } else { # this check prevents adding multiple comments
57     # FIXME biblionumber, borrowernumber should be a unique key of reviews
58     $savedreview = Koha::Reviews->search({ biblionumber => $biblionumber, borrowernumber => $borrowernumber })->next;
59     $review = $savedreview? $savedreview->review: $review;
60 }
61
62 if( $op =~ /^cud-/ && !@errors && defined $review ) {
63         if ($review !~ /\S/) {
64                 push @errors, {empty=>1};
65         } else {
66                 $clean = C4::Scrubber->new('comment')->scrub($review);
67                 if ($clean !~ /\S/) {
68                         push @errors, {scrubbed_all=>1};
69                 } else {
70                         if ($clean ne $review) {
71                                 push @errors, {scrubbed=>$clean};
72                         }
73             if ($savedreview) {
74                 $savedreview->set(
75                     {
76                         review        => $clean,
77                         approved      => 0,
78                         datereviewed  => dt_from_string
79                     }
80                 )->store;
81             } else {
82                 $reviewid = Koha::Review->new(
83                     {   biblionumber   => $biblionumber,
84                         borrowernumber => $borrowernumber,
85                         review         => $clean,
86                         datereviewed   => dt_from_string
87                     }
88                 )->store->reviewid;
89             }
90                         unless (@errors){ $template->param(WINDOW_CLOSE=>1); }
91                 }
92         }
93 }
94 (@errors   ) and $template->param(   ERRORS=>\@errors);
95 $review = $clean;
96 $review ||= $savedreview->review if $savedreview;
97 $template->param(
98     'borrowernumber' => $borrowernumber,
99     'review'         => $review,
100     'reviewid'       => $reviewid || 0,
101     'biblio'         => $biblio,
102 );
103
104 output_html_with_http_headers $query, $cookie, $template->output;