csv reactor
authorBill Erickson <berick@esilibrary.com>
Tue, 4 Dec 2012 21:06:32 +0000 (16:06 -0500)
committerBill Erickson <berick@esilibrary.com>
Tue, 4 Dec 2012 21:06:32 +0000 (16:06 -0500)
Signed-off-by: Bill Erickson <berick@esilibrary.com>
Open-ILS/src/perlmods/lib/OpenILS/Application/Trigger/Reactor.pm
Open-ILS/src/perlmods/lib/OpenILS/Application/Trigger/Reactor/NotifyCSV.pm [new file with mode: 0644]
itiva_pusher.pl [new file with mode: 0644]
notify-csv.tt2 [new file with mode: 0644]

index a56a2cd..d53a52d 100644 (file)
@@ -98,6 +98,12 @@ $_TT_helpers = {
         return $U->get_copy_price(new_editor(xact=>1), $copy_id);
     },
 
+    get_org_unit => sub {
+        my $org_id = shift;
+        return $org_id if ref $org_id;
+        return new_editor()->get_actor_org_unit($org_id);
+    },
+
     # given a copy, returns the title and author in a hash
     get_copy_bib_basics => sub {
         my $copy_id = shift;
@@ -165,6 +171,14 @@ $_TT_helpers = {
         return $U->ou_ancestor_setting_value($org_id, $setting);
     },
 
+    get_user_setting => sub {
+        my ($user_id, $setting) = @_;
+        my $val = new_editor()->search_actor_user_setting(
+            {usr => $user_id, name => $setting})->[0];
+        return undef unless $val; 
+        return OpenSRF::Utils::JSON->JSON2perl($val->value);  
+    },
+
     # This basically greps/maps out ths isbn string values, but also promotes the first isbn-13 to the
     # front of the line (so that the EDI translator takes it as primary) if there is one.
     get_li_isbns => sub {
@@ -351,6 +365,13 @@ $_TT_helpers = {
         my $unapi = new_editor()->json_query($query);
         return undef unless @$unapi;
         return $_TT_helpers->{xml_doc}->($unapi->[0]->{'unapi.bre'});
+    },
+
+    # escapes quotes in csv string values
+    escape_csv => sub {
+        my $string = shift;
+        $string =~ s/"/""/og;
+        return $string;
     }
 };
 
diff --git a/Open-ILS/src/perlmods/lib/OpenILS/Application/Trigger/Reactor/NotifyCSV.pm b/Open-ILS/src/perlmods/lib/OpenILS/Application/Trigger/Reactor/NotifyCSV.pm
new file mode 100644 (file)
index 0000000..c4fd23f
--- /dev/null
@@ -0,0 +1,46 @@
+package OpenILS::Application::Trigger::Reactor::NotifyCSV;
+use base "OpenILS::Application::Trigger::Reactor";
+use strict; use warnings;
+
+sub ABOUT {
+    return q|
+Creates a CSV representation of patron actions for the purposes of notifying 
+the patron via external notification mechanisms.  
+
+Event Environment Requirements
+
+* patron object with card
+* org unit
+
+Required Event Parameters
+
+notify_media (e.g. phone)
+notify_type  (e.g. overdue)
+notify_level (e.g. "1" -- first overdue)
+
+The set of options for each event parameter is dependent on the 3rd-party
+processing the CSV file.  Using iTiva as the reference implementation, event 
+parameter values options include:
+
+notify_media : 'V' (voice), 'T' (text)
+notify_level : '1' (1st notice), '2' (2nd notice...) , '3'
+notify_type : 
+    'FINES', 
+    'OVERDUE', 
+    'PREOVERDUE', 
+    'PRERESERVE', 
+    'RECALL', 
+    'RESERVE', 
+    'RESERVECANCEL', 
+    'RESERVEEXPIRE', 
+    'SUSPEND'
+|;
+}
+
+sub handler {
+    my ($self, $env) = @_;
+    return 1 if $self->run_TT($env);
+    return 0;
+}
+
+1;
diff --git a/itiva_pusher.pl b/itiva_pusher.pl
new file mode 100644 (file)
index 0000000..b923a7f
--- /dev/null
@@ -0,0 +1,5 @@
+
+
+my $header = <<CSV;
+"Media Type","Language","Notice Type","Notification Level","Patron Number","Patron Title","Patron First Names","Patron Surname","Telephone Number","Email Address","Library Code","Site Code","Site Name","Item Barcode","Due Date","Item Title","Transaction ID"
+CSV
diff --git a/notify-csv.tt2 b/notify-csv.tt2
new file mode 100644 (file)
index 0000000..be00608
--- /dev/null
@@ -0,0 +1,76 @@
+[%
+    USE date;
+    core_type = event.event_def.hook.core_type;
+
+    SET target = [target] UNLESS event.event_def.group_field;
+    notice_org_unit = helpers.get_org_unit(event.event_def.owner);
+
+    FOR target_obj IN target;
+
+        # Mangle the data into a consistent shape
+
+        circ = '';
+        hold = '';
+        copy = '';
+        user = '';
+        title = '';
+        org_unit = '';
+        due_date = '';
+
+        IF core_type == 'circ';
+            # e.g. overdue circ
+            circ = target_obj;
+            user = circ.usr;
+            copy = circ.target_copy;
+            org_unit = circ.circ_lib;
+            due_date = date.format(
+                helpers.format_date(circ.due_date), '%d/%m/%Y');
+
+        ELSIF core_type == 'ahr':
+            # e.g. hold ready for pickup
+            hold = target_obj;
+            user = hold.usr;
+            copy = hold.current_copy;
+            org_unit = hold.pickup_lib;
+
+        ELSIF core_type == 'ausp';
+            # e.g. max fines
+            user = target_obj.usr;
+            org_unit = target_obj.org_unit;
+
+        ELSIF core_type == 'au':
+            # e.g. barred
+            user = target_obj;
+            org_unit = user.home_ou;
+        END;
+
+        user_locale = helpers.get_user_locale(user.id));
+        user_lang = user_locale | replace('-.*', ''); # ISO 639-1 language
+        user_phone = helpers.get_user_setting(
+            user.id, 'opac.default_phone') || user.day_phone;
+
+        IF copy;
+            bib_data = helpers.get_copy_bib_basics(copy.id);
+            title = bib_data.title;
+        END;
+
+        # Print the data for each target object as CSV
+%]
+[%- '"' _ helpers.escape_csv(params.notify_media) _ '",' -%]
+[%- '"' _ helpers.escape_csv(user_lang) _ '",' -%]
+[%- '"' _ helpers.escape_csv(params.notify_type) _ '",' -%]
+[%- '"' _ helpers.escape_csv(params.notify_level) _ '",' -%]
+[%- '"' _ helpers.escape_csv(user.card.barcode) _ '",' -%]
+[%- '"' _ helpers.escape_csv(user.prefix) _ '",' -%]
+[%- '"' _ helpers.escape_csv(user.first_given_name) _ '",' -%]
+[%- '"' _ helpers.escape_csv(user.family_name) _ '",' -%]
+[%- '"' _ helpers.escape_csv(user_phone) _ '",' -%]
+[%- '"' _ helpers.escape_csv(user.email) _ '",' -%]
+[%- '"' _ helpers.escape_csv(notice_org_unit.shortname) _ '",' -%]
+[%- '"' _ helpers.escape_csv(org_unit.shortname) _ '",' -%]
+[%- '"' _ helpers.escape_csv(org_name.name) _ '",' -%]
+[%- '"' _ helpers.escape_csv(copy.barcode) _ '",' -%]
+[%- '"' _ helpers.escape_csv(due_date) _ '",' -%]
+[%- '"' _ helpers.escape_csv(title) _ '",' -%]
+[%- '"' _ helpers.escape_csv(event.id) _ '"'  -%]
+[% END %]