Bug 33974: (QA follow-up) Remove superflous import
[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     #FIXME: Currently fetching all requests, it'd be great if we could fetch distinct(status).
52     # Even doing it with distinct status, we need the ILL request object, so that strings_map works and
53     # the ILL request returns the correct status and info respective to its backend.
54     my $ill_requests = Koha::Illrequests->search(
55             {backend => $backend_id},
56             # {
57             #     columns => [ qw/status/ ],
58             #     group_by => [ qw/status/ ],
59             # }
60         );
61
62     my @data;
63     while (my $request = $ill_requests->next) {
64         my $status_data = $request->strings_map;
65
66         foreach my $status_class ( qw(status_alias status) ){
67             if ($status_data->{$status_class}){
68                 push @data, {
69                     $status_data->{$status_class}->{str} ? (str => $status_data->{$status_class}->{str}) :
70                         $status_data->{$status_class}->{code} ? (str => $status_data->{$status_class}->{code}) : (),
71                     $status_data->{$status_class}->{code} ? (code => $status_data->{$status_class}->{code}) : (),
72                 }
73             }
74         }
75     }
76
77     # Remove duplicate statuses
78     my %seen;
79     @data =  grep { my $e = $_; my $key = join '___', map { $e->{$_}; } sort keys %$_;!$seen{$key}++ } @data;
80
81     return \@data;
82 }
83
84 =head3 embed
85
86     Embed info in backend for API response
87
88 =cut
89
90 sub embed {
91     my ( $self, $backend_id, $embed_header ) = @_;
92     $embed_header ||= q{};
93
94     my $return_embed;
95
96     foreach my $embed_req ( split /\s*,\s*/, $embed_header ) {
97         if ( $embed_req eq 'statuses+strings' ) {
98             $return_embed->{statuses} = $self->existing_statuses( $backend_id );
99         }
100     }
101     return $return_embed;
102 }
103
104 =head2 Internal methods
105
106 =head3 _type
107
108     my $type = Koha::Illbackend->_type;
109
110 Return this object's type
111
112 =cut
113
114 sub _type {
115     return 'Illbackend';
116 }
117
118 =head1 AUTHOR
119
120 Pedro Amorim <pedro.amorim@ptfs-europe.com>
121
122 =cut
123
124 1;