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