Bug 36414: Staff UI - Skip csrf_token
[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 qw( get_template_and_user );
25 use C4::Output qw( output_html_with_http_headers );
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 = CGI->new;
32
33 my $op = $cgi->param('op') || q{};
34 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
35     {
36         template_name   => 'clubs/clubs-add-modify.tt',
37         query           => $cgi,
38         type            => 'intranet',
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
48 my $stored =
49     $cgi->param('name')
50   ? $id
51       ? 'updated'
52       : 'stored'
53   : undef;
54
55 my $club_template_id = $cgi->param('club_template_id');
56 my $club_template = $club->club_template() || Koha::Club::Templates->find($club_template_id);
57 $club_template_id ||= $club_template->id();
58
59 my $date_start = $cgi->param('date_start') || undef;
60 my $date_end = $cgi->param('date_end') || undef;
61
62 if ( $cgi->param('name') && $op eq 'cud-update' ) {    # Update or create club
63     $club->set(
64         {
65             club_template_id => scalar $cgi->param('club_template_id') || undef,
66             name             => scalar $cgi->param('name')             || undef,
67             description      => scalar $cgi->param('description')      || undef,
68             branchcode       => scalar $cgi->param('branchcode')       || undef,
69             date_start       => $date_start,
70             date_end         => $date_end,
71             date_updated     => dt_from_string(),
72         }
73     )->store();
74
75     my @club_template_field_id = $cgi->multi_param('club_template_field_id');
76     my @club_field_id          = $cgi->multi_param('club_field_id');
77     my @club_field             = $cgi->multi_param('club_field');
78
79     for ( my $i = 0 ; $i < @club_template_field_id ; $i++ ) {
80         my $club_template_field_id = $club_template_field_id[$i] || undef;
81         my $club_field_id          = $club_field_id[$i]          || undef;
82         my $club_field             = $club_field[$i]             || undef;
83
84         my $field =
85           $club_field_id
86           ? Koha::Club::Fields->find($club_field_id)
87           : Koha::Club::Field->new();
88
89         $field->set(
90             {
91                 club_id                => $club->id(),
92                 club_template_field_id => $club_template_field_id,
93                 value                  => $club_field,
94             }
95         )->store();
96     }
97
98     $id ||= $club->id();
99
100     print $cgi->redirect("/cgi-bin/koha/clubs/clubs.pl?stored=$stored&club_id=$id");
101     exit;
102 }
103
104 $club = Koha::Clubs->find($id);
105
106 $template->param(
107     club_template => $club_template,
108     club          => $club,
109 );
110
111 output_html_with_http_headers( $cgi, $cookie, $template->output );