Bug 25375: New atomicupdate syntax
[koha.git] / cataloguing / additem.pl
1 #!/usr/bin/perl
2
3 # Copyright 2000-2002 Katipo Communications
4 # Copyright 2004-2010 BibLibre
5 # Parts Copyright Catalyst IT 2011
6 #
7 # This file is part of Koha.
8 #
9 # Koha is free software; you can redistribute it and/or modify it
10 # under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 3 of the License, or
12 # (at your option) any later version.
13 #
14 # Koha is distributed in the hope that it will be useful, but
15 # WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with Koha; if not, see <http://www.gnu.org/licenses>.
21
22 use Modern::Perl;
23
24 use CGI qw ( -utf8 );
25 use C4::Auth qw( get_template_and_user haspermission );
26 use C4::Output qw( output_and_exit_if_error output_and_exit output_html_with_http_headers );
27 use C4::Biblio qw(
28     GetFrameworkCode
29     GetMarcBiblio
30     GetMarcFromKohaField
31     GetMarcStructure
32     IsMarcStructureInternal
33     ModBiblio
34 );
35 use C4::Context;
36 use C4::Circulation qw( barcodedecode LostItem );
37 use C4::Barcodes;
38 use C4::Barcodes::ValueBuilder;
39 use Koha::DateUtils qw( dt_from_string );
40 use Koha::Items;
41 use Koha::ItemTypes;
42 use Koha::Libraries;
43 use Koha::Patrons;
44 use Koha::SearchEngine::Indexer;
45 use C4::Search qw( enabled_staff_search_views );
46 use Storable qw( freeze thaw );
47 use URI::Escape qw( uri_escape_utf8 );
48 use C4::Members;
49 use Koha::UI::Form::Builder::Item;
50
51 use MARC::File::XML;
52 use URI::Escape qw( uri_escape_utf8 );
53 use Encode qw( encode_utf8 );
54 use MIME::Base64 qw( decode_base64url encode_base64url );
55 use List::Util qw( first );
56 use List::MoreUtils qw( any uniq );
57
58 our $dbh = C4::Context->dbh;
59
60 sub get_item_from_cookie {
61     my ( $input ) = @_;
62
63     my $item_from_cookie;
64     my $lastitemcookie = $input->cookie('LastCreatedItem');
65     if ($lastitemcookie) {
66         $lastitemcookie = decode_base64url($lastitemcookie);
67         eval {
68             if ( thaw($lastitemcookie) ) {
69                 $item_from_cookie = thaw($lastitemcookie);
70             }
71         };
72         if ($@) {
73             $lastitemcookie ||= 'undef';
74             warn "Storable::thaw failed to thaw LastCreatedItem-cookie. Cookie value '".encode_base64url($lastitemcookie)."'. Caught error follows: '$@'";
75         }
76     }
77     return $item_from_cookie;
78 }
79
80 my $input        = CGI->new;
81
82 my $biblionumber;
83 my $itemnumber;
84 if( $input->param('itemnumber') && !$input->param('biblionumber') ){
85     $itemnumber = $input->param('itemnumber');
86     my $item = Koha::Items->find( $itemnumber );
87     $biblionumber = $item->biblionumber;
88 } else {
89     $biblionumber = $input->param('biblionumber');
90     $itemnumber = $input->param('itemnumber');
91 }
92
93 my $biblio = Koha::Biblios->find($biblionumber);
94
95 my $op           = $input->param('op') || q{};
96 my $hostitemnumber = $input->param('hostitemnumber');
97 my $marcflavour  = C4::Context->preference("marcflavour");
98 my $searchid     = $input->param('searchid');
99 # fast cataloguing datas
100 my $fa_circborrowernumber = $input->param('circborrowernumber');
101 my $fa_barcode            = $input->param('barcode');
102 my $fa_branch             = $input->param('branch');
103 my $fa_stickyduedate      = $input->param('stickyduedate');
104 my $fa_duedatespec        = $input->param('duedatespec');
105
106 our $frameworkcode = &GetFrameworkCode($biblionumber);
107
108 # Defining which userflag is needing according to the framework currently used
109 my $userflags;
110 if (defined $input->param('frameworkcode')) {
111     $userflags = ($input->param('frameworkcode') eq 'FA') ? "fast_cataloging" : "edit_items";
112 }
113
114 if (not defined $userflags) {
115     $userflags = ($frameworkcode eq 'FA') ? "fast_cataloging" : "edit_items";
116 }
117
118 my ($template, $loggedinuser, $cookie)
119     = get_template_and_user({template_name => "cataloguing/additem.tt",
120                  query => $input,
121                  type => "intranet",
122                  flagsrequired => {editcatalogue => $userflags},
123                  });
124
125
126 # Does the user have a restricted item editing permission?
127 my $uid = Koha::Patrons->find( $loggedinuser )->userid;
128 my $restrictededition = $uid ? haspermission($uid,  {'editcatalogue' => 'edit_items_restricted'}) : undef;
129 # In case user is a superlibrarian, editing is not restricted
130 $restrictededition = 0 if ($restrictededition != 0 &&  C4::Context->IsSuperLibrarian());
131 # In case user has fast cataloging permission (and we're in fast cataloging), editing is not restricted
132 $restrictededition = 0 if ($restrictededition != 0 && $frameworkcode eq 'FA' && haspermission($uid, {'editcatalogue' => 'fast_cataloging'}));
133
134 our $tagslib = &GetMarcStructure(1,$frameworkcode);
135 my $record = GetMarcBiblio({ biblionumber => $biblionumber });
136
137 output_and_exit_if_error( $input, $cookie, $template,
138     { module => 'cataloguing', record => $record } );
139
140 my $current_item;
141 my $nextop="additem";
142 my @errors; # store errors found while checking data BEFORE saving item.
143
144 # Getting last created item cookie
145 my $prefillitem = C4::Context->preference('PrefillItem');
146
147 #-------------------------------------------------------------------------------
148 if ($op eq "additem") {
149
150     my $add_submit                 = $input->param('add_submit');
151     my $add_duplicate_submit       = $input->param('add_duplicate_submit');
152     my $add_multiple_copies_submit = $input->param('add_multiple_copies_submit');
153     my $number_of_copies           = $input->param('number_of_copies');
154
155     my @columns = Koha::Items->columns;
156     my $item = Koha::Item->new;
157     $item->biblionumber($biblio->biblionumber);
158     for my $c ( @columns ) {
159         if ( $c eq 'more_subfields_xml' ) {
160             my @more_subfields_xml = $input->multi_param("items.more_subfields_xml");
161             my @unlinked_item_subfields;
162             for my $subfield ( @more_subfields_xml ) {
163                 my $v = $input->param('items.more_subfields_xml_' . $subfield);
164                 push @unlinked_item_subfields, $subfield, $v;
165             }
166             if ( @unlinked_item_subfields ) {
167                 my $marc = MARC::Record->new();
168                 # use of tag 999 is arbitrary, and doesn't need to match the item tag
169                 # used in the framework
170                 $marc->append_fields(MARC::Field->new('999', ' ', ' ', @unlinked_item_subfields));
171                 $marc->encoding("UTF-8");
172                 $item->more_subfields_xml($marc->as_xml("USMARC"));
173                 next;
174             }
175             $item->more_subfields_xml(undef);
176         } else {
177             my @v = grep { $_ ne "" }
178                 uniq $input->multi_param( "items." . $c );
179
180             next unless @v;
181
182             if ( $c eq 'permanent_location' ) { # See 27837
183                 $item->make_column_dirty('permanent_location');
184             }
185
186             $item->$c(join ' | ', @v);
187         }
188     }
189
190     # if autoBarcode is set to 'incremental', calculate barcode...
191     if ( ! defined $item->barcode && C4::Context->preference('autoBarcode') eq 'incremental' ) {
192         my ( $barcode ) = C4::Barcodes::ValueBuilder::incremental::get_barcode;
193         $item->barcode($barcode);
194     }
195
196     $item->barcode(barcodedecode($item->barcode));
197
198     # If we have to add or add & duplicate, we add the item
199     if ( $add_submit || $add_duplicate_submit || $prefillitem) {
200
201         # check for item barcode # being unique
202         if ( defined $item->barcode
203             && Koha::Items->search( { barcode => $item->barcode } )->count )
204         {
205             # if barcode exists, don't create, but report The problem.
206             push @errors, "barcode_not_unique";
207
208             $current_item = $item->unblessed; # Restore edit form for the same item
209         }
210         else {
211             $item->store->discard_changes;
212
213             # This is a bit tricky : if there is a cookie for the last created item and
214             # we just added an item, the cookie value is not correct yet (it will be updated
215             # next page). To prevent the form from being filled with outdated values, we
216             # force the use of "add and duplicate" feature, so the form will be filled with
217             # correct values.
218
219             # Pushing the last created item cookie back
220             if ( $prefillitem ) {
221                 my $last_created_item_cookie = $input->cookie(
222                     -name => 'LastCreatedItem',
223                     # We encode_base64url the whole freezed structure so we're sure we won't have any encoding problems
224                     -value   => encode_base64url( freeze( { %{$item->unblessed}, itemnumber => undef } ) ),
225                     -HttpOnly => 1,
226                     -expires => '',
227                     -sameSite => 'Lax'
228                 );
229
230                 $cookie = [ $cookie, $last_created_item_cookie ];
231             }
232
233         }
234         $nextop = "additem";
235
236     }
237
238     # If we have to add & duplicate
239     if ($prefillitem || $add_duplicate_submit) {
240
241         $current_item = $item->unblessed;
242
243         if (C4::Context->preference('autoBarcode') eq 'incremental') {
244             my ( $barcode ) = C4::Barcodes::ValueBuilder::incremental::get_barcode;
245             $current_item->{barcode} = $barcode;
246         }
247         else {
248             # we have to clear the barcode field in the duplicate item record to make way for the new one generated by the javascript plugin
249             $current_item->{barcode} = undef; # FIXME or delete?
250         }
251
252         # Don't use the "prefill" feature if we want to generate the form with all the info from this item
253         # It will remove subfields that are not in SubfieldsToUseWhenPrefill.
254         $prefillitem = 0 if $add_duplicate_submit;
255     }
256
257     # If we have to add multiple copies
258     if ($add_multiple_copies_submit) {
259
260         $current_item = $item->unblessed;
261
262         my $copynumber = $current_item->{copynumber};
263         my $oldbarcode = $current_item->{barcode};
264
265         # If there is a barcode and we can't find their new values, we can't add multiple copies
266         my $testbarcode;
267         my $barcodeobj = C4::Barcodes->new;
268         $testbarcode = $barcodeobj->next_value($oldbarcode) if $barcodeobj;
269         if ( $oldbarcode && !$testbarcode ) {
270
271             push @errors, "no_next_barcode";
272
273         }
274         else {
275             # We add each item
276
277             # For the first iteration
278             my $barcodevalue = $oldbarcode;
279             my $exist_itemnumber;
280
281             for ( my $i = 0 ; $i < $number_of_copies ; ) {
282
283                 # If there is a barcode
284                 if ($barcodevalue) {
285
286 # Getting a new barcode (if it is not the first iteration or the barcode we tried already exists)
287                     $barcodevalue = $barcodeobj->next_value($oldbarcode)
288                       if ( $i > 0 || $exist_itemnumber );
289
290                     # Putting it into the record
291                     if ($barcodevalue) {
292                         if ( C4::Context->preference("autoBarcode") eq
293                             'hbyymmincr' && $i > 0 )
294                         { # The first copy already contains the homebranch prefix
295                              # This is terribly hacky but the easiest way to fix the way hbyymmincr is working
296                              # Contrary to what one might think, the barcode plugin does not prefix the returned string with the homebranch
297                              # For a single item, it is handled with some JS code (see cataloguing/value_builder/barcode.pl)
298                              # But when adding multiple copies we need to prefix it here,
299                              # so we retrieve the homebranch from the item and prefix the barcode with it.
300                             my $homebranch = $current_item->{homebranch};
301                             $barcodevalue = $homebranch . $barcodevalue;
302                         }
303                         $current_item->{barcode} = $barcodevalue;
304                     }
305
306                     # Checking if the barcode already exists
307                     $exist_itemnumber = Koha::Items->search({ barcode => $barcodevalue })->count;
308                 }
309
310                 # Updating record with the new copynumber
311                 if ($copynumber) {
312                     $current_item->{copynumber} = $copynumber;
313                 }
314
315                 # Adding the item
316                 if ( !$exist_itemnumber ) {
317                     delete $current_item->{itemnumber};
318                     $current_item = Koha::Item->new($current_item)->store(
319                         { skip_record_index => 1 } );
320                     $current_item->discard_changes; # Cannot chain discard_changes
321                     $current_item = $current_item->unblessed;
322
323 # We count the item only if it was really added
324 # That way, all items are added, even if there was some already existing barcodes
325 # FIXME : Please note that there is a risk of infinite loop here if we never find a suitable barcode
326                     $i++;
327
328                     # Only increment copynumber if item was really added
329                     $copynumber++ if ( $copynumber && $copynumber =~ m/^\d+$/ );
330                 }
331
332                 # Preparing the next iteration
333                 $oldbarcode = $barcodevalue;
334             }
335
336             my $indexer = Koha::SearchEngine::Indexer->new(
337                 { index => $Koha::SearchEngine::BIBLIOS_INDEX } );
338             $indexer->index_records( $biblionumber, "specialUpdate",
339                 "biblioserver" );
340
341             undef($current_item);
342         }
343     }
344     if ($frameworkcode eq 'FA' && $fa_circborrowernumber){
345         print $input->redirect(
346            '/cgi-bin/koha/circ/circulation.pl?'
347            .'borrowernumber='.$fa_circborrowernumber
348            .'&barcode='.uri_escape_utf8($fa_barcode)
349            .'&duedatespec='.$fa_duedatespec
350            .'&stickyduedate='.$fa_stickyduedate
351         );
352         exit;
353     }
354
355
356 #-------------------------------------------------------------------------------
357 } elsif ($op eq "edititem") {
358 #-------------------------------------------------------------------------------
359 # retrieve item if exist => then, it's a modif
360     $current_item = Koha::Items->find($itemnumber)->unblessed;
361     # FIXME Handle non existent item
362     $nextop = "saveitem";
363 #-------------------------------------------------------------------------------
364 } elsif ($op eq "dupeitem") {
365 #-------------------------------------------------------------------------------
366 # retrieve item if exist => then, it's a modif
367     $current_item = Koha::Items->find($itemnumber)->unblessed;
368     # FIXME Handle non existent item
369     if (C4::Context->preference('autoBarcode') eq 'incremental') {
370         my ( $barcode ) = C4::Barcodes::ValueBuilder::incremental::get_barcode;
371         $current_item->{barcode} = $barcode;
372     }
373     else {
374         $current_item->{barcode} = undef; # Don't save it!
375     }
376
377     $nextop = "additem";
378 #-------------------------------------------------------------------------------
379 } elsif ($op eq "delitem") {
380 #-------------------------------------------------------------------------------
381     # check that there is no issue on this item before deletion.
382     my $item = Koha::Items->find($itemnumber);
383     my $deleted = $item->safe_delete;
384     if ( $deleted ) {
385         print $input->redirect("additem.pl?biblionumber=$biblionumber&frameworkcode=$frameworkcode&searchid=$searchid");
386         exit;
387     }
388     else {
389         push @errors, @{ $deleted->messages }[0]->message;
390         $nextop = "additem";
391     }
392 #-------------------------------------------------------------------------------
393 } elsif ($op eq "delallitems") {
394 #-------------------------------------------------------------------------------
395     my $items = Koha::Items->search({ biblionumber => $biblionumber });
396     while ( my $item = $items->next ) {
397         my $deleted = $item->safe_delete({ skip_record_index => 1 });
398         push @errors, @{$deleted->messages}[0]->message unless $deleted;
399     }
400     my $indexer = Koha::SearchEngine::Indexer->new({ index => $Koha::SearchEngine::BIBLIOS_INDEX });
401     $indexer->index_records( $biblionumber, "specialUpdate", "biblioserver" );
402     if ( @errors ) {
403         $nextop="additem";
404     } else {
405         my $defaultview = C4::Context->preference('IntranetBiblioDefaultView');
406         my $views = { C4::Search::enabled_staff_search_views };
407         if ($defaultview eq 'isbd' && $views->{can_view_ISBD}) {
408             print $input->redirect("/cgi-bin/koha/catalogue/ISBDdetail.pl?biblionumber=$biblionumber&searchid=$searchid");
409         } elsif  ($defaultview eq 'marc' && $views->{can_view_MARC}) {
410             print $input->redirect("/cgi-bin/koha/catalogue/MARCdetail.pl?biblionumber=$biblionumber&searchid=$searchid");
411         } elsif  ($defaultview eq 'labeled_marc' && $views->{can_view_labeledMARC}) {
412             print $input->redirect("/cgi-bin/koha/catalogue/labeledMARCdetail.pl?biblionumber=$biblionumber&searchid=$searchid");
413         } else {
414             print $input->redirect("/cgi-bin/koha/catalogue/detail.pl?biblionumber=$biblionumber&searchid=$searchid");
415         }
416         exit;
417     }
418 #-------------------------------------------------------------------------------
419 } elsif ($op eq "saveitem") {
420 #-------------------------------------------------------------------------------
421
422     my $itemnumber = $input->param('itemnumber');
423     my $item = Koha::Items->find($itemnumber);
424     # FIXME Handle non existent item
425     my $olditemlost = $item->itemlost;
426     my @columns = Koha::Items->columns;
427     my $new_values = $item->unblessed;
428     for my $c ( @columns ) {
429         if ( $c eq 'more_subfields_xml' ) {
430             my @more_subfields_xml = $input->multi_param("items.more_subfields_xml");
431             my @unlinked_item_subfields;
432             for my $subfield ( uniq @more_subfields_xml ) {
433                 my @v = $input->multi_param('items.more_subfields_xml_' . encode_utf8($subfield));
434                 push @unlinked_item_subfields, $subfield, $_ for @v;
435             }
436             if ( @unlinked_item_subfields ) {
437                 my $marc = MARC::Record->new();
438                 # use of tag 999 is arbitrary, and doesn't need to match the item tag
439                 # used in the framework
440                 $marc->append_fields(MARC::Field->new('999', ' ', ' ', @unlinked_item_subfields));
441                 $marc->encoding("UTF-8");
442                 $new_values->{more_subfields_xml} = $marc->as_xml("USMARC");
443                 next;
444             }
445             $item->more_subfields_xml(undef);
446         } else {
447             my @v = map { ( defined $_ && $_ eq '' ) ? undef : $_ } $input->multi_param( "items." . $c );
448             next unless @v;
449
450             if ( $c eq 'permanent_location' ) { # See 27837
451                 $item->make_column_dirty('permanent_location');
452             }
453
454             if ( scalar(@v) == 1 && not defined $v[0] ) {
455                 delete $new_values->{$c};
456             } else {
457                 $new_values->{$c} = join ' | ', @v;
458             }
459         }
460     }
461     $item = $item->set_or_blank($new_values);
462
463     # check that the barcode don't exist already
464     if (
465         defined $item->barcode
466         && Koha::Items->search(
467             {
468                 barcode    => $item->barcode,
469                 itemnumber => { '!=' => $item->itemnumber }
470             }
471         )->count
472       )
473     {
474         # FIXME We shouldn't need that, ->store would explode as there is a unique constraint on items.barcode
475         push @errors,"barcode_not_unique";
476         $current_item = $item->unblessed; # Restore edit form for the same item
477     } else {
478         my $newitemlost = $item->itemlost;
479         if ( $newitemlost && $newitemlost ge '1' && !$olditemlost ) {
480             LostItem( $item->itemnumber, 'additem' );
481         }
482         $item->store;
483     }
484
485     $nextop="additem";
486 } elsif ($op eq "delinkitem"){
487
488     my $analyticfield = '773';
489     if ($marcflavour  eq 'MARC21'){
490         $analyticfield = '773';
491     } elsif ($marcflavour eq 'UNIMARC') {
492         $analyticfield = '461';
493     }
494     foreach my $field ($record->field($analyticfield)){
495         if ($field->subfield('9') eq $hostitemnumber){
496             $record->delete_field($field);
497             last;
498         }
499     }
500         my $modbibresult = ModBiblio($record, $biblionumber,'');
501 }
502
503 # update OAI-PMH sets
504 if ($op) {
505     if (C4::Context->preference("OAI-PMH:AutoUpdateSets")) {
506         C4::OAI::Sets::UpdateOAISetsBiblio($biblionumber, $record);
507     }
508 }
509
510 #
511 #-------------------------------------------------------------------------------
512 # build screen with existing items. and "new" one
513 #-------------------------------------------------------------------------------
514
515 # now, build existiing item list
516
517 my @items;
518 for my $item ( $biblio->items->as_list, $biblio->host_items->as_list ) {
519     push @items, $item->columns_to_str;
520 }
521
522 my @witness_attributes = uniq map {
523     my $item = $_;
524     map { defined $item->{$_} && $item->{$_} ne "" ? $_ : () } keys %$item
525 } @items;
526
527 our ( $itemtagfield, $itemtagsubfield ) = GetMarcFromKohaField("items.itemnumber");
528
529 my $subfieldcode_attribute_mappings;
530 for my $subfield_code ( keys %{ $tagslib->{$itemtagfield} } ) {
531
532     my $subfield = $tagslib->{$itemtagfield}->{$subfield_code};
533
534     next if IsMarcStructureInternal( $subfield );
535     next unless $subfield->{tab} eq 10; # Is this really needed?
536
537     my $attribute;
538     if ( $subfield->{kohafield} ) {
539         ( $attribute = $subfield->{kohafield} ) =~ s|^items\.||;
540     } else {
541         $attribute = $subfield_code; # It's in more_subfields_xml
542     }
543     next unless grep { $attribute eq $_ } @witness_attributes;
544     $subfieldcode_attribute_mappings->{$subfield_code} = $attribute;
545 }
546
547 my @header_value_loop = map {
548     {
549         header_value  => $tagslib->{$itemtagfield}->{$_}->{lib},
550         attribute     => $subfieldcode_attribute_mappings->{$_},
551         subfield_code => $_,
552     }
553 } sort keys %$subfieldcode_attribute_mappings;
554
555 # Using last created item if it exists
556 if (   $prefillitem
557     && $op ne "additem"
558     && $op ne "edititem"
559     && $op ne "dupeitem" )
560 {
561     my $item_from_cookie = get_item_from_cookie($input);
562     $current_item = $item_from_cookie if $item_from_cookie;
563 }
564
565 if ( $current_item->{more_subfields_xml} ) {
566     # FIXME Use Maybe MARC::Record::new_from_xml if encoding issues on subfield (??)
567     $current_item->{marc_more_subfields_xml} = MARC::Record->new_from_xml($current_item->{more_subfields_xml}, 'UTF-8');
568 }
569
570 my $branchcode = $input->param('branch') || C4::Context->userenv->{branch};
571
572 # If we are not adding a new item
573 # OR
574 # If the subfield must be prefilled with last catalogued item
575 my @subfields_to_prefill;
576 if ( $nextop eq 'additem' && $op ne 'dupeitem' && $prefillitem ) {
577     @subfields_to_prefill = split(' ', C4::Context->preference('SubfieldsToUseWhenPrefill'));
578     # Setting to 1 element if SubfieldsToUseWhenPrefill is empty to prevent all the subfields to be prefilled
579     @subfields_to_prefill = ("") unless @subfields_to_prefill;
580 }
581
582 # Getting list of subfields to keep when restricted editing is enabled
583 my @subfields_to_allow = $restrictededition ? split ' ', C4::Context->preference('SubfieldsToAllowForRestrictedEditing') : ();
584
585 my $subfields =
586   Koha::UI::Form::Builder::Item->new(
587     { biblionumber => $biblionumber, item => $current_item } )->edit_form(
588     {
589         branchcode           => $branchcode,
590         restricted_editition => $restrictededition,
591         (
592             @subfields_to_allow
593             ? ( subfields_to_allow => \@subfields_to_allow )
594             : ()
595         ),
596         (
597             @subfields_to_prefill
598             ? ( subfields_to_prefill => \@subfields_to_prefill )
599             : ()
600         ),
601         prefill_with_default_values => 1,
602         branch_limit => C4::Context->userenv->{"branch"},
603         (
604             $op eq 'dupeitem'
605             ? ( ignore_invisible_subfields => 1 )
606             : ()
607         ),
608     }
609 );
610
611 if (   $frameworkcode eq 'FA' ) {
612     my ( $barcode_field ) = grep {$_->{kohafield} eq 'items.barcode'} @$subfields;
613     $barcode_field->{marc_value}->{value} ||= $input->param('barcode');
614 }
615
616 if( my $default_location = C4::Context->preference('NewItemsDefaultLocation') ) {
617     my ( $location_field ) = grep {$_->{kohafield} eq 'items.location'} @$subfields;
618     $location_field->{marc_value}->{value} ||= $default_location;
619 }
620
621 # what's the next op ? it's what we are not in : an add if we're editing, otherwise, and edit.
622 $template->param(
623     biblio       => $biblio,
624     items        => \@items,
625     item_header_loop => \@header_value_loop,
626     subfields        => $subfields,
627     itemnumber       => $itemnumber,
628     barcode          => $current_item->{barcode},
629     op      => $nextop,
630     popup => scalar $input->param('popup') ? 1: 0,
631     C4::Search::enabled_staff_search_views,
632 );
633 $template->{'VARS'}->{'searchid'} = $searchid;
634
635 if ($frameworkcode eq 'FA'){
636     # fast cataloguing datas
637     $template->param(
638         'circborrowernumber' => $fa_circborrowernumber,
639         'barcode'            => $fa_barcode,
640         'branch'             => $fa_branch,
641         'stickyduedate'      => $fa_stickyduedate,
642         'duedatespec'        => $fa_duedatespec,
643     );
644 }
645
646 foreach my $error (@errors) {
647     $template->param($error => 1);
648 }
649 output_html_with_http_headers $input, $cookie, $template->output;