View | Details | Raw Unified | Return to bug 12478
Collapse All | Expand All

(-)a/Koha/ItemType.pm (-70 lines)
Lines 1-70 Link Here
1
package Koha::ItemType;
2
3
# This represents a single itemtype
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::ItemType - represents a single itemtype
25
26
=head1 DESCRIPTION
27
28
This contains the data relating to a single itemtype
29
30
=head1 SYNOPSIS
31
32
    use Koha::ItemTypes;
33
    my $types = Koha::ItemTypes->new();
34
    my $type  = $types->get_itemtype('CODE');
35
    print $type->code, $type->description, $type->rentalcharge,
36
      $type->imageurl, $type->summary, $type->checkinmsg,
37
      $type->checkinmsgtype;
38
39
Creating an instance of C<Koha::ItemType> without using L<Koha::ItemTypes>
40
can be done simply by passing a hashref containing the values to C<new()>.
41
Note when doing this that a value for C<itemtype> will become a value for
42
C<code>.
43
44
=head1 FUNCTIONS
45
46
In addition to the read-only accessors mentioned above, the following functions
47
exist.
48
49
=cut
50
51
use Modern::Perl;
52
53
use base qw(Class::Accessor);
54
55
# TODO can we make these auto-generate from the input hash so it doesn't
56
# have to be updated when the database is?
57
__PACKAGE__->mk_ro_accessors(
58
    qw(code description rentalcharge imageurl
59
      summary checkinmsg checkinmsgtype)
60
);
61
62
sub new {
63
    my $class = shift @_;
64
65
    my %data = ( %{ $_[0] }, code => $_[0]->{itemtype} );
66
    my $self = $class->SUPER::new( \%data );
67
    return $self;
68
}
69
70
1;
(-)a/Koha/ItemTypes.pm (-114 lines)
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
- 

Return to bug 12478