Merge branch 'new/security-release-19.11.09' into 19.11.x
[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         flagsrequired   => { parameters => 'manage_libraries' },
38         debug           => 1,
39     }
40 );
41
42 my $action = $cgi->param('action') || q{};
43
44 if ( $action eq 'add' ) {
45     my $parent_id   = $cgi->param('parent_id')   || undef;
46     my $title       = $cgi->param('title')       || undef;
47     my $description = $cgi->param('description') || undef;
48     my $branchcode  = $cgi->param('branchcode')  || undef;
49     my $ft_hide_patron_info    = $cgi->param('ft_hide_patron_info')    || 0;
50     my $ft_search_groups_opac  = $cgi->param('ft_search_groups_opac')  || 0;
51     my $ft_search_groups_staff = $cgi->param('ft_search_groups_staff') || 0;
52
53     if ( !$branchcode && Koha::Library::Groups->search( { title => $title } )->count() ) {
54         $template->param( error_duplicate_title => $title );
55     }
56     else {
57         my $group = Koha::Library::Group->new(
58             {
59                 parent_id   => $parent_id,
60                 title       => $title,
61                 description => $description,
62                 ft_hide_patron_info    => $ft_hide_patron_info,
63                 ft_search_groups_opac  => $ft_search_groups_opac,
64                 ft_search_groups_staff => $ft_search_groups_staff,
65                 branchcode  => $branchcode,
66             }
67         )->store();
68
69         $template->param( added => $group );
70     }
71 }
72 elsif ( $action eq 'edit' ) {
73     my $id          = $cgi->param('id')          || undef;
74     my $title       = $cgi->param('title')       || undef;
75     my $description = $cgi->param('description') || undef;
76     my $ft_hide_patron_info    = $cgi->param('ft_hide_patron_info')    || 0;
77     my $ft_search_groups_opac  = $cgi->param('ft_search_groups_opac')  || 0;
78     my $ft_search_groups_staff = $cgi->param('ft_search_groups_staff') || 0;
79
80     if ($id) {
81         my $group = Koha::Library::Groups->find($id);
82
83         $group->set(
84             {
85                 title       => $title,
86                 description => $description,
87                 ft_hide_patron_info      => $ft_hide_patron_info,
88                 ft_search_groups_opac    => $ft_search_groups_opac,
89                 ft_search_groups_staff   => $ft_search_groups_staff,
90             }
91         )->store();
92
93         $template->param( edited => $group );
94     }
95 }
96 elsif ( $action eq 'delete' ) {
97     my $id = $cgi->param('id');
98
99     my $group = Koha::Library::Groups->find($id);
100
101     if ($group) {
102         $group->delete();
103         $template->param(
104             deleted => {
105                 title   => $group->title(),
106                 library => $group->library()
107                 ? $group->library()->branchname
108                 : undef
109             }
110         );
111     }
112 }
113
114 my $root_groups = Koha::Library::Groups->get_root_groups();
115
116 $template->param( root_groups => $root_groups, );
117
118 output_html_with_http_headers $cgi, $cookie, $template->output;