ffzg/recall_notices.pl: added --interval and --dedup
[koha.git] / opac / opac-shareshelf.pl
index 99133cd..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,33 +123,32 @@ 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
-
-    # We could have used ->find with the share id, but we don't want to change
-    # the url sent to the patron
-    my $shared_shelf = Koha::Virtualshelfshares->search(
-        {
-            shelfnumber => $param->{shelfnumber},
-        },
-        {
-            order_by => 'sharedate desc',
-            limit => 1,
-        }
-    );
+    # 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 ) {
-        $shared_shelf = $shared_shelf->next;
-        my $key = keytostring( stringtokey( $param->{key}, 0 ), 1 );
         my $is_accepted = eval { $shared_shelf->accept( $key, $param->{loggedinuser} ) };
-        if ( $is_accepted ) {
+        if( $is_accepted ) {
             notify_owner($param);
-
             #redirect to view of this shared list
             print $param->{query}->redirect(
                 -uri    => SHELVES_URL . $param->{shelfnumber},
@@ -148,23 +156,24 @@ sub show_accept {
             );
             exit;
         }
-        $param->{errcode} = 7;    #not accepted (key not found or expired)
-    } else {
-        # This shelf is not shared
     }
+    $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}, },
     );
@@ -230,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},