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 );
26 use C4::Biblio qw( GetMarcStructure GetMarcFromKohaField );
29 use Koha::SearchEngine::Indexer;
31 use Koha::Item::Attributes;
33 use Koha::CirculationRules;
35 use base qw(Koha::Objects);
37 use Koha::SearchEngine::Indexer;
41 Koha::Items - Koha Item object set class
49 =head3 filter_by_for_hold
51 my $filtered_items = $items->filter_by_for_hold;
53 Return the items of the set that are *potentially* holdable.
55 Caller has the responsibility to call C4::Reserves::CanItemBeReserved before
56 placing a hold on one of those items.
60 sub filter_by_for_hold {
63 my @hold_not_allowed_itypes = Koha::CirculationRules->search(
65 rule_name => 'holdallowed',
67 categorycode => undef,
68 rule_value => 'not_allowed',
70 )->get_column('itemtype');
71 push @hold_not_allowed_itypes, Koha::ItemTypes->search({ notforloan => 1 })->get_column('itemtype');
76 notforloan => { '<=' => 0 }, # items with negative or zero notforloan value are holdable
77 ( C4::Context->preference('AllowHoldsOnDamagedItems')? (): ( damaged => 0 ) ),
80 if ( C4::Context->preference("item-level_itypes") ) {
84 itype => { -not_in => \@hold_not_allowed_itypes },
91 'biblioitem.itemtype' => { -not_in => \@hold_not_allowed_itypes },
100 =head3 filter_by_visible_in_opac
102 my $filered_items = $items->filter_by_visible_in_opac(
104 [ patron => $patron ]
108 Returns a new resultset, containing those items that are not expected to be hidden in OPAC
109 for the passed I<Koha::Patron> object that is passed.
111 The I<OpacHiddenItems>, I<hidelostitems> and I<OpacHiddenItemsExceptions> system preferences
116 sub filter_by_visible_in_opac {
117 my ($self, $params) = @_;
119 my $patron = $params->{patron};
123 # Filter out OpacHiddenItems unless disabled by OpacHiddenItemsExceptions
124 unless ( $patron and $patron->category->override_hidden_items ) {
125 my $rules = C4::Context->yaml_preference('OpacHiddenItems') // {};
128 foreach my $field ( keys %$rules ) {
129 $rules_params->{$field} =
130 [ { '-not_in' => $rules->{$field} }, undef ];
133 $result = $result->search( $rules_params );
136 if (C4::Context->preference('hidelostitems')) {
137 $result = $result->filter_out_lost;
143 =head3 filter_out_lost
145 my $filered_items = $items->filter_out_lost;
147 Returns a new resultset, containing those items that are not marked as lost.
151 sub filter_out_lost {
154 my $params = { itemlost => 0 };
156 return $self->search( $params );
160 =head3 move_to_biblio
162 $items->move_to_biblio($to_biblio);
164 Move items to a given biblio.
169 my ( $self, $to_biblio ) = @_;
171 my $biblionumbers = { $to_biblio->biblionumber => 1 };
172 while ( my $item = $self->next() ) {
173 $biblionumbers->{ $item->biblionumber } = 1;
174 $item->move_to_biblio( $to_biblio, { skip_record_index => 1 } );
176 my $indexer = Koha::SearchEngine::Indexer->new({ index => $Koha::SearchEngine::BIBLIOS_INDEX });
177 for my $biblionumber ( keys %{$biblionumbers} ) {
178 $indexer->index_records( $biblionumber, "specialUpdate", "biblioserver" );
184 Koha::Items->search->batch_update
187 itemnotes => $new_item_notes,
191 itemnotes_nonpublic => {
197 exclude_from_local_holds_priority => 1|0,
199 # increment something here
204 Batch update the items.
206 Returns ( $report, $self )
208 * modified_itemnumbers - list of the modified itemnumbers
209 * modified_fields - number of fields modified
217 Allows to set a new value for given fields.
218 The key can be one of the item's column name, or one subfieldcode of a MARC subfields not linked with a Koha field
222 Allows to modify existing subfield's values using a regular expression
224 =item exclude_from_local_holds_priority
226 Set the passed boolean value to items.exclude_from_local_holds_priority
230 Callback function to call after an item has been modified
237 my ( $self, $params ) = @_;
239 my $regex_mod = $params->{regex_mod} || {};
240 my $new_values = $params->{new_values} || {};
241 my $exclude_from_local_holds_priority = $params->{exclude_from_local_holds_priority};
242 my $callback = $params->{callback};
244 my (@modified_itemnumbers, $modified_fields);
246 my $schema = Koha::Database->new->schema;
247 while ( my $item = $self->next ) {
249 try {$schema->txn_do(sub {
250 my $modified_holds_priority = 0;
251 if ( defined $exclude_from_local_holds_priority ) {
252 if(!defined $item->exclude_from_local_holds_priority || $item->exclude_from_local_holds_priority != $exclude_from_local_holds_priority) {
253 $item->exclude_from_local_holds_priority($exclude_from_local_holds_priority)->store;
254 $modified_holds_priority = 1;
259 my $new_values = {%$new_values}; # Don't modify the original
261 my $old_values = $item->unblessed;
262 if ( $item->more_subfields_xml ) {
265 %{$item->additional_attributes->to_hashref},
269 for my $attr ( keys %$regex_mod ) {
270 my $old_value = $old_values->{$attr};
272 next unless $old_value;
274 my $value = apply_regex(
276 %{ $regex_mod->{$attr} },
281 $new_values->{$attr} = $value;
284 for my $attribute ( keys %$new_values ) {
285 next if $attribute eq 'more_subfields_xml'; # Already counted before
287 my $old = $old_values->{$attribute};
288 my $new = $new_values->{$attribute};
290 if ( defined $old xor defined $new )
291 || ( defined $old && defined $new && $new ne $old );
294 { # Dealing with more_subfields_xml
296 my $frameworkcode = $item->biblio->frameworkcode;
297 my $tagslib = C4::Biblio::GetMarcStructure( 1, $frameworkcode, { unsafe => 1 });
298 my ( $itemtag, $itemsubfield ) = C4::Biblio::GetMarcFromKohaField( "items.itemnumber" );
300 my @more_subfield_tags = map {
304 && !$_->{kohafield} # Get subfields that are not mapped
308 } values %{ $tagslib->{$itemtag} };
310 my $more_subfields_xml = Koha::Item::Attributes->new(
313 exists $new_values->{$_} ? ( $_ => $new_values->{$_} )
314 : exists $old_values->{$_}
315 ? ( $_ => $old_values->{$_} )
317 } @more_subfield_tags
319 )->to_marcxml($frameworkcode);
321 $new_values->{more_subfields_xml} = $more_subfields_xml;
323 delete $new_values->{$_} for @more_subfield_tags; # Clean the hash
328 my $itemlost_pre = $item->itemlost;
329 $item->set($new_values)->store({skip_record_index => 1});
332 $item->itemnumber, 'batchmod', undef,
333 { skip_record_index => 1 }
335 and not $itemlost_pre;
338 push @modified_itemnumbers, $item->itemnumber if $modified || $modified_holds_priority;
339 $modified_fields += $modified + $modified_holds_priority;
347 if (@modified_itemnumbers) {
348 my @biblionumbers = uniq(
349 Koha::Items->search( { itemnumber => \@modified_itemnumbers } )
350 ->get_column('biblionumber'));
352 my $indexer = Koha::SearchEngine::Indexer->new(
353 { index => $Koha::SearchEngine::BIBLIOS_INDEX } );
354 $indexer->index_records( \@biblionumbers, 'specialUpdate',
355 "biblioserver", undef )
359 return ( { modified_itemnumbers => \@modified_itemnumbers, modified_fields => $modified_fields }, $self );
362 sub apply_regex { # FIXME Should be moved outside of Koha::Items
364 my $search = $params->{search};
365 my $replace = $params->{replace};
366 my $modifiers = $params->{modifiers} || q{};
367 my $value = $params->{value};
369 my @available_modifiers = qw( i g );
370 my $retained_modifiers = q||;
371 for my $modifier ( split //, $modifiers ) {
372 $retained_modifiers .= $modifier
373 if grep { /$modifier/ } @available_modifiers;
375 if ( $retained_modifiers =~ m/^(ig|gi)$/ ) {
376 $value =~ s/$search/$replace/ig;
378 elsif ( $retained_modifiers eq 'i' ) {
379 $value =~ s/$search/$replace/i;
381 elsif ( $retained_modifiers eq 'g' ) {
382 $value =~ s/$search/$replace/g;
385 $value =~ s/$search/$replace/;
392 =head2 Internal methods
412 Kyle M Hall <kyle@bywatersolutions.com>
413 Tomas Cohen Arazi <tomascohen@theke.io>
414 Martin Renvoize <martin.renvoize@ptfs-europe.com>