LP1825851 server print additions WIP
authorBill Erickson <berickxx@gmail.com>
Wed, 10 Jul 2019 21:40:38 +0000 (17:40 -0400)
committerBill Erickson <berickxx@gmail.com>
Wed, 10 Jul 2019 21:40:38 +0000 (17:40 -0400)
Signed-off-by: Bill Erickson <berickxx@gmail.com>
Open-ILS/src/eg2/src/app/share/print/print.service.ts
Open-ILS/src/perlmods/lib/OpenILS/WWW/PrintTemplate.pm
Open-ILS/src/sql/Pg/upgrade/XXXX.schema.server-print-templates.sql

index d4d0263..79762af 100644 (file)
@@ -5,6 +5,7 @@ import {LocaleService} from '@eg/core/locale.service';
 import {AuthService} from '@eg/core/auth.service';
 
 declare var js2JSON: (jsThing: any) => string;
+declare var OpenSRF;
 
 const PRINT_TEMPLATE_PATH = '/print_template';
 
@@ -74,6 +75,12 @@ export class PrintService {
         formData.append('template_data', js2JSON(printReq.contextData));
         formData.append('locale', this.locale.currentLocaleCode());
 
+        // Sometimes we want to know the time zone of the browser,
+        // regardless of any org unit settings.
+        if (OpenSRF.tz) {
+            formData.append('client_timezone', OpenSRF.tz);
+        }
+
         return new Promise((resolve, reject) => {
             const xhttp = new XMLHttpRequest();
             xhttp.onreadystatechange = function() {
index 457a0f5..7cfc448 100644 (file)
@@ -5,13 +5,19 @@ use Apache2::Const -compile =>
 use Apache2::RequestRec;
 use CGI;
 use HTML::Defang;
+use DateTime;
+use DateTime::Format::ISO8601;
+use Unicode::Normalize;
 use OpenSRF::Utils::JSON;
 use OpenSRF::System;
 use OpenSRF::Utils::SettingsClient;
 use OpenILS::Utils::CStoreEditor q/:funcs/;
 use OpenSRF::Utils::Logger q/$logger/;
 use OpenILS::Application::AppUtils;
+use OpenILS::Utils::DateTime qw/:datetime/;
+
 my $U = 'OpenILS::Application::AppUtils';
+my $helpers;
 
 my $bs_config;
 my $enable_cache; # Enable process-level template caching
@@ -53,6 +59,7 @@ sub handler {
 
     my $owner = $cgi->param('owner') || $e->requestor->ws_ou;
     my $locale = $cgi->param('locale') || 'en-US';
+    my $client_timezone = $cgi->param('client_timezone');
     my $template_id = $cgi->param('template_id');
     my $template_name = $cgi->param('template_name');
     my $template_data = $cgi->param('template_data');
@@ -70,11 +77,23 @@ sub handler {
         return Apache2::Const::HTTP_BAD_REQUEST;
     }
 
+    my ($staff_org) = $U->fetch_org_unit($e->requestor->ws_ou);
+
     my $output = '';
     my $tt = Template->new;
     my $tmpl = $template->template;
 
-    my $stat = $tt->process(\$tmpl, {template_data => $data}, \$output);
+    my $context = {
+        locale => $locale,
+        client_timezone => $client_timezone,
+        staff => $e->requestor,
+        staff_org => $staff_org,
+        staff_org_timezone => get_org_timezone($staff_org->id),
+        helpers => $helpers,
+        template_data => $data
+    };
+
+    my $stat = $tt->process(\$tmpl, $context, \$output);
 
     if ($stat) { # OK
         my $ctype = $template->content_type;
@@ -96,6 +115,20 @@ sub handler {
     }
 }
 
+my %org_timezone_cache;
+sub get_org_timezone {
+    my ($e, $org_id) = @_;
+
+    if (!$org_timezone_cache{$org_id}) {
+        $org_timezone_cache{$org_id} = 
+            $U->ou_ancestor_setting_value($org_id, 'lib.timezone')
+            || DateTime->now(time_zone => 'local')->time_zone->name;
+    }
+
+    return $org_timezone_cache{$org_id};
+}
+
+
 # Find the template closest to the specific org unit owner.
 my %template_cache;
 sub find_template {
@@ -141,4 +174,37 @@ sub find_template {
     return undef;
 }
 
+# Utility / helper functions passed into every template
+
+$helpers = {
+
+    # turns a date w/ optional timezone modifier into something 
+    # TT can understand
+    format_date => sub {
+        my $date = shift;
+        my $tz = shift;
+
+        $date->set_time_zone($tz) if $tz;
+        $date = DateTime::Format::ISO8601->new->parse_datetime(clean_ISO8601($date));
+        return sprintf(
+            "%0.2d:%0.2d:%0.2d %0.2d-%0.2d-%0.4d",
+            $date->hour,
+            $date->minute,
+            $date->second,
+            $date->day,
+            $date->month,
+            $date->year
+        );
+    }
+
+    format_current_date => sub {
+        my $tz = shift || 'local';
+        my $date = DateTime->new(time_zone => $tz);
+        return $helpers->format_date($date);
+    }
+};
+
+
+
+
 1;
index f191566..96ce998 100644 (file)
@@ -42,6 +42,56 @@ $TEMPLATE$
 $TEMPLATE$
 );
 
+INSERT INTO config.print_template 
+    (id, name, locale, active, owner, label, template) 
+VALUES (
+    2, 'holds_for_bib', 'en-US', FALSE,
+    (SELECT id FROM actor.org_unit WHERE parent_ou IS NULL),
+    oils_i18n_gettext(2, 'Holds for Bib Record', 'cpt', 'label'),
+$TEMPLATE$
+[%-
+    USE date;
+    SET holds = template_data.holds;
+-%]
+<div>
+  <div>Holds for record: [% holds.0.title %]</div>
+  <hr/>
+  <style>#holds-for-bib-table td { padding: 5px; }</style>
+  <table id="holds-for-bib-table">
+    <thead>
+      <tr>
+        <th>Request Date</th>
+        <th>Patron Barcode</th>
+        <th>Patron Last</th>
+        <th>Patron Alias</th>
+        <th>Current Item</th>
+      </tr>
+    </thead>
+    <tbody>
+      [% FOR hold IN holds %]
+      <tr>
+        <td>[% 
+          date.format(helpers.format_date(
+            hold.hold.request_time, staff_org_timezone), '%x %r', locale) 
+        %]</td>
+        <td>[% hold.patron.barcode %]</td>
+        <td>[% hold.patron.last %]</td>
+        <td>[% hold.patron.alias %]</td>
+        <td>[% hold.copy.barcode %]</td>
+      </tr>
+    </tbody>
+  </table>
+  <hr/>
+  <div>
+    [% staff_org.shortname %] 
+    [% date.format(helpers.format_current_date(client_timezone), '%x %r', locale) %]
+  </div>
+  <div>Printed by [% staff.first_given_name %]</div>
+<br/>
+
+$TEMPLATE$
+);
+
 -- Allow for 1k stock templates
 SELECT SETVAL('config.print_template_id_seq'::TEXT, 1000);