@@ -, +, @@
using Template Toolkit
*inside* the login section:
---
C4/SIP/ILS/Item.pm | 62 +++++++++++++++++++++++++++++++++++-
C4/SIP/Sip/MsgType.pm | 1 +
etc/SIPconfig.xml | 1 +
t/db_dependent/SIP/Message.t | 32 ++++++++++++++++++-
4 files changed, 94 insertions(+), 2 deletions(-)
--- a/C4/SIP/ILS/Item.pm
+++ a/C4/SIP/ILS/Item.pm
@@ -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 );
@@ -431,9 +431,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__
--- a/C4/SIP/Sip/MsgType.pm
+++ a/C4/SIP/Sip/MsgType.pm
@@ -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} );
--- a/etc/SIPconfig.xml
+++ a/etc/SIPconfig.xml
@@ -87,6 +87,7 @@
0
+
--- a/t/db_dependent/SIP/Message.t
+++ a/t/db_dependent/SIP/Message.t
@@ -21,7 +21,7 @@
# along with Koha; if not, see .
use Modern::Perl;
-use Test::More tests => 13;
+use Test::More tests => 14;
use Test::Exception;
use Test::MockObject;
use Test::MockModule;
@@ -230,6 +230,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;
--