Bugzilla – Attachment 173135 Details for
Bug 38224
Move populate_empty_callnumbers logic into Koha::Biblio for reusability
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
Bug 38224: Add Koha::Biblio::populate_item_callnumbers
Bug-38224-Add-KohaBibliopopulateitemcallnumbers.patch (text/plain), 4.73 KB, created by
Tomás Cohen Arazi (tcohen)
on 2024-10-22 12:45:22 UTC
(
hide
)
Description:
Bug 38224: Add Koha::Biblio::populate_item_callnumbers
Filename:
MIME Type:
Creator:
Tomás Cohen Arazi (tcohen)
Created:
2024-10-22 12:45:22 UTC
Size:
4.73 KB
patch
obsolete
>From c6fa070e7352cd039163a5494ba864f96fdbf6f3 Mon Sep 17 00:00:00 2001 >From: Tomas Cohen Arazi <tomascohen@theke.io> >Date: Tue, 22 Oct 2024 08:48:37 -0300 >Subject: [PATCH] Bug 38224: Add Koha::Biblio::populate_item_callnumbers > >This patch introduces two methods to the `Koha::Biblio` class: > >* `get_first_callnumber`: returns the first found callnumber on the > record, based on the `itemcallnumber` syspref. >* `populate_item_callnumbers`: populates the bibliographic record's > items based on the return value of `get_first_callnumber`. Can be > passed a filter on the items. > >To test: >1. Apply this patch >2. Run: > $ ktd --shell > k$ prove t/db_dependent/Koha/Biblio.t >=> SUCCESS: Tests pass! >3. Sign off :-D >--- > Koha/Biblio.pm | 96 ++++++++++++++++++++++++++++++++++++++- > Koha/Exceptions/Biblio.pm | 19 ++++++++ > 2 files changed, 114 insertions(+), 1 deletion(-) > create mode 100644 Koha/Exceptions/Biblio.pm > >diff --git a/Koha/Biblio.pm b/Koha/Biblio.pm >index 8829a3e63c5..ea8819b0341 100644 >--- a/Koha/Biblio.pm >+++ b/Koha/Biblio.pm >@@ -33,6 +33,8 @@ use C4::Acquisition qw( ModOrder GetOrdersByBiblionumber ); > use Koha::Database; > use Koha::DateUtils qw( dt_from_string ); > use Koha::Exception; >+use Koha::Exceptions::Biblio; >+use Koha::Exceptions::Syspref; > > use base qw(Koha::Object); > >@@ -2208,9 +2210,101 @@ sub merge_with { > return \%results; > } > >+=head3 populate_item_callnumbers >+ >+ $biblio->populate_item_callnumbers( [ $filter ] ); >+ >+Populates the linked items callnumbers following the I<itemcallnumber> >+system preference. Optionally, this action can be restricted by appliying >+a I<$filter> on the items iterator. >+ >+=cut >+ >+sub populate_item_callnumbers { >+ my ( $self, $filter ) = @_; >+ >+ my $callnumber = $self->get_first_callnumber(); >+ my $items = $self->items->search($filter); >+ >+ if ( $items->count ) { >+ my ($res) = $items->batch_update( { new_values => { itemcallnumber => $callnumber } } ); >+ my @modified_itemnumbers = @{ $res->{modified_itemnumbers} }; >+ $self->add_message( >+ { >+ message => q{populate_item_callnumbers}, >+ type => q{info}, >+ payload => { >+ updated_items_count => scalar @modified_itemnumbers, >+ callnumber => $callnumber, >+ modified_item_ids => \@modified_itemnumbers, >+ } >+ } >+ ); >+ } else { >+ $self->add_message( >+ { >+ message => q{populate_item_callnumbers}, >+ type => q{info}, >+ payload => { >+ updated_items_count => 0, >+ callnumber => $callnumber, >+ } >+ } >+ ); >+ } >+ >+ return $self; >+} >+ >+=head3 get_first_callnumber >+ >+ my $callnumber = $biblio->get_first_callnumber(); >+ >+Retrieves the first callnumber it finds using the I<itemcallnumber> system >+preference. Returns undef if it doesn't find any. >+ >+Throws a I<Koha::Exceptions::SysPrefs::NotSet> exception if the relevant >+system preference is not set. >+ >+Throws a I<Koha::Exceptions::Biblio::MissingField> exception if the record >+doesn't contain a callnumber. >+ >+=cut >+ >+sub get_first_callnumber { >+ my ($self) = @_; >+ >+ my $cn_fields = C4::Context->preference(q{itemcallnumber}); >+ Koha::Exceptions::SysPref::NotSet->throw( syspref => q{itemcallnumber} ) >+ unless $cn_fields; >+ >+ my $record = $self->record; >+ my $callnumber; >+ >+ foreach my $callnumber_marc_field ( split( /,/, $cn_fields ) ) { >+ >+ my $callnumber_tag = substr( $callnumber_marc_field, 0, 3 ); >+ my $callnumber_subfields = substr( $callnumber_marc_field, 3 ); >+ >+ next unless $callnumber_tag && $callnumber_subfields; >+ >+ my $field = $record->field($callnumber_tag); >+ >+ next unless $field; >+ >+ $callnumber = $field->as_string( $callnumber_subfields, '' ); >+ last if $callnumber; >+ } >+ >+ Koha::Exceptions::Biblio::MissingField->throw( field => q{callnumber} ) >+ unless defined $callnumber; >+ >+ return $callnumber; >+} >+ > =head2 Internal methods > >-=head3 type >+=head3 _type > > =cut > >diff --git a/Koha/Exceptions/Biblio.pm b/Koha/Exceptions/Biblio.pm >new file mode 100644 >index 00000000000..6fd1abd811d >--- /dev/null >+++ b/Koha/Exceptions/Biblio.pm >@@ -0,0 +1,19 @@ >+package Koha::Exceptions::Biblio; >+ >+use Modern::Perl; >+ >+use Koha::Exception; >+ >+use Exception::Class ( >+ >+ 'Koha::Exceptions::Biblio' => { >+ isa => 'Koha::Exception', >+ }, >+ 'Koha::Exceptions::Biblio::MissingField' => { >+ isa => 'Koha::Exceptions::Biblio', >+ description => 'The required field is missing in the record', >+ fields => ['field'] >+ } >+); >+ >+1; >-- >2.47.0
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
|
Splinter Review
Attachments on
bug 38224
:
173134
|
173135
|
173136
|
174244
|
174245
|
174246