Bug 6977 : Adds support for repeatable subfields when importing authorities.
authorFrédérick Capovilla <frederick.capovilla@libeo.com>
Wed, 5 Oct 2011 17:16:37 +0000 (13:16 -0400)
committerChris Cormack <chrisc@catalyst.net.nz>
Wed, 12 Oct 2011 22:07:17 +0000 (11:07 +1300)
Before this patch, if we tried to import an authority with multiple $x
subfields into a bibliographic record, only the last value get added
to the form.

All repeated values should now be sent to the form.

Signed-off-by: Jared Camins-Esakov <jcamins@cpbibliography.com>
Signed-off-by: Paul Poulain <paul.poulain@biblibre.com>
Signed-off-by: Ian Walls <ian.walls@bywatersolutions.com>
Signed-off-by: Chris Cormack <chrisc@catalyst.net.nz>
authorities/blinddetail-biblio-search.pl
koha-tmpl/intranet-tmpl/prog/en/modules/authorities/blinddetail-biblio-search.tt

index 0f734b2..7de7db3 100755 (executable)
@@ -80,10 +80,20 @@ if ($authid) {
     my @fields = $record->field( $auth_type->{auth_tag_to_report} );
     my $repet = ($query->param('repet') || 1) - 1;
     my $field = $fields[$repet];
+
+    # Get all values for each distinct subfield
+    my %subfields;
     for ( $field->subfields ) {
-        my ($letter, $value) = @$_;
-        $letter = '@' unless $letter;
-        push @subfield_loop, { marc_subfield => $letter, marc_value => $value };
+        my $letter = $_->[0];
+        next if defined $subfields{$letter};
+        my @values = $field->subfield($letter);
+        $subfields{$letter} = \@values;
+    }
+
+    # Add all subfields to the subfield_loop
+    for( keys %subfields ) {
+        my $letter = $_ || '@';
+        push( @subfield_loop, {marc_subfield => $letter, marc_values => $subfields{$_}} );
     }
 }
 else {
@@ -91,11 +101,16 @@ else {
     $template->param( "clear" => 1 );
 }
 
+# Extract the tag number from the index
+my $tag_number = $index;
+$tag_number =~ s/^tag_(\d*)_.*$/$1/;
+
 $template->param(
     authid          => $authid ? $authid : "",
     index           => $index,
     tagid           => $tagid,
     SUBFIELD_LOOP   => \@subfield_loop,
+    tag_number      => $tag_number,
 );
 
 output_html_with_http_headers $query, $cookie, $template->output;
index 6a7064b..1697289 100644 (file)
         } catch(e) {
             return;
         }
-        
-        // browse all its subfields
-        var subfields = whichfield.parentNode.parentNode.getElementsByTagName('input');
+
+        var field_start = whichfield.parentNode.parentNode;
+      
+        // browse all its subfields (clear and $9)
+        var subfields = field_start.getElementsByTagName('input');
         var re = /^tag_\d*_code_/;
         for(var i=0, len = subfields.length ; i< len ; i++) { // browse all subfields
             if(subfields[i].getAttribute('name').match(re)){ // it s a subfield
 
             [% IF ( clear ) %]
                 if (subfield){subfield.value="" ;}
-            [% ELSE %]      
-              [% FOREACH SUBFIELD_LOO IN SUBFIELD_LOOP %]
-                if (code.value == "[% SUBFIELD_LOO.marc_subfield |replace("'", "\'") |replace('"', '\"') |replace('\n', '\\n') |replace('\r', '\\r') %]"){
-                        subfield.value = "[% SUBFIELD_LOO.marc_value |replace("'", "\'") |replace('"', '\"') |replace('\n', '\\n') |replace('\r', '\\r') %]";
+            [% ELSE %]
+                if(code.value=='9'){
+                    subfield.value = "[% authid |replace("'", "\'") |replace('"', '\"') |replace('\n', '\\n') |replace('\r', '\\r') %]";
                 }
-              [% END %]
-              if(code.value=='9'){
-                  subfield.value = "[% authid |replace("'", "\'") |replace('"', '\"') |replace('\n', '\\n') |replace('\r', '\\r') %]";
-              }
             [% END %]
             }
         }
+
+        // Sets the good number of form fields for the specified subfield
+        function SetSubfieldNumber(subfield_name, nb) {
+            // Nothing to do if we only have one value
+            if(nb <= 1) {
+                return;
+            }
+            
+            // Find the subfield we want to clone
+            var re = new RegExp('^subfield' + subfield_name,'g');
+            var subfields = $(field_start).children('div').filter( function() {
+                return this.id.match(re);
+            });
+
+            // Add as many clones as needed
+            for(var i=0; i<nb-subfields.length; i++) {
+                window.opener.opener.CloneSubfield(subfields[0].getAttribute('id'));
+            }
+        }
+
+        // Fills the subfield with the values entered in argument
+        function SetSubfieldValues() {
+            // Get the arguments
+            var subfield_name = arguments[0];
+            var values = new Array();
+            for(var i=1; i<arguments.length; i++) {
+                values.push(arguments[i]);
+            }
+
+            // Create the correct number of form fields for all values
+            SetSubfieldNumber(subfield_name, values.length);
+            
+            // Find the subfields where we will add the new values
+            var re = new RegExp('^subfield' + subfield_name,'g');
+            var subfields = $(field_start).children('div').filter( function() {
+                return this.id.match(re);
+            });
+
+            // Add the new values to those subfields, empty the additional fields
+            var i=0;
+            subfields.each(function() {
+                if(i in values) {
+                    this.getElementsByTagName('input')[1].value = values[i];
+                }
+                else {
+                    this.getElementsByTagName('input')[1].value = "";
+                }
+                i++;
+            });
+        }
+
+        [% FOREACH SUBFIELD_LOO IN SUBFIELD_LOOP %]
+            SetSubfieldValues(
+                "[% tag_number |replace("'", "\'") |replace('"', '\"') |replace('\n', '\\n') |replace('\r', '\\r') %][% SUBFIELD_LOO.marc_subfield |replace("'", "\'") |replace('"', '\"') |replace('\n', '\\n') |replace('\r', '\\r') %]"
+            [% FOREACH marc_value IN SUBFIELD_LOO.marc_values %]
+                ,"[% marc_value |replace("'", "\'") |replace('"', '\"') |replace('\n', '\\n') |replace('\r', '\\r') %]"
+            [% END %]
+            );
+        [% END %]
+
        opener.close();
                window.close();