Bug 33606: (QA follow-up) Cosmetic changes
[koha.git] / Koha / Illbackend.pm
1 package Koha::Illbackend;
2
3 # Copyright PTFS Europe 2023
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use Modern::Perl;
21
22 use base qw(Koha::Object);
23
24 =head1 NAME
25
26 Koha::Illbackend - Koha Illbackend Object class
27
28 =head2 Class methods
29
30 =head3 new
31
32 New illbackend
33
34 =cut
35
36 sub new {
37     my $class = shift;
38     my $self = {};
39     return bless $self, $class;
40 }
41
42 =head3 existing_statuses
43
44 Return a list of existing ILL statuses
45
46 =cut
47
48 sub existing_statuses {
49     my ( $self, $backend_id ) = @_;
50
51     my @data;
52
53     # NOTE: This query fetches all distinct status's found in the database for this backend.
54     # We need the 'status' field for obvious reasons, the 'backend' field is required to not
55     # throw 'Koha::Exceptions::Ill::InvalidBackendId' when we're converting to a Koha object.
56     # Finally, to get around 'ONLY_FULL_GROUP_BY', we have to be explicit about which
57     # 'request_id' we want to return, hense the 'MAX' call.
58     my $ill_requests = Koha::Illrequests->search(
59         { backend => $backend_id },
60         {
61             select   => [ 'status', \'MAX(illrequest_id)', 'backend' ],
62             as       => [qw/ status illrequest_id backend /],
63             group_by => [qw/status backend/],
64             order_by => [qw/status backend/],
65         }
66     );
67     while ( my $request = $ill_requests->next ) {
68         my $status_data = $request->strings_map;
69
70         if ( $status_data->{status} ) {
71             push @data, {
72                   $status_data->{status}->{str}  ? ( str => $status_data->{status}->{str} )
73                 : $status_data->{status}->{code} ? ( str => $status_data->{status}->{code} )
74                 : (),
75                 $status_data->{status}->{code} ? ( code => $status_data->{status}->{code} ) : (),
76             };
77         }
78     }
79
80     # Now do the same to get all status_aliases
81     $ill_requests = Koha::Illrequests->search(
82         { backend => $backend_id },
83         {
84             select   => [ 'status_alias', \'MAX(illrequest_id)', 'backend' ],
85             as       => [qw/ status_alias illrequest_id backend /],
86             group_by => [qw/status_alias backend/],
87             order_by => [qw/status_alias backend/],
88         }
89     );
90     while ( my $request = $ill_requests->next ) {
91         my $status_data = $request->strings_map;
92
93         if ( $status_data->{status_alias} ) {
94             push @data, {
95                   $status_data->{status_alias}->{str}  ? ( str => $status_data->{status_alias}->{str} )
96                 : $status_data->{status_alias}->{code} ? ( str => $status_data->{status_alias}->{code} )
97                 : (),
98                 $status_data->{status_alias}->{code} ? ( code => $status_data->{status_alias}->{code} ) : (),
99             };
100         }
101     }
102
103     return \@data;
104 }
105
106 =head3 embed
107
108     Embed info in backend for API response
109
110 =cut
111
112 sub embed {
113     my ( $self, $backend_id, $embed_header ) = @_;
114     $embed_header ||= q{};
115
116     my $return_embed;
117
118     foreach my $embed_req ( split /\s*,\s*/, $embed_header ) {
119         if ( $embed_req eq 'statuses+strings' ) {
120             $return_embed->{statuses} = $self->existing_statuses( $backend_id );
121         }
122     }
123     return $return_embed;
124 }
125
126 =head2 Internal methods
127
128 =head3 _type
129
130     my $type = Koha::Illbackend->_type;
131
132 Return this object's type
133
134 =cut
135
136 sub _type {
137     return 'Illbackend';
138 }
139
140 =head1 AUTHOR
141
142 Pedro Amorim <pedro.amorim@ptfs-europe.com>
143
144 =cut
145
146 1;