From 1b4405840603b525781bcc11faab998c19490115 Mon Sep 17 00:00:00 2001
From: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Date: Fri, 10 Jun 2022 08:26:16 +0000
Subject: [PATCH] Bug 25498: Add a transfer form for public lists

Test plan:
[1] Valid transfer:
    Log in with staff patron C having edit_public_lists.
    Pick a public list. Choose Transfer. Select a patron.
    Submit and check if transfer was successful.
[2] Permission problem:
    Pick a staff user D. Temporary enable edit_public_lists.
    Add another private tab. Login with D.
    Pick a public list. Transfer. Choose another patron. Wait.
    Switch tab: remove added permission from D.
    Switch tab again: Submit transfer form. Error? Close tab.
[3] Bonus test - Shelf disappeared:
    Pick a public list. Transfer. Choose another patron. Wait.
    Open another tab. Delete the selected public list.
    Close tab. Submit the transfer. Not exist error?
[4] Bonus test - Patron not found:
    Pick a public list. Transfer. Choose another patron. Wait.
    Open another tab. Delete selected patron.
    Close tab. Submit transfer. Patron not found error?

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Signed-off-by: Owen Leonard <oleonard@myacpl.org>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
---
 .../prog/en/modules/virtualshelves/shelves.tt | 72 ++++++++++++++++++-
 virtualshelves/shelves.pl                     | 19 +++++
 2 files changed, 90 insertions(+), 1 deletion(-)

diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/virtualshelves/shelves.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/virtualshelves/shelves.tt
index e0e8754c03..0c21b70122 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/virtualshelves/shelves.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/virtualshelves/shelves.tt
@@ -114,6 +114,10 @@
                 </a>
             </li>
         [% END %]
+
+        [% IF op == 'transfer' %]
+            <li><a href="#" aria-current="page">Transfer list <em>[% shelf.shelfname | html %]</em></a></li>
+        [% END %]
     </ol>
 </nav>
 
@@ -122,7 +126,7 @@
             <div class="col-sm-10 col-sm-push-1">
                 <main>
 
-                [% INCLUDE 'virtualshelves-toolbar.inc' %]
+                [% IF op != 'transfer' %][% INCLUDE 'virtualshelves-toolbar.inc' %][% END %]
 
 [% FOR m IN messages %]
     <div class="dialog [% m.type | html %]">
@@ -169,20 +173,57 @@
            <span>You do not have permission to delete this list.</span>
         [% CASE 'unauthorized_on_add_biblio' %]
             <span>You do not have permission to add a record to this list.</span>
+        [% CASE 'unauthorized_transfer' %]
+            <span>You do not have permission to transfer ownership of this list.</span>
+        [% CASE 'new_owner_not_found' %]
+            <span>The new owner could not be found anymore.</span>
         [% CASE 'no_biblio_removed' %]
             <span>No record was removed.</span>
+
         [% CASE 'Koha::Exceptions::Virtualshelf::DuplicateObject' %]
             <span>An error occurred when creating this list. The name [% shelfname | html %] already exists.</span>
         [% CASE 'Koha::Exceptions::Virtualshelf::UseDbAdminAccount' %]
             <span>List could not be created. (Do not use the database administrator account.)</span>
         [% CASE 'DBIx::Class::Exception' %]
             [% m.msg | html %]
+
         [% CASE %]
             [% m.code | html %] [% m.msg | html %]
         [% END %]
     </div>
 [% END %]
 
+[% IF op == 'transfer' %]
+    <h1>Transfer ownership of public list [% shelf.shelfname | html %]</h1>
+
+    <form action="/cgi-bin/koha/virtualshelves/shelves.pl" id="transferform" method="post">
+    <fieldset>
+        <input type="hidden" name="op" value="transfer" />
+        <input type="hidden" name="public" value="1" />
+        <input type="hidden" name="shelfnumber" value="[% shelf.shelfnumber | html %]" />
+
+        <div>
+            <label for="find_patron">Search for new owner: </label>
+            <input autocomplete="off" id="find_patron" type="text" style="width:150px" class="noEnterSubmit" />
+        </div>
+
+        <br/>
+        <div>
+            <label for="find_patron">New owner: </label>
+            <span id="new_owner_name"></span>
+            <input type="hidden" id="new_owner" name="new_owner" value="" />
+        </div>
+
+        <br/>
+        <fieldset class="action">
+            <input type="submit" value="Transfer" class="submit" />
+            <a href="/cgi-bin/koha/virtualshelves/shelves.pl?op=list&amp;public=1" class="cancel">Cancel</a>
+        </fieldset>
+
+    </fieldset>
+    </form>
+[% END %]
+
 [% IF op == 'view' %]
     <h1>Contents of <em>[% shelf.shelfname | html %]</em></h1>
     [% IF itemsloop %]
@@ -848,6 +889,35 @@
              });
 
         [% END %]
+
+        [% IF op == 'transfer' %]
+            $(document).ready(function() {
+                $('#find_patron').autocomplete({
+                    source: "/cgi-bin/koha/circ/ysearch.pl",
+                    minLength: 3,
+                    select: function( event, ui ) {
+                        $('#new_owner_name').html( ui.item.firstname + " " + ui.item.surname );
+                        $('#new_owner').val( ui.item.borrowernumber );
+                        $('#find_patron').val('').focus();
+                        return false;
+                    },
+                }).data('ui-autocomplete')._renderItem = function( ul, item ) {
+                    return $('<li></li>')
+                        .data( 'ui-autocomplete-item', item )
+                        .append( '<a>' + item.surname + ', ' + item.firstname + '</a>' )
+                        .appendTo(ul);
+                };
+
+                $('#transferform').submit(function() {
+                    if( $('#new_owner').val() == '' ) {
+                        alert( _("Please select a new owner first") );
+                        return false;
+                    }
+                    return true;
+                });
+             });
+        [% END %]
+
     </script>
 [% END %]
 
diff --git a/virtualshelves/shelves.pl b/virtualshelves/shelves.pl
index 44706800c7..0386268cb6 100755
--- a/virtualshelves/shelves.pl
+++ b/virtualshelves/shelves.pl
@@ -234,6 +234,25 @@ if ( $op eq 'add_form' ) {
         push @messages, { type => 'alert', code => 'does_not_exist' };
     }
     $op = $referer;
+} elsif ( $op eq 'transfer' ) {
+    $shelfnumber = $query->param('shelfnumber');
+    $shelf = Koha::Virtualshelves->find($shelfnumber) if $shelfnumber;
+    my $new_owner = $query->param('new_owner'); # is a borrowernumber
+
+    if( $new_owner ) {
+        $op = 'list';
+        # First check: shelf found, permission, patron found?
+        if( !$shelf ) {
+            push @messages, { type => 'alert', code => 'does_not_exist' };
+        } elsif( !haspermission(C4::Context->userenv->{id}, { lists => 'edit_public_lists' }) ) {
+            push @messages, { type => 'alert', code => 'unauthorized_transfer' };
+        } elsif( !Koha::Patrons->find($new_owner) ) {
+            push @messages, { type => 'alert', code => 'new_owner_not_found' };
+            $op = 'transfer'; # find again..
+        } else { # success
+            $shelf->owner($new_owner)->store;
+        }
+    }
 }
 
 if ( $op eq 'view' ) {
-- 
2.20.1