Bug 18260: [QA Follow-up] Always something left?
[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;
23 use C4::Koha;
24 use C4::Output;
25 use C4::Biblio;
26 use C4::Scrubber;
27 use C4::Debug;
28 use Koha::DateUtils;
29 use Koha::Review;
30 use Koha::Reviews;
31
32 my $query        = new CGI;
33 my $biblionumber = $query->param('biblionumber');
34 my $review       = $query->param('review');
35 my $reviewid     = $query->param('reviewid');
36 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
37     {
38         template_name   => "opac-review.tt",
39         query           => $query,
40         type            => "opac",
41         authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
42     }
43 );
44
45 # FIXME: need to allow user to delete their own comment(s)
46
47 my ( $clean, @errors, $savedreview );
48 my $biblio = GetBiblioData($biblionumber);
49
50 if( !$biblio ) {
51     push @errors, { nobiblio => 1 };
52 } elsif( $reviewid ) { # edit existing one, check on creator
53     $savedreview = Koha::Reviews->search({ reviewid => $reviewid, borrowernumber => $borrowernumber })->next;
54     push @errors, { unauthorized => 1 } if !$savedreview;
55 } else { # this check prevents adding multiple comments
56     # FIXME biblionumber, borrowernumber should be a unique key of reviews
57     $savedreview = Koha::Reviews->search({ biblionumber => $biblionumber, borrowernumber => $borrowernumber })->next;
58     $review = $savedreview? $savedreview->review: $review;
59 }
60
61 if( !@errors && defined $review ) {
62         if ($review !~ /\S/) {
63                 push @errors, {empty=>1};
64         } else {
65                 $clean = C4::Scrubber->new('comment')->scrub($review);
66                 if ($clean !~ /\S/) {
67                         push @errors, {scrubbed_all=>1};
68                 } else {
69                         if ($clean ne $review) {
70                                 push @errors, {scrubbed=>$clean};
71                         }
72                         my $js_ok_review = $clean;
73                         $js_ok_review =~ s/"/&quot;/g;  # probably redundant w/ TMPL ESCAPE=JS
74                         $template->param(clean_review=>$js_ok_review);
75             if ($savedreview) {
76                 $savedreview->set(
77                     {
78                         review        => $clean,
79                         approved      => 0,
80                         datereviewed  => dt_from_string
81                     }
82                 )->store;
83             } else {
84                 $reviewid = Koha::Review->new(
85                     {   biblionumber   => $biblionumber,
86                         borrowernumber => $borrowernumber,
87                         review         => $clean,
88                     }
89                 )->store->reviewid;
90             }
91                         unless (@errors){ $template->param(WINDOW_CLOSE=>1); }
92                 }
93         }
94 }
95 (@errors   ) and $template->param(   ERRORS=>\@errors);
96 ($cgi_debug) and $template->param(cgi_debug=>1       );
97 $review = $clean;
98 $review ||= $savedreview->review if $savedreview;
99 $template->param(
100     'biblionumber'   => $biblionumber,
101     'borrowernumber' => $borrowernumber,
102     'review'         => $review,
103     'reviewid'       => $reviewid || 0,
104     'title'          => $biblio->{'title'},
105 );
106
107 output_html_with_http_headers $query, $cookie, $template->output;