From c0d1f2b1cb0e36488a50c5b99b0e55482074593c Mon Sep 17 00:00:00 2001 From: Julian Maurice Date: Wed, 18 Apr 2018 23:22:08 +0200 Subject: [PATCH] Bug 20582: Add routes for some svc/ scripts svc/club/enroll, svc/club/cancel_enrollment and svc/report are available in different versions: one for the OPAC, one for the intranet This patch creates new routes that override the CGI scripts and that work for both OPAC and intranet Signed-off-by: Josef Moravec --- Koha/App/Plugin/CGIBinKoha.pm | 10 ++++- Koha/Controller/Svc/Club.pm | 96 +++++++++++++++++++++++++++++++++++++++++++ Koha/Controller/Svc/Report.pm | 85 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 189 insertions(+), 2 deletions(-) create mode 100644 Koha/Controller/Svc/Club.pm create mode 100644 Koha/Controller/Svc/Report.pm diff --git a/Koha/App/Plugin/CGIBinKoha.pm b/Koha/App/Plugin/CGIBinKoha.pm index e3edeffdf4..13f8a2857d 100644 --- a/Koha/App/Plugin/CGIBinKoha.pm +++ b/Koha/App/Plugin/CGIBinKoha.pm @@ -11,7 +11,14 @@ use IO::Scalar; sub register { my ($self, $app) = @_; - $app->routes->any('/cgi-bin/koha/*name' => sub { + my $r = $app->routes; + + # Rewrites of those scripts that work for both OPAC and Intranet + $r->post('/cgi-bin/koha/svc/club/enroll')->to('svc-club#enroll'); + $r->post('/cgi-bin/koha/svc/club/cancel_enrollment')->to('svc-club#cancel_enrollment'); + $r->get('/cgi-bin/koha/svc/report')->to('svc-report#report'); + + $r->any('/cgi-bin/koha/*name' => sub { my ($c) = @_; my $script = $c->stash('name'); @@ -25,7 +32,6 @@ sub register { # Guess if it's an OPAC script, or an Intranet script # FIXME: errors scripts (400.pl, 401.pl, ...) exist in both intranet and # opac. The intranet ones will never be executed - # Same for svc/club/cancel_enrollment, svc/club/enroll and svc/report if (-e $c->app->home->rel_file("opac/$script")) { $script = "opac/$script"; } diff --git a/Koha/Controller/Svc/Club.pm b/Koha/Controller/Svc/Club.pm new file mode 100644 index 0000000000..ece4b1ecc9 --- /dev/null +++ b/Koha/Controller/Svc/Club.pm @@ -0,0 +1,96 @@ +package Koha::Controller::Svc::Club; + +use Modern::Perl; + +use Mojo::Base 'Mojolicious::Controller'; + +use C4::Auth qw(check_cookie_auth); +use Koha::Club::Enrollment::Field; +use Koha::Club::Enrollment; +use Koha::Clubs; + +sub enroll { + my ($c) = @_; + + my $cookie = $c->cookie('CGISESSID'); + my ($status, $sessionId) = check_cookie_auth($cookie, {}, { + remote_addr => $c->tx->remote_address, + }); + + if ($status ne 'ok') { + return $c->render(json => {success => 0}); + } + + my $session = C4::Auth::get_session($sessionId); + my $userid = $session->param('id'); + + my $can_enroll = C4::Auth::haspermission($userid, {clubs => 'enroll'}); + my $id = $c->param('id'); + my $borrowernumber = $c->param('borrowernumber'); + if (!$borrowernumber || !$can_enroll) { + $borrowernumber = $session->param('number'); + } + + my $enrollment; + my $club = Koha::Clubs->find($id); + my $club_template = $club->club_template; + my $is_enrollable_from_opac = $club_template->is_enrollable_from_opac; + if ($club && $borrowernumber && ($can_enroll || $is_enrollable_from_opac)) { + $enrollment = Koha::Club::Enrollment->new({ + club_id => $club->id, + borrowernumber => $borrowernumber, + date_enrolled => \'NOW()', + date_created => \'NOW()', + branchcode => $session->param('branch'), + })->store(); + + if ($enrollment) { + my @enrollment_fields = $club_template->club_template_enrollment_fields; + + foreach my $field (@enrollment_fields) { + my $value = $c->param($field->id); + Koha::Club::Enrollment::Field->new({ + club_enrollment_id => $enrollment->id, + club_template_enrollment_field_id => $field->id, + value => $value, + })->store(); + } + } + } + + my $success = $enrollment ? 1 : 0; + $c->render(json => {success => $success}); +} + +sub cancel_enrollment { + my ($c) = @_; + + my $cookie = $c->cookie('CGISESSID'); + my ($status, $sessionId) = check_cookie_auth($cookie, {}, { + remote_addr => $c->tx->remote_address, + }); + + if ($status ne 'ok') { + return $c->render(json => {success => 0}); + } + + my $session = C4::Auth::get_session($sessionId); + my $userid = $session->param('id'); + my $borrowernumber = $session->param('number'); + + my $can_enroll = C4::Auth::haspermission($userid, {clubs => 'enroll'}); + + my $id = $c->param('id'); + + my $enrollment = Koha::Club::Enrollments->find($id); + if ($enrollment) { + if ($can_enroll || $enrollment->borrowernumber eq $borrowernumber) { + $enrollment->cancel; + } + } + + my $success = $enrollment ? 1 : 0; + $c->render(json => {success => $success}); +} + +1; diff --git a/Koha/Controller/Svc/Report.pm b/Koha/Controller/Svc/Report.pm new file mode 100644 index 0000000000..00231117f6 --- /dev/null +++ b/Koha/Controller/Svc/Report.pm @@ -0,0 +1,85 @@ +package Koha::Controller::Svc::Report; + +use Modern::Perl; + +use Mojo::Base 'Mojolicious::Controller'; + +use JSON; + +use C4::Auth qw(check_cookie_auth); +use C4::Context; +use C4::Reports::Guided; + +use Koha::Caches; + +sub report { + my ($c) = @_; + + my $cookie = $c->cookie('CGISESSID'); + my ($status, $sessionId) = check_cookie_auth($cookie, {}, { + remote_addr => $c->tx->remote_address, + }); + + if ($status ne 'ok') { + return $c->render(json => {success => 0}); + } + + my $session = C4::Auth::get_session($sessionId); + my $userid = $session->param('id'); + + my $can_catalogue = C4::Auth::haspermission($userid, {catalogue => 1}); + + my $id = $c->param('id'); + my $name = $c->param('name'); + my $annotated = $c->param('annotated') // 0; + + my $report = get_saved_report({ + name => $name, + id => $id, + }); + if (!$report) { die "There is no such report.\n"; } + + if (!$can_catalogue and !$report->{public}) { + die "Sorry this report is not public\n"; + } + + my $sql_params = $c->every_param('sql_params'); + + my $cache = Koha::Caches->get_instance(); + my $cache_active = $cache->is_cache_active; + my ($cache_key, $json_text); + if ($cache_active) { + $cache_key = sprintf('report:%s:%s:%s', $report->{id}, $annotated, join('-', @$sql_params)); + $json_text = $cache->get_from_cache($cache_key); + } + + unless ($json_text) { + my $offset = 0; + my $limit = C4::Context->preference("SvcMaxReportRows") || 10; + my $sql = $report->{savedsql}; + + # convert SQL parameters to placeholders + $sql =~ s/(<<.*?>>)/\?/g; + + my ( $sth, $errors ) = execute_query( $sql, $offset, $limit, $sql_params, $report->{id} ); + if ($sth) { + my $lines; + if ($annotated) { + $lines = $sth->fetchall_arrayref({}); + } else { + $lines = $sth->fetchall_arrayref; + } + $json_text = encode_json($lines); + + if ($cache_active) { + $cache->set_in_cache( $cache_key, $json_text, { expiry => $report->{cache_expiry} } ); + } + } else { + $json_text = encode_json($errors); + } + } + + $c->render(text => $json_text, format => 'json'); +} + +1; -- 2.11.0