Bug 12461 - Add patron clubs feature
[koha.git] / clubs / clubs-add-modify.pl
1 #!/usr/bin/perl
2
3 # Copyright 2013 ByWater Solutions
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
22 use CGI;
23
24 use C4::Auth;
25 use C4::Output;
26 use Koha::Database;
27 use Koha::DateUtils qw(dt_from_string);
28 use Koha::Clubs;
29 use Koha::Club::Fields;
30
31 my $cgi = new CGI;
32
33 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
34     {
35         template_name   => 'clubs/clubs-add-modify.tt',
36         query           => $cgi,
37         type            => 'intranet',
38         authnotrequired => 0,
39         flagsrequired   => { clubs => 'edit_clubs' },
40     }
41 );
42
43 my $schema = Koha::Database->new()->schema();
44
45 my $id = $cgi->param('id');
46 my $club = $id ? Koha::Clubs->find($id) : Koha::Club->new();
47 my $stored = $cgi->param('name') ? $id ? 'updated' : 'stored' : undef;
48
49 my $club_template_id = $cgi->param('club_template_id');
50 my $club_template = $club->club_template() || Koha::Club::Templates->find($club_template_id);
51 $club_template_id ||= $club_template->id();
52
53 my $date_start = $cgi->param('date_start');
54 $date_start = $date_start ? dt_from_string($date_start) : undef;
55 my $date_end = $cgi->param('date_end');
56 $date_end = $date_end ? dt_from_string($date_end) : undef;
57
58 if ( $cgi->param('name') ) {    # Update or create club
59     $club->set(
60         {
61             club_template_id => $cgi->param('club_template_id') || undef,
62             name             => $cgi->param('name')             || undef,
63             description      => $cgi->param('description')      || undef,
64             branchcode       => $cgi->param('branchcode')       || undef,
65             date_start       => $date_start,
66             date_end         => $date_end,
67             date_updated     => dt_from_string(),
68         }
69     )->store();
70
71     my @club_template_field_id = $cgi->multi_param('club_template_field_id');
72     my @club_field_id          = $cgi->multi_param('club_field_id');
73     my @club_field             = $cgi->multi_param('club_field');
74
75     for ( my $i = 0 ; $i < @club_template_field_id ; $i++ ) {
76         my $club_template_field_id = $club_template_field_id[$i] || undef;
77         my $club_field_id          = $club_field_id[$i]          || undef;
78         my $club_field             = $club_field[$i]             || undef;
79
80         my $field =
81           $club_field_id
82           ? Koha::Club::Fields->find($club_field_id)
83           : Koha::Club::Field->new();
84
85         $field->set(
86             {
87                 club_id                => $club->id(),
88                 club_template_field_id => $club_template_field_id,
89                 value                  => $club_field,
90             }
91         )->store();
92     }
93
94     $id ||= $club->id();
95
96     print $cgi->redirect("/cgi-bin/koha/clubs/clubs.pl?stored=$stored&club_id=$id");
97     exit;
98 }
99
100 $club = Koha::Clubs->find($id);
101
102 $template->param(
103     club_template => $club_template,
104     club          => $club,
105 );
106
107 output_html_with_http_headers( $cgi, $cookie, $template->output );