Bug 31236: Add ability to send custom item fields via SIP using Template Toolkit

Koha has been able to send arbitrary item fields via the "item_field" parameter in the config. We have partners that need the ability to created custom item fields from templates, as the item_fields feature cannot accomplish what they need. We need to add a templated custom field feature for items, similar to what we have for patrons.

Test Plan:
1) Apply this patch
2) Choose a SIP login to use, edit that account and add the following
   *inside* the login section:
   <custom_item_field field="IN" template="[% item.itemnumber %]" />
3) Restart SIP
4) Run an item information query
5) Note the itemnumber is sent in the IN field!

Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
Edit: fixed tests count
This commit is contained in:
Kyle Hall 2022-07-25 10:56:08 -04:00 committed by Tomas Cohen Arazi
parent 1e9f6e61d5
commit 14cfdfd727
4 changed files with 94 additions and 2 deletions

View File

@ -14,7 +14,7 @@ use Carp;
use Template;
use C4::SIP::ILS::Transaction;
use C4::SIP::Sip qw(add_field);
use C4::SIP::Sip qw(add_field maybe_add);
use C4::Biblio;
use C4::Circulation qw( barcodedecode );
@ -435,9 +435,69 @@ sub build_additional_item_fields_string {
}
}
if ( $server->{account}->{custom_item_field} ) {
my @custom_fields =
ref $server->{account}->{custom_item_field} eq "ARRAY"
? @{ $server->{account}->{custom_item_field} }
: $server->{account}->{custom_item_field};
foreach my $custom_field ( @custom_fields ) {
my $field = $custom_field->{field};
return q{} unless defined $field;
my $template = $custom_field->{template};
my $formatted = $self->format( $template );
my $substring = maybe_add( $field, $formatted, $server );
$string .= $substring;
}
}
return $string;
}
=head2 build_custom_field_string
This method builds the part of the sip message for custom item fields as defined in the sip config
=cut
sub build_custom_field_string {
my ( $self, $server ) = @_;
my $string = q{};
return $string;
}
=head2 format
This method uses a template to build a string from a Koha::Item object
If errors are encountered in processing template we log them and return nothing
=cut
sub format {
my ( $self, $template ) = @_;
if ($template) {
require Template;
my $tt = Template->new();
my $item = $self->{_object};
my $output;
eval {
$tt->process( \$template, { item => $item }, \$output );
};
if ( $@ ){
siplog("LOG_DEBUG", "Error processing template: $template");
return "";
}
return $output;
}
}
1;
__END__

View File

@ -1279,6 +1279,7 @@ sub handle_item_information {
$resp .= maybe_add( FID_PRINT_LINE, $item->print_line, $server );
$resp .= $item->build_additional_item_fields_string( $server );
$resp .= $item->build_custom_field_string( $server );
}
$self->write_msg( $resp, undef, $server->{account}->{terminator}, $server->{account}->{encoding} );

View File

@ -88,6 +88,7 @@
<AllFinesNeedOverride>0</AllFinesNeedOverride>
</syspref_overrides>
<custom_patron_field field="DE" template="[% patron.dateexpiry %]" />
<custom_item_field field="IN" template="[% item.itemnumber %]" />
</login>
</accounts>

View File

@ -21,7 +21,7 @@
# along with Koha; if not, see <http://www.gnu.org/licenses>.
use Modern::Perl;
use Test::More tests => 14;
use Test::More tests => 15;
use Test::Exception;
use Test::MockObject;
use Test::MockModule;
@ -265,6 +265,36 @@ subtest "Test build_additional_item_fields_string" => sub {
$schema->storage->txn_rollback;
};
subtest "Test build_custom_field_string" => sub {
my $schema = Koha::Database->new->schema;
$schema->storage->txn_begin;
plan tests => 2;
my $builder = t::lib::TestBuilder->new();
my $item = $builder->build_sample_item;
my $item_id = $item->id;
my $item_barcode = $item->barcode;
my $ils_item = C4::SIP::ILS::Item->new( $item->barcode );
my $server = {};
$server->{account}->{custom_item_field}->{field} = "XY";
$server->{account}->{custom_item_field}->{template} = "[% item.id %] [% item.barcode %], woo!";
my $attribute_string = $ils_item->build_additional_item_fields_string( $server );
is( $attribute_string, "XY$item_id $item_barcode, woo!|", 'Attribute field generated correctly with single param' );
$server = {};
$server->{account}->{custom_item_field}->[0]->{field} = "ZY";
$server->{account}->{custom_item_field}->[0]->{template} = "[% item.id %]!";
$server->{account}->{custom_item_field}->[1]->{field} = "ZX";
$server->{account}->{custom_item_field}->[1]->{template} = "[% item.barcode %]*";
$attribute_string = $ils_item->build_additional_item_fields_string( $server );
is( $attribute_string, sprintf("ZY%s!|ZX%s*|", $item_id, $item_barcode), 'Attribute field generated correctly with multiple params' );
$schema->storage->txn_rollback;
};
subtest "Test cr_item_field" => sub {
plan tests => 2;