From fa66a9604fe37a742ec1dfabbaadd32accd064b4 Mon Sep 17 00:00:00 2001 From: Nick Clemens Date: Wed, 19 Jun 2019 17:59:14 +0000 Subject: [PATCH] Bug 20816: Add ability to define custom templated fields in SIP patron responses To test: 1 - You will need to enable SIP on your testing instance cp etc/SIPconfig.xml /etc/koha/sites/kohadev/ sudo koha-start-sip add a user listed in the SIPconfig to your system and give them permissions (superlibrarian works) 2 - If you copied the above file you should be set to get custom field DE with dateexpiry 3 - send a status test using the sip cli tester: perl misc/sip_cli_emulator.pl -a 127.0.0.1 -p 6001 -su lpl-sc-beacock -sp xyzzy -l LPL --patron nick -m patron_status_request 4 - send an information test using the sip cli tester: perl misc/sip_cli_emulator.pl -a 127.0.0.1 -p 6001 -su lpl-sc-beacock -sp xyzzy -l LPL --patron nick -m patron_information 5 - confirm you receive the DE field with a dateexpiry 6 - Add your own custom fields and confirm it works with several 7 - prove -v t/db_dependent/SIP/Patron.t 8 - prove -v t/db_dependent/SIP/ --- C4/SIP/ILS/Patron.pm | 47 ++++++++++++++++++++++++++++++++++++++------- C4/SIP/Sip.pm | 1 - C4/SIP/Sip/MsgType.pm | 17 +++++++++++++--- etc/SIPconfig.xml | 1 + t/db_dependent/SIP/Patron.t | 46 +++++++++++++++++++++++++++++++++++++++++++- 5 files changed, 100 insertions(+), 12 deletions(-) diff --git a/C4/SIP/ILS/Patron.pm b/C4/SIP/ILS/Patron.pm index 34866508e3..dbdaf0ad35 100644 --- a/C4/SIP/ILS/Patron.pm +++ b/C4/SIP/ILS/Patron.pm @@ -15,7 +15,7 @@ use Carp; use Sys::Syslog qw(syslog); use Data::Dumper; -use C4::SIP::Sip qw(add_field); +use C4::SIP::Sip qw(add_field maybe_add); use C4::Debug; use C4::Context; @@ -207,7 +207,14 @@ sub AUTOLOAD { } } -sub name { +=head2 format + +This method uses a template to build a string from a Koha::Patron object +If errors are encountered in processing template we log them and return nothing + +=cut + +sub format { my ( $self, $template ) = @_; if ($template) { @@ -219,12 +226,15 @@ sub name { my $patron = Koha::Patrons->find( $self->{borrowernumber} ); my $output; - $tt->process( \$template, { patron => $patron }, \$output ); + eval { + $tt->process( \$template, { patron => $patron }, \$output ); + }; + if ( $@ ){ + syslog("LOG_DEBUG", "Error processing template: $template"); + return ""; + } return $output; } - else { - return $self->{name}; - } } sub check_password { @@ -519,7 +529,6 @@ sub build_patron_attributes_string { my ( $self, $server ) = @_; my $string = q{}; - if ( $server->{account}->{patron_attribute} ) { my @attributes_to_send = ref $server->{account}->{patron_attribute} eq "ARRAY" @@ -544,6 +553,30 @@ sub build_patron_attributes_string { return $string; } + +=head2 build_custom_field_string + +This method builds the part of the sip message for custom patron fields as defined in the sip config + +=cut + +sub build_custom_field_string { + my ( $self, $server ) = @_; + + my $string = q{}; + + if ( $server->{account}->{custom_patron_field} ) { + my @custom_fields = + ref $server->{account}->{custom_patron_field} eq "ARRAY" + ? @{ $server->{account}->{custom_patron_field} } + : $server->{account}->{custom_patron_field}; + foreach my $custom_field ( @custom_fields ) { + $string .= maybe_add( $custom_field->{field}, $self->format( $custom_field->{template} ) ) if defined $custom_field->{field}; + } + } + return $string; +} + 1; __END__ diff --git a/C4/SIP/Sip.pm b/C4/SIP/Sip.pm index 3f41226352..07153c40b0 100644 --- a/C4/SIP/Sip.pm +++ b/C4/SIP/Sip.pm @@ -96,7 +96,6 @@ sub maybe_add { $value =~ s/$regex->{find}/$regex->{replace}/g; } } - return (defined($value) && $value) ? add_field($fid, $value) : ''; } diff --git a/C4/SIP/Sip/MsgType.pm b/C4/SIP/Sip/MsgType.pm index 6c2e4fc98c..ffe6c92404 100644 --- a/C4/SIP/Sip/MsgType.pm +++ b/C4/SIP/Sip/MsgType.pm @@ -422,7 +422,12 @@ sub build_patron_status { $resp .= patron_status_string($patron); $resp .= $lang . timestamp(); - $resp .= add_field( FID_PERSONAL_NAME, $patron->name( $server->{account}->{ae_field_template} ) ); + if ( defined $server->{account}->{ae_field_template} ) { + $resp .= add_field( FID_PERSONAL_NAME, $patron->format( $server->{account}->{ae_field_template} ) ); + } else { + $resp .= add_field( FID_PERSONAL_NAME, $patron->name ); + } + # while the patron ID we got from the SC is valid, let's # use the one returned from the ILS, just in case... @@ -445,6 +450,7 @@ sub build_patron_status { if ( $server->{account}->{send_patron_home_library_in_af} ); $resp .= maybe_add( FID_PRINT_LINE, $patron->print_line ); + $resp .= $patron->build_custom_field_string( $server ); $resp .= $patron->build_patron_attributes_string( $server ); } else { @@ -957,7 +963,11 @@ sub handle_patron_info { # while the patron ID we got from the SC is valid, let's # use the one returned from the ILS, just in case... $resp .= add_field( FID_PATRON_ID, $patron->id ); - $resp .= add_field( FID_PERSONAL_NAME, $patron->name( $server->{account}->{ae_field_template} ) ); + if ( defined $server->{account}->{ae_field_template} ) { + $resp .= add_field( FID_PERSONAL_NAME, $patron->format( $server->{account}->{ae_field_template} ) ); + } else { + $resp .= add_field( FID_PERSONAL_NAME, $patron->name ); + } # TODO: add code for the fields # hold items limit @@ -1012,6 +1022,7 @@ sub handle_patron_info { } $resp .= maybe_add( FID_PRINT_LINE, $patron->print_line ); + $resp .= $patron->build_custom_field_string( $server ); $resp .= $patron->build_patron_attributes_string( $server ); } else { @@ -1260,7 +1271,7 @@ sub handle_patron_enable { $resp .= $patron->language . timestamp(); $resp .= add_field( FID_PATRON_ID, $patron->id ); - $resp .= add_field( FID_PERSONAL_NAME, $patron->name( $server->{account}->{ae_field_template} ) ); + $resp .= add_field( FID_PERSONAL_NAME, $patron->format( $server->{account}->{ae_field_template} ) ); if ( defined($patron_pwd) ) { $resp .= add_field( FID_VALID_PATRON_PWD, sipbool( $patron->check_password($patron_pwd) ) ); } diff --git a/etc/SIPconfig.xml b/etc/SIPconfig.xml index 1e17ef0338..c36f953ca4 100644 --- a/etc/SIPconfig.xml +++ b/etc/SIPconfig.xml @@ -60,6 +60,7 @@ + diff --git a/t/db_dependent/SIP/Patron.t b/t/db_dependent/SIP/Patron.t index 0f20ac3e5c..d517c7693b 100755 --- a/t/db_dependent/SIP/Patron.t +++ b/t/db_dependent/SIP/Patron.t @@ -4,7 +4,7 @@ # This needs to be extended! Your help is appreciated.. use Modern::Perl; -use Test::More tests => 5; +use Test::More tests => 6; use Koha::Database; use Koha::Patrons; @@ -116,6 +116,50 @@ subtest "Test build_patron_attribute_string" => sub { is( $attribute_string, "XYTest Attribute|YZAnother Test Attribute|", 'Attribute field generated correctly with multiple params' ); }; +subtest "Test build_custom_field_string" => sub { + + plan tests => 5; + + my $patron = $builder->build_object( { class => 'Koha::Patrons',value=>{surname => "Duck", firstname => "Darkwing"} } ); + + + my $ils_patron = C4::SIP::ILS::Patron->new( $patron->cardnumber ); + + my $server = {}; + $server->{account}->{custom_patron_field}->{field} = "DW"; + my $attribute_string = $ils_patron->build_custom_field_string( $server ); + is( $attribute_string, "", 'Custom field not generated if no value passed' ); + + $server = {}; + $server->{account}->{custom_patron_field}->{template} = "[% patron.surname %]"; + $attribute_string = $ils_patron->build_custom_field_string( $server ); + is( $attribute_string, "", 'Custom field not generated if no field passed' ); + + + $server = {}; + $server->{account}->{custom_patron_field}->{field} = "DW"; + $server->{account}->{custom_patron_field}->{template} = "[% patron.firstname %] [% patron.surname %], let's get dangerous!"; + $attribute_string = $ils_patron->build_custom_field_string( $server ); + is( $attribute_string, "DWDarkwing Duck, let's get dangerous!|", 'Custom field processed correctly' ); + + $server = {}; + $server->{account}->{custom_patron_field}->[0]->{field} = "DW"; + $server->{account}->{custom_patron_field}->[0]->{template} = "[% patron.firstname %] [% patron.surname %], let's get dangerous!"; + $server->{account}->{custom_patron_field}->[1]->{field} = "LM"; + $server->{account}->{custom_patron_field}->[1]->{template} = "Launchpad McQuack crashed on [% patron.dateexpiry %]"; + $attribute_string = $ils_patron->build_custom_field_string( $server ); + is( $attribute_string, "DWDarkwing Duck, let's get dangerous!|LMLaunchpad McQuack crashed on ".$patron->dateexpiry."|", 'Custom fields processed correctly when multiple exist' ); + + $server = {}; + $server->{account}->{custom_patron_field}->[0]->{field} = "DW"; + $server->{account}->{custom_patron_field}->[0]->{template} = "[% IF (patron.firstname) %] patron.surname, let's get dangerous!"; + $server->{account}->{custom_patron_field}->[1]->{field} = "LM"; + $server->{account}->{custom_patron_field}->[1]->{template} = "Launchpad McQuack crashed on [% patron.dateexpiry %]"; + $attribute_string = $ils_patron->build_custom_field_string( $server ); + is( $attribute_string, "LMLaunchpad McQuack crashed on ".$patron->dateexpiry."|", 'Custom fields processed correctly, bad template generate no text' ); + +}; + subtest "update_lastseen tests" => sub { plan tests => 2; -- 2.11.0