Bug 21674: Prevent to insert twice the same library in a group
authorJonathan Druart <jonathan.druart@bugs.koha-community.org>
Wed, 11 Dec 2019 11:51:23 +0000 (12:51 +0100)
committerMartin Renvoize <martin.renvoize@ptfs-europe.com>
Thu, 30 Jan 2020 15:36:25 +0000 (15:36 +0000)
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 <nick@bywatersolutions.com>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>

admin/library_groups.pl
koha-tmpl/intranet-tmpl/prog/en/modules/admin/library_groups.tt

index f8b2574..dcf71c8 100755 (executable)
@@ -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;
index c4d1a83..8bfc06d 100644 (file)
     &rsaquo; <a href="/cgi-bin/koha/admin/library_groups.pl">Library groups</a>
 </div>
 
+[% FOR m IN messages %]
+    <div class="dialog [% m.type | html %]">
+        [% 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 %]
+    </div>
+[% END %]
+
 [% IF added %]
     <div class="dialog message group-added">
         [% IF added.branchcode %]