Bug 31028: (follow-up) Fix permissions names
[srvgit] / cataloguing / addbiblio.pl
index 9a50ce0..9711a89 100755 (executable)
@@ -28,7 +28,6 @@ use C4::Biblio qw(
     AddBiblio
     DelBiblio
     GetFrameworkCode
-    GetMarcBiblio
     GetMarcFromKohaField
     GetMarcStructure
     GetUsedMarcStructure
@@ -47,6 +46,7 @@ use C4::Charset qw( SetMarcUnicodeFlag );
 use Koha::BiblioFrameworks;
 use Koha::DateUtils qw( dt_from_string );
 
+use Koha::Biblios;
 use Koha::ItemTypes;
 use Koha::Libraries;
 
@@ -82,6 +82,10 @@ sub MARCfindbreeding {
     # remove the - in isbn, koha store isbn without any -
     if ($marc) {
         my $record = MARC::Record->new_from_usmarc($marc);
+        if(C4::Context->preference('autoControlNumber') eq 'biblionumber'){
+            my @control_num = $record->field('001');
+            $record->delete_fields(@control_num);
+        }
         my ($isbnfield,$isbnsubfield) = GetMarcFromKohaField( 'biblioitems.isbn' );
         if ( $record->field($isbnfield) ) {
             foreach my $field ( $record->field($isbnfield) ) {
@@ -285,11 +289,22 @@ sub GetMandatoryFieldZ3950 {
 
 sub create_input {
     my ( $tag, $subfield, $value, $index_tag, $rec, $authorised_values_sth,$cgi ) = @_;
-    
+
     my $index_subfield = CreateKey(); # create a specifique key for each subfield
 
-    # if there is no value provided but a default value in parameters, get it
-    if ( $value eq '' ) {
+    # Apply optional framework default value when it is a new record,
+    # or when editing as new (duplicating a record),
+    # or when changing a record's framework,
+    # or when importing a record,
+    # based on the ApplyFrameworkDefaults setting.
+    # Substitute date parts, user name
+    my $applydefaults = C4::Context->preference('ApplyFrameworkDefaults');
+    if ( $value eq '' && (
+        ( $applydefaults =~ /new/ && !$cgi->param('biblionumber') ) ||
+        ( $applydefaults =~ /duplicate/ && $cgi->param('op') eq 'duplicate' ) ||
+        ( $applydefaults =~ /changed/ && $cgi->param('changed_framework') ) ||
+        ( $applydefaults =~ /imported/ && $cgi->param('breedingid') )
+    ) ) {
         $value = $tagslib->{$tag}->{$subfield}->{defaultvalue} // q{};
 
         # get today date & replace <<YYYY>>, <<YY>>, <<MM>>, <<DD>> if provided in the default value
@@ -305,8 +320,8 @@ sub create_input {
         # And <<USER>> with surname (?)
         my $username=(C4::Context->userenv?C4::Context->userenv->{'surname'}:"superlibrarian");
         $value=~s/<<USER>>/$username/g;
-    
     }
+
     my $dbh = C4::Context->dbh;
 
     # map '@' as "subfield" label for fixed fields
@@ -375,7 +390,7 @@ sub create_input {
     }
     elsif ( $tagslib->{$tag}->{$subfield}->{authtypecode} ) {
         # when authorities auto-creation is allowed, do not set readonly
-        my $is_readonly = !C4::Context->preference("BiblioAddsAuthorities");
+        my $is_readonly = C4::Context->preference("RequireChoosingExistingAuthority");
 
         $subfield_data{marc_value} = {
             type      => 'text',
@@ -591,7 +606,7 @@ sub build_tabs {
                     }
 
                     # now, loop again to add parameter subfield that are not in the MARC::Record
-                    foreach my $subfield ( keys %{ $tagslib->{$tag} } )
+                    foreach my $subfield ( sort( keys %{ $tagslib->{$tag} } ) )
                     {
                         next if ( length $subfield != 1 );
                         next if ( $tagslib->{$tag}->{$subfield}->{tab} ne $tabloop );
@@ -714,7 +729,7 @@ my $parentbiblio  = $input->param('parentbiblionumber');
 my $breedingid    = $input->param('breedingid');
 my $z3950         = $input->param('z3950');
 my $op            = $input->param('op') // q{};
-my $mode          = $input->param('mode');
+my $mode          = $input->param('mode') // q{};
 my $frameworkcode = $input->param('frameworkcode');
 my $redirect      = $input->param('redirect');
 my $searchid      = $input->param('searchid') // "";
@@ -748,9 +763,10 @@ my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
     }
 );
 
+my $biblio;
 if ($biblionumber){
-    my $does_bib_exist = Koha::Biblios->find($biblionumber);
-    if (!defined $does_bib_exist){
+    $biblio = Koha::Biblios->find($biblionumber);
+    unless ( $biblio ) {
         $biblionumber = undef;
         $template->param( bib_doesnt_exist => 1 );
     }
@@ -799,13 +815,17 @@ my (
        $biblioitemnumber
 );
 
-if (($biblionumber) && !($breedingid)){
-    $record = GetMarcBiblio({ biblionumber => $biblionumber });
+if ( $biblio && !$breedingid ) {
+    $record = $biblio->metadata->record;
 }
 if ($breedingid) {
     ( $record, $encoding ) = MARCfindbreeding( $breedingid ) ;
 }
-
+if ( $record && $op eq 'duplicate' &&
+     C4::Context->preference('autoControlNumber') eq 'biblionumber' ){
+    my @control_num = $record->field('001');
+    $record->delete_fields(@control_num);
+}
 #populate hostfield if hostbiblionumber is available
 if ($hostbiblionumber) {
     my $marcflavour = C4::Context->preference("marcflavour");
@@ -866,11 +886,15 @@ if ( $op eq "addbiblio" ) {
         my $oldbibitemnum;
         if ( $is_a_modif ) {
             my $member = Koha::Patrons->find($loggedinuser);
-            ModBiblio( $record, $biblionumber, $frameworkcode, {
-                    context => {
-                        source => $z3950 ? 'z39.50' : 'intranet',
+            ModBiblio(
+                $record,
+                $biblionumber,
+                $frameworkcode,
+                {
+                    overlay_context => {
+                        source       => $z3950 ? 'z39.50' : 'intranet',
                         categorycode => $member->categorycode,
-                        userid => $member->userid
+                        userid       => $member->userid
                     }
                 }
             );