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