Bug 14330: Remove unused email_sender from sendbasket/sendshelf
[koha.git] / Koha / Objects.pm
index 1eaf2be..8ef6390 100644 (file)
@@ -17,8 +17,6 @@ package Koha::Objects;
 # with Koha; if not, write to the Free Software Foundation, Inc.,
 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 
-use overload "0+" => "count", "<>" => "next", fallback => 1;
-
 use Modern::Perl;
 
 use Carp;
@@ -48,7 +46,7 @@ This class must be subclassed.
 
 =head3 Koha::Objects->new();
 
-my $object = Koha::Object->new();
+my $object = Koha::Objects->new();
 
 =cut
 
@@ -59,13 +57,13 @@ sub new {
     bless( $self, $class );
 }
 
-=head3 Koha::Objects->new_from_dbic();
+=head3 Koha::Objects->_new_from_dbic();
 
-my $object = Koha::Object->new_from_dbic( $resultset );
+my $object = Koha::Objects->_new_from_dbic( $resultset );
 
 =cut
 
-sub new_from_dbic {
+sub _new_from_dbic {
     my ( $class, $resultset ) = @_;
     my $self = { _resultset => $resultset };
 
@@ -74,47 +72,51 @@ sub new_from_dbic {
 
 =head3 Koha::Objects->find();
 
-my $object = Koha::Object->find($id);
-my $object = Koha::Object->find( { keypart1 => $keypart1, keypart2 => $keypart2 } );
+my $object = Koha::Objects->find($id);
+my $object = Koha::Objects->find( { keypart1 => $keypart1, keypart2 => $keypart2 } );
 
 =cut
 
 sub find {
     my ( $self, $id ) = @_;
 
+    return unless $id;
+
     my $result = $self->_resultset()->find($id);
 
-    my $object = $self->object_class()->new_from_dbic( $result );
+    return unless $result;
+
+    my $object = $self->object_class()->_new_from_dbic( $result );
 
     return $object;
 }
 
 =head3 Koha::Objects->search();
 
-my @objects = Koha::Object->search($params);
+my @objects = Koha::Objects->search($params);
 
 =cut
 
 sub search {
-    my ( $self, $params ) = @_;
+    my ( $self, $params, $attributes ) = @_;
 
     if (wantarray) {
-        my @dbic_rows = $self->_resultset()->search($params);
+        my @dbic_rows = $self->_resultset()->search($params, $attributes);
 
         return $self->_wrap(@dbic_rows);
 
     }
     else {
-        my $class = ref( $self );
-        my $rs = $self->_resultset()->search($params);
+        my $class = ref($self) ? ref($self) : $self;
+        my $rs = $self->_resultset()->search($params, $attributes);
 
-        return $class->new_from_dbic($rs);
+        return $class->_new_from_dbic($rs);
     }
 }
 
 =head3 Koha::Objects->count();
 
-my @objects = Koha::Object->count($params);
+my @objects = Koha::Objects->count($params);
 
 =cut
 
@@ -126,7 +128,7 @@ sub count {
 
 =head3 Koha::Objects->next();
 
-my $object = Koha::Object->next();
+my $object = Koha::Objects->next();
 
 Returns the next object that is part of this set.
 Returns undef if there are no more objects to return.
@@ -134,12 +136,12 @@ Returns undef if there are no more objects to return.
 =cut
 
 sub next {
-    my ( $self, $id ) = @_;
+    my ( $self ) = @_;
 
     my $result = $self->_resultset()->next();
     return unless $result;
 
-    my $object = $self->object_class()->new_from_dbic( $result );
+    my $object = $self->object_class()->_new_from_dbic( $result );
 
     return $object;
 }
@@ -154,7 +156,7 @@ with the first object in a set.
 =cut
 
 sub reset {
-    my ( $self, $id ) = @_;
+    my ( $self ) = @_;
 
     $self->_resultset()->reset();
 
@@ -170,7 +172,7 @@ Returns an arrayref of the objects in this set.
 =cut
 
 sub as_list {
-    my ( $self, $id ) = @_;
+    my ( $self ) = @_;
 
     my @dbic_rows = $self->_resultset()->all();
 
@@ -181,14 +183,14 @@ sub as_list {
 
 =head3 Koha::Objects->_wrap
 
-wraps the DBIC object in a corrosponding Koha object
+wraps the DBIC object in a corresponding Koha object
 
 =cut
 
 sub _wrap {
     my ( $self, @dbic_rows ) = @_;
 
-    my @objects = map { $self->object_class()->new_from_dbic( $_ ) } @dbic_rows;
+    my @objects = map { $self->object_class()->_new_from_dbic( $_ ) } @dbic_rows;
 
     return @objects;
 }
@@ -202,10 +204,15 @@ Returns the internal resultset or creates it if undefined
 sub _resultset {
     my ($self) = @_;
 
-    $self->{_resultset} ||=
-      Koha::Database->new()->schema()->resultset( $self->type() );
+    if ( ref($self) ) {
+        $self->{_resultset} ||=
+          Koha::Database->new()->schema()->resultset( $self->type() );
 
-    $self->{_resultset};
+        return $self->{_resultset};
+    }
+    else {
+        return Koha::Database->new()->schema()->resultset( $self->type() );
+    }
 }
 
 =head3 type