Bug 7295: More granular permissions for baskets
[koha.git] / cataloguing / additem.pl
index 43fb19f..a819019 100755 (executable)
@@ -1,8 +1,8 @@
 #!/usr/bin/perl
 
-
 # Copyright 2000-2002 Katipo Communications
 # Copyright 2004-2010 BibLibre
+# Parts Copyright Catalyst IT 2011
 #
 # This file is part of Koha.
 #
@@ -27,15 +27,21 @@ use C4::Output;
 use C4::Biblio;
 use C4::Items;
 use C4::Context;
+use C4::Circulation;
 use C4::Koha; # XXX subfield_is_koha_internal_p
 use C4::Branch; # XXX subfield_is_koha_internal_p
 use C4::ClassSource;
 use C4::Dates;
 use List::MoreUtils qw/any/;
+use C4::Search;
+use Storable qw(thaw freeze);
+use URI::Escape;
+
 
 use MARC::File::XML;
+use URI::Escape;
 
-my $dbh = C4::Context->dbh;
+our $dbh = C4::Context->dbh;
 
 sub find_value {
     my ($tagfield,$insubfield,$record) = @_;
@@ -65,12 +71,16 @@ sub get_item_from_barcode {
 
 sub set_item_default_location {
     my $itemnumber = shift;
+    my $item = GetItem( $itemnumber );
     if ( C4::Context->preference('NewItemsDefaultLocation') ) {
-        my $item = GetItem( $itemnumber );
         $item->{'permanent_location'} = $item->{'location'};
         $item->{'location'} = C4::Context->preference('NewItemsDefaultLocation');
         ModItem( $item, undef, $itemnumber);
     }
+    else {
+      $item->{'permanent_location'} = $item->{'location'} if !defined($item->{'permanent_location'});
+      ModItem( $item, undef, $itemnumber);
+    }
 }
 
 # NOTE: This code is subject to change in the future with the implemenation of ajax based autobarcode code
@@ -95,10 +105,10 @@ sub _increment_barcode {
 
 sub generate_subfield_form {
         my ($tag, $subfieldtag, $value, $tagslib,$subfieldlib, $branches, $today_iso, $biblionumber, $temp, $loop_data, $i) = @_;
-        
+  
+  my $frameworkcode = &GetFrameworkCode($biblionumber);
         my %subfield_data;
-        my $dbh = C4::Context->dbh;        
-        my $authorised_values_sth = $dbh->prepare("SELECT authorised_value,lib FROM authorised_values WHERE category=? ORDER BY lib");
+        my $dbh = C4::Context->dbh;
         
         my $index_subfield = int(rand(1000000)); 
         if ($subfieldtag eq '@'){
@@ -113,6 +123,7 @@ sub generate_subfield_form {
         $subfield_data{marc_lib}   ="<span id=\"error$i\" title=\"".$subfieldlib->{lib}."\">".$subfieldlib->{lib}."</span>";
         $subfield_data{mandatory}  = $subfieldlib->{mandatory};
         $subfield_data{repeatable} = $subfieldlib->{repeatable};
+        $subfield_data{maxlength}  = $subfieldlib->{maxlength};
         
         $value =~ s/"/&quot;/g;
         if ( ! defined( $value ) || $value eq '')  {
@@ -139,7 +150,12 @@ sub generate_subfield_form {
             }
         }
         
-        my $attributes_no_value = qq(tabindex="1" id="$subfield_data{id}" name="field_value" class="input_marceditor" size="67" maxlength="255" );
+        if ($frameworkcode eq 'FA' && $subfieldlib->{kohafield} eq 'items.barcode' && !$value){
+           my $input = new CGI;
+           $value = $input->param('barcode');
+       }
+        my $attributes_no_value = qq(tabindex="1" id="$subfield_data{id}" name="field_value" class="input_marceditor" size="67" maxlength="$subfield_data{maxlength}" );
+        my $attributes_no_value_textarea = qq(tabindex="1" id="$subfield_data{id}" name="field_value" class="input_marceditor" rows="5" cols="64" );
         my $attributes          = qq($attributes_no_value value="$value" );
         
         if ( $subfieldlib->{authorised_value} ) {
@@ -188,36 +204,41 @@ sub generate_subfield_form {
                   #---- "true" authorised value
             }
             else {
-                  push @authorised_values, "" unless ( $subfieldlib->{mandatory} );
-                  $authorised_values_sth->execute( $subfieldlib->{authorised_value} );
-                  while ( my ( $value, $lib ) = $authorised_values_sth->fetchrow_array ) {
-                      push @authorised_values, $value;
-                      $authorised_lib{$value} = $lib;
+                  push @authorised_values, qq{} unless ( $subfieldlib->{mandatory} );
+                  my $av = GetAuthorisedValues( $subfieldlib->{authorised_value} );
+                  for my $r ( @$av ) {
+                      push @authorised_values, $r->{authorised_value};
+                      $authorised_lib{$r->{authorised_value}} = $r->{lib};
                   }
             }
 
-            $subfield_data{marc_value} =CGI::scrolling_list(      # FIXME: factor out scrolling_list
-                  -name     => "field_value",
-                  -values   => \@authorised_values,
-                  -default  => $value,
-                  -labels   => \%authorised_lib,
-                  -override => 1,
-                  -size     => 1,
-                  -multiple => 0,
-                  -tabindex => 1,
-                  -id       => "tag_".$tag."_subfield_".$subfieldtag."_".$index_subfield,
-                  -class    => "input_marceditor",
-            );
+            if ($subfieldlib->{'hidden'}) {
+                $subfield_data{marc_value} = qq(<input type="hidden" $attributes /> $authorised_lib{$value});
+            }
+            else {
+                $subfield_data{marc_value} =CGI::scrolling_list(      # FIXME: factor out scrolling_list
+                    -name     => "field_value",
+                    -values   => \@authorised_values,
+                    -default  => $value,
+                    -labels   => \%authorised_lib,
+                    -override => 1,
+                    -size     => 1,
+                    -multiple => 0,
+                    -tabindex => 1,
+                    -id       => "tag_".$tag."_subfield_".$subfieldtag."_".$index_subfield,
+                    -class    => "input_marceditor",
+                );
+            }
 
-            # it's a thesaurus / authority field
         }
+            # it's a thesaurus / authority field
         elsif ( $subfieldlib->{authtypecode} ) {
                 $subfield_data{marc_value} = "<input type=\"text\" $attributes />
                     <a href=\"#\" class=\"buttonDot\"
                         onclick=\"Dopop('/cgi-bin/koha/authorities/auth_finder.pl?authtypecode=".$subfieldlib->{authtypecode}."&index=$subfield_data{id}','$subfield_data{id}'); return false;\" title=\"Tag Editor\">...</a>
             ";
-            # it's a plugin field
         }
+            # it's a plugin field
         elsif ( $subfieldlib->{value_builder} ) {
                 # opening plugin
                 my $plugin = C4::Context->intranetdir . "/cataloguing/value_builder/" . $subfieldlib->{'value_builder'};
@@ -227,7 +248,7 @@ sub generate_subfield_form {
                    my $change = index($javascript, 'function Change') > -1 ?
                        "return Change$function_name($subfield_data{random}, '$subfield_data{id}');" :
                        'return 1;';
-                    $subfield_data{marc_value} = qq[<input $attributes
+                    $subfield_data{marc_value} = qq[<input type="text" $attributes
                         onfocus="Focus$function_name($subfield_data{random}, '$subfield_data{id}');"
                        onchange=" $change"
                          onblur=" Blur$function_name($subfield_data{random}, '$subfield_data{id}');" />
@@ -235,7 +256,7 @@ sub generate_subfield_form {
                         $javascript];
                 } else {
                     warn "Plugin Failed: $plugin";
-                    $subfield_data{marc_value} = "<input $attributes />"; # supply default input form
+                    $subfield_data{marc_value} = "<input type=\"text\" $attributes />"; # supply default input form
                 }
         }
         elsif ( $tag eq '' ) {       # it's an hidden field
@@ -251,22 +272,57 @@ sub generate_subfield_form {
                           500 <= $tag && $tag < 600                     )
                   ) {
             # oversize field (textarea)
-            $subfield_data{marc_value} = "<textarea $attributes_no_value>$value</textarea>\n";
+            $subfield_data{marc_value} = "<textarea $attributes_no_value_textarea>$value</textarea>\n";
         } else {
            # it's a standard field
-           $subfield_data{marc_value} = "<input $attributes />";
+           $subfield_data{marc_value} = "<input type=\"text\" $attributes />";
         }
         
         return \%subfield_data;
 }
 
+# Removes some subfields when prefilling items
+# This function will remove any subfield that is not in the SubfieldsToUseWhenPrefill syspref
+sub removeFieldsForPrefill {
+
+    my $item = shift;
+
+    # Getting item tag
+    my ($tag, $subtag) = GetMarcFromKohaField("items.barcode", '');
+
+    # Getting list of subfields to keep
+    my $subfieldsToUseWhenPrefill = C4::Context->preference('SubfieldsToUseWhenPrefill');
+
+    # Removing subfields that are not in the syspref
+    if ($tag && $subfieldsToUseWhenPrefill) {
+        my $field = $item->field($tag);
+        my @subfieldsToUse= split(/ /,$subfieldsToUseWhenPrefill);
+        foreach my $subfield ($field->subfields()) {
+            if (!grep { $subfield->[0] eq $_ } @subfieldsToUse) {
+                $field->delete_subfield(code => $subfield->[0]);
+            }
+
+        }
+    }
+
+    return $item;
+
+}
 
 my $input        = new CGI;
-my $dbh          = C4::Context->dbh;
 my $error        = $input->param('error');
 my $biblionumber = $input->param('biblionumber');
 my $itemnumber   = $input->param('itemnumber');
 my $op           = $input->param('op');
+my $hostitemnumber = $input->param('hostitemnumber');
+my $marcflavour  = C4::Context->preference("marcflavour");
+my $searchid     = $input->param('searchid');
+# fast cataloguing datas
+my $fa_circborrowernumber = $input->param('circborrowernumber');
+my $fa_barcode            = $input->param('barcode');
+my $fa_branch             = $input->param('branch');
+my $fa_stickyduedate      = $input->param('stickyduedate');
+my $fa_duedatespec        = $input->param('duedatespec');
 
 my $frameworkcode = &GetFrameworkCode($biblionumber);
 
@@ -281,7 +337,7 @@ if (not defined $userflags) {
 }
 
 my ($template, $loggedinuser, $cookie)
-    = get_template_and_user({template_name => "cataloguing/additem.tmpl",
+    = get_template_and_user({template_name => "cataloguing/additem.tt",
                  query => $input,
                  type => "intranet",
                  authnotrequired => 0,
@@ -291,17 +347,32 @@ my ($template, $loggedinuser, $cookie)
 
 
 my $today_iso = C4::Dates->today('iso');
-$template->param(today_iso => $today_iso);
-
 my $tagslib = &GetMarcStructure(1,$frameworkcode);
 my $record = GetMarcBiblio($biblionumber);
 my $oldrecord = TransformMarcToKoha($dbh,$record);
 my $itemrecord;
 my $nextop="additem";
 my @errors; # store errors found while checking data BEFORE saving item.
+
+# Getting last created item cookie
+my $prefillitem = C4::Context->preference('PrefillItem');
+my $justaddeditem;
+my $cookieitemrecord;
+if ($prefillitem) {
+    my $lastitemcookie = $input->cookie('LastCreatedItem');
+    if ($lastitemcookie) {
+        $lastitemcookie = uri_unescape($lastitemcookie);
+        if ( thaw($lastitemcookie) ) {
+            $cookieitemrecord = thaw($lastitemcookie) ;
+            $cookieitemrecord = removeFieldsForPrefill($cookieitemrecord);
+        }
+    }
+}
+
 #-------------------------------------------------------------------------------
 if ($op eq "additem") {
-#-------------------------------------------------------------------------------
+
+    #-------------------------------------------------------------------------------
     # rebuild
     my @tags      = $input->param('tag');
     my @subfields = $input->param('subfield');
@@ -318,26 +389,51 @@ if ($op eq "additem") {
     my $add_multiple_copies_submit = $input->param('add_multiple_copies_submit');
     my $number_of_copies           = $input->param('number_of_copies');
 
-    if (C4::Context->preference('autoBarcode') eq 'incremental') {
+    # This is a bit tricky : if there is a cookie for the last created item and
+    # we just added an item, the cookie value is not correct yet (it will be updated
+    # next page). To prevent the form from being filled with outdated values, we
+    # force the use of "add and duplicate" feature, so the form will be filled with
+    # correct values.
+    $add_duplicate_submit = 1 if ($prefillitem);
+    $justaddeditem = 1;
+
+    # if autoBarcode is set to 'incremental', calculate barcode...
+    if ( C4::Context->preference('autoBarcode') eq 'incremental' ) {
         $record = _increment_barcode($record, $frameworkcode);
     }
 
-    my $addedolditem = TransformMarcToKoha($dbh,$record);
+    my $addedolditem = TransformMarcToKoha( $dbh, $record );
 
     # If we have to add or add & duplicate, we add the item
-    if ($add_submit || $add_duplicate_submit) {
-       # check for item barcode # being unique
-       my $exist_itemnumber = get_item_from_barcode($addedolditem->{'barcode'});
-       push @errors,"barcode_not_unique" if($exist_itemnumber);
-       # if barcode exists, don't create, but report The problem.
-    unless ($exist_itemnumber) {
-           my ($oldbiblionumber,$oldbibnum,$oldbibitemnum) = AddItemFromMarc($record,$biblionumber);
-        set_item_default_location($oldbibitemnum);
-    }
-       $nextop = "additem";
-       if ($exist_itemnumber) {
-           $itemrecord = $record;
-       }
+    if ( $add_submit || $add_duplicate_submit ) {
+
+        # check for item barcode # being unique
+        my $exist_itemnumber = get_item_from_barcode( $addedolditem->{'barcode'} );
+        push @errors, "barcode_not_unique" if ($exist_itemnumber);
+
+        # if barcode exists, don't create, but report The problem.
+        unless ($exist_itemnumber) {
+            my ( $oldbiblionumber, $oldbibnum, $oldbibitemnum ) = AddItemFromMarc( $record, $biblionumber );
+            set_item_default_location($oldbibitemnum);
+
+            # Pushing the last created item cookie back
+            if ($prefillitem && defined $record) {
+                my $itemcookie = $input->cookie(
+                    -name => 'LastCreatedItem',
+                    # We uri_escape the whole freezed structure so we're sure we won't have any encoding problems
+                    -value   => uri_escape_utf8( freeze( $record ) ),
+                    -HttpOnly => 1,
+                    -expires => ''
+                );
+
+                $cookie = [ $cookie, $itemcookie ];
+            }
+
+        }
+        $nextop = "additem";
+        if ($exist_itemnumber) {
+            $itemrecord = $record;
+        }
     }
 
     # If we have to add & duplicate
@@ -354,6 +450,7 @@ if ($op eq "additem") {
             $fieldItem->delete_subfields($tagsubfield);
             $itemrecord->insert_fields_ordered($fieldItem);
         }
+    $itemrecord = removeFieldsForPrefill($itemrecord) if ($prefillitem);
     }
 
     # If we have to add multiple copies
@@ -365,7 +462,8 @@ if ($op eq "additem") {
         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
-        my $testbarcode = $barcodeobj->next_value($oldbarcode) if $barcodeobj;
+       my $testbarcode;
+        $testbarcode = $barcodeobj->next_value($oldbarcode) if $barcodeobj;
        if ($oldbarcode && !$testbarcode) {
 
            push @errors, "no_next_barcode";
@@ -412,6 +510,16 @@ if ($op eq "additem") {
            }
            undef($itemrecord);
        }
+    }  
+    if ($frameworkcode eq 'FA' && $fa_circborrowernumber){
+        print $input->redirect(
+           '/cgi-bin/koha/circ/circulation.pl?'
+           .'borrowernumber='.$fa_circborrowernumber
+           .'&barcode='.uri_escape($fa_barcode)
+           .'&duedatespec='.$fa_duedatespec
+           .'&stickyduedate=1'
+        );
+        exit;
     }
 
 
@@ -427,7 +535,7 @@ if ($op eq "additem") {
     # check that there is no issue on this item before deletion.
     $error = &DelItemCheck($dbh,$biblionumber,$itemnumber);
     if($error == 1){
-        print $input->redirect("additem.pl?biblionumber=$biblionumber&frameworkcode=$frameworkcode");
+        print $input->redirect("additem.pl?biblionumber=$biblionumber&frameworkcode=$frameworkcode&searchid=$searchid");
     }else{
         push @errors,$error;
         $nextop="additem";
@@ -456,7 +564,17 @@ if ($op eq "additem") {
             $nextop="additem";
         } 
         else {
-            print $input->redirect("/cgi-bin/koha/catalogue/moredetail.pl?biblionumber=$biblionumber");
+            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;
         }
        }
@@ -481,10 +599,33 @@ if ($op eq "additem") {
     if ($exist_itemnumber && $exist_itemnumber != $itemnumber) {
         push @errors,"barcode_not_unique";
     } else {
-        my ($oldbiblionumber,$oldbibnum,$oldbibitemnum) = ModItemFromMarc($itemtosave,$biblionumber,$itemnumber);
+        ModItemFromMarc($itemtosave,$biblionumber,$itemnumber);
         $itemnumber="";
     }
+  my $item = GetItem( $itemnumber );
+    my $olditemlost =  $item->{'itemlost'};
+
+   my ($lost_tag,$lost_subfield) = GetMarcFromKohaField("items.itemlost",'');
+
+   my $newitemlost = $itemtosave->subfield( $lost_tag, $lost_subfield );
+    if (($olditemlost eq '0' or $olditemlost eq '' ) and $newitemlost ge '1'){
+  LostItem($itemnumber,'MARK RETURNED');
+    }
     $nextop="additem";
+} elsif ($op eq "delinkitem"){
+    my $analyticfield = '773';
+       if ($marcflavour  eq 'MARC21' || $marcflavour eq 'NORMARC'){
+        $analyticfield = '773';
+    } elsif ($marcflavour eq 'UNIMARC') {
+        $analyticfield = '461';
+    }
+    foreach my $field ($record->field($analyticfield)){
+        if ($field->subfield('9') eq $hostitemnumber){
+            $record->delete_field($field);
+            last;
+        }
+    }
+       my $modbibresult = ModBiblio($record, $biblionumber,'');
 }
 
 #
@@ -495,6 +636,8 @@ if ($op eq "additem") {
 # now, build existiing item list
 my $temp = GetMarcBiblio( $biblionumber );
 #my @fields = $record->fields();
+
+
 my %witness; #---- stores the list of subfields used at least once, with the "meaning" of the code
 my @big_array;
 #---- finds where items.itemnumber is stored
@@ -503,6 +646,33 @@ my ($branchtagfield, $branchtagsubfield) = &GetMarcFromKohaField("items.homebran
 C4::Biblio::EmbedItemsInMarcBiblio($temp, $biblionumber);
 my @fields = $temp->fields();
 
+
+my @hostitemnumbers;
+if ( C4::Context->preference('EasyAnalyticalRecords') ) {
+    my $analyticfield = '773';
+    if ($marcflavour  eq 'MARC21' || $marcflavour eq 'NORMARC') {
+        $analyticfield = '773';
+    } elsif ($marcflavour eq 'UNIMARC') {
+        $analyticfield = '461';
+    }
+    foreach my $hostfield ($temp->field($analyticfield)){
+        my $hostbiblionumber = $hostfield->subfield('0');
+        if ($hostbiblionumber){
+            my $hostrecord = GetMarcBiblio($hostbiblionumber, 1);
+            if ($hostrecord) {
+                my ($itemfield, undef) = GetMarcFromKohaField( 'items.itemnumber', GetFrameworkCode($hostbiblionumber) );
+                foreach my $hostitem ($hostrecord->field($itemfield)){
+                    if ($hostitem->subfield('9') eq $hostfield->subfield('9')){
+                        push (@fields, $hostitem);
+                        push (@hostitemnumbers, $hostfield->subfield('9'));
+                    }
+                }
+            }
+        }
+    }
+}
+
+
 foreach my $field (@fields) {
     next if ( $field->tag() < 10 );
 
@@ -525,7 +695,7 @@ foreach my $field (@fields) {
                                                || $subfieldvalue;
         }
 
-        if (($field->tag eq $branchtagfield) && ($subfieldcode eq $branchtagsubfield) && C4::Context->preference("IndependantBranches")) {
+        if (($field->tag eq $branchtagfield) && ($subfieldcode eq $branchtagsubfield) && C4::Context->preference("IndependentBranches")) {
             #verifying rights
             my $userenv = C4::Context->userenv();
             unless (($userenv->{'flags'} == 1) or (($userenv->{'branch'} eq $subfieldvalue))){
@@ -533,6 +703,22 @@ foreach my $field (@fields) {
             }
         }
         $this_row{itemnumber} = $subfieldvalue if ($field->tag() eq $itemtagfield && $subfieldcode eq $itemtagsubfield);
+
+       if ( C4::Context->preference('EasyAnalyticalRecords') ) {
+           foreach my $hostitemnumber (@hostitemnumbers){
+               if ($this_row{itemnumber} eq $hostitemnumber){
+                       $this_row{hostitemflag} = 1;
+                       $this_row{hostbiblionumber}= GetBiblionumberFromItemnumber($hostitemnumber);
+                       last;
+               }
+           }
+
+#          my $countanalytics=GetAnalyticsCount($this_row{itemnumber});
+#           if ($countanalytics > 0){
+#                $this_row{countanalytics} = $countanalytics;
+#           }
+       }
+
     }
     if (%this_row) {
         push(@big_array, \%this_row);
@@ -553,6 +739,9 @@ for my $row ( @big_array ) {
     $row_data{itemnumber} = $row->{itemnumber};
     #reporting this_row values
     $row_data{'nomod'} = $row->{'nomod'};
+    $row_data{'hostitemflag'} = $row->{'hostitemflag'};
+    $row_data{'hostbiblionumber'} = $row->{'hostbiblionumber'};
+#      $row_data{'countanalytics'} = $row->{'countanalytics'};
     push(@item_value_loop,\%row_data);
 }
 foreach my $subfield_code (sort keys(%witness)) {
@@ -567,14 +756,15 @@ my $i=0;
 
 my $pref_itemcallnumber = C4::Context->preference('itemcallnumber');
 
-my $onlymine = C4::Context->preference('IndependantBranches') && 
+my $onlymine = C4::Context->preference('IndependentBranches') &&
                C4::Context->userenv                           && 
                C4::Context->userenv->{flags}!=1               && 
                C4::Context->userenv->{branch};
-my $branches = GetBranchesLoop(C4::Context->userenv->{branch},$onlymine);  # build once ahead of time, instead of multiple times later.
+my $branch = $input->param('branch') || C4::Context->userenv->{branch};
+my $branches = GetBranchesLoop($branch,$onlymine);  # build once ahead of time, instead of multiple times later.
 
 # We generate form, from actuel record
-my @fields;
+@fields = ();
 if($itemrecord){
     foreach my $field ($itemrecord->fields()){
         my $tag = $field->{_tag};
@@ -598,6 +788,11 @@ if($itemrecord){
             }
     # and now we add fields that are empty
 
+# Using last created item if it exists
+
+$itemrecord = $cookieitemrecord if ($prefillitem and not $justaddeditem and $op ne "edititem");
+
+# We generate form, and fill with values if defined
 foreach my $tag ( keys %{$tagslib}){
     foreach my $subtag (keys %{$tagslib->{$tag}}){
         next if subfield_is_koha_internal_p($subtag);
@@ -605,7 +800,7 @@ foreach my $tag ( keys %{$tagslib}){
         next if any { /^$tag$subtag$/ }  @fields;
 
         my @values = (undef);
-        @values = $itemrecord->field($tag)->subfield($subtag) if ($itemrecord && defined($itemrecord->field($tag)->subfield($subtag)));
+        @values = $itemrecord->field($tag)->subfield($subtag) if ($itemrecord && defined($itemrecord->field($tag)) && defined($itemrecord->field($tag)->subfield($subtag)));
         for my $value (@values){
             my $subfield_data = generate_subfield_form($tag, $subtag, $value, $tagslib, $tagslib->{$tag}->{$subtag}, $branches, $today_iso, $biblionumber, $temp, \@loop_data, $i); 
             push (@loop_data, $subfield_data);
@@ -625,12 +820,27 @@ $template->param(
     item_header_loop => \@header_value_loop,
     item             => \@loop_data,
     itemnumber       => $itemnumber,
+    barcode          => GetBarcodeFromItemnumber($itemnumber),
     itemtagfield     => $itemtagfield,
     itemtagsubfield  => $itemtagsubfield,
     op      => $nextop,
     opisadd => ($nextop eq "saveitem") ? 0 : 1,
+    popup => $input->param('popup') ? 1: 0,
     C4::Search::enabled_staff_search_views,
 );
+$template->{'VARS'}->{'searchid'} = $searchid;
+
+if ($frameworkcode eq 'FA'){
+    # fast cataloguing datas
+    $template->param(
+        'circborrowernumber' => $fa_circborrowernumber,
+        'barcode'            => $fa_barcode,
+        'branch'             => $fa_branch,
+        'stickyduedate'      => $fa_stickyduedate,
+        'duedatespec'        => $fa_duedatespec,
+    );
+}
+
 foreach my $error (@errors) {
     $template->param($error => 1);
 }