From 32d5382ea1cc73c7aa3ae4c21c44f86097406865 Mon Sep 17 00:00:00 2001
From: Bouzid Fergani <bouzid.fergani@inlibro.com>
Date: Wed, 7 Jun 2017 07:15:55 -0400
Subject: [PATCH] Bug11299 - Prepopulate the fields of new authority by the
 values writed in biblio

Signed-off-by: Katrin Fischer <katrin.fischer.83@web.de>
---
 C4/Biblio.pm                                       |  6 +--
 authorities/authorities.pl                         |  3 ++
 .../prog/en/modules/cataloguing/addbiblio.tt       | 47 ++++++++++++++++++++--
 3 files changed, 50 insertions(+), 6 deletions(-)

diff --git a/C4/Biblio.pm b/C4/Biblio.pm
index 3e8a012c05..cf66c19960 100644
--- a/C4/Biblio.pm
+++ b/C4/Biblio.pm
@@ -538,6 +538,7 @@ sub LinkBibHeadingsToAuthorities {
             push(@{$results{'details'}}, { tag => $field->tag(), authid => $authid, status => $status || 'LOCAL_FOUND'}) if $verbose;
         }
         else {
+            my $authority_type = Koha::Authority::Types->find( $heading->auth_type() );
             if ( defined $current_link
                 && (!$allowrelink || C4::Context->preference('LinkerKeepStale')) )
             {
@@ -549,7 +550,6 @@ sub LinkBibHeadingsToAuthorities {
                     $results{'linked'}->{ $heading->display_form() }++;
                 }
                 else {
-                    my $authority_type = Koha::Authority::Types->find( $heading->auth_type() );
                     my $marcrecordauth = MARC::Record->new();
                     if ( C4::Context->preference('marcflavour') eq 'MARC21' ) {
                         $marcrecordauth->leader('     nz  a22     o  4500');
@@ -619,12 +619,12 @@ sub LinkBibHeadingsToAuthorities {
                     $field->delete_subfield( code => '9' );
                     $num_headings_changed++;
                     $results{'unlinked'}->{ $heading->display_form() }++;
-                    push(@{$results{'details'}}, { tag => $field->tag(), authid => undef, status => 'NONE_FOUND', auth_type => $heading->auth_type()}) if $verbose;
+                    push(@{$results{'details'}}, { tag => $field->tag(), authid => undef, status => 'NONE_FOUND', auth_type => $heading->auth_type(), tag_to_report => $authority_type->auth_tag_to_report}) if $verbose;
                 }
             }
             else {
                 $results{'unlinked'}->{ $heading->display_form() }++;
-                push(@{$results{'details'}}, { tag => $field->tag(), authid => undef, status => 'NONE_FOUND', auth_type => $heading->auth_type()}) if $verbose;
+                push(@{$results{'details'}}, { tag => $field->tag(), authid => undef, status => 'NONE_FOUND', auth_type => $heading->auth_type(), tag_to_report => $authority_type->auth_tag_to_report}) if $verbose;
             }
         }
 
diff --git a/authorities/authorities.pl b/authorities/authorities.pl
index ff87131a46..49546cafad 100755
--- a/authorities/authorities.pl
+++ b/authorities/authorities.pl
@@ -289,6 +289,9 @@ sub create_input {
 
         }
     }
+    if ($cgi->param('tagreport') && $subfield_data{tag} == $cgi->param('tagreport')) {
+        $subfield_data{marc_value}{value} = $cgi->param('tag'. $cgi->param('tagbiblio') . 'subfield' . $subfield_data{subfield});
+    }
     $subfield_data{'index_subfield'} = $index_subfield;
     return \%subfield_data;
 }
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/cataloguing/addbiblio.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/cataloguing/addbiblio.tt
index aed9a4fc19..1b13e282f0 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/cataloguing/addbiblio.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/cataloguing/addbiblio.tt
@@ -172,11 +172,52 @@ function PopupZ3950() {
 * this function append button for create new authority if not found
 */
 
-function addCreateAuthorityButton(tag_subfield_line, auth_type) {
+function addCreateAuthorityButton(tag_subfield_line, heading, tag_index) {
     var title = _("Create authority");
     var elem = $('<a class="subfield_status" href="#"><img src="[% interface %]/[% theme %]/img/edit-tag.png" title="' + title + '" /></a>');
-    elem.attr("onclick","window.open('../authorities/authorities.pl?authtypecode=" + auth_type  + "','','fullscreen','scrollbars')");
     tag_subfield_line.append(elem);
+    var tag_subfield_line_a = $('.subfield_line[id^=subfield' + heading.tag + 'a]').eq(tag_index);
+    var subfield_a = tag_subfield_line_a.children('.input_marceditor').eq(0);
+    elem.click(function() {
+        var popup = window.open("", "new_auth_popup",'fullscreen,toolbar=false,scrollbars=yes');
+        if(popup !== null) {
+            // Create a new form that will be POSTed in the new window
+            var form = $('<form>').attr({
+            method: 'post',
+            action: "../authorities/authorities.pl",
+            target: "new_auth_popup"
+            });
+            //add the authtypecode
+            form.append($('<input>').attr({
+                type: 'hidden',
+                name: 'authtypecode',
+                value: heading.auth_type
+            }));
+            form.append($('<input>').attr({
+                type: 'hidden',
+                name: 'tagreport',
+                value: heading.tag_to_report
+            }));
+            form.append($('<input>').attr({
+                type: 'hidden',
+                name: 'tagbiblio',
+                value: heading.tag
+            }));
+            $('.tag[id^=tag_' + heading.tag + '_]').eq(tag_index).find(':input').each(function(){
+                form.append($('<input>').attr({
+                    type: 'hidden',
+                    name: this.name.split('_',4).join(''),
+                    value: $(this).val()
+                }));
+            });
+
+            $('body').append(form);
+            form.submit();
+            form.remove();
+        }
+        return false;
+    });
+
 }
 
 /**
@@ -253,7 +294,7 @@ function updateHeadingLinks(links) {
 
         // Add a link to create a new authority if none was found
         if(heading.status == 'NONE_FOUND') {
-            addCreateAuthorityButton(tag_subfield_line, heading.auth_type);
+            addCreateAuthorityButton(tag_subfield_line, heading , tag_index);
         }
     });
 
-- 
2.11.0