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; |