Bug 15707: Add library groups editor
[koha.git] / admin / library_groups.pl
1 #! /usr/bin/perl
2
3 # Copyright 2016 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 use CGI qw ( -utf8 );
22 use C4::Context;
23 use C4::Auth;
24 use C4::Output;
25
26 use Koha::Libraries;
27 use Koha::Library::Group;
28 use Koha::Library::Groups;
29
30 my $cgi = new CGI;
31
32 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
33     {
34         template_name   => "admin/library_groups.tt",
35         query           => $cgi,
36         type            => "intranet",
37         authnotrequired => 0,
38         flagsrequired   => { parameters => 'parameters_remaining_permissions' },
39         debug           => 1,
40     }
41 );
42
43 my $action = $cgi->param('action') || q{};
44
45 if ( $action eq 'add' ) {
46     my $parent_id   = $cgi->param('parent_id')   || undef;
47     my $title       = $cgi->param('title')       || undef;
48     my $description = $cgi->param('description') || undef;
49     my $branchcode  = $cgi->param('branchcode')  || undef;
50
51     my $group = Koha::Library::Group->new(
52         {
53             parent_id   => $parent_id,
54             title       => $title,
55             description => $description,
56             branchcode  => $branchcode,
57         }
58     )->store();
59
60     $template->param( added => $group );
61 }
62 elsif ( $action eq 'edit' ) {
63     my $id          = $cgi->param('id')          || undef;
64     my $title       = $cgi->param('title')       || undef;
65     my $description = $cgi->param('description') || undef;
66
67     if ($id) {
68         my $group = Koha::Library::Groups->find($id);
69
70         $group->set(
71             {
72                 title       => $title,
73                 description => $description,
74             }
75         )->store();
76
77         $template->param( edited => $group );
78     }
79 }
80 elsif ( $action eq 'delete' ) {
81     my $id = $cgi->param('id');
82
83     my $group = Koha::Library::Groups->find($id);
84
85     if ($group) {
86         $group->delete();
87         $template->param(
88             deleted => {
89                 title   => $group->title(),
90                 library => $group->library()
91                 ? $group->library()->branchname
92                 : undef
93             }
94         );
95     }
96 }
97
98 my $root_groups = Koha::Library::Groups->get_root_groups();
99
100 $template->param( root_groups => $root_groups, );
101
102 output_html_with_http_headers $cgi, $cookie, $template->output;