Bug 27378: Introduce cookie consent to OPAC and staff client
[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         }
65     );
66     while ( my $request = $ill_requests->next ) {
67         my $status_data = $request->strings_map;
68
69         if ( $status_data->{status} ) {
70             push @data, {
71                   $status_data->{status}->{str}  ? ( str => $status_data->{status}->{str} )
72                 : $status_data->{status}->{code} ? ( str => $status_data->{status}->{code} )
73                 : (),
74                 $status_data->{status}->{code} ? ( code => $status_data->{status}->{code} ) : (),
75             };
76         }
77     }
78
79     # Now do the same to get all status_aliases
80     $ill_requests = Koha::Illrequests->search(
81         { backend => $backend_id },
82         {
83             select   => [ 'status_alias', \'MAX(illrequest_id)', 'backend' ],
84             as       => [qw/ status_alias illrequest_id backend /],
85             group_by => [qw/status_alias backend/],
86         }
87     );
88     while ( my $request = $ill_requests->next ) {
89         my $status_data = $request->strings_map;
90
91         if ( $status_data->{status_alias} ) {
92             push @data, {
93                   $status_data->{status_alias}->{str}  ? ( str => $status_data->{status_alias}->{str} )
94                 : $status_data->{status_alias}->{code} ? ( str => $status_data->{status_alias}->{code} )
95                 : (),
96                 $status_data->{status_alias}->{code} ? ( code => $status_data->{status_alias}->{code} ) : (),
97             };
98         }
99     }
100
101     return \@data;
102 }
103
104 =head3 embed
105
106     Embed info in backend for API response
107
108 =cut
109
110 sub embed {
111     my ( $self, $backend_id, $embed_header ) = @_;
112     $embed_header ||= q{};
113
114     my $return_embed;
115
116     foreach my $embed_req ( split /\s*,\s*/, $embed_header ) {
117         if ( $embed_req eq 'statuses+strings' ) {
118             $return_embed->{statuses} = $self->existing_statuses( $backend_id );
119         }
120     }
121     return $return_embed;
122 }
123
124 =head2 Internal methods
125
126 =head3 _type
127
128     my $type = Koha::Illbackend->_type;
129
130 Return this object's type
131
132 =cut
133
134 sub _type {
135     return 'Illbackend';
136 }
137
138 =head1 AUTHOR
139
140 Pedro Amorim <pedro.amorim@ptfs-europe.com>
141
142 =cut
143
144 1;