Lines 1-113
Link Here
|
1 |
package Koha::ItemTypes; |
|
|
2 |
|
3 |
# This contains the item types that the system knows about. |
4 |
|
5 |
# Copyright 2014 Catalyst IT |
6 |
# |
7 |
# This file is part of Koha. |
8 |
# |
9 |
# Koha is free software; you can redistribute it and/or modify it under the |
10 |
# terms of the GNU General Public License as published by the Free Software |
11 |
# Foundation; either version 3 of the License, or (at your option) any later |
12 |
# version. |
13 |
# |
14 |
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY |
15 |
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR |
16 |
# A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
17 |
# |
18 |
# You should have received a copy of the GNU General Public License along |
19 |
# with Koha; if not, write to the Free Software Foundation, Inc., |
20 |
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
21 |
|
22 |
=head1 NAME |
23 |
|
24 |
Koha::ItemTypes - handles the item types that Koha knows about |
25 |
|
26 |
=head1 DESCRIPTION |
27 |
|
28 |
This contains functions to access the item types. |
29 |
|
30 |
Note that any changes that happen to the database while this object is live |
31 |
may not be reflected, so best don't hold onto it for a long time |
32 |
|
33 |
=cut |
34 |
|
35 |
use Koha::Database; |
36 |
use Koha::ItemType; |
37 |
use Modern::Perl; |
38 |
|
39 |
use Data::Dumper; # TODO remove |
40 |
use base qw(Class::Accessor); |
41 |
|
42 |
__PACKAGE__->mk_accessors(qw()); |
43 |
|
44 |
=head1 FUNCTIONS |
45 |
|
46 |
=head2 new |
47 |
|
48 |
my $itypes = Koha::ItemTypes->new(); |
49 |
|
50 |
Creates a new instance of the object. |
51 |
|
52 |
=cut |
53 |
|
54 |
# Handled by Class::Accessor |
55 |
|
56 |
=head2 get_itemtype |
57 |
|
58 |
my @itype = $itypes->get_itemtype('CODE1', 'CODE2'); |
59 |
|
60 |
This returns a L<Koha::ItemType> object for each of the provided codes. For |
61 |
any that don't exist, an C<undef> is returned. |
62 |
|
63 |
=cut |
64 |
|
65 |
sub get_itemtype { |
66 |
my ($self, @codes) = @_; |
67 |
|
68 |
my $schema = Koha::Database->new()->schema(); |
69 |
my @res; |
70 |
|
71 |
foreach my $c (@codes) { |
72 |
if (exists $self->{cached}{$c}) { |
73 |
push @res, $self->{cached}{$c}; |
74 |
next; |
75 |
} |
76 |
my $rs = $schema->resultset('Itemtype')->search( { itemtype => $c } ); |
77 |
my $r = $rs->next; |
78 |
if (!$r) { |
79 |
push @res, undef; |
80 |
next; |
81 |
} |
82 |
my %data = $r->get_inflated_columns; |
83 |
my $it = Koha::ItemType->new(\%data); |
84 |
push @res, $it; |
85 |
$self->{cached}{$c} = $it; |
86 |
} |
87 |
if (wantarray) { |
88 |
return @res; |
89 |
} else { |
90 |
return @res ? $res[0] : undef; |
91 |
} |
92 |
} |
93 |
|
94 |
=head2 get_description_for_code |
95 |
|
96 |
my $desc = $itypes->get_description_for_code($code); |
97 |
|
98 |
This returns the description for an itemtype code. As a special case, if |
99 |
there is no itemtype for this code, it'll return what it was given. |
100 |
|
101 |
It is mostly as a convenience function rather than using L<get_itemtype>. |
102 |
|
103 |
=cut |
104 |
|
105 |
sub get_description_for_code { |
106 |
my ($self, $code) = @_; |
107 |
|
108 |
my $itype = $self->get_itemtype($code); |
109 |
return $code if !$itype; |
110 |
return $itype->description; |
111 |
} |
112 |
|
113 |
1; |
114 |
- |