3 # Copyright ByWater Solutions 2014
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
21 use Array::Utils qw( array_minus );
22 use List::MoreUtils qw( uniq );
25 use C4::Biblio qw( GetMarcStructure GetMarcFromKohaField );
28 use Koha::SearchEngine::Indexer;
30 use Koha::Item::Attributes;
32 use Koha::CirculationRules;
34 use base qw(Koha::Objects);
36 use Koha::SearchEngine::Indexer;
40 Koha::Items - Koha Item object set class
48 =head3 filter_by_for_hold
50 my $filtered_items = $items->filter_by_for_hold;
52 Return the items of the set that are *potentially* holdable.
54 Caller has the responsibility to call C4::Reserves::CanItemBeReserved before
55 placing a hold on one of those items.
59 sub filter_by_for_hold {
62 my @hold_not_allowed_itypes = Koha::CirculationRules->search(
64 rule_name => 'holdallowed',
66 categorycode => undef,
67 rule_value => 'not_allowed',
69 )->get_column('itemtype');
70 push @hold_not_allowed_itypes, Koha::ItemTypes->search({ notforloan => 1 })->get_column('itemtype');
75 notforloan => { '<=' => 0 }, # items with negative or zero notforloan value are holdable
76 ( C4::Context->preference('AllowHoldsOnDamagedItems')? (): ( damaged => 0 ) ),
79 if ( C4::Context->preference("item-level_itypes") ) {
83 itype => { -not_in => \@hold_not_allowed_itypes },
90 'biblioitem.itemtype' => { -not_in => \@hold_not_allowed_itypes },
99 =head3 filter_by_visible_in_opac
101 my $filered_items = $items->filter_by_visible_in_opac(
103 [ patron => $patron ]
107 Returns a new resultset, containing those items that are not expected to be hidden in OPAC
108 for the passed I<Koha::Patron> object that is passed.
110 The I<OpacHiddenItems>, I<hidelostitems> and I<OpacHiddenItemsExceptions> system preferences
115 sub filter_by_visible_in_opac {
116 my ($self, $params) = @_;
118 my $patron = $params->{patron};
122 # Filter out OpacHiddenItems unless disabled by OpacHiddenItemsExceptions
123 unless ( $patron and $patron->category->override_hidden_items ) {
124 my $rules = C4::Context->yaml_preference('OpacHiddenItems') // {};
127 foreach my $field ( keys %$rules ) {
128 $rules_params->{$field} =
129 [ { '-not_in' => $rules->{$field} }, undef ];
132 $result = $result->search( $rules_params );
135 if (C4::Context->preference('hidelostitems')) {
136 $result = $result->filter_out_lost;
142 =head3 filter_out_lost
144 my $filered_items = $items->filter_out_lost;
146 Returns a new resultset, containing those items that are not marked as lost.
150 sub filter_out_lost {
153 my $params = { itemlost => 0 };
155 return $self->search( $params );
159 =head3 move_to_biblio
161 $items->move_to_biblio($to_biblio);
163 Move items to a given biblio.
168 my ( $self, $to_biblio ) = @_;
170 my $biblionumbers = { $to_biblio->biblionumber => 1 };
171 while ( my $item = $self->next() ) {
172 $biblionumbers->{ $item->biblionumber } = 1;
173 $item->move_to_biblio( $to_biblio, { skip_record_index => 1 } );
175 my $indexer = Koha::SearchEngine::Indexer->new({ index => $Koha::SearchEngine::BIBLIOS_INDEX });
176 for my $biblionumber ( keys %{$biblionumbers} ) {
177 $indexer->index_records( $biblionumber, "specialUpdate", "biblioserver" );
183 Koha::Items->search->batch_update
186 itemnotes => $new_item_notes,
190 itemnotes_nonpublic => {
196 exclude_from_local_holds_priority => 1|0,
198 # increment something here
203 Batch update the items.
205 Returns ( $report, $self )
207 * modified_itemnumbers - list of the modified itemnumbers
208 * modified_fields - number of fields modified
216 Allows to set a new value for given fields.
217 The key can be one of the item's column name, or one subfieldcode of a MARC subfields not linked with a Koha field
221 Allows to modify existing subfield's values using a regular expression
223 =item exclude_from_local_holds_priority
225 Set the passed boolean value to items.exclude_from_local_holds_priority
229 Callback function to call after an item has been modified
236 my ( $self, $params ) = @_;
238 my $regex_mod = $params->{regex_mod} || {};
239 my $new_values = $params->{new_values} || {};
240 my $exclude_from_local_holds_priority = $params->{exclude_from_local_holds_priority};
241 my $callback = $params->{callback};
243 my (@modified_itemnumbers, $modified_fields);
245 while ( my $item = $self->next ) {
247 my $modified_holds_priority = 0;
248 if ( defined $exclude_from_local_holds_priority ) {
249 if(!defined $item->exclude_from_local_holds_priority || $item->exclude_from_local_holds_priority != $exclude_from_local_holds_priority) {
250 $item->exclude_from_local_holds_priority($exclude_from_local_holds_priority)->store;
251 $modified_holds_priority = 1;
256 my $new_values = {%$new_values}; # Don't modify the original
258 my $old_values = $item->unblessed;
259 if ( $item->more_subfields_xml ) {
262 %{$item->additional_attributes->to_hashref},
266 for my $attr ( keys %$regex_mod ) {
267 my $old_value = $old_values->{$attr};
269 next unless $old_value;
271 my $value = apply_regex(
273 %{ $regex_mod->{$attr} },
278 $new_values->{$attr} = $value;
281 for my $attribute ( keys %$new_values ) {
282 next if $attribute eq 'more_subfields_xml'; # Already counted before
284 my $old = $old_values->{$attribute};
285 my $new = $new_values->{$attribute};
287 if ( defined $old xor defined $new )
288 || ( defined $old && defined $new && $new ne $old );
291 { # Dealing with more_subfields_xml
293 my $frameworkcode = $item->biblio->frameworkcode;
294 my $tagslib = C4::Biblio::GetMarcStructure( 1, $frameworkcode, { unsafe => 1 });
295 my ( $itemtag, $itemsubfield ) = C4::Biblio::GetMarcFromKohaField( "items.itemnumber" );
297 my @more_subfield_tags = map {
301 && !$_->{kohafield} # Get subfields that are not mapped
305 } values %{ $tagslib->{$itemtag} };
307 my $more_subfields_xml = Koha::Item::Attributes->new(
310 exists $new_values->{$_} ? ( $_ => $new_values->{$_} )
311 : exists $old_values->{$_}
312 ? ( $_ => $old_values->{$_} )
314 } @more_subfield_tags
316 )->to_marcxml($frameworkcode);
318 $new_values->{more_subfields_xml} = $more_subfields_xml;
320 delete $new_values->{$_} for @more_subfield_tags; # Clean the hash
325 my $itemlost_pre = $item->itemlost;
326 $item->set($new_values)->store({skip_record_index => 1});
329 $item->itemnumber, 'batchmod', undef,
330 { skip_record_index => 1 }
332 and not $itemlost_pre;
334 push @modified_itemnumbers, $item->itemnumber if $modified || $modified_holds_priority;
335 $modified_fields += $modified + $modified_holds_priority;
343 if (@modified_itemnumbers) {
344 my @biblionumbers = uniq(
345 Koha::Items->search( { itemnumber => \@modified_itemnumbers } )
346 ->get_column('biblionumber'));
348 my $indexer = Koha::SearchEngine::Indexer->new(
349 { index => $Koha::SearchEngine::BIBLIOS_INDEX } );
350 $indexer->index_records( \@biblionumbers, 'specialUpdate',
351 "biblioserver", undef )
355 return ( { modified_itemnumbers => \@modified_itemnumbers, modified_fields => $modified_fields }, $self );
358 sub apply_regex { # FIXME Should be moved outside of Koha::Items
360 my $search = $params->{search};
361 my $replace = $params->{replace};
362 my $modifiers = $params->{modifiers} || q{};
363 my $value = $params->{value};
365 my @available_modifiers = qw( i g );
366 my $retained_modifiers = q||;
367 for my $modifier ( split //, $modifiers ) {
368 $retained_modifiers .= $modifier
369 if grep { /$modifier/ } @available_modifiers;
371 if ( $retained_modifiers =~ m/^(ig|gi)$/ ) {
372 $value =~ s/$search/$replace/ig;
374 elsif ( $retained_modifiers eq 'i' ) {
375 $value =~ s/$search/$replace/i;
377 elsif ( $retained_modifiers eq 'g' ) {
378 $value =~ s/$search/$replace/g;
381 $value =~ s/$search/$replace/;
388 =head2 Internal methods
408 Kyle M Hall <kyle@bywatersolutions.com>
409 Tomas Cohen Arazi <tomascohen@theke.io>
410 Martin Renvoize <martin.renvoize@ptfs-europe.com>