From 9192dfe19a8068192f75dcab5ab4efad10425fe0 Mon Sep 17 00:00:00 2001
From: Cori Lynn Arnold <carnold@dgiinc.com>
Date: Tue, 9 Oct 2018 12:48:58 +0000
Subject: [PATCH] Bug 19263: 001 widget only for configured client

Tests:

1) Verify that a new entry for CONTROL_NUM_SEQUENCE is added to table
authorised_value_categories
2) Edit a bib record using advanced editor and note that 001 widget
isn't there when adding/editing 001 field
3) Add a new row into authorised_values table with:
    a) CONTROL_NUM_SEQUENCE in category column
    b) authorised_value column has a string ending with a number i.e.
    "faw0001"
    c) lib column has a short string indicating type of control number
    i.e. "FAW"
4) Edit a bib record using the advanced editor and note that the 001
widget is there when adding / editing 001 field
---
 installer/data/mysql/mandatory/auth_val_cat.sql    |   3 +-
 .../prog/en/includes/cateditor-widgets-marc21.inc  | 106 +++++++++++----------
 2 files changed, 59 insertions(+), 50 deletions(-)

diff --git a/installer/data/mysql/mandatory/auth_val_cat.sql b/installer/data/mysql/mandatory/auth_val_cat.sql
index 6fff954..df119a0 100644
--- a/installer/data/mysql/mandatory/auth_val_cat.sql
+++ b/installer/data/mysql/mandatory/auth_val_cat.sql
@@ -44,7 +44,8 @@ INSERT IGNORE INTO authorised_value_categories( category_name )
     ('HINGS_C'),
     ('HINGS_AS'),
     ('HINGS_RD'),
-    ('STACK');
+    ('STACK')
+    ('CONTROL_NUM_SEQUENCE');
 
 -- UNIMARC specific?
 INSERT IGNORE INTO authorised_value_categories( category_name ) VALUES
diff --git a/koha-tmpl/intranet-tmpl/prog/en/includes/cateditor-widgets-marc21.inc b/koha-tmpl/intranet-tmpl/prog/en/includes/cateditor-widgets-marc21.inc
index 92b8b8c..970f58e 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/includes/cateditor-widgets-marc21.inc
+++ b/koha-tmpl/intranet-tmpl/prog/en/includes/cateditor-widgets-marc21.inc
@@ -113,56 +113,64 @@ require( [ 'koha-backend', 'widget' ], function( KohaBackend, Widget ) {
         },
     } );
 
-    Widget.Register( '001@', {
-        init: function() {
-            var $result = $(
-                '<span class="subfield-widget">'
-                + _("Control number: ")
-                + '<span class="control-number-widget-contents"></span>'
-                + '<button class="control-number-widget-assign">' + _("Assign next") + '</button>'
-                + '<select class="control-number-widget-sequence"></select>'
-                + '<button class="control-number-widget-override">Override</button>'
-                + '</span>'
-            );
+    /* Check to see if there are any Control Number entries
+     *  in the authorised values table.
+     * If not, let's not show the widget
+     */
+    var reg001 = KohaBackend.GetAuthorisedValues( 'CONTROL_NUM_SEQUENCE');
+    if(reg001) {
+
+        Widget.Register( '001@', {
+            init: function() {
+                var $result = $(
+                    '<span class="subfield-widget">'
+                    + _("Control number: ")
+                    + '<span class="control-number-widget-contents"></span>'
+                    + '<button class="control-number-widget-assign">' + _("Assign next") + '</button>'
+                    + '<select class="control-number-widget-sequence"></select>'
+                    + '<button class="control-number-widget-override">Override</button>'
+                    + '</span>'
+                );
 
-            return $result[0];
-        },
-        setControlNumber: function( text ) {
-            if ( text ) this.setText( text );
-            $( this.node ).find('.control-number-widget-contents')
-                .html( text == '<empty>' ? ( '<span class="hint">' + _("unset") + '</span>' ) : text );
-            this.mark.changed();
-        },
-        postCreate: function( node, mark ) {
-            var widget = this;
-            this.setControlNumber( this.text );
-            $( this.node )
-                .find('.control-number-widget-assign').click( function() {
-                    var sequence = $( widget.node ).find('.control-number-widget-sequence').val();
-                    $.post(
-                        '/cgi-bin/koha/svc/cataloguing/control_num_sequences/' + sequence
-                    ).done( function( result ) {
-                        if ( result.next_value ) widget.setControlNumber( result.next_value );
-                    } );
-                } ).end()
-                .find('.control-number-widget-override').click( function() {
-                    var result = prompt( _("Enter new control number") );
-
-                    if ( result ) widget.setControlNumber( result );
-                } ).end();
-
-            var sequence_list = $.map( KohaBackend.GetAuthorisedValues( 'CONTROL_NUM_SEQUENCE' ), function( authval ) {
-                return authval.lib;
-            } );
-            sequence_list.sort();
-
-            $.each( sequence_list, function( undef, sequence ) {
-                $( widget.node ).find('.control-number-widget-sequence').append( '<option>' + sequence + '</option>' );
-            } );
-
-            // TODO: Make Enter on select click "Assign"
-        }
-    } );
+                return $result[0];
+            },
+            setControlNumber: function( text ) {
+                if ( text ) this.setText( text );
+                $( this.node ).find('.control-number-widget-contents')
+                    .html( text == '<empty>' ? ( '<span class="hint">' + _("unset") + '</span>' ) : text );
+                this.mark.changed();
+            },
+            postCreate: function( node, mark ) {
+                var widget = this;
+                this.setControlNumber( this.text );
+                $( this.node )
+                    .find('.control-number-widget-assign').click( function() {
+                        var sequence = $( widget.node ).find('.control-number-widget-sequence').val();
+                        $.post(
+                            '/cgi-bin/koha/svc/cataloguing/control_num_sequences/' + sequence
+                        ).done( function( result ) {
+                            if ( result.next_value ) widget.setControlNumber( result.next_value );
+                        } );
+                    } ).end()
+                    .find('.control-number-widget-override').click( function() {
+                        var result = prompt( _("Enter new control number") );
+
+                        if ( result ) widget.setControlNumber( result );
+                    } ).end();
+
+                var sequence_list = $.map( KohaBackend.GetAuthorisedValues( 'CONTROL_NUM_SEQUENCE' ), function( authval ) {
+                    return authval.lib;
+                } );
+                sequence_list.sort();
+
+                $.each( sequence_list, function( undef, sequence ) {
+                    $( widget.node ).find('.control-number-widget-sequence').append( '<option>' + sequence + '</option>' );
+                } );
+
+                // TODO: Make Enter on select click "Assign"
+            }
+        } );
+        };
 
     Widget.Register( '005@', {
         init: function() {
-- 
2.1.4