Bug 15774: Use Koha::Object(s) for additional fields
[koha_ffzg] / Koha / Illrequest.pm
index 4754b43..98e4d70 100644 (file)
@@ -31,7 +31,9 @@ use Koha::Email;
 use Koha::Exceptions::Ill;
 use Koha::Illcomments;
 use Koha::Illrequestattributes;
+use Koha::AuthorisedValue;
 use Koha::Patron;
+use Koha::AuthorisedValues;
 
 use base qw(Koha::Object);
 
@@ -109,6 +111,30 @@ available for request.
 
 =head2 Class methods
 
+=head3 statusalias
+
+    my $statusalias = $request->statusalias;
+
+Returns a request's status alias, as a Koha::AuthorisedValue instance
+or implicit undef. This is distinct from status_alias, which only returns
+the value in the status_alias column, this method returns the entire
+AuthorisedValue object
+
+=cut
+
+sub statusalias {
+    my ( $self ) = @_;
+    return unless $self->status_alias;
+    # We can't know which result is the right one if there are multiple
+    # ILLSTATUS authorised values with the same authorised_value column value
+    # so we just use the first
+    return Koha::AuthorisedValues->search({
+        branchcode => $self->branchcode,
+        category => 'ILLSTATUS',
+        authorised_value => $self->SUPER::status_alias
+    })->next;
+}
+
 =head3 illrequestattributes
 
 =cut
@@ -142,6 +168,69 @@ sub patron {
     );
 }
 
+=head3 status_alias
+Overloaded getter/setter for status_alias,
+that only returns authorised values from the
+correct category
+
+=cut
+
+sub status_alias {
+    my ($self, $newval) = @_;
+    if ($newval) {
+        # This is hackery to enable us to undefine
+        # status_alias, since we need to have an overloaded
+        # status_alias method to get us around the problem described
+        # here:
+        # https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=20581#c156
+        # We need a way of accepting implied undef, so we can nullify
+        # the status_alias column, when called from $self->status
+        my $val = $newval eq "-1" ? undef : $newval;
+        my $newval = $self->SUPER::status_alias($val);
+        if ($newval) {
+            return $newval;
+        } else {
+            return;
+        }
+    }
+    # We can't know which result is the right one if there are multiple
+    # ILLSTATUS authorised values with the same authorised_value column value
+    # so we just use the first
+    my $alias = Koha::AuthorisedValues->search({
+        branchcode => $self->branchcode,
+        category => 'ILLSTATUS',
+        authorised_value => $self->SUPER::status_alias
+    })->next;
+    if ($alias) {
+        return $alias->authorised_value;
+    } else {
+        return;
+    }
+}
+
+=head3 status
+
+Overloaded getter/setter for request status,
+also nullifies status_alias
+
+=cut
+
+sub status {
+    my ( $self, $newval) = @_;
+    if ($newval) {
+        # This is hackery to enable us to undefine
+        # status_alias, since we need to have an overloaded
+        # status_alias method to get us around the problem described
+        # here:
+        # https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=20581#c156
+        # We need a way of passing implied undef to nullify status_alias
+        # so we pass -1, which is special cased in the overloaded setter
+        $self->status_alias("-1");
+        return $self->SUPER::status($newval);
+    }
+    return $self->SUPER::status;
+}
+
 =head3 load_backend
 
 Require "Base.pm" from the relevant ILL backend.
@@ -527,6 +616,23 @@ sub mark_completed {
     };
 }
 
+=head2 backend_migrate
+
+Migrate a request from one backend to another.
+
+=cut
+
+sub backend_migrate {
+    my ( $self, $params ) = @_;
+
+    my $response = $self->_backend_capability('migrate',{
+            request    => $self,
+            other      => $params,
+        });
+    return $self->expandTemplate($response) if $response;
+    return $response;
+}
+
 =head2 backend_confirm
 
 Confirm a request. The backend handles setting of mandatory fields in the commit stage:
@@ -1018,37 +1124,6 @@ sub TO_JSON {
     my ( $self, $embed ) = @_;
 
     my $object = $self->SUPER::TO_JSON();
-    $object->{id_prefix} = $self->id_prefix;
-
-    if ( scalar (keys %$embed) ) {
-        # Augment the request response with patron details if appropriate
-        if ( $embed->{patron} ) {
-            my $patron = $self->patron;
-            $object->{patron} = {
-                firstname  => $patron->firstname,
-                surname    => $patron->surname,
-                cardnumber => $patron->cardnumber
-            };
-        }
-        # Augment the request response with metadata details if appropriate
-        if ( $embed->{metadata} ) {
-            $object->{metadata} = $self->metadata;
-        }
-        # Augment the request response with status details if appropriate
-        if ( $embed->{capabilities} ) {
-            $object->{capabilities} = $self->capabilities;
-        }
-        # Augment the request response with library details if appropriate
-        if ( $embed->{library} ) {
-            $object->{library} = Koha::Libraries->find(
-                $self->branchcode
-            )->TO_JSON;
-        }
-        # Augment the request response with the number of comments if appropriate
-        if ( $embed->{comments} ) {
-            $object->{comments} = $self->illcomments->count;
-        }
-    }
 
     return $object;
 }