TPac: Circulation history CSV export
authorBill Erickson <berick@esilibrary.com>
Wed, 22 Feb 2012 18:14:13 +0000 (13:14 -0500)
committerDan Scott <dscott@laurentian.ca>
Thu, 22 Mar 2012 16:43:47 +0000 (12:43 -0400)
This adds a new "Download CSV" button to the TPac circulation history
interface.  The CSV is generated from a new Action/Trigger event
definition.

Signed-off-by: Bill Erickson <berick@esilibrary.com>
Conflicts:

Open-ILS/src/perlmods/lib/OpenILS/WWW/EGCatLoader/Util.pm
Open-ILS/src/sql/Pg/950.data.seed-values.sql

Signed-off-by: Dan Scott <dan@coffeecode.net>
Open-ILS/src/perlmods/lib/OpenILS/WWW/EGCatLoader.pm
Open-ILS/src/perlmods/lib/OpenILS/WWW/EGCatLoader/Account.pm
Open-ILS/src/perlmods/lib/OpenILS/WWW/EGCatLoader/Util.pm
Open-ILS/src/sql/Pg/950.data.seed-values.sql
Open-ILS/src/sql/Pg/upgrade/XXXX.data.circ_history_export_csv.sql [new file with mode: 0644]
Open-ILS/src/templates/opac/myopac/circ_history.tt2
Open-ILS/src/templates/opac/myopac/circ_history/export.tt2 [new file with mode: 0644]

index 14e0760..755f403 100644 (file)
@@ -155,6 +155,7 @@ sub load {
     return $self->load_myopac_bookbags if $path =~ m|opac/myopac/lists|;
     return $self->load_myopac_bookbag_print if $path =~ m|opac/myopac/list/print|;
     return $self->load_myopac_bookbag_update if $path =~ m|opac/myopac/list/update|;
+    return $self->load_myopac_circ_history_export if $path =~ m|opac/myopac/circ_history/export|;
     return $self->load_myopac_circ_history if $path =~ m|opac/myopac/circ_history|;
     return $self->load_myopac_hold_history if $path =~ m|opac/myopac/hold_history|;
     return $self->load_myopac_prefs_notify if $path =~ m|opac/myopac/prefs_notify|;
index 378bfd5..8e1e931 100644 (file)
@@ -1907,8 +1907,6 @@ sub update_bookbag_item_notes {
 sub load_myopac_bookbag_print {
     my ($self) = @_;
 
-    $self->apache->content_type("text/plain; encoding=utf8");
-
     my $id = int($self->cgi->param("list"));
 
     my ($sorter, $modifier) = $self->_get_bookbag_sort_params("sort");
@@ -1950,12 +1948,34 @@ sub load_myopac_bookbag_print {
     # provoke browser download dialogs.
     (my $filename = $bbag->id . $bbag->name) =~ s/[^a-z0-9_ -]//gi;
 
-    $self->apache->headers_out->add(
-        "Content-Disposition",
-        "attachment;filename=$filename.csv"
+    return $self->set_file_download_headers("$filename.csv");
+}
+
+sub load_myopac_circ_history_export {
+    my $self = shift;
+    my $e = $self->editor;
+    my $filename = $self->cgi->param('filename') || 'circ_history.csv';
+
+    my $ids = $e->json_query({
+        select => {
+            au => [{
+                column => 'id', 
+                transform => 'action.usr_visible_circs', 
+                result_field => 'id'
+            }]
+        },
+        from => 'au',
+        where => {id => $e->requestor->id} 
+    });
+
+    $self->ctx->{csv} = $U->fire_object_event(
+        undef, 
+        'circ.format.history.csv',
+        $e->search_action_circulation({id => [map {$_->{id}} @$ids]}, {substream =>1}),
+        $self->editor->requestor->home_ou
     );
 
-    return Apache2::Const::OK;
+    return $self->set_file_download_headers($filename);
 }
 
 sub load_password_reset {
index e672eac..b7c015b 100644 (file)
@@ -392,6 +392,19 @@ sub load_copy_location_groups {
     $ctx->{copy_location_groups} = \%buckets;
 }
 
+sub set_file_download_headers {
+    my $self = shift;
+    my $filename = shift;
+    my $ctype = shift || "text/plain; encoding=utf8";
+
+    $self->apache->content_type($ctype);
 
+    $self->apache->headers_out->add(
+        "Content-Disposition",
+        "attachment;filename=$filename"
+    );
+
+    return Apache2::Const::OK;
+}
 
 1;
index cec8061..3e6346b 100644 (file)
@@ -11401,4 +11401,49 @@ INSERT INTO config.org_unit_setting_type
         'gui',
         'integer'
     );
+-- circ export csv export --
+
+INSERT INTO action_trigger.hook (key, core_type, description, passive)
+VALUES (
+    'circ.format.history.csv',
+    'circ',
+    oils_i18n_gettext(
+        'circ.format.history.csv',
+        'Produce CSV of circulation history',
+        'ath',
+        'description'
+    ),
+    FALSE
+);
+
+INSERT INTO action_trigger.event_definition (
+    active, owner, name, hook, reactor, validator, group_field, template) 
+VALUES (
+    TRUE, 1, 'Circ History CSV', 'circ.format.history.csv', 'ProcessTemplate', 'NOOP_True', 'usr',
+$$
+Title,Author,Call Number,Barcode,Format
+[%-
+FOR circ IN target;
+    bibxml = helpers.unapi_bre(circ.target_copy.call_number.record, {flesh => '{mra}'});
+    title = "";
+    FOR part IN bibxml.findnodes('//*[@tag="245"]/*[@code="a" or @code="b"]');
+        title = title _ part.textContent;
+    END;
+    author = bibxml.findnodes('//*[@tag="100"]/*[@code="a"]').textContent;
+    item_type = bibxml.findnodes('//*[local-name()="attributes"]/*[local-name()="field"][@name="item_type"]').getAttribute('coded-value') %]
+
+    [%- helpers.csv_datum(title) -%],
+    [%- helpers.csv_datum(author) -%],
+    [%- helpers.csv_datum(circ.target_copy.call_number.label) -%],
+    [%- helpers.csv_datum(circ.target_copy.barcode) -%],
+    [%- helpers.csv_datum(item_type) %]
+[%- END -%]
+$$
+);
+
+INSERT INTO action_trigger.environment (event_def, path)
+    VALUES (
+        currval('action_trigger.event_definition_id_seq'),
+        'target_copy.call_number'
+    );
 
diff --git a/Open-ILS/src/sql/Pg/upgrade/XXXX.data.circ_history_export_csv.sql b/Open-ILS/src/sql/Pg/upgrade/XXXX.data.circ_history_export_csv.sql
new file mode 100644 (file)
index 0000000..47cad2c
--- /dev/null
@@ -0,0 +1,55 @@
+
+BEGIN;
+
+INSERT INTO action_trigger.hook (key, core_type, description, passive)
+VALUES (
+    'circ.format.history.csv',
+    'circ',
+    oils_i18n_gettext(
+        'circ.format.history.csv',
+        'Produce CSV of circulation history',
+        'ath',
+        'description'
+    ),
+    FALSE
+);
+
+INSERT INTO action_trigger.event_definition (
+    active, owner, name, hook, reactor, validator, group_field, template) 
+VALUES (
+    TRUE, 1, 'Circ History CSV', 'circ.format.history.csv', 'ProcessTemplate', 'NOOP_True', 'usr',
+$$
+Title,Author,Call Number,Barcode,Format
+[%-
+FOR circ IN target;
+    bibxml = helpers.unapi_bre(circ.target_copy.call_number.record, {flesh => '{mra}'});
+    title = "";
+    FOR part IN bibxml.findnodes('//*[@tag="245"]/*[@code="a" or @code="b"]');
+        title = title _ part.textContent;
+    END;
+    author = bibxml.findnodes('//*[@tag="100"]/*[@code="a"]').textContent;
+    item_type = bibxml.findnodes('//*[local-name()="attributes"]/*[local-name()="field"][@name="item_type"]').getAttribute('coded-value') %]
+
+    [%- helpers.csv_datum(title) -%],
+    [%- helpers.csv_datum(author) -%],
+    [%- helpers.csv_datum(circ.target_copy.call_number.label) -%],
+    [%- helpers.csv_datum(circ.target_copy.barcode) -%],
+    [%- helpers.csv_datum(item_type) %]
+[%- END -%]
+$$
+);
+
+INSERT INTO action_trigger.environment (event_def, path)
+    VALUES (
+        currval('action_trigger.event_definition_id_seq'),
+        'target_copy.call_number'
+    );
+
+COMMIT;
+
+/* UNDO
+DELETE FROM action_trigger.event WHERE event_def = (
+    SELECT id FROM action_trigger.event_definition WHERE hook = 'circ.format.history.csv');
+DELETE FROM action_trigger.event_definition WHERE hook = 'circ.format.history.csv');
+DELETE FROM action_trigger.hook WHERE key = 'circ.format.history.csv';
+*/
index 0728245..4e8c861 100644 (file)
             <a href='[% mkurl('circ_history', {limit => limit, offset => (offset + limit)}) %]'
                [% IF ctx.circs.size < limit %] class='invisible' [% END %] >[% l('Next') %]<span class="nav_arrow_fix">&#9658;</span></a>
         </span>
-        <span class="float-right">
-            <a class="hide_me" href="#">[% l('Export List') %]</a>
+        <span class="float-left">
+            <form action="[% mkurl(ctx.opac_root _ '/myopac/circ_history/export') %]" method="POST">
+                <div>
+                    [%- INCLUDE "opac/parts/preserve_params.tt2" %]
+                    <input type="hidden" name="filename" value="[% l('circ_history.csv') %]"/> 
+                    <button type="submit">[% l('Download CSV') %]</button>
+                </div>
+            </form>
         </span>
     </div>
     <div class="clear-both"></div>
diff --git a/Open-ILS/src/templates/opac/myopac/circ_history/export.tt2 b/Open-ILS/src/templates/opac/myopac/circ_history/export.tt2
new file mode 100644 (file)
index 0000000..fa3988f
--- /dev/null
@@ -0,0 +1 @@
+[%- ctx.csv.template_output.data -%]