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

(-)a/C4/Koha.pm (-85 / +1 lines)
Lines 41-47 BEGIN { Link Here
41
	@ISA    = qw(Exporter);
41
	@ISA    = qw(Exporter);
42
	@EXPORT = qw(
42
	@EXPORT = qw(
43
        &GetPrinters &GetPrinter
43
        &GetPrinters &GetPrinter
44
        &GetItemTypes &getitemtypeinfo
44
        &getitemtypeinfo
45
        &GetItemTypesCategorized
45
        &GetItemTypesCategorized
46
        &getallthemes
46
        &getallthemes
47
        &getFacets
47
        &getFacets
Lines 85-174 Koha.pm provides many functions for Koha scripts. Link Here
85
85
86
=cut
86
=cut
87
87
88
=head2 GetItemTypes
89
90
  $itemtypes = &GetItemTypes( style => $style );
91
92
Returns information about existing itemtypes.
93
94
Params:
95
    style: either 'array' or 'hash', defaults to 'hash'.
96
           'array' returns an arrayref,
97
           'hash' return a hashref with the itemtype value as the key
98
99
build a HTML select with the following code :
100
101
=head3 in PERL SCRIPT
102
103
    my $itemtypes = GetItemTypes;
104
    my @itemtypesloop;
105
    foreach my $thisitemtype (sort keys %$itemtypes) {
106
        my $selected = 1 if $thisitemtype eq $itemtype;
107
        my %row =(value => $thisitemtype,
108
                    selected => $selected,
109
                    description => $itemtypes->{$thisitemtype}->{'description'},
110
                );
111
        push @itemtypesloop, \%row;
112
    }
113
    $template->param(itemtypeloop => \@itemtypesloop);
114
115
=head3 in TEMPLATE
116
117
    <form action='<!-- TMPL_VAR name="script_name" -->' method=post>
118
        <select name="itemtype">
119
            <option value="">Default</option>
120
        <!-- TMPL_LOOP name="itemtypeloop" -->
121
            <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="description" --></option>
122
        <!-- /TMPL_LOOP -->
123
        </select>
124
        <input type=text name=searchfield value="<!-- TMPL_VAR name="searchfield" -->">
125
        <input type="submit" value="OK" class="button">
126
    </form>
127
128
=cut
129
130
sub GetItemTypes {
131
    my ( %params ) = @_;
132
    my $style = defined( $params{'style'} ) ? $params{'style'} : 'hash';
133
134
    require C4::Languages;
135
    my $language = C4::Languages::getlanguage();
136
    # returns a reference to a hash of references to itemtypes...
137
    my $dbh   = C4::Context->dbh;
138
    my $query = q|
139
        SELECT
140
               itemtypes.itemtype,
141
               itemtypes.description,
142
               itemtypes.rentalcharge,
143
               itemtypes.notforloan,
144
               itemtypes.imageurl,
145
               itemtypes.summary,
146
               itemtypes.checkinmsg,
147
               itemtypes.checkinmsgtype,
148
               itemtypes.sip_media_type,
149
               itemtypes.hideinopac,
150
               itemtypes.searchcategory,
151
               COALESCE( localization.translation, itemtypes.description ) AS translated_description
152
        FROM   itemtypes
153
        LEFT JOIN localization ON itemtypes.itemtype = localization.code
154
            AND localization.entity = 'itemtypes'
155
            AND localization.lang = ?
156
        ORDER BY itemtype
157
    |;
158
    my $sth = $dbh->prepare($query);
159
    $sth->execute( $language );
160
161
    if ( $style eq 'hash' ) {
162
        my %itemtypes;
163
        while ( my $IT = $sth->fetchrow_hashref ) {
164
            $itemtypes{ $IT->{'itemtype'} } = $IT;
165
        }
166
        return ( \%itemtypes );
167
    } else {
168
        return [ sort { lc $a->{translated_description} cmp lc $b->{translated_description} } @{ $sth->fetchall_arrayref( {} ) } ];
169
    }
170
}
171
172
=head2 GetItemTypesCategorized
88
=head2 GetItemTypesCategorized
173
89
174
    $categories = GetItemTypesCategorized();
90
    $categories = GetItemTypesCategorized();
(-)a/t/db_dependent/Koha.t (-11 / +1 lines)
Lines 9-15 use Koha::DateUtils qw(dt_from_string); Link Here
9
use Koha::AuthorisedValue;
9
use Koha::AuthorisedValue;
10
use Koha::AuthorisedValueCategories;
10
use Koha::AuthorisedValueCategories;
11
11
12
use Test::More tests => 8;
12
use Test::More tests => 7;
13
use DateTime::Format::MySQL;
13
use DateTime::Format::MySQL;
14
14
15
BEGIN {
15
BEGIN {
Lines 294-306 subtest 'GetItemTypesCategorized test' => sub{ Link Here
294
    is_deeply(\@results,\@expected, 'GetItemTypesCategorized: grouped and ungrouped items returned as expected.');
294
    is_deeply(\@results,\@expected, 'GetItemTypesCategorized: grouped and ungrouped items returned as expected.');
295
};
295
};
296
296
297
subtest 'GetItemTypes test' => sub {
298
    plan tests => 1;
299
    $dbh->do(q|DELETE FROM itemtypes|);
300
    $dbh->do(q|INSERT INTO itemtypes(itemtype, description) VALUES ('a', 'aa desc'), ('b', 'zz desc'), ('d', 'dd desc'), ('c', 'yy desc')|);
301
    my $itemtypes = C4::Koha::GetItemTypes( style => 'array' );
302
    $itemtypes = [ map { $_->{itemtype} } @$itemtypes ];
303
    is_deeply( $itemtypes, [ 'a', 'd', 'c', 'b' ], 'GetItemTypes(array) should return itemtypes ordered by description');
304
};
305
306
$dbh->rollback();
297
$dbh->rollback();
307
- 

Return to bug 17835