webstaff: prune empty fields and subfields before saving MARC record
authorGalen Charlton <gmc@esilibrary.com>
Thu, 3 Sep 2015 17:17:59 +0000 (17:17 +0000)
committerJason Stephenson <jstephenson@mvlc.org>
Mon, 14 Sep 2015 19:44:19 +0000 (15:44 -0400)
This implements functionality that was handled server-side
by the XUL staff client.

Signed-off-by: Galen Charlton <gmc@esilibrary.com>
Signed-off-by: Jason Stephenson <jstephenson@mvlc.org>
Open-ILS/web/js/ui/default/staff/cat/services/marcedit.js
Open-ILS/web/js/ui/default/staff/marcrecord.js

index 58ec75b..ca161f5 100644 (file)
@@ -999,6 +999,7 @@ angular.module('egMarcMod', ['egCoreMod', 'ui.bootstrap'])
                     $scope.mangle_005();
                     $scope.Record().editor(egCore.auth.user().id());
                     $scope.Record().edit_date('now');
+                    $scope.record.pruneEmptyFieldsAndSubfields();
                     $scope.Record().marc($scope.record.toXmlString());
                     if ($scope.recordId) {
                         return egCore.pcrud.update(
index 2f03706..95318ba 100644 (file)
@@ -335,6 +335,27 @@ var MARC21 = {
             return this;
         }
 
+        this.pruneEmptyFieldsAndSubfields = function() {
+            var me = this;
+            var fields_to_remove = [];
+            for (var i = 0; i < this.fields.length; i++) {
+                var f = this.fields[i];
+                if (f.isControlfield()) {
+                    if (!f.data){
+                        fields_to_remove.push(f);
+                    }
+                } else {
+                    f.pruneEmptySubfields();
+                    if (f.isEmptyDatafield()) {
+                        fields_to_remove.push(f);
+                    }
+                }
+            }
+            fields_to_remove.forEach(function(f) {
+                me.deleteField(f);
+            });
+        }
+
         this.toBreaker = function () {
 
             var me = this;
@@ -658,6 +679,30 @@ var MARC21 = {
             return this.tag < '010' ? true : false;
         }
 
+        this.pruneEmptySubfields = function () {
+            if (this.isControlfield()) return;
+            var me = this;
+            var subfields_to_remove = [];
+            this.subfields.forEach( function(f) {
+                if (f[1] == '') {
+                    subfields_to_remove.push(f);
+                }
+            });
+            subfields_to_remove.forEach(function(f) {
+                me.deleteExactSubfields(f);
+            });
+        }
+        this.isEmptyDatafield = function () {
+            if (this.isControlfield()) return false;
+            var isEmpty = true;
+            this.subfields.forEach( function(f) {
+                if (isEmpty && f[1] != '') {
+                    isEmpty = false;
+                }
+            });
+            return isEmpty;
+        }
+
         this.indicator = function (num, value) {
             if (value !== undefined) {
                 if (num == 1) this.ind1 = value;