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