]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/lib/OpenILS/SIP/Patron.pm
Use hold current shelf lib to determine availability : SIP
[Evergreen.git] / Open-ILS / src / perlmods / lib / OpenILS / SIP / Patron.pm
1 #
2
3 # A Class for hiding the ILS's concept of the patron from the OpenSIP
4 # system
5 #
6
7 package OpenILS::SIP::Patron;
8
9 use strict;
10 use warnings;
11 use Exporter;
12
13 use Sys::Syslog qw(syslog);
14 use Data::Dumper;
15 use Digest::MD5 qw(md5_hex);
16
17 use OpenILS::SIP;
18 use OpenILS::Application::AppUtils;
19 use OpenILS::Application::Actor;
20 use OpenSRF::Utils qw/:datetime/;
21 use DateTime::Format::ISO8601;
22 my $U = 'OpenILS::Application::AppUtils';
23
24 our (@ISA, @EXPORT_OK);
25
26 @ISA = qw(Exporter);
27
28 @EXPORT_OK = qw(invalid_patron);
29
30 my $INET_PRIVS;
31
32 #
33 # OpenILS::SIP::Patron->new($barcode);
34 # OpenILS::SIP::Patron->new(barcode => $barcode);   # same as above
35 # OpenILS::SIP::Patron->new(    usr => $id);       
36
37 sub new {
38     my $class = shift;
39     my $key   = (@_ > 1) ? shift : 'barcode';  # if we have multiple args, the first is the key index (default barcode)
40     my $patron_id = shift;
41     my %args = @_;
42
43     if ($key ne 'usr' and $key ne 'barcode') {
44         syslog("LOG_ERROR", "Patron (card) lookup requested by illegeal key '$key'");
45         return undef;
46     }
47
48     unless(defined $patron_id) {
49         syslog("LOG_WARNING", "No patron ID provided to ILS::Patron->new");
50         return undef;
51     }
52
53     my $type = ref($class) || $class;
54     my $self = bless({}, $type);
55
56     syslog("LOG_DEBUG", "OILS: new OpenILS Patron(%s => %s): searching...", $key, $patron_id);
57
58     my $e = OpenILS::SIP->editor();
59
60     my $usr_flesh = {
61         flesh => 2,
62         flesh_fields => {
63             au => [
64                 "card",
65                 "addresses",
66                 "billing_address",
67                 "mailing_address",
68                 'profile',
69                 "stat_cat_entries",
70             ],
71             actscecm => [
72                 "stat_cat",
73             ],
74         }
75     };
76
77     # in some cases, we don't need all of this data.  Only fetch the user + barcode
78     $usr_flesh = {flesh => 1, flesh_fields => {au => ['card']}} if $args{slim_user};
79     
80     my $user;
81     if($key eq 'barcode') { # retrieve user by barcode
82
83         $$usr_flesh{flesh} += 1;
84         $$usr_flesh{flesh_fields}{ac} = ['usr'];
85
86         my $card = $e->search_actor_card([{barcode => $patron_id}, $usr_flesh])->[0];
87
88         if(!$card or !$U->is_true($card->active)) {
89             syslog("LOG_WARNING", "No such patron barcode: $patron_id");
90             return undef;
91         }
92
93         $user = $card->usr;
94
95     } else {
96             $user = $e->retrieve_actor_user([$patron_id, $usr_flesh]);
97     }
98
99     if(!$user or $U->is_true($user->deleted)) {
100         syslog("LOG_WARNING", "OILS: Unable to find patron %s => %s", $key, $patron_id);
101         return undef;
102     }
103
104     if(!$U->is_true($user->active)) {
105         syslog("LOG_WARNING", "OILS: Patron is inactive %s => %s", $key, $patron_id);
106         return undef;
107     }
108
109     # now grab the user's penalties
110
111     $self->flesh_user_penalties($user, $e) unless $args{slim_user};
112
113     $self->{editor} = $e;
114     $self->{user}   = $user;
115     $self->{id}     = ($key eq 'barcode') ? $patron_id : $user->card->barcode;   # The barcode IS the ID to SIP.  
116     # We give back the passed barcode if the key was indeed a barcode, just to be safe.  Otherwise pull it from the card.
117
118     syslog("LOG_DEBUG", "OILS: new OpenILS Patron(%s => %s): found patron : barred=%s, card:active=%s", 
119         $key, $patron_id, $user->barred, $user->card->active );
120
121     return $self;
122 }
123
124 # grab patron penalties.  Only grab non-archived penalties that are for fines,
125 # excessive overdues, or otherwise block circluation activity
126 sub flesh_user_penalties {
127     my ($self, $user, $e) = @_;
128
129     $user->standing_penalties(
130         $e->search_actor_user_standing_penalty([
131             {   
132                 usr => $user->id,
133                 '-or' => [
134
135                     # ignore "archived" penalties
136                     {stop_date => undef},
137                     {stop_date => {'>' => 'now'}}
138                 ],
139
140                 org_unit => {
141                     in  => {
142                         select => {
143                             aou => [{
144                                 column => 'id', 
145                                 transform => 'actor.org_unit_ancestors', 
146                                 result_field => 'id'
147                             }]
148                         },
149                         from => 'aou',
150
151                         # at this point, there is no concept of "here", so fetch penalties 
152                         # for the patron's home lib plus ancestors
153                         where => {id => $user->home_ou}, 
154                         distinct => 1
155                     }
156                 },
157
158                 # in addition to fines and excessive overdue penalties, 
159                 # we only care about penalties that result in blocks
160                 standing_penalty => {
161                     in => {
162                         select => {csp => ['id']},
163                         from => 'csp',
164                         where => {
165                             '-or' => [
166                                 {id => [1,2]}, # fines / overdues
167                                 {block_list => {'!=' => undef}}
168                             ]
169                         },
170                     }
171                 }
172             },
173         ])
174     );
175 }
176
177 sub id {
178     my $self = shift;
179     return $self->{id};
180 }
181
182 sub name {
183     my $self = shift;
184     return format_name($self->{user});
185 }
186
187 sub format_name {
188     my $u = shift;
189     return OpenILS::SIP::clean_text(
190         sprintf('%s %s %s', 
191             ($u->first_given_name || ''),
192             ($u->second_given_name || ''),
193             ($u->family_name || '')));
194 }
195
196 sub home_library {
197     my $self = shift;
198     my $lib = OpenILS::SIP::shortname_from_id($self->{user}->home_ou);
199         syslog('LOG_DEBUG', "OILS: Patron->home_library() = $lib");
200     return $lib;
201 }
202
203 sub __addr_string {
204     my $addr = shift;
205     return "" unless $addr;
206     my $return = OpenILS::SIP::clean_text(
207         join( ' ', map {$_ || ''} (
208             $addr->street1,
209             $addr->street2,
210             $addr->city . ',',
211             $addr->county,
212             $addr->state,
213             $addr->country,
214             $addr->post_code
215             )
216         )
217     );
218     $return =~ s/\s+/ /sg;     # Compress any run of of whitespace to one space
219     return $return;
220 }
221
222 sub internal_id {
223     my $self = shift;
224     return $self->{user}->id;
225 }
226
227 sub address {
228         my $self = shift;
229         my $u    = $self->{user};
230         my $str  = __addr_string($u->billing_address || $u->mailing_address);
231         syslog('LOG_DEBUG', "OILS: Patron address: $str");
232         return $str;
233 }
234
235 sub email_addr {
236     my $self = shift;
237     return OpenILS::SIP::clean_text($self->{user}->email);
238 }
239
240 sub home_phone {
241     my $self = shift;
242     return $self->{user}->day_phone;
243 }
244
245 sub sip_birthdate {
246         my $self = shift;
247         my $dob = OpenILS::SIP->format_date($self->{user}->dob);
248         syslog('LOG_DEBUG', "OILS: Patron DOB = $dob");
249         return $dob;
250 }
251
252 sub sip_expire {
253     my $self = shift;
254     my $expire = OpenILS::SIP->format_date($self->{user}->expire_date);
255     syslog('LOG_DEBUG', "OILS: Patron Expire = $expire");
256     return $expire;
257 }
258
259 sub ptype {
260     my $self = shift;
261
262         my $use_code = OpenILS::SIP->get_option_value('patron_type_uses_code') || '';
263
264     # should we use the no_i18n version of patron profile name (as a 'code')?
265     return $self->{editor}->retrieve_permission_grp_tree(
266         [$self->{user}->profile->id, {no_i18n => 1}])->name
267         if $use_code =~ /true/io;
268
269     return OpenILS::SIP::clean_text($self->{user}->profile->name);
270 }
271
272 sub language {
273     my $self = shift;
274     return '000'; # Unspecified
275 }
276
277 # How much more detail do we need to check here?
278 # sec: adding logic to return false if user is barred, has a circulation block
279 # or an expired card
280 sub charge_ok {
281     my $self = shift;
282     my $u = $self->{user};
283
284     # compute expiration date for borrowing privileges
285     my $expire = DateTime::Format::ISO8601->new->parse_datetime(cleanse_ISO8601($u->expire_date));
286
287     # determine whether patron should be allowed to circulate materials:
288     # not barred, doesn't owe too much wrt fines/fees, privileges haven't
289     # expired
290     my $circ_is_blocked = 
291         (($u->barred eq 't') or
292          ($u->standing_penalties and @{$u->standing_penalties}) or
293          (CORE::time > $expire->epoch));
294
295     return
296         !$circ_is_blocked and
297         $u->active eq 't' and
298         $u->card->active eq 't';
299 }
300
301
302
303 # How much more detail do we need to check here?
304 sub renew_ok {
305     my $self = shift;
306     return $self->charge_ok;
307 }
308
309 sub recall_ok {
310     my $self = shift;
311     return 0;
312 }
313
314 sub hold_ok {
315     my $self = shift;
316     return $self->charge_ok;
317 }
318
319 # return true if the card provided is marked as lost
320 sub card_lost {
321     my $self = shift;
322     return $self->{user}->card->active eq 'f';
323 }
324
325 sub recall_overdue {        # not implemented
326     my $self = shift;
327     return 0;
328 }
329
330 sub check_password {
331         my ($self, $pwd) = @_;
332         syslog('LOG_DEBUG', 'OILS: Patron->check_password()');
333     return 0 unless (defined $pwd and $self->{user});
334         return md5_hex($pwd) eq $self->{user}->passwd;
335 }
336
337 sub currency {              # not really implemented
338         my $self = shift;
339         syslog('LOG_DEBUG', 'OILS: Patron->currency()');
340         return 'USD';
341 }
342
343 sub fee_amount {
344         my $self = shift;
345         syslog('LOG_DEBUG', 'OILS: Patron->fee_amount()');
346     my $user_id = $self->{user}->id;
347
348     my $e = $self->{editor};
349     $e->xact_begin;
350     my $summary = $e->retrieve_money_open_user_summary($user_id);
351     $e->rollback; # xact_rollback + disconnect
352
353     my $total = ($summary) ? $summary->balance_owed : 0;
354         syslog('LOG_INFO', "User ".$self->{id} .":$user_id has a fee amount of \$$total");
355         return $total;
356 }
357
358 sub screen_msg {
359         my $self = shift;
360         my $u = $self->{user};
361
362         return 'barred' if $u->barred eq 't';
363
364         my $b = 'blocked';
365
366         return $b if $u->active eq 'f';
367         return $b if $u->card->active eq 'f';
368
369     # if we have any penalties at this point, they are blocking penalties
370     return $b if $u->standing_penalties and @{$u->standing_penalties};
371
372     # has the patron account expired?
373         my $expire = DateTime::Format::ISO8601->new->parse_datetime(cleanse_ISO8601($u->expire_date));
374         return $b if CORE::time > $expire->epoch;
375
376         return 'OK';
377 }
378
379 sub print_line {            # not implemented
380     my $self = shift;
381         return '';
382 }
383
384 sub too_many_charged {      # not implemented
385     my $self = shift;
386         return 0;
387 }
388
389 sub too_many_overdue { 
390         my $self = shift;
391     return scalar( # PATRON_EXCEEDS_OVERDUE_COUNT
392         grep { $_->standing_penalty == 2 } @{$self->{user}->standing_penalties}
393     );
394 }
395
396 # not completely sure what this means
397 sub too_many_renewal {
398     my $self = shift;
399     return 0;
400 }
401
402 # not relevant, handled by fines/fees
403 sub too_many_claim_return {
404     my $self = shift;
405     return 0;
406 }
407
408 # not relevant, handled by fines/fees
409 sub too_many_lost {
410     my $self = shift;
411     return 0;
412 }
413
414 sub excessive_fines { 
415     my $self = shift;
416     return scalar( # PATRON_EXCEEDS_FINES
417         grep { $_->standing_penalty == 1 } @{$self->{user}->standing_penalties}
418     );
419 }
420
421 # Until someone suggests otherwise, fees and fines are the same
422
423 sub excessive_fees {
424         my $self = shift;
425     return $self->excessive_fines;
426 }
427
428 # not relevant, handled by fines/fees
429 sub too_many_billed {
430     my $self = shift;
431         return 0;
432 }
433
434
435
436 #
437 # List of outstanding holds placed
438 #
439 sub hold_items {
440     my ($self, $start, $end) = @_;
441         syslog('LOG_DEBUG', 'OILS: Patron->hold_items()');
442
443          my $holds = $self->{editor}->search_action_hold_request(
444                 { usr => $self->{user}->id, fulfillment_time => undef, cancel_time => undef }
445          );
446
447         my @holds;
448         push( @holds, OpenILS::SIP::clean_text($self->__hold_to_title($_)) ) for @$holds;
449
450         return (defined $start and defined $end) ? 
451                 [ $holds[($start-1)..($end-1)] ] : 
452                 \@holds;
453 }
454
455 sub __hold_to_title {
456         my $self = shift;
457         my $hold = shift;
458         my $e = $self->{editor};
459
460         my( $id, $mods, $title, $volume, $copy );
461
462         return __copy_to_title($e, 
463                 $e->retrieve_asset_copy($hold->target)) 
464                 if $hold->hold_type eq 'C';
465
466         return __volume_to_title($e, 
467                 $e->retrieve_asset_call_number($hold->target))
468                 if $hold->hold_type eq 'V';
469
470         return __record_to_title(
471                 $e, $hold->target) if $hold->hold_type eq 'T';
472
473         return __metarecord_to_title(
474                 $e, $hold->target) if $hold->hold_type eq 'M';
475 }
476
477 sub __copy_to_title {
478         my( $e, $copy ) = @_;
479         #syslog('LOG_DEBUG', "OILS: copy_to_title(%s)", $copy->id);
480         return $copy->dummy_title if $copy->call_number == -1;  
481
482         my $vol = (ref $copy->call_number) ?
483                 $copy->call_number :
484                 $e->retrieve_asset_call_number($copy->call_number);
485
486         return __volume_to_title($e, $vol);
487 }
488
489
490 sub __volume_to_title {
491         my( $e, $volume ) = @_;
492         #syslog('LOG_DEBUG', "OILS: volume_to_title(%s)", $volume->id);
493         return __record_to_title($e, $volume->record);
494 }
495
496
497 sub __record_to_title {
498         my( $e, $title_id ) = @_;
499         #syslog('LOG_DEBUG', "OILS: record_to_title($title_id)");
500         my $mods = $U->simplereq(
501                 'open-ils.search',
502                 'open-ils.search.biblio.record.mods_slim.retrieve', $title_id );
503         return ($mods) ? $mods->title : "";
504 }
505
506 sub __metarecord_to_title {
507         my( $e, $m_id ) = @_;
508         #syslog('LOG_DEBUG', "OILS: metarecord_to_title($m_id)");
509         my $mods = $U->simplereq(
510                 'open-ils.search',
511                 'open-ils.search.biblio.metarecord.mods_slim.retrieve', $m_id);
512         return ($U->event_code($mods)) ? "<unknown>" : $mods->title;
513 }
514
515
516 #
517 # remove the hold on item item_id from my hold queue.
518 # return true if I was holding the item, false otherwise.
519
520 sub drop_hold {
521     my ($self, $item_id) = @_;
522     return 0;
523 }
524
525 sub __patron_items_info {
526         my $self = shift;
527         return if $self->{item_info};
528         $self->{item_info} = 
529                 OpenILS::Application::Actor::_checked_out(
530                         0, $self->{editor}, $self->{user}->id);;
531 }
532
533
534
535 sub overdue_items {
536         my ($self, $start, $end) = @_;
537
538         $self->__patron_items_info();
539         my @overdues = @{$self->{item_info}->{overdue}};
540         #$overdues[$_] = __circ_to_title($self->{editor}, $overdues[$_]) for @overdues;
541
542         my @o;
543         syslog('LOG_DEBUG', "OILS: overdue_items() fleshing circs @overdues");
544
545         my $return_datatype = OpenILS::SIP->get_option_value('msg64_summary_datatype') || '';
546         
547         for my $circid (@overdues) {
548                 next unless $circid;
549                 if($return_datatype eq 'barcode') {
550                         push( @o, __circ_to_barcode($self->{editor}, $circid));
551                 } else {
552                         push( @o, OpenILS::SIP::clean_text(__circ_to_title($self->{editor}, $circid)));
553                 }
554         }
555         @overdues = @o;
556
557         return (defined $start and defined $end) ? 
558                 [ $overdues[($start-1)..($end-1)] ] : \@overdues;
559 }
560
561 sub __circ_to_barcode {
562         my ($e, $circ) = @_;
563         return unless $circ;
564         $circ = $e->retrieve_action_circulation($circ);
565         my $copy = $e->retrieve_asset_copy($circ->target_copy);
566         return $copy->barcode;
567 }
568
569 sub __circ_to_title {
570         my( $e, $circ ) = @_;
571         return unless $circ;
572         $circ = $e->retrieve_action_circulation($circ);
573         return __copy_to_title( $e, 
574                 $e->retrieve_asset_copy($circ->target_copy) );
575 }
576
577 sub charged_items {
578         my ($self, $start, $end) = shift;
579
580         $self->__patron_items_info();
581
582         my @charges = (
583                 @{$self->{item_info}->{out}},
584                 @{$self->{item_info}->{overdue}}
585                 );
586
587         #$charges[$_] = __circ_to_title($self->{editor}, $charges[$_]) for @charges;
588
589         my @c;
590         syslog('LOG_DEBUG', "OILS: charged_items() fleshing circs @charges");
591
592         my $return_datatype = OpenILS::SIP->get_option_value('msg64_summary_datatype') || '';
593
594         for my $circid (@charges) {
595                 next unless $circid;
596                 if($return_datatype eq 'barcode') {
597                         push( @c, __circ_to_barcode($self->{editor}, $circid));
598                 } else {
599                         push( @c, OpenILS::SIP::clean_text(__circ_to_title($self->{editor}, $circid)));
600                 }
601         }
602
603         @charges = @c;
604
605         return (defined $start and defined $end) ? 
606                 [ $charges[($start-1)..($end-1)] ] : 
607                 \@charges;
608 }
609
610 sub fine_items {
611         my ($self, $start, $end) = @_;
612         my @fines;
613         syslog('LOG_DEBUG', 'OILS: Patron->fine_items()');
614         return (defined $start and defined $end) ? 
615                 [ $fines[($start-1)..($end-1)] ] : \@fines;
616 }
617
618 # not currently supported
619 sub recall_items {
620     my ($self, $start, $end) = @_;
621     return [];
622 }
623
624 sub unavail_holds {
625      my ($self, $start, $end) = @_;
626      syslog('LOG_DEBUG', 'OILS: Patron->unavail_holds()');
627
628      my $ids = $self->{editor}->json_query({
629         select => {ahr => ['id']},
630         from => 'ahr',
631         where => {
632             usr => $self->{user}->id,
633             fulfillment_time => undef,
634             cancel_time => undef,
635             '-or' => [
636                 {current_shelf_lib => undef},
637                 {current_shelf_lib => {'!=' => {'+ahr' => 'pickup_lib'}}}
638             ]
639         }
640     });
641  
642      my @holds_sip_output = map {
643         OpenILS::SIP::clean_text($self->__hold_to_title($_))
644      } @{
645         $self->{editor}->search_action_hold_request(
646             {id => [map {$_->{id}} @$ids]}
647         )
648      };
649  
650      return (defined $start and defined $end) ?
651          [ @holds_sip_output[($start-1)..($end-1)] ] :
652          \@holds_sip_output;
653 }
654
655 sub block {
656         my ($self, $card_retained, $blocked_card_msg) = @_;
657     $blocked_card_msg ||= '';
658
659     my $e = $self->{editor};
660         my $u = $self->{user};
661
662         syslog('LOG_INFO', "OILS: Blocking user %s", $u->card->barcode );
663
664         return $self if $u->card->active eq 'f';
665
666     $e->xact_begin;    # connect and start a new transaction
667
668         $u->card->active('f');
669         if( ! $e->update_actor_card($u->card) ) {
670                 syslog('LOG_ERR', "OILS: Block card update failed: %s", $e->event->{textcode});
671                 $e->rollback; # rollback + disconnect
672                 return $self;
673         }
674
675         # retrieve the un-fleshed user object for update
676         $u = $e->retrieve_actor_user($u->id);
677         my $note = OpenILS::SIP::clean_text($u->alert_message) || "";
678         $note = "<sip> CARD BLOCKED BY SELF-CHECK MACHINE. $blocked_card_msg</sip>\n$note"; # XXX Config option
679     $note =~ s/\s*$//;  # kill trailng whitespace
680         $u->alert_message($note);
681
682         if( ! $e->update_actor_user($u) ) {
683                 syslog('LOG_ERR', "OILS: Block: patron alert update failed: %s", $e->event->{textcode});
684                 $e->rollback; # rollback + disconnect
685                 return $self;
686         }
687
688         # stay in synch
689         $self->{user}->alert_message( $note );
690
691         $e->commit; # commits and disconnects
692         return $self;
693 }
694
695 # Testing purposes only
696 sub enable {
697     my ($self, $card_retained) = @_;
698     $self->{screen_msg} = "All privileges restored.";
699
700     # Un-mark card as inactive, grep out the patron alert
701     my $e = $self->{editor};
702     my $u = $self->{user};
703
704     syslog('LOG_INFO', "OILS: Unblocking user %s", $u->card->barcode );
705
706     return $self if $u->card->active eq 't';
707
708     $e->xact_begin;    # connect and start a new transaction
709
710     $u->card->active('t');
711     if( ! $e->update_actor_card($u->card) ) {
712         syslog('LOG_ERR', "OILS: Unblock card update failed: %s", $e->event->{textcode});
713         $e->rollback; # rollback + disconnect
714         return $self;
715     }
716
717     # retrieve the un-fleshed user object for update
718     $u = $e->retrieve_actor_user($u->id);
719     my $note = OpenILS::SIP::clean_text($u->alert_message) || "";
720     $note =~ s#<sip>.*</sip>##;
721     $note =~ s/^\s*//;  # kill leading whitespace
722     $note =~ s/\s*$//;  # kill trailng whitespace
723     $u->alert_message($note);
724
725     if( ! $e->update_actor_user($u) ) {
726         syslog('LOG_ERR', "OILS: Unblock: patron alert update failed: %s", $e->event->{textcode});
727         $e->rollback; # rollback + disconnect
728         return $self;
729     }
730
731     # stay in synch
732     $self->{user}->alert_message( $note );
733
734     $e->commit; # commits and disconnects
735     return $self;
736 }
737
738 #
739 # Messages
740 #
741
742 sub invalid_patron {
743     return "Please contact library staff";
744 }
745
746 sub charge_denied {
747     return "Please contact library staff";
748 }
749
750 sub inet_privileges {
751     my( $self ) = @_;
752     my $e = OpenILS::SIP->editor();
753     $INET_PRIVS = $e->retrieve_all_config_net_access_level() unless $INET_PRIVS;
754     my ($level) = grep { $_->id eq $self->{user}->net_access_level } @$INET_PRIVS;
755     my $name = OpenILS::SIP::clean_text($level->name);
756     syslog('LOG_DEBUG', "OILS: Patron inet_privs = $name");
757     return $name;
758 }
759
760 sub extra_fields {
761     my( $self ) = @_;
762     my $extra_fields = {};
763         my $u = $self->{user};
764     foreach my $stat_cat_entry (@{$u->stat_cat_entries}) {
765         my $stat_cat = $stat_cat_entry->stat_cat;
766         next unless ($stat_cat->sip_field);
767         my $value = $stat_cat_entry->stat_cat_entry;
768         if(defined $stat_cat->sip_format && length($stat_cat->sip_format) > 0) { # Has a format string?
769             if($stat_cat->sip_format =~ /^\|(.*)\|$/) { # Regex match?
770                 if($value =~ /($1)/) { # If we have a match
771                     if(defined $2) { # Check to see if they embedded a capture group
772                         $value = $2; # If so, use it
773                     }
774                     else { # No embedded capture group?
775                         $value = $1; # Use our outer one
776                     }
777                 }
778                 else { # No match?
779                     $value = ''; # Empty string. Will be checked for below.
780                 }
781             }
782             else { # Not a regex match - Try sprintf match (looking for a %s, if any)
783                 $value = sprintf($stat_cat->sip_format, $value);
784             }
785         }
786         next unless length($value) > 0; # No value = no export
787         $value =~ s/\|//g; # Remove all lingering pipe chars for sane output purposes
788         $extra_fields->{ $stat_cat->sip_field } = [] unless (defined $extra_fields->{$stat_cat->sip_field});
789         push(@{$extra_fields->{ $stat_cat->sip_field}}, $value);
790     }
791     return $extra_fields;
792 }
793
794 1;