server templates: angjs fall-thru
authorBill Erickson <berickxx@gmail.com>
Fri, 19 Apr 2019 19:59:04 +0000 (15:59 -0400)
committerBill Erickson <berickxx@gmail.com>
Fri, 19 Apr 2019 19:59:04 +0000 (15:59 -0400)
Signed-off-by: Bill Erickson <berickxx@gmail.com>
Open-ILS/src/perlmods/lib/OpenILS/WWW/PrintTemplate.pm
Open-ILS/src/templates/staff/share/print_templates/t_patron_address.tt2 [deleted file]
Open-ILS/web/js/ui/default/staff/circ/patron/app.js
Open-ILS/web/js/ui/default/staff/services/print.js

index 79307ca..bd9f52b 100644 (file)
@@ -76,28 +76,29 @@ sub handler {
     # Let pcrud handle the authz
     $e->personality('open-ils.pcrud');
 
-    my $template_id = $cgi->param('template_id');
     my $owner = $cgi->param('owner') || $e->requestor->ws_ou;
     my $locale = $cgi->param('locale') || 'en-US';
+    my $template_id = $cgi->param('template_id');
     my $template_name = $cgi->param('template_name');
     my $template_data = $cgi->param('template_data');
 
-    return Apache2::Const::FORBIDDEN unless $template_name || $template_id;
+    return Apache2::Const::HTTP_BAD_REQUEST 
+        unless $template_name || $template_id;
 
     my $template = find_template($e, $template_id, $template_name, $locale, $owner)
         or return Apache2::Const::NOT_FOUND;
 
-    my $output = '';
-    my $tt = Template->new;
-    my $tmpl = $template->template;
     my $data;
-
     eval { $data = OpenSRF::Utils::JSON->JSON2perl($template_data); };
     if ($@) {
         $logger->error("Invalid JSON in template compilation: $template_data");
         return Apache2::Const::HTTP_BAD_REQUEST;
     }
 
+    my $output = '';
+    my $tt = Template->new;
+    my $tmpl = $template->template;
+
     my $stat = $tt->process(\$tmpl, {template_data => $data}, \$output);
 
     if ($stat) { # OK
diff --git a/Open-ILS/src/templates/staff/share/print_templates/t_patron_address.tt2 b/Open-ILS/src/templates/staff/share/print_templates/t_patron_address.tt2
deleted file mode 100644 (file)
index 55996b2..0000000
+++ /dev/null
@@ -1,25 +0,0 @@
-<!--
-Template for printing a patron address. Fields include:
-
-* first_given_name
-* second_given_name
-* family_name
-* address.street1
-* address.street2
-* address.city
-* address.state
-* address.post_code
-
--->
-<div>
-  <div>
-    {{patron.first_given_name}} 
-    {{patron.second_given_name}} 
-    {{patron.family_name}}
-  </div>
-  <div>{{address.street1}}</div>
-  <div ng-if="address.street2">{{address.street2}}</div>
-  <div>
-    {{address.city}}, {{address.state}} {{address.post_code}}
-  </div>
-</div>
index a86afae..83b4332 100644 (file)
@@ -333,8 +333,8 @@ function($scope,  $q , $location , $filter , egCore , egNet , egUser , egAlertDi
             context : 'default', 
             template: 'patron_address',
             scope : {
-                patron : patronSvc.current,
-                address : addr
+                patron : egCore.idl.toHash(patronSvc.current),
+                address : egCore.idl.toHash(addr)
             }
         });
     }
index 95c3a07..b07c4c8 100644 (file)
@@ -37,19 +37,10 @@ function($q , $window , $timeout , $http , egHatch , egAuth , egIDL , egOrg , eg
     service.print = function(args) {
         if (!args) return $q.when();
 
-        if (args.server_hosted) {
-
-            return service.compileRemoteTemplate(args.template, args.scope)
-            .then(function(response) {
-                args.content_type = response.contentType;
-                args.content = response.content;
-                return service.print_content(args);
-            });
-
-        } else if (args.template) {
+        if (args.template) {
             // fetch the template, then proceed to printing
 
-            return service.getPrintTemplate(args.template)
+            return service.getPrintTemplate(args)
             .then(function(content) {
                 args.content = content;
                 if (!args.content_type) args.content_type = 'text/html';
@@ -243,17 +234,20 @@ function($q , $window , $timeout , $http , egHatch , egAuth , egIDL , egOrg , eg
         });
     }
 
-    // loads an HTML print template by name from the server
-    // If no template is available in local/hatch storage, 
-    // fetch the template as an HTML file from the server.
-    service.getPrintTemplate = function(name) {
+    // loads an HTML print template by name from the server If no
+    // template is available in local/hatch storage, fetch the template
+    // as an HTML file from the server. if no HTML file is available,
+    // try loading the content from a server-hosted template.
+    service.getPrintTemplate = function(args) {
         var deferred = $q.defer();
+        var name = args.template;
 
         egHatch.getItem('eg.print.template.' + name)
         .then(function(html) {
 
             if (html) {
                 // we have a locally stored template
+                console.debug('Found saved template for ' + name);
                 deferred.resolve(html);
                 return;
             }
@@ -262,7 +256,31 @@ function($q , $window , $timeout , $http , egHatch , egAuth , egIDL , egOrg , eg
             console.debug('fetching template ' + path);
 
             $http.get(path).then(
-                function(data) { deferred.resolve(data.data) },
+                function(data) { 
+
+                    if (data.data.match(/Print Template Not Found/)) {
+
+                        // AngJS templates return a dummy template w/ the
+                        // above text if the template is not found instead
+                        // of a 404.
+                        return service.compileRemoteTemplate(name, args.scope)
+                        .then(
+                            function(response) {
+                                console.debug('Found server-hosted template for ' + name);
+                                args.content_type = response.contentType;
+                                args.content = response.content;
+                                deferred.resolve(args.content);
+                            },
+                            function() {
+                                console.error('unable to locate print template: ' + name);
+                                deferred.reject();
+                            }
+                        );
+                    }
+
+                    console.debug('Found server template file for ' + name);
+                    deferred.resolve(data.data) 
+                },
                 function() {
                     console.error('unable to locate print template: ' + name);
                     deferred.reject();