LP1691269: Webstaff Copy Editor Templates
authorJason Boyer <JBoyer@library.in.gov>
Sun, 22 Oct 2017 19:42:02 +0000 (15:42 -0400)
committerMike Rylander <mrylander@gmail.com>
Mon, 27 Nov 2017 18:47:28 +0000 (13:47 -0500)
This branch will translate and transfer your XUL client templates
the first time you load the web client copy editor. From that point
on the two diverge with no further connection to each other.

This branch also allows your copy templates to travel with you
between browsers, machines, and so on, while still allowing users
that share a browser but not usernames to have their own templates.

Signed-off-by: Jason Boyer <JBoyer@library.in.gov>
Signed-off-by: Cesar Velez <cesar.velez@equinoxinitiative.org>
Signed-off-by: Galen Charlton <gmc@equinoxinitiative.org>
Signed-off-by: Mike Rylander <mrylander@gmail.com>
Open-ILS/src/sql/Pg/upgrade/XXXX.schema.webstaff.cat.copy.templates.sql [new file with mode: 0644]
Open-ILS/web/js/ui/default/staff/cat/volcopy/app.js

diff --git a/Open-ILS/src/sql/Pg/upgrade/XXXX.schema.webstaff.cat.copy.templates.sql b/Open-ILS/src/sql/Pg/upgrade/XXXX.schema.webstaff.cat.copy.templates.sql
new file mode 100644 (file)
index 0000000..1202751
--- /dev/null
@@ -0,0 +1,8 @@
+BEGIN;
+
+SELECT evergreen.upgrade_deps_block_check('XXXX', :eg_version);
+
+INSERT INTO config.usr_setting_type (name, label, description, datatype)
+  VALUES ('webstaff.cat.copy.templates', 'Web Client Copier Editor Templates', 'Web Client Copy Editor Templates', 'object');
+
+COMMIT;
index 44007e1..2097608 100644 (file)
@@ -250,6 +250,105 @@ function(egCore , $q) {
 
     };
 
 
     };
 
+   service.get_acp_templates = function() {
+       // Already downloaded for this user? Return local copy. Changing users or logging out causes another download
+       // so users always have their own templates, and any changes made on other machines appear as expected.
+       if (egCore.hatch.getSessionItem('cat.copy.templates.usr') == egCore.auth.user().id()) {
+           return egCore.hatch.getItem('cat.copy.templates').then(function(templ) {
+               return templ;
+           });
+       } else {
+          // this can be disabled for debugging to force a re-download and translation of test templates
+          egCore.hatch.setSessionItem('cat.copy.templates.usr', egCore.auth.user().id());
+          return service.load_remote_acp_templates();
+      }
+
+   };
+
+   service.save_acp_templates = function(t) {
+       egCore.hatch.setItem('cat.copy.templates', t);
+       egCore.net.request('open-ils.actor', 'open-ils.actor.patron.settings.update',
+           egCore.auth.token(), egCore.auth.user().id(), { "webstaff.cat.copy.templates": t });
+       // console.warn('Saved ' + JSON.stringify({"webstaff.cat.copy.templates": t}));
+   };
+
+   service.load_remote_acp_templates = function() {
+       // After the XUL Client is completely removed everything related to staff_client.copy_editor.templates and convert_xul_templates can be thrown away.
+       return egCore.net.request('open-ils.actor', 'open-ils.actor.patron.settings.retrieve.authoritative',
+           egCore.auth.token(), egCore.auth.user().id(),
+           ['webstaff.cat.copy.templates','staff_client.copy_editor.templates']).then(function(settings) {
+               if (settings['webstaff.cat.copy.templates']) {
+                   egCore.hatch.setItem('cat.copy.templates', settings['webstaff.cat.copy.templates']);
+                   return settings['webstaff.cat.copy.templates'];
+               } else {
+                   if (settings['staff_client.copy_editor.templates']) {
+                      var new_templ = service.convert_xul_templates(settings['staff_client.copy_editor.templates']);
+                      egCore.hatch.setItem('cat.copy.templates', new_templ);
+                      // console.warn('Saving: ' + JSON.stringify({'webstaff.cat.copy.templates' : new_templ}));
+                      egCore.net.request('open-ils.actor', 'open-ils.actor.patron.settings.update',
+                          egCore.auth.token(), egCore.auth.user().id(), {'webstaff.cat.copy.templates' : new_templ});
+                      return new_templ;
+                   }
+               }
+               return {};
+       });
+   };
+
+   service.convert_xul_templates = function(xultempl) {
+       var conv_templ = {};
+       var templ_names = Object.keys(xultempl);
+       var name;
+       var xul_t;
+       var curr_templ;
+       var stat_cats;
+       var fields;
+       var field_name;
+       var curr_field;
+       var tmp_val;
+       var i, j;
+
+       if (templ_names){
+         for (i=0; i < templ_names.length; i++) {
+           name = templ_names[i];
+           curr_templ = {};
+           stat_cats = {};
+           xul_t  = xultempl[name];
+           fields = Object.keys(xul_t);
+
+           if (fields.length > 0) {
+             for (j=0; j < fields.length; j++) {
+               field_name = fields[j];
+               curr_field = xul_t[field_name];
+
+               if ( curr_field["field"] == null ) { continue; }
+               if ( curr_field["value"] == "<HACK:KLUDGE:NULL>" ) { continue; }
+
+               // floating changed from a boolean to an integer at one point; take this opportunity to remove the boolean from any old templates
+               if ( curr_field["type"] === "attribute" && curr_field["field"] === "floating" ) {
+                 if ( curr_field["value"].match(/[tf]/) ) { continue; }
+               }
+
+               if ( curr_field["type"] === "stat_cat" ) {
+                 stat_cats[curr_field["field"]] = parseInt(curr_field["value"]);
+               }
+               else {
+                 tmp_val = curr_field['value']; // so... some of the number fields are actually strings. Groovy.
+                 if ( tmp_val.match(/^[-0-9.]+$/) && !(curr_field["field"].match(/(?:loan_duration|fine_level)/))) { tmp_val = parseFloat(tmp_val); }
+                 curr_templ[curr_field["field"]] = tmp_val;
+               }
+             }
+
+             if ( (Object.keys(stat_cats)).length > 0 ){
+               curr_templ["statcats"] = stat_cats;
+             }
+
+             conv_templ[name] = curr_templ;
+           }
+         }
+       }
+       return conv_templ;
+   };
+
     service.flesh = {   
         flesh : 3, 
         flesh_fields : {
     service.flesh = {   
         flesh : 3, 
         flesh_fields : {
@@ -1085,10 +1184,10 @@ function($scope , $q , $window , $routeParams , $location , $timeout , egCore ,
         $scope.template_name_list = [];
 
         $scope.fetchTemplates = function () {
         $scope.template_name_list = [];
 
         $scope.fetchTemplates = function () {
-            egCore.hatch.getItem('cat.copy.templates').then(function(t) {
+            itemSvc.get_acp_templates().then(function(t) {
                 if (t) {
                     $scope.templates = t;
                 if (t) {
                     $scope.templates = t;
-                    $scope.template_name_list = Object.keys(t);
+                    $scope.template_name_list = Object.keys(t).sort();
                 }
             });
             egCore.hatch.getItem('cat.copy.last_template').then(function(t) {
                 }
             });
             egCore.hatch.getItem('cat.copy.last_template').then(function(t) {
@@ -1933,10 +2032,10 @@ function($scope , $q , $window , $routeParams , $location , $timeout , egCore ,
                 $scope.template_controls = true;
 
                 $scope.fetchTemplates = function () {
                 $scope.template_controls = true;
 
                 $scope.fetchTemplates = function () {
-                    egCore.hatch.getItem('cat.copy.templates').then(function(t) {
+                    itemSvc.get_acp_templates().then(function(t) {
                         if (t) {
                             $scope.templates = t;
                         if (t) {
                             $scope.templates = t;
-                            $scope.template_name_list = Object.keys(t);
+                            $scope.template_name_list = Object.keys(t).sort();
                         }
                     });
                 }
                         }
                     });
                 }
@@ -1962,9 +2061,9 @@ function($scope , $q , $window , $routeParams , $location , $timeout , egCore ,
                 $scope.deleteTemplate = function (n) {
                     if (n) {
                         delete $scope.templates[n]
                 $scope.deleteTemplate = function (n) {
                     if (n) {
                         delete $scope.templates[n]
-                        $scope.template_name_list = Object.keys($scope.templates);
+                        $scope.template_name_list = Object.keys($scope.templates).sort();
                         $scope.template_name = '';
                         $scope.template_name = '';
-                        egCore.hatch.setItem('cat.copy.templates', $scope.templates);
+                        itemSvc.save_acp_templates($scope.templates);
                         $scope.$parent.fetchTemplates();
                         ngToast.create(egCore.strings.VOL_COPY_TEMPLATE_SUCCESS_DELETE);
                     }
                         $scope.$parent.fetchTemplates();
                         ngToast.create(egCore.strings.VOL_COPY_TEMPLATE_SUCCESS_DELETE);
                     }
@@ -1984,15 +2083,15 @@ function($scope , $q , $window , $routeParams , $location , $timeout , egCore ,
                         });
             
                         $scope.templates[n] = tmpl;
                         });
             
                         $scope.templates[n] = tmpl;
-                        $scope.template_name_list = Object.keys($scope.templates);
+                        $scope.template_name_list = Object.keys($scope.templates).sort();
             
             
-                        egCore.hatch.setItem('cat.copy.templates', $scope.templates);
+                        itemSvc.save_acp_templates($scope.templates);
                         $scope.$parent.fetchTemplates();
 
                         $scope.dirty = false;
                     } else {
                         // save all templates, as we might do after an import
                         $scope.$parent.fetchTemplates();
 
                         $scope.dirty = false;
                     } else {
                         // save all templates, as we might do after an import
-                        egCore.hatch.setItem('cat.copy.templates', $scope.templates);
+                        itemSvc.save_acp_templates($scope.templates);
                         $scope.$parent.fetchTemplates();
                     }
                     ngToast.create(egCore.strings.VOL_COPY_TEMPLATE_SUCCESS_SAVE);
                         $scope.$parent.fetchTemplates();
                     }
                     ngToast.create(egCore.strings.VOL_COPY_TEMPLATE_SUCCESS_SAVE);
@@ -2008,9 +2107,11 @@ function($scope , $q , $window , $routeParams , $location , $timeout , egCore ,
                         try {
                             var newTemplates = JSON.parse(newVal);
                             if (!Object.keys(newTemplates).length) return;
                         try {
                             var newTemplates = JSON.parse(newVal);
                             if (!Object.keys(newTemplates).length) return;
-                            $scope.templates = newTemplates;
-                            $scope.template_name_list = Object.keys(newTemplates);
-                            $scope.template_name = '';
+                            angular.forEach(Object.keys(newTemplates), function (k) {
+                                $scope.templates[k] = newTemplates[k];
+                            });
+                            itemSvc.save_acp_templates($scope.templates);
+                            $scope.fetchTemplates();
                         } catch (E) {
                             console.log('tried to import an invalid copy template file');
                         }
                         } catch (E) {
                             console.log('tried to import an invalid copy template file');
                         }