From 3e95a107e0c4ebbec150799576d8d6e02057fd1a Mon Sep 17 00:00:00 2001 From: Pedro Amorim Date: Thu, 19 Dec 2024 11:55:48 +0000 Subject: [PATCH] Bug 38751: Update ILL::Request->extended_attributes This updates the extended_attributes method to also act as setter. Additionally, it'll skip adding any attribute that result in error, preventing an ugly error 500. Test plan: 1) Enable ILLModule sys pref 2) Attempt to create the following openurl request through the OPAC: http://localhost:8080/cgi-bin/koha/opac-illrequests.pl?DOI=10.1001%2Fjama.2024.11146&atitle=Interventions%20for%20High%20Body%20Mass%20Index%20in%20Children%20and%20Adolescents%3A%20US%20Preventive%20Services%20Task%20Force%20Recommendation%20Statement.&aulast=Nicholson%2C%20Wanda%20K.&backend=FreeForm&cardnumber=53807898&date=20240716&genre=article&id=DOI%3A10.1001%2Fjama.2024.11146&issn=00987484&issue=3&method=create&opac=1&openurl=1 3) Notice it fails with a Duplicate ID error and the OPAC blows up. This is because Koha is trying to add the 'DOI' illrequestattribute twice and that is resulting in a duplicate ID 4) Apply patches, repeat test plan, notice the error no longer happens, the request is created correctly and all successful attribute have been added. Signed-off-by: David Nind Signed-off-by: Martin Renvoize --- Koha/ILL/Request.pm | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/Koha/ILL/Request.pm b/Koha/ILL/Request.pm index 4bf239ce6ac..dbb809b48d5 100644 --- a/Koha/ILL/Request.pm +++ b/Koha/ILL/Request.pm @@ -277,12 +277,42 @@ sub library { my $extended_attributes = $request->extended_attributes; -Returns the linked I resultset object. + my $extended_attributes = $request->extended_attributes([ + { + type => 'type', + value => 'type_value', + }, + { + type => 'type2', + value => 'type2_value', + }, + ]); + +Getter or setter for extended attributes + +Getter: Returns the linked I resultset object. + +Setter: Adds the supplied extended attributes to the request =cut sub extended_attributes { - my ( $self ) = @_; + my ( $self, $attributes ) = @_; + + if ($attributes) { + my $schema = $self->_result->result_source->schema; + $schema->txn_do( + sub { + for my $attribute (@$attributes) { + $attribute->{backend} = $self->backend; + eval { $self->_result->add_to_illrequestattributes($attribute); }; + if ($@) { + warn "Error adding attribute $attribute->{type}: $@"; + } + } + } + ); + } my $rs = $self->_result->extended_attributes; # We call search to use the filters in Koha::ILL::Request::Attributes->search -- 2.47.1