From 15704fff6da56ef26740e1afb9c7414a36b96aee Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Wed, 11 Dec 2019 12:51:23 +0100 Subject: [PATCH] Bug 21674: Prevent to insert twice the same library in a group MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When you create hierarchical groups of libraries you are not allowed to add several times the same library to a given group/subgroup. The libraries that are part of the group is not displayed on the interface, to prevent the user to select it. However this restriction is only done template-side, the controller and module does not handle it. To prevent that this patchset enforces the constraint at DB level, and display a message to the end user. Test plan: - Go to Home › Administration › Library groups - Add a group - Add a library - reload the page several times => Without this patch the same library appears several times in the group => With this patch applied you will see a friendly error message Signed-off-by: Nick Clemens Signed-off-by: Martin Renvoize --- admin/library_groups.pl | 37 +++++++++++-------- .../prog/en/modules/admin/library_groups.tt | 11 ++++++ 2 files changed, 33 insertions(+), 15 deletions(-) diff --git a/admin/library_groups.pl b/admin/library_groups.pl index f8b2574cda..dcf71c87a3 100755 --- a/admin/library_groups.pl +++ b/admin/library_groups.pl @@ -41,6 +41,7 @@ my ( $template, $loggedinuser, $cookie ) = get_template_and_user( ); my $action = $cgi->param('action') || q{}; +my @messages; if ( $action eq 'add' ) { my $parent_id = $cgi->param('parent_id') || undef; @@ -56,20 +57,26 @@ if ( $action eq 'add' ) { $template->param( error_duplicate_title => $title ); } else { - my $group = Koha::Library::Group->new( - { - parent_id => $parent_id, - title => $title, - description => $description, - ft_hide_patron_info => $ft_hide_patron_info, - ft_search_groups_opac => $ft_search_groups_opac, - ft_search_groups_staff => $ft_search_groups_staff, - ft_local_hold_group => $ft_local_hold_group, - branchcode => $branchcode, - } - )->store(); - - $template->param( added => $group ); + my $group = eval { + Koha::Library::Group->new( + { + parent_id => $parent_id, + title => $title, + description => $description, + ft_hide_patron_info => $ft_hide_patron_info, + ft_search_groups_opac => $ft_search_groups_opac, + ft_search_groups_staff => $ft_search_groups_staff, + ft_local_hold_group => $ft_local_hold_group, + branchcode => $branchcode, + } + )->store(); + }; + if ($@) { + push @messages, { type => 'alert', code => 'error_on_insert' }; + } + else { + $template->param( added => $group ); + } } } elsif ( $action eq 'edit' ) { @@ -118,6 +125,6 @@ elsif ( $action eq 'delete' ) { my $root_groups = Koha::Library::Groups->get_root_groups(); -$template->param( root_groups => $root_groups, ); +$template->param( root_groups => $root_groups, messages => \@messages, ); output_html_with_http_headers $cgi, $cookie, $template->output; diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/library_groups.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/library_groups.tt index c4d1a83813..8bfc06d61a 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/library_groups.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/library_groups.tt @@ -18,6 +18,17 @@ › Library groups +[% FOR m IN messages %] +
+ [% SWITCH m.code %] + [% CASE 'error_on_insert' %] + An error occurred when adding this library. The library id might already exist in this group. + [% CASE %] + [% m.code | html %] + [% END %] +
+[% END %] + [% IF added %]
[% IF added.branchcode %] -- 2.20.1