Bug 18318: Unicode support for Elasticsearch
[koha.git] / cataloguing / additem.pl
index 9f0f146..94b971f 100755 (executable)
@@ -31,7 +31,10 @@ use C4::Circulation;
 use C4::Koha;
 use C4::ClassSource;
 use Koha::DateUtils;
+use Koha::Items;
+use Koha::ItemTypes;
 use Koha::Libraries;
+use Koha::Patrons;
 use List::MoreUtils qw/any/;
 use C4::Search;
 use Storable qw(thaw freeze);
@@ -130,11 +133,11 @@ sub generate_subfield_form {
             $value = $subfieldlib->{defaultvalue};
             # get today date & replace <<YYYY>>, <<MM>>, <<DD>> if provided in the default value
             my $today_dt = dt_from_string;
-            my $year = $today_dt->year;
-            my $month = $today_dt->month;
-            my $day = $today_dt->day;
+            my $year = $today_dt->strftime('%Y');
+            my $month = $today_dt->strftime('%m');
+            my $day = $today_dt->strftime('%d');
             $value =~ s/<<YYYY>>/$year/g;
-            $value =~ s/<<MM></$month/g;
+            $value =~ s/<<MM>>/$month/g;
             $value =~ s/<<DD>>/$day/g;
             # And <<USER>> with surname (?)
             my $username=(C4::Context->userenv?C4::Context->userenv->{'surname'}:"superlibrarian");
@@ -161,14 +164,6 @@ sub generate_subfield_form {
            $value = $input->param('barcode');
        }
 
-        # Getting list of subfields to keep when restricted editing is enabled
-        my $subfieldsToAllowForRestrictedEditing = C4::Context->preference('SubfieldsToAllowForRestrictedEditing');
-        my $allowAllSubfields = (
-            not defined $subfieldsToAllowForRestrictedEditing
-              or $subfieldsToAllowForRestrictedEditing == q||
-        ) ? 1 : 0;
-        my @subfieldsToAllow = split(/ /, $subfieldsToAllowForRestrictedEditing);
-
         if ( $subfieldlib->{authorised_value} ) {
             my @authorised_values;
             my %authorised_lib;
@@ -182,10 +177,10 @@ sub generate_subfield_form {
             }
             elsif ( $subfieldlib->{authorised_value} eq "itemtypes" ) {
                   push @authorised_values, "" unless ( $subfieldlib->{mandatory} );
-                  my $itemtypes = GetItemTypes( style => 'array' );
-                  for my $itemtype ( @$itemtypes ) {
-                      push @authorised_values, $itemtype->{itemtype};
-                      $authorised_lib{$itemtype->{itemtype}} = $itemtype->{translated_description};
+                  my $itemtypes = Koha::ItemTypes->search_with_localization;
+                  while ( my $itemtype = $itemtypes->next ) {
+                      push @authorised_values, $itemtype->itemtype;
+                      $authorised_lib{$itemtype->itemtype} = $itemtype->translated_description;
                   }
 
                   unless ( $value ) {
@@ -238,13 +233,6 @@ sub generate_subfield_form {
                     labels   => \%authorised_lib,
                     default  => $value,
                 };
-                # If we're on restricted editing, and our field is not in the list of subfields to allow,
-                # then it is read-only
-                $subfield_data{marc_value}->{readonlyselect} = (
-                    not $allowAllSubfields
-                    and $restrictededition
-                    and !grep { $tag . '$' . $subfieldtag  eq $_ } @subfieldsToAllow
-                ) ? 1: 0;
             }
         }
             # it's a thesaurus / authority field
@@ -330,7 +318,23 @@ sub generate_subfield_form {
                 value       => $value,
             };
         }
-        
+
+        # Getting list of subfields to keep when restricted editing is enabled
+        my $subfieldsToAllowForRestrictedEditing = C4::Context->preference('SubfieldsToAllowForRestrictedEditing');
+        my $allowAllSubfields = (
+            not defined $subfieldsToAllowForRestrictedEditing
+              or $subfieldsToAllowForRestrictedEditing == q||
+        ) ? 1 : 0;
+        my @subfieldsToAllow = split(/ /, $subfieldsToAllowForRestrictedEditing);
+
+        # If we're on restricted editing, and our field is not in the list of subfields to allow,
+        # then it is read-only
+        $subfield_data{marc_value}->{readonly} = (
+            not $allowAllSubfields
+            and $restrictededition
+            and !grep { $tag . '$' . $subfieldtag  eq $_ } @subfieldsToAllow
+        ) ? 1: 0;
+
         return \%subfield_data;
 }
 
@@ -400,7 +404,7 @@ my ($template, $loggedinuser, $cookie)
 
 
 # Does the user have a restricted item editing permission?
-my $uid = $loggedinuser ? GetMember( borrowernumber => $loggedinuser )->{userid} : undef;
+my $uid = Koha::Patrons->find( $loggedinuser )->userid;
 my $restrictededition = $uid ? haspermission($uid,  {'editcatalogue' => 'edit_items_restricted'}) : undef;
 # In case user is a superlibrarian, editing is not restricted
 $restrictededition = 0 if ($restrictededition != 0 &&  C4::Context->IsSuperLibrarian());
@@ -408,7 +412,7 @@ $restrictededition = 0 if ($restrictededition != 0 &&  C4::Context->IsSuperLibra
 $restrictededition = 0 if ($restrictededition != 0 && $frameworkcode eq 'FA' && haspermission($uid, {'editcatalogue' => 'fast_cataloging'}));
 
 my $tagslib = &GetMarcStructure(1,$frameworkcode);
-my $record = GetMarcBiblio($biblionumber);
+my $record = GetMarcBiblio({ biblionumber => $biblionumber });
 my $oldrecord = TransformMarcToKoha($record);
 my $itemrecord;
 my $nextop="additem";
@@ -527,7 +531,7 @@ if ($op eq "additem") {
         my $oldbarcode = $addedolditem->{'barcode'};
         my ($tagfield,$tagsubfield) = &GetMarcFromKohaField("items.barcode",$frameworkcode);
 
-       # If there is a barcode and we can't find him new values, we can't add multiple copies
+    # If there is a barcode and we can't find their new values, we can't add multiple copies
        my $testbarcode;
         $testbarcode = $barcodeobj->next_value($oldbarcode) if $barcodeobj;
        if ($oldbarcode && !$testbarcode) {
@@ -643,41 +647,28 @@ if ($op eq "additem") {
 #-------------------------------------------------------------------------------
 } elsif ($op eq "delallitems") {
 #-------------------------------------------------------------------------------
-    my @biblioitems = &GetBiblioItemByBiblioNumber($biblionumber);
-    my $errortest=0;
-    my $itemfail;
-    foreach my $biblioitem (@biblioitems) {
-        my $items = &GetItemsByBiblioitemnumber( $biblioitem->{biblioitemnumber} );
-
-        foreach my $item (@$items) {
-            $error =&DelItemCheck( $biblionumber, $item->{itemnumber} );
-            $itemfail =$item;
-        if($error == 1){
-            next
-            }
-        else {
-            push @errors,$error;
-            $errortest++
-            }
-        }
-        if($errortest > 0){
-            $nextop="additem";
-        } 
-        else {
-            my $defaultview = C4::Context->preference('IntranetBiblioDefaultView');
-            my $views = { C4::Search::enabled_staff_search_views };
-            if ($defaultview eq 'isbd' && $views->{can_view_ISBD}) {
-                print $input->redirect("/cgi-bin/koha/catalogue/ISBDdetail.pl?biblionumber=$biblionumber&searchid=$searchid");
-            } elsif  ($defaultview eq 'marc' && $views->{can_view_MARC}) {
-                print $input->redirect("/cgi-bin/koha/catalogue/MARCdetail.pl?biblionumber=$biblionumber&searchid=$searchid");
-            } elsif  ($defaultview eq 'labeled_marc' && $views->{can_view_labeledMARC}) {
-                print $input->redirect("/cgi-bin/koha/catalogue/labeledMARCdetail.pl?biblionumber=$biblionumber&searchid=$searchid");
-            } else {
-                print $input->redirect("/cgi-bin/koha/catalogue/detail.pl?biblionumber=$biblionumber&searchid=$searchid");
-            }
-            exit;
+    my $itemnumbers = C4::Items::GetItemnumbersForBiblio( $biblionumber );
+    foreach my $itemnumber ( @$itemnumbers ) {
+        $error = C4::Items::DelItemCheck( $biblionumber, $itemnumber );
+        next if $error == 1; # Means ok
+        push @errors,$error;
+    }
+    if ( @errors ) {
+        $nextop="additem";
+    } else {
+        my $defaultview = C4::Context->preference('IntranetBiblioDefaultView');
+        my $views = { C4::Search::enabled_staff_search_views };
+        if ($defaultview eq 'isbd' && $views->{can_view_ISBD}) {
+            print $input->redirect("/cgi-bin/koha/catalogue/ISBDdetail.pl?biblionumber=$biblionumber&searchid=$searchid");
+        } elsif  ($defaultview eq 'marc' && $views->{can_view_MARC}) {
+            print $input->redirect("/cgi-bin/koha/catalogue/MARCdetail.pl?biblionumber=$biblionumber&searchid=$searchid");
+        } elsif  ($defaultview eq 'labeled_marc' && $views->{can_view_labeledMARC}) {
+            print $input->redirect("/cgi-bin/koha/catalogue/labeledMARCdetail.pl?biblionumber=$biblionumber&searchid=$searchid");
+        } else {
+            print $input->redirect("/cgi-bin/koha/catalogue/detail.pl?biblionumber=$biblionumber&searchid=$searchid");
         }
-       }
+        exit;
+    }
 #-------------------------------------------------------------------------------
 } elsif ($op eq "saveitem") {
 #-------------------------------------------------------------------------------
@@ -734,7 +725,7 @@ if ($op eq "additem") {
 #-------------------------------------------------------------------------------
 
 # now, build existiing item list
-my $temp = GetMarcBiblio( $biblionumber );
+my $temp = GetMarcBiblio({ biblionumber => $biblionumber });
 #my @fields = $record->fields();
 
 
@@ -758,7 +749,9 @@ if ( C4::Context->preference('EasyAnalyticalRecords') ) {
     foreach my $hostfield ($temp->field($analyticfield)){
         my $hostbiblionumber = $hostfield->subfield('0');
         if ($hostbiblionumber){
-            my $hostrecord = GetMarcBiblio($hostbiblionumber, 1);
+            my $hostrecord = GetMarcBiblio({
+                biblionumber => $hostbiblionumber,
+                embed_items  => 1 });
             if ($hostrecord) {
                 my ($itemfield, undef) = GetMarcFromKohaField( 'items.itemnumber', GetFrameworkCode($hostbiblionumber) );
                 foreach my $hostitem ($hostrecord->field($itemfield)){
@@ -806,9 +799,10 @@ foreach my $field (@fields) {
 
        if ( C4::Context->preference('EasyAnalyticalRecords') ) {
            foreach my $hostitemnumber (@hostitemnumbers){
+            my $item = Koha::Items->find( $hostitemnumber );
                if ($this_row{itemnumber} eq $hostitemnumber){
                        $this_row{hostitemflag} = 1;
-                       $this_row{hostbiblionumber}= GetBiblionumberFromItemnumber($hostitemnumber);
+            $this_row{hostbiblionumber}= $item->biblio->biblionumber;
                        last;
                }
            }