Bug 33159: Simplify ES handling and fix zebra handling
[srvgit] / Koha / Objects.pm
index 2f16b35..133d0cb 100644 (file)
@@ -19,7 +19,7 @@ package Koha::Objects;
 
 use Modern::Perl;
 
-use Carp;
+use Carp qw( carp );
 use List::MoreUtils qw( none );
 use Class::Inspector;
 
@@ -34,7 +34,7 @@ Koha::Objects - Koha Object set base class
 =head1 SYNOPSIS
 
     use Koha::Objects;
-    my @objects = Koha::Objects->search({ borrowernumber => $borrowernumber});
+    my $objects = Koha::Objects->search({ borrowernumber => $borrowernumber});
 
 =head1 DESCRIPTION
 
@@ -122,8 +122,6 @@ sub find_or_create {
 
 =head3 search
 
-    # list context
-    my @objects = Koha::Objects->search([$params, $attributes]);
     # scalar context
     my $objects = Koha::Objects->search([$params, $attributes]);
     while (my $object = $objects->next) {
@@ -133,31 +131,19 @@ sub find_or_create {
 This B<instantiates> the I<Koha::Objects> class, and generates a resultset
 based on the query I<$params> and I<$attributes> that are passed (like in DBIC).
 
-In B<list context> it returns an array of I<Koha::Object> objects.
-In B<scalar context> it returns an iterator.
-
 =cut
 
 sub search {
     my ( $self, $params, $attributes ) = @_;
 
-    if (wantarray) {
-        my @dbic_rows = $self->_resultset()->search($params, $attributes);
-
-        return $self->_wrap(@dbic_rows);
+    my $class = ref($self) ? ref($self) : $self;
+    my $rs = $self->_resultset()->search($params, $attributes);
 
-    }
-    else {
-        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 search_related
 
-    my @objects = Koha::Objects->search_related( $rel_name, $cond?, \%attrs? );
     my $objects = Koha::Objects->search_related( $rel_name, $cond?, \%attrs? );
 
 Searches the specified relationship, optionally specifying a condition and attributes for matching records.
@@ -168,22 +154,13 @@ sub search_related {
     my ( $self, $rel_name, @params ) = @_;
 
     return if !$rel_name;
-    if (wantarray) {
-        my @dbic_rows = $self->_resultset()->search_related($rel_name, @params);
-        return if !@dbic_rows;
-        my $object_class = _get_objects_class( $dbic_rows[0]->result_class );
 
-        eval "require $object_class";
-        return _wrap( $object_class, @dbic_rows );
+    my $rs = $self->_resultset()->search_related($rel_name, @params);
+    return if !$rs;
+    my $object_class = _get_objects_class( $rs->result_class );
 
-    } else {
-        my $rs = $self->_resultset()->search_related($rel_name, @params);
-        return if !$rs;
-        my $object_class = _get_objects_class( $rs->result_class );
-
-        eval "require $object_class";
-        return _new_from_dbic( $object_class, $rs );
-    }
+    eval "require $object_class";
+    return _new_from_dbic( $object_class, $rs );
 }
 
 =head3 delete
@@ -257,7 +234,7 @@ sub update {
 
 my $filtered_objects = $objects->filter_by_last_update
 
-days exclusive
+days exclusive by default (will be inclusive if days_inclusive is passed and set)
 from inclusive
 to   inclusive
 
@@ -266,6 +243,7 @@ to   inclusive
 sub filter_by_last_update {
     my ( $self, $params ) = @_;
     my $timestamp_column_name = $params->{timestamp_column_name} || 'timestamp';
+    my $days_inclusive = $params->{days_inclusive} || 0;
     my $conditions;
     Koha::Exceptions::MissingParameter->throw(
         "Missing mandatory parameter: days or from or to")
@@ -275,7 +253,9 @@ sub filter_by_last_update {
 
     my $dtf = Koha::Database->new->schema->storage->datetime_parser;
     if ( exists $params->{days} ) {
-        $conditions->{'<'} = $dtf->format_date( dt_from_string->subtract( days => $params->{days} ) );
+        my $dt = Koha::DateUtils::dt_from_string();
+        my $operator = $days_inclusive ? '<=' : '<';
+        $conditions->{$operator} = $dtf->format_date( $dt->subtract( days => $params->{days} ) );
     }
     if ( exists $params->{from} ) {
         my $from = ref($params->{from}) ? $params->{from} : dt_from_string($params->{from});
@@ -286,7 +266,7 @@ sub filter_by_last_update {
         $conditions->{'<='} = $dtf->format_date( $to );
     }
 
-    return $self->_resultset->search(
+    return $self->search(
         {
             $timestamp_column_name => $conditions
         }