View | Details | Raw Unified | Return to bug 20816
Collapse All | Expand All

(-)a/C4/SIP/ILS/Patron.pm (-7 / +40 lines)
Lines 15-21 use Carp; Link Here
15
use Sys::Syslog qw(syslog);
15
use Sys::Syslog qw(syslog);
16
use Data::Dumper;
16
use Data::Dumper;
17
17
18
use C4::SIP::Sip qw(add_field);
18
use C4::SIP::Sip qw(add_field maybe_add);
19
19
20
use C4::Debug;
20
use C4::Debug;
21
use C4::Context;
21
use C4::Context;
Lines 207-213 sub AUTOLOAD { Link Here
207
    }
207
    }
208
}
208
}
209
209
210
sub name {
210
=head2 format
211
212
This method uses a template to build a string from a Koha::Patron object
213
If errors are encountered in processing template we log them and return nothing
214
215
=cut
216
217
sub format {
211
    my ( $self, $template ) = @_;
218
    my ( $self, $template ) = @_;
212
219
213
    if ($template) {
220
    if ($template) {
Lines 219-230 sub name { Link Here
219
        my $patron = Koha::Patrons->find( $self->{borrowernumber} );
226
        my $patron = Koha::Patrons->find( $self->{borrowernumber} );
220
227
221
        my $output;
228
        my $output;
222
        $tt->process( \$template, { patron => $patron }, \$output );
229
        eval {
230
            $tt->process( \$template, { patron => $patron }, \$output );
231
        };
232
        if ( $@ ){
233
            syslog("LOG_DEBUG", "Error processing template: $template");
234
            return "";
235
        }
223
        return $output;
236
        return $output;
224
    }
237
    }
225
    else {
226
        return $self->{name};
227
    }
228
}
238
}
229
239
230
sub check_password {
240
sub check_password {
Lines 519-525 sub build_patron_attributes_string { Link Here
519
    my ( $self, $server ) = @_;
529
    my ( $self, $server ) = @_;
520
530
521
    my $string = q{};
531
    my $string = q{};
522
523
    if ( $server->{account}->{patron_attribute} ) {
532
    if ( $server->{account}->{patron_attribute} ) {
524
        my @attributes_to_send =
533
        my @attributes_to_send =
525
          ref $server->{account}->{patron_attribute} eq "ARRAY"
534
          ref $server->{account}->{patron_attribute} eq "ARRAY"
Lines 544-549 sub build_patron_attributes_string { Link Here
544
    return $string;
553
    return $string;
545
}
554
}
546
555
556
557
=head2 build_custom_field_string
558
559
This method builds the part of the sip message for custom patron fields as defined in the sip config
560
561
=cut
562
563
sub build_custom_field_string {
564
    my ( $self, $server ) = @_;
565
566
    my $string = q{};
567
568
    if ( $server->{account}->{custom_patron_field} ) {
569
        my @custom_fields =
570
            ref $server->{account}->{custom_patron_field} eq "ARRAY"
571
            ? @{ $server->{account}->{custom_patron_field} }
572
            : $server->{account}->{custom_patron_field};
573
        foreach my $custom_field ( @custom_fields ) {
574
            $string .= maybe_add( $custom_field->{field}, $self->format( $custom_field->{template} ) ) if defined $custom_field->{field};
575
        }
576
    }
577
    return $string;
578
}
579
547
1;
580
1;
548
__END__
581
__END__
549
582
(-)a/C4/SIP/Sip.pm (-1 lines)
Lines 96-102 sub maybe_add { Link Here
96
            $value =~ s/$regex->{find}/$regex->{replace}/g;
96
            $value =~ s/$regex->{find}/$regex->{replace}/g;
97
        }
97
        }
98
    }
98
    }
99
100
    return (defined($value) && $value) ? add_field($fid, $value) : '';
99
    return (defined($value) && $value) ? add_field($fid, $value) : '';
101
}
100
}
102
101
(-)a/C4/SIP/Sip/MsgType.pm (-3 / +14 lines)
Lines 422-428 sub build_patron_status { Link Here
422
422
423
        $resp .= patron_status_string($patron);
423
        $resp .= patron_status_string($patron);
424
        $resp .= $lang . timestamp();
424
        $resp .= $lang . timestamp();
425
        $resp .= add_field( FID_PERSONAL_NAME, $patron->name( $server->{account}->{ae_field_template} ) );
425
        if ( defined $server->{account}->{ae_field_template} ) {
426
            $resp .= add_field( FID_PERSONAL_NAME, $patron->format( $server->{account}->{ae_field_template} ) );
427
        } else {
428
            $resp .= add_field( FID_PERSONAL_NAME, $patron->name );
429
        }
430
426
431
427
        # while the patron ID we got from the SC is valid, let's
432
        # while the patron ID we got from the SC is valid, let's
428
        # use the one returned from the ILS, just in case...
433
        # use the one returned from the ILS, just in case...
Lines 445-450 sub build_patron_status { Link Here
445
          if ( $server->{account}->{send_patron_home_library_in_af} );
450
          if ( $server->{account}->{send_patron_home_library_in_af} );
446
        $resp .= maybe_add( FID_PRINT_LINE, $patron->print_line );
451
        $resp .= maybe_add( FID_PRINT_LINE, $patron->print_line );
447
452
453
        $resp .= $patron->build_custom_field_string( $server );
448
        $resp .= $patron->build_patron_attributes_string( $server );
454
        $resp .= $patron->build_patron_attributes_string( $server );
449
455
450
    } else {
456
    } else {
Lines 957-963 sub handle_patron_info { Link Here
957
        # while the patron ID we got from the SC is valid, let's
963
        # while the patron ID we got from the SC is valid, let's
958
        # use the one returned from the ILS, just in case...
964
        # use the one returned from the ILS, just in case...
959
        $resp .= add_field( FID_PATRON_ID,     $patron->id );
965
        $resp .= add_field( FID_PATRON_ID,     $patron->id );
960
        $resp .= add_field( FID_PERSONAL_NAME, $patron->name( $server->{account}->{ae_field_template} ) );
966
        if ( defined $server->{account}->{ae_field_template} ) {
967
            $resp .= add_field( FID_PERSONAL_NAME, $patron->format( $server->{account}->{ae_field_template} ) );
968
        } else {
969
            $resp .= add_field( FID_PERSONAL_NAME, $patron->name );
970
        }
961
971
962
        # TODO: add code for the fields
972
        # TODO: add code for the fields
963
        #   hold items limit
973
        #   hold items limit
Lines 1012-1017 sub handle_patron_info { Link Here
1012
        }
1022
        }
1013
        $resp .= maybe_add( FID_PRINT_LINE, $patron->print_line );
1023
        $resp .= maybe_add( FID_PRINT_LINE, $patron->print_line );
1014
1024
1025
        $resp .= $patron->build_custom_field_string( $server );
1015
        $resp .= $patron->build_patron_attributes_string( $server );
1026
        $resp .= $patron->build_patron_attributes_string( $server );
1016
    } else {
1027
    } else {
1017
1028
Lines 1260-1266 sub handle_patron_enable { Link Here
1260
        $resp .= $patron->language . timestamp();
1271
        $resp .= $patron->language . timestamp();
1261
1272
1262
        $resp .= add_field( FID_PATRON_ID,     $patron->id );
1273
        $resp .= add_field( FID_PATRON_ID,     $patron->id );
1263
        $resp .= add_field( FID_PERSONAL_NAME, $patron->name( $server->{account}->{ae_field_template} ) );
1274
        $resp .= add_field( FID_PERSONAL_NAME, $patron->format( $server->{account}->{ae_field_template} ) );
1264
        if ( defined($patron_pwd) ) {
1275
        if ( defined($patron_pwd) ) {
1265
            $resp .= add_field( FID_VALID_PATRON_PWD, sipbool( $patron->check_password($patron_pwd) ) );
1276
            $resp .= add_field( FID_VALID_PATRON_PWD, sipbool( $patron->check_password($patron_pwd) ) );
1266
        }
1277
        }
(-)a/etc/SIPconfig.xml (+1 lines)
Lines 60-65 Link Here
60
          <screen_msg_regex find="Greetings from Koha." replace="Welcome to your library!" />
60
          <screen_msg_regex find="Greetings from Koha." replace="Welcome to your library!" />
61
          <screen_msg_regex find="Invalid patron barcode." replace="Barcode not found, are you sure this is your library card?" />
61
          <screen_msg_regex find="Invalid patron barcode." replace="Barcode not found, are you sure this is your library card?" />
62
          <patron_attribute field="XY" code="CODE" />
62
          <patron_attribute field="XY" code="CODE" />
63
          <custom_patron_field field="DE" code="[% patron.dateexpiry %]" />
63
      </login>
64
      </login>
64
  </accounts>
65
  </accounts>
65
66
(-)a/t/db_dependent/SIP/Patron.t (-2 / +45 lines)
Lines 4-10 Link Here
4
# This needs to be extended! Your help is appreciated..
4
# This needs to be extended! Your help is appreciated..
5
5
6
use Modern::Perl;
6
use Modern::Perl;
7
use Test::More tests => 5;
7
use Test::More tests => 6;
8
8
9
use Koha::Database;
9
use Koha::Database;
10
use Koha::Patrons;
10
use Koha::Patrons;
Lines 116-121 subtest "Test build_patron_attribute_string" => sub { Link Here
116
    is( $attribute_string, "XYTest Attribute|YZAnother Test Attribute|", 'Attribute field generated correctly with multiple params' );
116
    is( $attribute_string, "XYTest Attribute|YZAnother Test Attribute|", 'Attribute field generated correctly with multiple params' );
117
};
117
};
118
118
119
subtest "Test build_custom_field_string" => sub {
120
121
    plan tests => 5;
122
123
    my $patron = $builder->build_object( { class => 'Koha::Patrons',value=>{surname => "Duck", firstname => "Darkwing"} } );
124
125
126
    my $ils_patron = C4::SIP::ILS::Patron->new( $patron->cardnumber );
127
128
    my $server = {};
129
    $server->{account}->{custom_patron_field}->{field} = "DW";
130
    my $attribute_string = $ils_patron->build_custom_field_string( $server );
131
    is( $attribute_string, "", 'Custom field not generated if no value passed' );
132
133
    $server = {};
134
    $server->{account}->{custom_patron_field}->{template} = "[% patron.surname %]";
135
    $attribute_string = $ils_patron->build_custom_field_string( $server );
136
    is( $attribute_string, "", 'Custom field not generated if no field passed' );
137
138
139
    $server = {};
140
    $server->{account}->{custom_patron_field}->{field} = "DW";
141
    $server->{account}->{custom_patron_field}->{template} = "[% patron.firstname %] [% patron.surname %], let's get dangerous!";
142
    $attribute_string = $ils_patron->build_custom_field_string( $server );
143
    is( $attribute_string, "DWDarkwing Duck, let's get dangerous!|", 'Custom field processed correctly' );
144
145
    $server = {};
146
    $server->{account}->{custom_patron_field}->[0]->{field} = "DW";
147
    $server->{account}->{custom_patron_field}->[0]->{template} = "[% patron.firstname %] [% patron.surname %], let's get dangerous!";
148
    $server->{account}->{custom_patron_field}->[1]->{field} = "LM";
149
    $server->{account}->{custom_patron_field}->[1]->{template} = "Launchpad McQuack crashed on [% patron.dateexpiry %]";
150
    $attribute_string = $ils_patron->build_custom_field_string( $server );
151
    is( $attribute_string, "DWDarkwing Duck, let's get dangerous!|LMLaunchpad McQuack crashed on ".$patron->dateexpiry."|", 'Custom fields processed correctly when multiple exist' );
152
153
    $server = {};
154
    $server->{account}->{custom_patron_field}->[0]->{field} = "DW";
155
    $server->{account}->{custom_patron_field}->[0]->{template} = "[% IF (patron.firstname) %] patron.surname, let's get dangerous!";
156
    $server->{account}->{custom_patron_field}->[1]->{field} = "LM";
157
    $server->{account}->{custom_patron_field}->[1]->{template} = "Launchpad McQuack crashed on [% patron.dateexpiry %]";
158
    $attribute_string = $ils_patron->build_custom_field_string( $server );
159
    is( $attribute_string, "LMLaunchpad McQuack crashed on ".$patron->dateexpiry."|", 'Custom fields processed correctly, bad template generate no text' );
160
161
};
162
119
subtest "update_lastseen tests" => sub {
163
subtest "update_lastseen tests" => sub {
120
    plan tests => 2;
164
    plan tests => 2;
121
165
122
- 

Return to bug 20816