ffzg/recall_notices.pl: added --interval and --dedup
[koha.git] / opac / opac-shareshelf.pl
index a27aa03..8e79792 100755 (executable)
@@ -32,9 +32,18 @@ use C4::Context;
 use C4::Letters;
 use C4::Members ();
 use C4::Output;
-use C4::VirtualShelves;
 
+use Koha::Patrons;
 use Koha::Virtualshelves;
+use Koha::Virtualshelfshares;
+
+
+# if virtualshelves is disabled, leave immediately
+if ( ! C4::Context->preference('virtualshelves') ) {
+    my $query = new CGI;
+    print $query->redirect("/cgi-bin/koha/errors/404.pl");
+    exit;
+}
 
 #-------------------------------------------------------------------------------
 
@@ -114,42 +123,57 @@ sub confirm_invite {
 sub show_accept {
     my ($param) = @_;
 
-    my @rv = ShelfPossibleAction( $param->{loggedinuser},
-        $param->{shelfnumber}, 'acceptshare' );
-    $param->{errcode} = $rv[1] if !$rv[0];
+    my $shelfnumber = $param->{shelfnumber};
+    my $shelf = Koha::Virtualshelves->find( $shelfnumber );
+
+    # The key for accepting is checked later in Koha::Virtualshelfshare
+    # You must not be the owner and the list must be private
+    if( !$shelf ) {
+        $param->{errcode} = 2;
+    } elsif( $shelf->category == 2 ) {
+        $param->{errcode} = 5;
+    } elsif( $shelf->owner == $param->{loggedinuser} ) {
+        $param->{errcode} = 8;
+    }
     return if $param->{errcode};
 
-    #errorcode 5: should be private list
-    #errorcode 8: should not be owner
-
-    my $dbkey = keytostring( stringtokey( $param->{key}, 0 ), 1 );
-    if ( AcceptShare( $param->{shelfnumber}, $dbkey, $param->{loggedinuser} ) )
-    {
-        notify_owner($param);
-
-        #redirect to view of this shared list
-        print $param->{query}->redirect(
-            -uri    => SHELVES_URL . $param->{shelfnumber},
-            -cookie => $param->{cookie}
-        );
-        exit;
-    }
-    else {
-        $param->{errcode} = 7;    #not accepted (key not found or expired)
+    # Look for shelfnumber and invitekey in shares, expiration check later
+    my $key = keytostring( stringtokey( $param->{key}, 0 ), 1 );
+    my $shared_shelves = Koha::Virtualshelfshares->search({
+        shelfnumber => $param->{shelfnumber},
+        invitekey => $key,
+    });
+    my $shared_shelf = $shared_shelves ? $shared_shelves->next : undef; # we pick the first, but there should only be one
+
+    if ( $shared_shelf ) {
+        my $is_accepted = eval { $shared_shelf->accept( $key, $param->{loggedinuser} ) };
+        if( $is_accepted ) {
+            notify_owner($param);
+            #redirect to view of this shared list
+            print $param->{query}->redirect(
+                -uri    => SHELVES_URL . $param->{shelfnumber},
+                -cookie => $param->{cookie}
+            );
+            exit;
+        }
     }
+    $param->{errcode} = 7; # not accepted: key invalid or expired
 }
 
 sub notify_owner {
     my ($param) = @_;
 
-    my $toaddr = C4::Members::GetNoticeEmailAddress( $param->{owner} );
-    return if !$toaddr;
+    my $patron = Koha::Patrons->find( $param->{owner} );
+    return unless $patron;
+
+    my $toaddr = $patron->notice_email_address or return;
 
     #prepare letter
     my $letter = C4::Letters::GetPreparedLetter(
         module      => 'members',
         letter_code => 'SHARE_ACCEPT',
         branchcode  => C4::Context->userenv->{"branch"},
+        lang        => $patron->lang,
         tables      => { borrowers => $param->{loggedinuser}, },
         substitute  => { listname => $param->{shelfname}, },
     );
@@ -200,7 +224,11 @@ sub send_invitekey {
         my @newkey = randomlist( KEYLENGTH, 64 );    #generate a new key
 
         #add a preliminary share record
-        if ( !AddShare( $param->{shelfnumber}, keytostring( \@newkey, 1 ) ) ) {
+        my $shelf = Koha::Virtualshelves->find( $param->{shelfnumber} );
+        my $key = keytostring( \@newkey, 1 );
+        my $is_shared = eval { $shelf->share( $key ); };
+        # TODO Better error handling, catch the exceptions
+        if ( $@ or not $is_shared ) {
             push @{ $param->{fail_addr} }, $a;
             next;
         }
@@ -211,6 +239,7 @@ sub send_invitekey {
             module      => 'members',
             letter_code => 'SHARE_INVITE',
             branchcode  => C4::Context->userenv->{"branch"},
+            lang        => 'default', # Not sure how we could use something more useful else here
             tables      => { borrowers => $param->{loggedinuser}, },
             substitute  => {
                 listname => $param->{shelfname},