Bug 30477: Add new UNIMARC installer translation files
[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 ( $template, $borrowernumber, $cookie ) = get_template_and_user(
36     {
37         template_name   => "opac-review.tt",
38         query           => $query,
39         type            => "opac",
40         authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
41     }
42 );
43
44 # FIXME: need to allow user to delete their own comment(s)
45
46 my ( $clean, @errors, $savedreview );
47 my $biblio = Koha::Biblios->find( $biblionumber );
48
49 if( !$biblio ) {
50     push @errors, { nobiblio => 1 };
51 } elsif( $reviewid ) { # edit existing one, check on creator
52     $savedreview = Koha::Reviews->search({ reviewid => $reviewid, borrowernumber => $borrowernumber })->next;
53     push @errors, { unauthorized => 1 } if !$savedreview;
54 } else { # this check prevents adding multiple comments
55     # FIXME biblionumber, borrowernumber should be a unique key of reviews
56     $savedreview = Koha::Reviews->search({ biblionumber => $biblionumber, borrowernumber => $borrowernumber })->next;
57     $review = $savedreview? $savedreview->review: $review;
58 }
59
60 if( !@errors && defined $review ) {
61         if ($review !~ /\S/) {
62                 push @errors, {empty=>1};
63         } else {
64                 $clean = C4::Scrubber->new('comment')->scrub($review);
65                 if ($clean !~ /\S/) {
66                         push @errors, {scrubbed_all=>1};
67                 } else {
68                         if ($clean ne $review) {
69                                 push @errors, {scrubbed=>$clean};
70                         }
71             if ($savedreview) {
72                 $savedreview->set(
73                     {
74                         review        => $clean,
75                         approved      => 0,
76                         datereviewed  => dt_from_string
77                     }
78                 )->store;
79             } else {
80                 $reviewid = Koha::Review->new(
81                     {   biblionumber   => $biblionumber,
82                         borrowernumber => $borrowernumber,
83                         review         => $clean,
84                         datereviewed   => dt_from_string
85                     }
86                 )->store->reviewid;
87             }
88                         unless (@errors){ $template->param(WINDOW_CLOSE=>1); }
89                 }
90         }
91 }
92 (@errors   ) and $template->param(   ERRORS=>\@errors);
93 $review = $clean;
94 $review ||= $savedreview->review if $savedreview;
95 $template->param(
96     'borrowernumber' => $borrowernumber,
97     'review'         => $review,
98     'reviewid'       => $reviewid || 0,
99     'biblio'         => $biblio,
100 );
101
102 output_html_with_http_headers $query, $cookie, $template->output;