From f5ee112e6d8bf09840945ed89c7dcd3c51a1f8d0 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 You can 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 --- admin/library_groups.pl | 35 +++++++++++++--------- .../prog/en/modules/admin/library_groups.tt | 11 +++++++ 2 files changed, 32 insertions(+), 14 deletions(-) diff --git a/admin/library_groups.pl b/admin/library_groups.pl index 4c5ce25b4e..3a9bb33559 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; @@ -55,19 +56,25 @@ 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, - 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, + branchcode => $branchcode, + } + )->store(); + }; + if ($@) { + push @messages, { type => 'alert', code => 'error_on_insert' }; + } + else { + $template->param( added => $group ); + } } } elsif ( $action eq 'edit' ) { @@ -114,6 +121,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 d8981cf1fe..075e31471a 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.11.0