Bug 32734: Add REST endpoint to list biblios
[koha.git] / Koha / Biblios.pm
1 package Koha::Biblios;
2
3 # Copyright ByWater Solutions 2015
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
23 use Koha::Database;
24
25 use Koha::Biblio;
26 use Koha::Libraries;
27 use MARC::File::MiJ;
28 use MARC::File::USMARC;
29 use MARC::File::XML;
30 use MARC::Record;
31
32 use base qw(Koha::Objects Koha::Objects::Record::Collections);
33
34 =head1 NAME
35
36 Koha::Biblios - Koha Biblio object set class
37
38 =head1 API
39
40 =head2 Class methods
41
42 =head3 print_collection
43     my $collection_text = $result_set->print_collection($format)
44
45 Return a text representation of a collection (group of records) in the specified format.
46 Allowed formats are marcxml, mij, marc and txt. Defaults to marcxml.
47
48 =cut
49
50 sub print_collection {
51     my ( $self, $format ) = @_;
52
53     my ($start, $glue, $end, @parts);
54
55     my %serializers = (
56         'mij' => \&MARC::File::MiJ::encode,
57         'marc' => \&MARC::File::USMARC::encode,
58         'txt' => \&MARC::Record::as_formatted,
59         'marcxml' => \&MARC::File::XML::record
60     );
61     if ($format eq 'mij') {
62         $start = '[';
63         $glue = ',';
64         $end = ']';
65     } elsif ($format eq 'marc') {
66         $glue = "\n";
67     } elsif ($format eq 'txt') {
68         $glue = "\n\n";
69     } else {
70         $glue = '';
71         $format = 'marcxml';
72         $start = MARC::File::XML::header();
73         $end = MARC::File::XML::footer();
74     }
75     while (my $biblio = $self->next) {
76         push @parts, $serializers{$format}->($biblio->metadata->record);
77     }
78     return (defined $start ? $start : '').join($glue, @parts).(defined $end ? $end : '');
79 }
80
81 =head3 pickup_locations
82
83     my $biblios = Koha::Biblios->search(...);
84     my $pickup_locations = $biblios->pickup_locations({ patron => $patron });
85
86 For a given resultset, it returns all the pickup locations
87
88 =cut
89
90 sub pickup_locations {
91     my ( $self, $params ) = @_;
92
93     my $patron = $params->{patron};
94
95     my @pickup_locations;
96     foreach my $biblio ( $self->as_list ) {
97         push @pickup_locations,
98           $biblio->pickup_locations( { patron => $patron } )
99           ->_resultset->get_column('branchcode')->all;
100     }
101
102     return Koha::Libraries->search(
103         {
104             branchcode => \@pickup_locations
105         },
106         { order_by => ['branchname'] }
107     );
108 }
109
110 =head2 Internal methods
111
112 =head3 type
113
114 =cut
115
116 sub _type {
117     return 'Biblio';
118 }
119
120 =head3 object_class
121
122 =cut
123
124 sub object_class {
125     return 'Koha::Biblio';
126 }
127
128 =head1 AUTHOR
129
130 Kyle M Hall <kyle@bywatersolutions.com>
131
132 =cut
133
134 1;