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 216-222 sub AUTOLOAD { Link Here
216
    }
216
    }
217
}
217
}
218
218
219
sub name {
219
=head2 format
220
221
This method uses a template to build a string from a Koha::Patron object
222
If errors are encountered in processing template we log them and return nothing
223
224
=cut
225
226
sub format {
220
    my ( $self, $template ) = @_;
227
    my ( $self, $template ) = @_;
221
228
222
    if ($template) {
229
    if ($template) {
Lines 228-239 sub name { Link Here
228
        my $patron = Koha::Patrons->find( $self->{borrowernumber} );
235
        my $patron = Koha::Patrons->find( $self->{borrowernumber} );
229
236
230
        my $output;
237
        my $output;
231
        $tt->process( \$template, { patron => $patron }, \$output );
238
        eval {
239
            $tt->process( \$template, { patron => $patron }, \$output );
240
        };
241
        if ( $@ ){
242
            syslog("LOG_DEBUG", "Error processing template: $template");
243
            return "";
244
        }
232
        return $output;
245
        return $output;
233
    }
246
    }
234
    else {
235
        return $self->{name};
236
    }
237
}
247
}
238
248
239
sub check_password {
249
sub check_password {
Lines 528-534 sub build_patron_attributes_string { Link Here
528
    my ( $self, $server ) = @_;
538
    my ( $self, $server ) = @_;
529
539
530
    my $string = q{};
540
    my $string = q{};
531
532
    if ( $server->{account}->{patron_attribute} ) {
541
    if ( $server->{account}->{patron_attribute} ) {
533
        my @attributes_to_send =
542
        my @attributes_to_send =
534
          ref $server->{account}->{patron_attribute} eq "ARRAY"
543
          ref $server->{account}->{patron_attribute} eq "ARRAY"
Lines 553-558 sub build_patron_attributes_string { Link Here
553
    return $string;
562
    return $string;
554
}
563
}
555
564
565
566
=head2 build_custom_field_string
567
568
This method builds the part of the sip message for custom patron fields as defined in the sip config
569
570
=cut
571
572
sub build_custom_field_string {
573
    my ( $self, $server ) = @_;
574
575
    my $string = q{};
576
577
    if ( $server->{account}->{custom_patron_field} ) {
578
        my @custom_fields =
579
            ref $server->{account}->{custom_patron_field} eq "ARRAY"
580
            ? @{ $server->{account}->{custom_patron_field} }
581
            : $server->{account}->{custom_patron_field};
582
        foreach my $custom_field ( @custom_fields ) {
583
            $string .= maybe_add( $custom_field->{field}, $self->format( $custom_field->{template} ) ) if defined $custom_field->{field};
584
        }
585
    }
586
    return $string;
587
}
588
556
1;
589
1;
557
__END__
590
__END__
558
591
(-)a/C4/SIP/Sip.pm (-1 lines)
Lines 108-114 sub maybe_add { Link Here
108
            $value =~ s/$regex->{find}/$regex->{replace}/g;
108
            $value =~ s/$regex->{find}/$regex->{replace}/g;
109
        }
109
        }
110
    }
110
    }
111
112
    return (defined($value) && $value) ? add_field($fid, $value) : '';
111
    return (defined($value) && $value) ? add_field($fid, $value) : '';
113
}
112
}
114
113
(-)a/C4/SIP/Sip/MsgType.pm (-3 / +14 lines)
Lines 436-442 sub build_patron_status { Link Here
436
436
437
        $resp .= patron_status_string($patron);
437
        $resp .= patron_status_string($patron);
438
        $resp .= $lang . timestamp();
438
        $resp .= $lang . timestamp();
439
        $resp .= add_field( FID_PERSONAL_NAME, $patron->name( $server->{account}->{ae_field_template} ), $server );
439
        if ( defined $server->{account}->{ae_field_template} ) {
440
            $resp .= add_field( FID_PERSONAL_NAME, $patron->format( $server->{account}->{ae_field_template}, $server ) );
441
        } else {
442
            $resp .= add_field( FID_PERSONAL_NAME, $patron->name, $server );
443
        }
444
440
445
441
        # while the patron ID we got from the SC is valid, let's
446
        # while the patron ID we got from the SC is valid, let's
442
        # use the one returned from the ILS, just in case...
447
        # use the one returned from the ILS, just in case...
Lines 459-464 sub build_patron_status { Link Here
459
          if ( $server->{account}->{send_patron_home_library_in_af} );
464
          if ( $server->{account}->{send_patron_home_library_in_af} );
460
        $resp .= maybe_add( FID_PRINT_LINE, $patron->print_line, $server );
465
        $resp .= maybe_add( FID_PRINT_LINE, $patron->print_line, $server );
461
466
467
        $resp .= $patron->build_custom_field_string( $server );
462
        $resp .= $patron->build_patron_attributes_string( $server );
468
        $resp .= $patron->build_patron_attributes_string( $server );
463
469
464
    } else {
470
    } else {
Lines 972-978 sub handle_patron_info { Link Here
972
        # while the patron ID we got from the SC is valid, let's
978
        # while the patron ID we got from the SC is valid, let's
973
        # use the one returned from the ILS, just in case...
979
        # use the one returned from the ILS, just in case...
974
        $resp .= add_field( FID_PATRON_ID,     $patron->id, $server );
980
        $resp .= add_field( FID_PATRON_ID,     $patron->id, $server );
975
        $resp .= add_field( FID_PERSONAL_NAME, $patron->name( $server->{account}->{ae_field_template} ), $server );
981
        if ( defined $server->{account}->{ae_field_template} ) {
982
            $resp .= add_field( FID_PERSONAL_NAME, $patron->format( $server->{account}->{ae_field_template} ), $server );
983
        } else {
984
            $resp .= add_field( FID_PERSONAL_NAME, $patron->name, $server );
985
        }
976
986
977
        # TODO: add code for the fields
987
        # TODO: add code for the fields
978
        #   hold items limit
988
        #   hold items limit
Lines 1027-1032 sub handle_patron_info { Link Here
1027
        }
1037
        }
1028
        $resp .= maybe_add( FID_PRINT_LINE, $patron->print_line, $server );
1038
        $resp .= maybe_add( FID_PRINT_LINE, $patron->print_line, $server );
1029
1039
1040
        $resp .= $patron->build_custom_field_string( $server );
1030
        $resp .= $patron->build_patron_attributes_string( $server );
1041
        $resp .= $patron->build_patron_attributes_string( $server );
1031
    } else {
1042
    } else {
1032
1043
Lines 1315-1321 sub handle_patron_enable { Link Here
1315
        $resp .= $patron->language . timestamp();
1326
        $resp .= $patron->language . timestamp();
1316
1327
1317
        $resp .= add_field( FID_PATRON_ID,     $patron->id, $server );
1328
        $resp .= add_field( FID_PATRON_ID,     $patron->id, $server );
1318
        $resp .= add_field( FID_PERSONAL_NAME, $patron->name( $server->{account}->{ae_field_template} ), $server );
1329
        $resp .= add_field( FID_PERSONAL_NAME, $patron->format( $server->{account}->{ae_field_template} ), $server );
1319
        if ( defined($patron_pwd) ) {
1330
        if ( defined($patron_pwd) ) {
1320
            $resp .= add_field( FID_VALID_PATRON_PWD, sipbool( $patron->check_password($patron_pwd) ), $server );
1331
            $resp .= add_field( FID_VALID_PATRON_PWD, sipbool( $patron->check_password($patron_pwd) ), $server );
1321
        }
1332
        }
(-)a/etc/SIPconfig.xml (+1 lines)
Lines 66-71 Link Here
66
          <syspref_overrides>
66
          <syspref_overrides>
67
              <AllFinesNeedOverride>0</AllFinesNeedOverride>
67
              <AllFinesNeedOverride>0</AllFinesNeedOverride>
68
          </syspref_overrides>
68
          </syspref_overrides>
69
          <custom_patron_field field="DE" template="[% patron.dateexpiry %]" />
69
      </login>
70
      </login>
70
  </accounts>
71
  </accounts>
71
72
(-)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