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 86-175 Koha.pm provides many functions for Koha scripts. Link Here
86
86
87
=cut
87
=cut
88
88
89
=head2 GetItemTypes
90
91
  $itemtypes = &GetItemTypes( style => $style );
92
93
Returns information about existing itemtypes.
94
95
Params:
96
    style: either 'array' or 'hash', defaults to 'hash'.
97
           'array' returns an arrayref,
98
           'hash' return a hashref with the itemtype value as the key
99
100
build a HTML select with the following code :
101
102
=head3 in PERL SCRIPT
103
104
    my $itemtypes = GetItemTypes;
105
    my @itemtypesloop;
106
    foreach my $thisitemtype (sort keys %$itemtypes) {
107
        my $selected = 1 if $thisitemtype eq $itemtype;
108
        my %row =(value => $thisitemtype,
109
                    selected => $selected,
110
                    description => $itemtypes->{$thisitemtype}->{'description'},
111
                );
112
        push @itemtypesloop, \%row;
113
    }
114
    $template->param(itemtypeloop => \@itemtypesloop);
115
116
=head3 in TEMPLATE
117
118
    <form action='<!-- TMPL_VAR name="script_name" -->' method=post>
119
        <select name="itemtype">
120
            <option value="">Default</option>
121
        <!-- TMPL_LOOP name="itemtypeloop" -->
122
            <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="description" --></option>
123
        <!-- /TMPL_LOOP -->
124
        </select>
125
        <input type=text name=searchfield value="<!-- TMPL_VAR name="searchfield" -->">
126
        <input type="submit" value="OK" class="button">
127
    </form>
128
129
=cut
130
131
sub GetItemTypes {
132
    my ( %params ) = @_;
133
    my $style = defined( $params{'style'} ) ? $params{'style'} : 'hash';
134
135
    require C4::Languages;
136
    my $language = C4::Languages::getlanguage();
137
    # returns a reference to a hash of references to itemtypes...
138
    my $dbh   = C4::Context->dbh;
139
    my $query = q|
140
        SELECT
141
               itemtypes.itemtype,
142
               itemtypes.description,
143
               itemtypes.rentalcharge,
144
               itemtypes.notforloan,
145
               itemtypes.imageurl,
146
               itemtypes.summary,
147
               itemtypes.checkinmsg,
148
               itemtypes.checkinmsgtype,
149
               itemtypes.sip_media_type,
150
               itemtypes.hideinopac,
151
               itemtypes.searchcategory,
152
               COALESCE( localization.translation, itemtypes.description ) AS translated_description
153
        FROM   itemtypes
154
        LEFT JOIN localization ON itemtypes.itemtype = localization.code
155
            AND localization.entity = 'itemtypes'
156
            AND localization.lang = ?
157
        ORDER BY itemtype
158
    |;
159
    my $sth = $dbh->prepare($query);
160
    $sth->execute( $language );
161
162
    if ( $style eq 'hash' ) {
163
        my %itemtypes;
164
        while ( my $IT = $sth->fetchrow_hashref ) {
165
            $itemtypes{ $IT->{'itemtype'} } = $IT;
166
        }
167
        return ( \%itemtypes );
168
    } else {
169
        return [ sort { lc $a->{translated_description} cmp lc $b->{translated_description} } @{ $sth->fetchall_arrayref( {} ) } ];
170
    }
171
}
172
173
=head2 GetItemTypesCategorized
89
=head2 GetItemTypesCategorized
174
90
175
    $categories = GetItemTypesCategorized();
91
    $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