Bug 17600: Standardize our EXPORT_OK
[srvgit] / Koha / Virtualshelf.pm
index a693eba..afecb2b 100644 (file)
@@ -2,24 +2,23 @@ package Koha::Virtualshelf;
 
 # This file is part of Koha.
 #
-# Koha is free software; you can redistribute it and/or modify it under the
-# terms of the GNU General Public License as published by the Free Software
-# Foundation; either version 3 of the License, or (at your option) any later
-# version.
+# Koha is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
 #
-# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
-# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
-# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+# Koha is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
 #
-# You should have received a copy of the GNU General Public License along
-# with Koha; if not, write to the Free Software Foundation, Inc.,
-# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+# You should have received a copy of the GNU General Public License
+# along with Koha; if not, see <http://www.gnu.org/licenses>.
 
 use Modern::Perl;
 
-use Carp;
 
-use C4::Auth;
+use C4::Auth qw( haspermission );
 
 use Koha::Patrons;
 use Koha::Database;
@@ -28,6 +27,7 @@ use Koha::Exceptions;
 use Koha::Virtualshelfshare;
 use Koha::Virtualshelfshares;
 use Koha::Virtualshelfcontent;
+use Koha::Virtualshelfcontents;
 
 use base qw(Koha::Object);
 
@@ -59,18 +59,27 @@ sub store {
         Koha::Exceptions::Virtualshelves::DuplicateObject->throw;
     }
 
-    $self->allow_add( 0 )
-        unless defined $self->allow_add;
-    $self->allow_delete_own( 1 )
-        unless defined $self->allow_delete_own;
-    $self->allow_delete_other( 0 )
-        unless defined $self->allow_delete_other;
+    $self->allow_change_from_owner( 1 )
+        unless defined $self->allow_change_from_owner;
+    $self->allow_change_from_others( 0 )
+        unless defined $self->allow_change_from_others;
 
-    $self->created_on( dt_from_string );
+    $self->created_on( dt_from_string )
+        unless defined $self->created_on;
 
     return $self->SUPER::store( $self );
 }
 
+sub is_public {
+    my ( $self ) = @_;
+    return $self->category == $PUBLIC;
+}
+
+sub is_private {
+    my ( $self ) = @_;
+    return $self->category == $PRIVATE;
+}
+
 sub is_shelfname_valid {
     my ( $self ) = @_;
 
@@ -79,14 +88,14 @@ sub is_shelfname_valid {
         ( $self->shelfnumber ? ( "me.shelfnumber" => { '!=', $self->shelfnumber } ) : () ),
     };
 
-    if ( $self->category == $PRIVATE and defined $self->owner ) {
+    if ( $self->is_private and defined $self->owner ) {
         $conditions->{-or} = {
             "virtualshelfshares.borrowernumber" => $self->owner,
             "me.owner" => $self->owner,
         };
         $conditions->{category} = $PRIVATE;
     }
-    elsif ( $self->category == $PRIVATE and not defined $self->owner ) {
+    elsif ( $self->is_private and not defined $self->owner ) {
         $conditions->{owner} = undef;
         $conditions->{category} = $PRIVATE;
     }
@@ -105,13 +114,15 @@ sub is_shelfname_valid {
 
 sub get_shares {
     my ( $self ) = @_;
-    my $shares = $self->{_result}->virtualshelfshares;
+    my $rs = $self->{_result}->virtualshelfshares;
+    my $shares = Koha::Virtualshelfshares->_new_from_dbic( $rs );
     return $shares;
 }
 
 sub get_contents {
     my ( $self ) = @_;
-    my $contents = $self->{_result}->virtualshelfcontents;
+    my $rs = $self->{_result}->virtualshelfcontents;
+    my $contents = Koha::Virtualshelfcontents->_new_from_dbic( $rs );
     return $contents;
 }
 
@@ -171,6 +182,10 @@ sub add_biblio {
         }
     )->count;
     return if $already_exists;
+
+    # Check permissions
+    return unless ( $self->owner == $borrowernumber && $self->allow_change_from_owner ) || $self->allow_change_from_others;
+
     my $content = Koha::Virtualshelfcontent->new(
         {
             shelfnumber => $self->shelfnumber,
@@ -191,38 +206,18 @@ sub remove_biblios {
     return unless @$biblionumbers;
 
     my $number_removed = 0;
-    for my $biblionumber ( @$biblionumbers ) {
-        if ( $self->owner == $borrowernumber or $self->allow_delete_own ) {
-            $number_removed += $self->get_contents->search(
-                {
-                    biblionumber => $biblionumber,
-                    borrowernumber => $borrowernumber,
-                }
-            )->delete;
-        }
-        if ( $self->allow_delete_other ) {
-            $number_removed += $self->get_contents->search(
-                {
-                    biblionumber => $biblionumber,
-                    # FIXME
-                    # This does not make sense, but it's has been backported from DelFromShelf.
-                    # Why shouldn't we allow to remove his own contribution if allow_delete_other is on?
-                    borrowernumber => {
-                        -or => {
-                            '!=' => $borrowernumber,
-                            '=' => undef
-                        }
-                    },
-                }
-            )->delete;
-        }
+    if( ( $self->owner == $borrowernumber && $self->allow_change_from_owner )
+      || $self->allow_change_from_others ) {
+        $number_removed += $self->get_contents->search({
+            biblionumber => $biblionumbers,
+        })->delete;
     }
     return $number_removed;
 }
 
 sub can_be_viewed {
     my ( $self, $borrowernumber ) = @_;
-    return 1 if $self->category == $PUBLIC;
+    return 1 if $self->is_public;
     return 0 unless $borrowernumber;
     return 1 if $self->owner == $borrowernumber;
     return $self->get_shares->search(
@@ -240,7 +235,7 @@ sub can_be_deleted {
 
     my $patron = Koha::Patrons->find( $borrowernumber );
 
-    return 1 if $self->category == $PUBLIC and C4::Auth::haspermission( $patron->userid, { lists => 'delete_public_lists' } );
+    return 1 if $self->is_public and C4::Auth::haspermission( $patron->userid, { lists => 'delete_public_lists' } );
 
     return 0;
 }
@@ -257,20 +252,14 @@ sub can_biblios_be_added {
 
     return 1
       if $borrowernumber
-      and ( $self->owner == $borrowernumber
-         or $self->allow_add );
+      and ( ( $self->owner == $borrowernumber && $self->allow_change_from_owner ) or $self->allow_change_from_others );
     return 0;
 }
 
 sub can_biblios_be_removed {
     my ( $self, $borrowernumber ) = @_;
-
-    return 1
-      if $borrowernumber
-      and (  $self->owner == $borrowernumber
-          or $self->allow_delete_own
-          or $self->allow_delete_other );
-    return 0;
+    return $self->can_biblios_be_added( $borrowernumber );
+    # Same answer since bug 18228
 }
 
 sub _type {