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

(-)a/C4/ItemType.pm (-201 lines)
Lines 1-201 Link Here
1
package C4::ItemType;
2
3
# Copyright Liblime 2009
4
# Parts Copyright Tamil 2011
5
#
6
# This file is part of Koha.
7
#
8
# Koha is free software; you can redistribute it and/or modify it
9
# under the terms of the GNU General Public License as published by
10
# the Free Software Foundation; either version 3 of the License, or
11
# (at your option) any later version.
12
#
13
# Koha is distributed in the hope that it will be useful, but
14
# WITHOUT ANY WARRANTY; without even the implied warranty of
15
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
# GNU General Public License for more details.
17
#
18
# You should have received a copy of the GNU General Public License
19
# along with Koha; if not, see <http://www.gnu.org/licenses>.
20
21
use strict;
22
use warnings;
23
use C4::Context;
24
use C4::Languages;
25
use Encode qw( encode );
26
27
our $AUTOLOAD;
28
29
30
31
32
=head1 NAME
33
34
C4::ItemType - objects from the itemtypes table
35
36
=head1 SYNOPSIS
37
38
    use C4::ItemType;
39
    my @itemtypes = C4::ItemType->all;
40
    print join("\n", map { $_->description } @itemtypes), "\n";
41
42
=head1 DESCRIPTION
43
44
Objects of this class represent a row in the C<itemtypes> table.
45
46
Currently, the bare minimum for using this as a read-only data source has
47
been implemented.  The API was designed to make it easy to transition to
48
an ORM later on.
49
50
=head1 API
51
52
=head2 Class Methods
53
54
=cut
55
56
=head3 C4::ItemType->new(\%opts)
57
58
Given a hashref, a new (in-memory) C4::ItemType object will be instantiated.
59
The database is not touched.
60
61
=cut
62
63
sub new {
64
    my ($class, $opts) = @_;
65
    bless $opts => $class;
66
}
67
68
69
70
71
=head3 C4::ItemType->all
72
73
This returns all the itemtypes as objects.  By default they're ordered by
74
C<description>.
75
76
=cut
77
78
sub all {
79
    my ($class) = @_;
80
    my $dbh = C4::Context->dbh;
81
82
    my $language = C4::Languages::getlanguage();
83
    my @itypes;
84
    for ( @{$dbh->selectall_arrayref(q|
85
        SELECT *,
86
            COALESCE( localization.translation, itemtypes.description ) AS translated_description
87
        FROM itemtypes
88
        LEFT JOIN localization ON itemtypes.itemtype = localization.code
89
            AND localization.entity = 'itemtypes'
90
            AND localization.lang = ?
91
        ORDER BY description
92
    |, { Slice => {} }, $language)} )
93
    {
94
        push @itypes, $class->new($_);
95
    }
96
    return @itypes;
97
}
98
99
100
101
102
=head3 C4::ItemType->get
103
104
Return the itemtype indicated by the itemtype given as argument, as
105
an object.
106
107
=cut
108
109
sub get {
110
    my ($class, $itemtype) = @_;
111
112
    return unless defined $itemtype;
113
114
    my $dbh = C4::Context->dbh;
115
116
    my $data = $dbh->selectrow_hashref(
117
        "SELECT * FROM itemtypes WHERE itemtype = ?", undef, $itemtype
118
    );
119
    return $class->new($data);
120
}
121
122
123
124
125
=head2 Object Methods
126
127
These are read-only accessors for attributes of a C4::ItemType object.
128
129
=head3 $itemtype->itemtype
130
131
=cut
132
133
=head3 $itemtype->description
134
135
=cut
136
137
=head3 $itemtype->renewalsallowed
138
139
=cut
140
141
=head3 $itemtype->rentalcharge
142
143
=cut
144
145
=head3 $itemtype->notforloan
146
147
=cut
148
149
=head3 $itemtype->imageurl
150
151
=cut
152
153
=head3 $itemtype->checkinmsg
154
155
=cut
156
157
=head3 $itemtype->summary
158
159
=cut
160
161
sub AUTOLOAD {
162
    my $self = shift;
163
    my $attr = $AUTOLOAD;
164
    $attr =~ s/.*://;
165
    if (exists $self->{$attr}) {
166
        return $self->{$attr};
167
    } else {
168
        return undef;
169
    }
170
}
171
172
sub DESTROY { }
173
174
175
176
# ack itemtypes | grep '\.pm' | awk '{ print $1 }' | sed 's/:.*$//' | sort | uniq | sed -e 's,/,::,g' -e 's/\.pm//' -e 's/^/L<C4::/' -e 's/$/>,/'
177
178
=head1 SEE ALSO
179
180
The following modules make reference to the C<itemtypes> table.
181
182
L<C4::Biblio>,
183
L<C4::Circulation>,
184
L<C4::Context>,
185
L<C4::Items>,
186
L<C4::Koha>,
187
L<C4::Labels>,
188
L<C4::Overdues>,
189
L<C4::Reserves>,
190
L<C4::Search>,
191
L<C4::XSLT>
192
193
194
195
=head1 AUTHOR
196
197
John Beppu <john.beppu@liblime.com>
198
199
=cut
200
201
1;
(-)a/t/ItemType.t (-121 lines)
Lines 1-120 Link Here
1
#!/usr/bin/perl
2
3
# This file is part of Koha.
4
#
5
# Koha is free software; you can redistribute it and/or modify it
6
# under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 3 of the License, or
8
# (at your option) any later version.
9
#
10
# Koha is distributed in the hope that it will be useful, but
11
# WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18
use Modern::Perl;
19
20
use Test::More;
21
use t::lib::Mocks;
22
23
use Module::Load::Conditional qw/check_install/;
24
25
BEGIN {
26
    if ( check_install( module => 'Test::DBIx::Class' ) ) {
27
        plan tests => 25;
28
    } else {
29
        plan skip_all => "Need Test::DBIx::Class"
30
    }
31
}
32
33
use_ok('C4::ItemType');
34
35
use Test::DBIx::Class {
36
    schema_class => 'Koha::Schema',
37
    connect_info => ['dbi:SQLite:dbname=:memory:','',''],
38
    connect_opts => { name_sep => '.', quote_char => '`', },
39
    fixture_class => '::Populate',
40
}, 'Itemtype' ;
41
42
sub fixtures {
43
    my ( $data ) = @_;
44
    fixtures_ok [
45
        Itemtype => [
46
            [
47
                'itemtype', 'description', 'rentalcharge', 'notforloan',
48
                'imageurl', 'summary', 'checkinmsg'
49
            ],
50
            @$data,
51
        ],
52
    ], 'add fixtures';
53
}
54
55
my $db = Test::MockModule->new('Koha::Database');
56
$db->mock( _new_schema => sub { return Schema(); } );
57
58
# Mock data
59
my $itemtypes = [
60
    [ 'BK', 'Books', 0, 0, '', '', 'foo' ],
61
    [ 'CD', 'CDRom', 0, 0, '', '', 'bar' ]
62
];
63
64
my @itemtypes = C4::ItemType->all();
65
is( @itemtypes, 0, 'Testing all itemtypes is empty' );
66
67
# Now lets mock some data
68
fixtures($itemtypes);
69
70
@itemtypes = C4::ItemType->all();
71
72
is( @itemtypes, 2, 'ItemType->all should return an array with 2 elements' );
73
74
is( $itemtypes[0]->fish, undef, 'Calling a bad descriptor gives undef' );
75
76
is( $itemtypes[0]->itemtype, 'BK', 'First itemtype is bk' );
77
78
is( $itemtypes[1]->itemtype, 'CD', 'second itemtype is cd' );
79
80
is( $itemtypes[0]->description, 'Books', 'First description is books' );
81
82
is( $itemtypes[1]->description, 'CDRom', 'second description is CDRom' );
83
84
is( $itemtypes[0]->rentalcharge, '0', 'first rental charge is 0' );
85
86
is( $itemtypes[1]->rentalcharge, '0', 'second rental charge is 0' );
87
88
is( $itemtypes[0]->notforloan, '0', 'first not for loan is 0' );
89
90
is( $itemtypes[1]->notforloan, '0', 'second not for loan is 0' );
91
92
is( $itemtypes[0]->imageurl, '', 'first imageurl is undef' );
93
94
is( $itemtypes[1]->imageurl, '', 'second imageurl is undef' );
95
96
is( $itemtypes[0]->checkinmsg, 'foo', 'first checkinmsg is foo' );
97
98
is( $itemtypes[1]->checkinmsg, 'bar', 'second checkinmsg is bar' );
99
100
# Test get(), which should return one itemtype
101
my $itemtype = C4::ItemType->get( 'BK' );
102
103
is( $itemtype->fish, undef, 'Calling a bad descriptor gives undef' );
104
105
is( $itemtype->itemtype, 'BK', 'itemtype is bk' );
106
107
is( $itemtype->description, 'Books', 'description is books' );
108
109
is( $itemtype->rentalcharge, '0', 'rental charge is 0' );
110
111
is( $itemtype->notforloan, '0', 'not for loan is 0' );
112
113
is( $itemtype->imageurl, '', ' not for loan is undef' );
114
115
is( $itemtype->checkinmsg, 'foo', 'checkinmsg is foo' );
116
117
$itemtype = C4::ItemType->get;
118
is( $itemtype, undef, 'C4::ItemType->get should return unless if no parameter is given' );
119
120
1;
121
- 

Return to bug 14828