Bug 11944: use CGI( -utf8 ) everywhere
[koha.git] / opac / opac-ratings-ajax.pl
1 #!/usr/bin/perl
2
3 # Copyright 2011 KohaAloha, NZ
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 =head1 DESCRIPTION
21
22 A script that takes an ajax json query, and then inserts or modifies a star-rating.
23
24 =cut
25
26 use strict;
27 use warnings;
28
29 use CGI qw ( -utf8 );
30 use CGI::Cookie;  # need to check cookies before having CGI parse the POST request
31
32 use C4::Auth qw(:DEFAULT check_cookie_auth);
33 use C4::Context;
34 use C4::Debug;
35 use C4::Output qw(:html :ajax pagination_bar);
36 use C4::Ratings;
37 use JSON;
38
39 my $is_ajax = is_ajax();
40
41 my ( $query, $auth_status );
42 if ($is_ajax) {
43     ( $query, $auth_status ) = &ajax_auth_cgi( {} );
44 }
45 else {
46     $query = CGI->new();
47 }
48
49 my $biblionumber     = $query->param('biblionumber');
50 my $rating_value     = $query->param('rating_value');
51 my $rating_old_value = $query->param('rating_old_value');
52
53 my ( $template, $loggedinuser, $cookie );
54 if ($is_ajax) {
55     $loggedinuser = C4::Context->userenv->{'number'};
56 }
57 else {
58     ( $template, $loggedinuser, $cookie ) = get_template_and_user(
59         {
60             template_name   => "opac-detail.tt",
61             query           => $query,
62             type            => "opac",
63             authnotrequired => 0,                    # auth required to add tags
64             debug           => 1,
65         }
66     );
67 }
68
69 my $rating;
70
71 undef $rating_value if $rating_value eq '';
72
73 if ( !$rating_value ) {
74 #### delete
75     $rating = DelRating( $biblionumber, $loggedinuser );
76 }
77
78 elsif ( $rating_value and !$rating_old_value ) {
79 #### insert
80     $rating = AddRating( $biblionumber, $loggedinuser, $rating_value );
81 }
82
83 elsif ( $rating_value ne $rating_old_value ) {
84 #### mod
85     $rating = ModRating( $biblionumber, $loggedinuser, $rating_value );
86 }
87
88 my %js_reply = (
89     rating_total   => $rating->{'rating_total'},
90     rating_avg     => $rating->{'rating_avg'},
91     rating_avg_int => $rating->{'rating_avg_int'},
92     rating_value   => $rating->{'rating_value'},
93     auth_status    => $auth_status,
94
95 );
96
97 my $json_reply = JSON->new->encode( \%js_reply );
98
99 #### $rating
100 #### %js_reply
101 #### $json_reply
102
103 output_ajax_with_http_headers( $query, $json_reply );
104 exit;
105
106 # a ratings specific ajax return sub, returns CGI object, and an 'auth_success' value
107 sub ajax_auth_cgi {
108     my $needed_flags = shift;
109     my %cookies      = fetch CGI::Cookie;
110     my $input        = CGI->new;
111     my $sessid = $cookies{'CGISESSID'}->value || $input->param('CGISESSID');
112     my ( $auth_status, $auth_sessid ) =
113       check_cookie_auth( $sessid, $needed_flags );
114     return $input, $auth_status;
115 }