Bug 32550: (bug 28445 follow-up) Fix 'Clear on loan' link on item batch mod
[koha-ffzg.git] / Koha / UI / Table / Builder / Items.pm
1 package Koha::UI::Table::Builder::Items;
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 use List::MoreUtils qw( uniq );
20 use C4::Biblio qw( GetMarcStructure GetMarcFromKohaField IsMarcStructureInternal );
21 use Koha::Items;
22
23 =head1 NAME
24
25 Koha::UI::Table::Builder::Items
26
27 Helper to build a table with a list of items with all their information.
28
29 Items' attributes that are mapped and not mapped will be listed in the table.
30
31 Only attributes that have been defined only once will be displayed (empty string is considered as not defined).
32
33 =head1 API
34
35 =head2 Class methods
36
37 =cut
38
39 =head3 new
40
41     my $table = Koha::UI::Table::Builder::Items->new( { itemnumbers => \@itemnumbers } );
42
43 Constructor.
44
45 =cut
46
47 sub new {
48     my ( $class, $params ) = @_;
49
50     my $self;
51     $self->{itemnumbers} = $params->{itemnumbers} || [];
52
53     bless $self, $class;
54     return $self;
55 }
56
57 =head3 build_table
58
59     my $items_table = Koha::UI::Table::Builder::Items->new( { itemnumbers => \@itemnumbers } )
60                                                      ->build_table;
61
62     my $items   = $items_table->{items};
63     my $headers = $items_table->{headers};
64
65 Build the headers and rows for the table.
66
67 Use it with:
68     [% PROCESS items_table_batchmod headers => headers, items => items %]
69
70 =cut
71
72 sub build_table {
73     my ( $self, $params ) = @_;
74     my %itemnumbers_to_idx = map { $self->{itemnumbers}->[$_] => $_ } 0..$#{$self->{itemnumbers}};
75     my $items = Koha::Items->search( { itemnumber => $self->{itemnumbers} } );
76
77     my @items;
78     while ( my $item = $items->next ) {
79         my $item_info = $item->columns_to_str;
80         $item_info = {
81             %$item_info,
82             index          => $itemnumbers_to_idx{$item->itemnumber},
83             biblio         => $item->biblio,
84             safe_to_delete => $item->safe_to_delete,
85             holds          => $item->biblio->holds->count,
86             item_holds     => $item->holds->count,
87             is_checked_out => $item->checkout ? 1 : 0,
88         };
89         push @items, $item_info;
90     }
91
92     $self->{headers} = $self->_build_headers( \@items );
93     $self->{items}   = \@items;
94     return $self;
95 }
96
97 =head2 Internal methods
98
99 =cut
100
101 =head3 _build_headers
102
103 Build the headers given the items' info.
104
105 =cut
106
107 sub _build_headers {
108     my ( $self, $items ) = @_;
109
110     my @witness_attributes = uniq map {
111         my $item = $_;
112         map {
113             defined $item->{$_}
114               && !ref( $item->{$_} ) # biblio and safe_to_delete are objects
115               && $item->{$_} ne ""
116               ? $_
117               : ()
118           } keys %$item
119     } @$items;
120
121     my ( $itemtag, $itemsubfield ) =
122       C4::Biblio::GetMarcFromKohaField("items.itemnumber");
123     my $tagslib = C4::Biblio::GetMarcStructure(1);
124     my $subfieldcode_attribute_mappings;
125     for my $subfield_code ( keys %{ $tagslib->{$itemtag} } ) {
126
127         my $subfield = $tagslib->{$itemtag}->{$subfield_code};
128
129         next if IsMarcStructureInternal($subfield);
130         next unless $subfield->{tab} eq 10;    # Is this really needed?
131
132         my $attribute;
133         if ( $subfield->{kohafield} ) {
134             ( $attribute = $subfield->{kohafield} ) =~ s|^items\.||;
135         }
136         else {
137             $attribute = $subfield_code;       # It's in more_subfields_xml
138         }
139         next unless grep { $attribute eq $_ } @witness_attributes;
140         $subfieldcode_attribute_mappings->{$subfield_code} = $attribute;
141     }
142
143     return [
144         map {
145             {
146                 header_value  => $tagslib->{$itemtag}->{$_}->{lib},
147                 attribute     => $subfieldcode_attribute_mappings->{$_},
148                 subfield_code => $_,
149             }
150         } sort keys %$subfieldcode_attribute_mappings
151     ];
152 }
153
154 1