Bug 34883: Stop patron expiry date being set to NULL during import
[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 qw( get_template_and_user );
24 use C4::Output qw( output_html_with_http_headers );
25
26 use Koha::Libraries;
27 use Koha::Library::Group;
28 use Koha::Library::Groups;
29
30 my $cgi = CGI->new;
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     }
39 );
40
41 my $action = $cgi->param('action') || q{};
42 my @messages;
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_limit_item_editing  = $cgi->param('ft_limit_item_editing')  || 0;
51     my $ft_search_groups_opac  = $cgi->param('ft_search_groups_opac')  || 0;
52     my $ft_search_groups_staff = $cgi->param('ft_search_groups_staff') || 0;
53     my $ft_local_hold_group    = $cgi->param('ft_local_hold_group')    || 0;
54     my $ft_local_float_group   = $cgi->param('ft_local_float_group')   || 0;
55
56     if ( !$branchcode && Koha::Library::Groups->search( { title => $title } )->count() ) {
57         $template->param( error_duplicate_title => $title );
58     }
59     else {
60         my $group = eval {
61             Koha::Library::Group->new(
62                 {
63                     parent_id              => $parent_id,
64                     title                  => $title,
65                     description            => $description,
66                     ft_hide_patron_info    => $ft_hide_patron_info,
67                     ft_search_groups_opac  => $ft_search_groups_opac,
68                     ft_search_groups_staff => $ft_search_groups_staff,
69                     ft_local_hold_group    => $ft_local_hold_group,
70                     ft_limit_item_editing  => $ft_limit_item_editing,
71                     ft_local_float_group   => $ft_local_float_group,
72                     branchcode             => $branchcode,
73                 }
74             )->store();
75         };
76         if ($@) {
77             push @messages, { type => 'alert', code => 'error_on_insert' };
78         }
79         else {
80             $template->param( added => $group );
81         }
82     }
83 }
84 elsif ( $action eq 'edit' ) {
85     my $id          = $cgi->param('id')          || undef;
86     my $title       = $cgi->param('title')       || undef;
87     my $description = $cgi->param('description') || undef;
88     my $ft_hide_patron_info    = $cgi->param('ft_hide_patron_info')    || 0;
89     my $ft_limit_item_editing  = $cgi->param('ft_limit_item_editing')  || 0;
90     my $ft_search_groups_opac  = $cgi->param('ft_search_groups_opac')  || 0;
91     my $ft_search_groups_staff = $cgi->param('ft_search_groups_staff') || 0;
92     my $ft_local_hold_group    = $cgi->param('ft_local_hold_group')    || 0;
93     my $ft_local_float_group   = $cgi->param('ft_local_float_group')   || 0;
94
95     if ($id) {
96         my $group = Koha::Library::Groups->find($id);
97
98         $group->set(
99             {
100                 title                  => $title,
101                 description            => $description,
102                 ft_hide_patron_info    => $ft_hide_patron_info,
103                 ft_limit_item_editing  => $ft_limit_item_editing,
104                 ft_search_groups_opac  => $ft_search_groups_opac,
105                 ft_search_groups_staff => $ft_search_groups_staff,
106                 ft_local_hold_group    => $ft_local_hold_group,
107                 ft_local_float_group   => $ft_local_float_group,
108             }
109         )->store();
110
111         $template->param( edited => $group );
112     }
113 }
114 elsif ( $action eq 'delete' ) {
115     my $id = $cgi->param('id');
116
117     my $group = Koha::Library::Groups->find($id);
118
119     if ($group) {
120         $group->delete();
121         $template->param(
122             deleted => {
123                 title   => $group->title(),
124                 library => $group->library()
125                 ? $group->library()->branchname
126                 : undef
127             }
128         );
129     }
130 }
131
132 my $root_groups = Koha::Library::Groups->get_root_groups();
133
134 $template->param( root_groups => $root_groups, messages => \@messages, );
135
136 output_html_with_http_headers $cgi, $cookie, $template->output;