View | Details | Raw Unified | Return to bug 20582
Collapse All | Expand All

(-)a/Koha/App/Plugin/CGIBinKoha.pm (-2 / +8 lines)
Lines 11-17 use IO::Scalar; Link Here
11
sub register {
11
sub register {
12
    my ($self, $app) = @_;
12
    my ($self, $app) = @_;
13
13
14
    $app->routes->any('/cgi-bin/koha/*name' => sub {
14
    my $r = $app->routes;
15
16
    # Rewrites of those scripts that work for both OPAC and Intranet
17
    $r->post('/cgi-bin/koha/svc/club/enroll')->to('svc-club#enroll');
18
    $r->post('/cgi-bin/koha/svc/club/cancel_enrollment')->to('svc-club#cancel_enrollment');
19
    $r->get('/cgi-bin/koha/svc/report')->to('svc-report#report');
20
21
    $r->any('/cgi-bin/koha/*name' => sub {
15
        my ($c) = @_;
22
        my ($c) = @_;
16
23
17
        my $script = $c->stash('name');
24
        my $script = $c->stash('name');
Lines 25-31 sub register { Link Here
25
        # Guess if it's an OPAC script, or an Intranet script
32
        # Guess if it's an OPAC script, or an Intranet script
26
        # FIXME: errors scripts (400.pl, 401.pl, ...) exist in both intranet and
33
        # FIXME: errors scripts (400.pl, 401.pl, ...) exist in both intranet and
27
        # opac. The intranet ones will never be executed
34
        # opac. The intranet ones will never be executed
28
        # Same for svc/club/cancel_enrollment, svc/club/enroll and svc/report
29
        if (-e $c->app->home->rel_file("opac/$script")) {
35
        if (-e $c->app->home->rel_file("opac/$script")) {
30
            $script = "opac/$script";
36
            $script = "opac/$script";
31
        }
37
        }
(-)a/Koha/Controller/Svc/Club.pm (+96 lines)
Line 0 Link Here
1
package Koha::Controller::Svc::Club;
2
3
use Modern::Perl;
4
5
use Mojo::Base 'Mojolicious::Controller';
6
7
use C4::Auth qw(check_cookie_auth);
8
use Koha::Club::Enrollment::Field;
9
use Koha::Club::Enrollment;
10
use Koha::Clubs;
11
12
sub enroll {
13
    my ($c) = @_;
14
15
    my $cookie = $c->cookie('CGISESSID');
16
    my ($status, $sessionId) = check_cookie_auth($cookie, {}, {
17
        remote_addr => $c->tx->remote_address,
18
    });
19
20
    if ($status ne 'ok') {
21
        return $c->render(json => {success => 0});
22
    }
23
24
    my $session = C4::Auth::get_session($sessionId);
25
    my $userid = $session->param('id');
26
27
    my $can_enroll = C4::Auth::haspermission($userid, {clubs => 'enroll'});
28
    my $id = $c->param('id');
29
    my $borrowernumber = $c->param('borrowernumber');
30
    if (!$borrowernumber || !$can_enroll) {
31
        $borrowernumber = $session->param('number');
32
    }
33
34
    my $enrollment;
35
    my $club = Koha::Clubs->find($id);
36
    my $club_template = $club->club_template;
37
    my $is_enrollable_from_opac = $club_template->is_enrollable_from_opac;
38
    if ($club && $borrowernumber && ($can_enroll || $is_enrollable_from_opac)) {
39
        $enrollment = Koha::Club::Enrollment->new({
40
            club_id        => $club->id,
41
            borrowernumber => $borrowernumber,
42
            date_enrolled  => \'NOW()',
43
            date_created   => \'NOW()',
44
            branchcode     => $session->param('branch'),
45
        })->store();
46
47
        if ($enrollment) {
48
            my @enrollment_fields = $club_template->club_template_enrollment_fields;
49
50
            foreach my $field (@enrollment_fields) {
51
                my $value = $c->param($field->id);
52
                Koha::Club::Enrollment::Field->new({
53
                    club_enrollment_id => $enrollment->id,
54
                    club_template_enrollment_field_id => $field->id,
55
                    value => $value,
56
                })->store();
57
            }
58
        }
59
    }
60
61
    my $success = $enrollment ? 1 : 0;
62
    $c->render(json => {success => $success});
63
}
64
65
sub cancel_enrollment {
66
    my ($c) = @_;
67
68
    my $cookie = $c->cookie('CGISESSID');
69
    my ($status, $sessionId) = check_cookie_auth($cookie, {}, {
70
        remote_addr => $c->tx->remote_address,
71
    });
72
73
    if ($status ne 'ok') {
74
        return $c->render(json => {success => 0});
75
    }
76
77
    my $session = C4::Auth::get_session($sessionId);
78
    my $userid = $session->param('id');
79
    my $borrowernumber = $session->param('number');
80
81
    my $can_enroll = C4::Auth::haspermission($userid, {clubs => 'enroll'});
82
83
    my $id = $c->param('id');
84
85
    my $enrollment = Koha::Club::Enrollments->find($id);
86
    if ($enrollment) {
87
        if ($can_enroll || $enrollment->borrowernumber eq $borrowernumber) {
88
            $enrollment->cancel;
89
        }
90
    }
91
92
    my $success = $enrollment ? 1 : 0;
93
    $c->render(json => {success => $success});
94
}
95
96
1;
(-)a/Koha/Controller/Svc/Report.pm (-1 / +85 lines)
Line 0 Link Here
0
- 
1
package Koha::Controller::Svc::Report;
2
3
use Modern::Perl;
4
5
use Mojo::Base 'Mojolicious::Controller';
6
7
use JSON;
8
9
use C4::Auth qw(check_cookie_auth);
10
use C4::Context;
11
use C4::Reports::Guided;
12
13
use Koha::Caches;
14
15
sub report {
16
    my ($c) = @_;
17
18
    my $cookie = $c->cookie('CGISESSID');
19
    my ($status, $sessionId) = check_cookie_auth($cookie, {}, {
20
        remote_addr => $c->tx->remote_address,
21
    });
22
23
    if ($status ne 'ok') {
24
        return $c->render(json => {success => 0});
25
    }
26
27
    my $session = C4::Auth::get_session($sessionId);
28
    my $userid = $session->param('id');
29
30
    my $can_catalogue = C4::Auth::haspermission($userid, {catalogue => 1});
31
32
    my $id = $c->param('id');
33
    my $name = $c->param('name');
34
    my $annotated = $c->param('annotated') // 0;
35
36
    my $report = get_saved_report({
37
        name => $name,
38
        id => $id,
39
    });
40
    if (!$report) { die "There is no such report.\n"; }
41
42
    if (!$can_catalogue and !$report->{public}) {
43
        die "Sorry this report is not public\n";
44
    }
45
46
    my $sql_params = $c->every_param('sql_params');
47
48
    my $cache = Koha::Caches->get_instance();
49
    my $cache_active = $cache->is_cache_active;
50
    my ($cache_key, $json_text);
51
    if ($cache_active) {
52
        $cache_key = sprintf('report:%s:%s:%s', $report->{id}, $annotated, join('-', @$sql_params));
53
        $json_text = $cache->get_from_cache($cache_key);
54
    }
55
56
    unless ($json_text) {
57
        my $offset = 0;
58
        my $limit  = C4::Context->preference("SvcMaxReportRows") || 10;
59
        my $sql = $report->{savedsql};
60
61
        # convert SQL parameters to placeholders
62
        $sql =~ s/(<<.*?>>)/\?/g;
63
64
        my ( $sth, $errors ) = execute_query( $sql, $offset, $limit, $sql_params, $report->{id} );
65
        if ($sth) {
66
            my $lines;
67
            if ($annotated) {
68
                $lines = $sth->fetchall_arrayref({});
69
            } else {
70
                $lines = $sth->fetchall_arrayref;
71
            }
72
            $json_text = encode_json($lines);
73
74
            if ($cache_active) {
75
                $cache->set_in_cache( $cache_key, $json_text, { expiry => $report->{cache_expiry} } );
76
            }
77
        } else {
78
            $json_text = encode_json($errors);
79
        }
80
    }
81
82
    $c->render(text => $json_text, format => 'json');
83
}
84
85
1;

Return to bug 20582