Merge remote-tracking branch 'kc/master' into new/enh/bug_6716
[koha.git] / cataloguing / additem.pl
blob0db2d2df308c61a36960ac0200c0ee6164095a3f
1 #!/usr/bin/perl
3 # Copyright 2000-2002 Katipo Communications
4 # Copyright 2004-2010 BibLibre
5 # Parts Copyright Catalyst IT 2011
7 # This file is part of Koha.
9 # Koha is free software; you can redistribute it and/or modify it under the
10 # terms of the GNU General Public License as published by the Free Software
11 # Foundation; either version 2 of the License, or (at your option) any later
12 # version.
14 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License along
19 # with Koha; if not, write to the Free Software Foundation, Inc.,
20 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 use strict;
23 #use warnings; FIXME - Bug 2505
24 use CGI;
25 use C4::Auth;
26 use C4::Output;
27 use C4::Biblio;
28 use C4::Items;
29 use C4::Context;
30 use C4::Koha; # XXX subfield_is_koha_internal_p
31 use C4::Branch; # XXX subfield_is_koha_internal_p
32 use C4::ClassSource;
33 use C4::Dates;
34 use List::MoreUtils qw/any/;
36 use MARC::File::XML;
37 use URI::Escape;
39 my $dbh = C4::Context->dbh;
41 sub find_value {
42 my ($tagfield,$insubfield,$record) = @_;
43 my $result;
44 my $indicator;
45 foreach my $field ($record->field($tagfield)) {
46 my @subfields = $field->subfields();
47 foreach my $subfield (@subfields) {
48 if (@$subfield[0] eq $insubfield) {
49 $result .= @$subfield[1];
50 $indicator = $field->indicator(1).$field->indicator(2);
54 return($indicator,$result);
57 sub get_item_from_barcode {
58 my ($barcode)=@_;
59 my $dbh=C4::Context->dbh;
60 my $result;
61 my $rq=$dbh->prepare("SELECT itemnumber from items where items.barcode=?");
62 $rq->execute($barcode);
63 ($result)=$rq->fetchrow;
64 return($result);
67 sub set_item_default_location {
68 my $itemnumber = shift;
69 my $item = GetItem( $itemnumber );
70 if ( C4::Context->preference('NewItemsDefaultLocation') ) {
71 $item->{'permanent_location'} = $item->{'location'};
72 $item->{'location'} = C4::Context->preference('NewItemsDefaultLocation');
73 ModItem( $item, undef, $itemnumber);
75 else {
76 $item->{'permanent_location'} = $item->{'location'} if !defined($item->{'permanent_location'});
77 ModItem( $item, undef, $itemnumber);
81 # NOTE: This code is subject to change in the future with the implemenation of ajax based autobarcode code
82 # NOTE: 'incremental' is the ONLY autoBarcode option available to those not using javascript
83 sub _increment_barcode {
84 my ($record, $frameworkcode) = @_;
85 my ($tagfield,$tagsubfield) = &GetMarcFromKohaField("items.barcode",$frameworkcode);
86 unless ($record->field($tagfield)->subfield($tagsubfield)) {
87 my $sth_barcode = $dbh->prepare("select max(abs(barcode)) from items");
88 $sth_barcode->execute;
89 my ($newbarcode) = $sth_barcode->fetchrow;
90 $newbarcode++;
91 # OK, we have the new barcode, now create the entry in MARC record
92 my $fieldItem = $record->field($tagfield);
93 $record->delete_field($fieldItem);
94 $fieldItem->add_subfields($tagsubfield => $newbarcode);
95 $record->insert_fields_ordered($fieldItem);
97 return $record;
101 sub generate_subfield_form {
102 my ($tag, $subfieldtag, $value, $tagslib,$subfieldlib, $branches, $today_iso, $biblionumber, $temp, $loop_data, $i) = @_;
104 my $frameworkcode = &GetFrameworkCode($biblionumber);
105 my %subfield_data;
106 my $dbh = C4::Context->dbh;
107 my $authorised_values_sth = $dbh->prepare("SELECT authorised_value,lib FROM authorised_values WHERE category=? ORDER BY lib");
109 my $index_subfield = int(rand(1000000));
110 if ($subfieldtag eq '@'){
111 $subfield_data{id} = "tag_".$tag."_subfield_00_".$index_subfield;
112 } else {
113 $subfield_data{id} = "tag_".$tag."_subfield_".$subfieldtag."_".$index_subfield;
116 $subfield_data{tag} = $tag;
117 $subfield_data{subfield} = $subfieldtag;
118 $subfield_data{random} = int(rand(1000000)); # why do we need 2 different randoms?
119 $subfield_data{marc_lib} ="<span id=\"error$i\" title=\"".$subfieldlib->{lib}."\">".$subfieldlib->{lib}."</span>";
120 $subfield_data{mandatory} = $subfieldlib->{mandatory};
121 $subfield_data{repeatable} = $subfieldlib->{repeatable};
123 $value =~ s/"/&quot;/g;
124 if ( ! defined( $value ) || $value eq '') {
125 $value = $subfieldlib->{defaultvalue};
126 # get today date & replace YYYY, MM, DD if provided in the default value
127 my ( $year, $month, $day ) = split ',', $today_iso; # FIXME: iso dates don't have commas!
128 $value =~ s/YYYY/$year/g;
129 $value =~ s/MM/$month/g;
130 $value =~ s/DD/$day/g;
133 $subfield_data{visibility} = "display:none;" if (($subfieldlib->{hidden} > 4) || ($subfieldlib->{hidden} < -4));
135 my $pref_itemcallnumber = C4::Context->preference('itemcallnumber');
136 if (!$value && $subfieldlib->{kohafield} eq 'items.itemcallnumber' && $pref_itemcallnumber) {
137 my $CNtag = substr($pref_itemcallnumber, 0, 3);
138 my $CNsubfield = substr($pref_itemcallnumber, 3, 1);
139 my $CNsubfield2 = substr($pref_itemcallnumber, 4, 1);
140 my $temp2 = $temp->field($CNtag);
141 if ($temp2) {
142 $value = ($temp2->subfield($CNsubfield)).' '.($temp2->subfield($CNsubfield2));
143 #remove any trailing space incase one subfield is used
144 $value =~ s/^\s+|\s+$//g;
148 if ($frameworkcode eq 'FA' && $subfieldlib->{kohafield} eq 'items.barcode'){
149 my $input = new CGI;
150 $value = $input->param('barcode');
152 my $attributes_no_value = qq(tabindex="1" id="$subfield_data{id}" name="field_value" class="input_marceditor" size="67" maxlength="255" );
153 my $attributes = qq($attributes_no_value value="$value" );
155 if ( $subfieldlib->{authorised_value} ) {
156 my @authorised_values;
157 my %authorised_lib;
158 # builds list, depending on authorised value...
159 if ( $subfieldlib->{authorised_value} eq "branches" ) {
160 foreach my $thisbranch (@$branches) {
161 push @authorised_values, $thisbranch->{value};
162 $authorised_lib{$thisbranch->{value}} = $thisbranch->{branchname};
163 $value = $thisbranch->{value} if $thisbranch->{selected} && !$value;
166 elsif ( $subfieldlib->{authorised_value} eq "itemtypes" ) {
167 push @authorised_values, "" unless ( $subfieldlib->{mandatory} );
168 my $sth = $dbh->prepare("SELECT itemtype,description FROM itemtypes ORDER BY description");
169 $sth->execute;
170 while ( my ( $itemtype, $description ) = $sth->fetchrow_array ) {
171 push @authorised_values, $itemtype;
172 $authorised_lib{$itemtype} = $description;
175 unless ( $value ) {
176 my $itype_sth = $dbh->prepare("SELECT itemtype FROM biblioitems WHERE biblionumber = ?");
177 $itype_sth->execute( $biblionumber );
178 ( $value ) = $itype_sth->fetchrow_array;
181 #---- class_sources
183 elsif ( $subfieldlib->{authorised_value} eq "cn_source" ) {
184 push @authorised_values, "" unless ( $subfieldlib->{mandatory} );
186 my $class_sources = GetClassSources();
187 my $default_source = C4::Context->preference("DefaultClassificationSource");
189 foreach my $class_source (sort keys %$class_sources) {
190 next unless $class_sources->{$class_source}->{'used'} or
191 ($value and $class_source eq $value) or
192 ($class_source eq $default_source);
193 push @authorised_values, $class_source;
194 $authorised_lib{$class_source} = $class_sources->{$class_source}->{'description'};
196 $value = $default_source unless ($value);
198 #---- "true" authorised value
200 else {
201 push @authorised_values, "" unless ( $subfieldlib->{mandatory} );
202 $authorised_values_sth->execute( $subfieldlib->{authorised_value} );
203 while ( my ( $value, $lib ) = $authorised_values_sth->fetchrow_array ) {
204 push @authorised_values, $value;
205 $authorised_lib{$value} = $lib;
209 $subfield_data{marc_value} =CGI::scrolling_list( # FIXME: factor out scrolling_list
210 -name => "field_value",
211 -values => \@authorised_values,
212 -default => $value,
213 -labels => \%authorised_lib,
214 -override => 1,
215 -size => 1,
216 -multiple => 0,
217 -tabindex => 1,
218 -id => "tag_".$tag."_subfield_".$subfieldtag."_".$index_subfield,
219 -class => "input_marceditor",
222 # it's a thesaurus / authority field
224 elsif ( $subfieldlib->{authtypecode} ) {
225 $subfield_data{marc_value} = "<input type=\"text\" $attributes />
226 <a href=\"#\" class=\"buttonDot\"
227 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>
229 # it's a plugin field
231 elsif ( $subfieldlib->{value_builder} ) {
232 # opening plugin
233 my $plugin = C4::Context->intranetdir . "/cataloguing/value_builder/" . $subfieldlib->{'value_builder'};
234 if (do $plugin) {
235 my $extended_param = plugin_parameters( $dbh, $temp, $tagslib, $subfield_data{id}, $loop_data );
236 my ( $function_name, $javascript ) = plugin_javascript( $dbh, $temp, $tagslib, $subfield_data{id}, $loop_data );
237 my $change = index($javascript, 'function Change') > -1 ?
238 "return Change$function_name($subfield_data{random}, '$subfield_data{id}');" :
239 'return 1;';
240 $subfield_data{marc_value} = qq[<input $attributes
241 onfocus="Focus$function_name($subfield_data{random}, '$subfield_data{id}');"
242 onchange=" $change"
243 onblur=" Blur$function_name($subfield_data{random}, '$subfield_data{id}');" />
244 <a href="#" class="buttonDot" onclick="Clic$function_name('$subfield_data{id}'); return false;" title="Tag Editor">...</a>
245 $javascript];
246 } else {
247 warn "Plugin Failed: $plugin";
248 $subfield_data{marc_value} = "<input $attributes />"; # supply default input form
251 elsif ( $tag eq '' ) { # it's an hidden field
252 $subfield_data{marc_value} = qq(<input type="hidden" $attributes />);
254 elsif ( $subfieldlib->{'hidden'} ) { # FIXME: shouldn't input type be "hidden" ?
255 $subfield_data{marc_value} = qq(<input type="text" $attributes />);
257 elsif ( length($value) > 100
258 or (C4::Context->preference("marcflavour") eq "UNIMARC" and
259 300 <= $tag && $tag < 400 && $subfieldtag eq 'a' )
260 or (C4::Context->preference("marcflavour") eq "MARC21" and
261 500 <= $tag && $tag < 600 )
263 # oversize field (textarea)
264 $subfield_data{marc_value} = "<textarea $attributes_no_value>$value</textarea>\n";
265 } else {
266 # it's a standard field
267 $subfield_data{marc_value} = "<input $attributes />";
270 return \%subfield_data;
274 my $input = new CGI;
275 my $error = $input->param('error');
276 my $biblionumber = $input->param('biblionumber');
277 my $itemnumber = $input->param('itemnumber');
278 my $op = $input->param('op');
280 my $frameworkcode = &GetFrameworkCode($biblionumber);
282 # Defining which userflag is needing according to the framework currently used
283 my $userflags;
284 if (defined $input->param('frameworkcode')) {
285 $userflags = ($input->param('frameworkcode') eq 'FA') ? "fast_cataloging" : "edit_items";
288 if (not defined $userflags) {
289 $userflags = ($frameworkcode eq 'FA') ? "fast_cataloging" : "edit_items";
292 my ($template, $loggedinuser, $cookie)
293 = get_template_and_user({template_name => "cataloguing/additem.tt",
294 query => $input,
295 type => "intranet",
296 authnotrequired => 0,
297 flagsrequired => {editcatalogue => $userflags},
298 debug => 1,
302 my $today_iso = C4::Dates->today('iso');
303 $template->param(today_iso => $today_iso);
305 my $tagslib = &GetMarcStructure(1,$frameworkcode);
306 my $record = GetMarcBiblio($biblionumber);
307 my $oldrecord = TransformMarcToKoha($dbh,$record);
308 my $itemrecord;
309 my $nextop="additem";
310 my @errors; # store errors found while checking data BEFORE saving item.
311 #-------------------------------------------------------------------------------
312 if ($op eq "additem") {
313 #-------------------------------------------------------------------------------
314 # rebuild
315 my @tags = $input->param('tag');
316 my @subfields = $input->param('subfield');
317 my @values = $input->param('field_value');
318 # build indicator hash.
319 my @ind_tag = $input->param('ind_tag');
320 my @indicator = $input->param('indicator');
321 my $xml = TransformHtmlToXml(\@tags,\@subfields,\@values,\@indicator,\@ind_tag, 'ITEM');
322 my $record = MARC::Record::new_from_xml($xml, 'UTF-8');
324 # type of add
325 my $add_submit = $input->param('add_submit');
326 my $add_duplicate_submit = $input->param('add_duplicate_submit');
327 my $add_multiple_copies_submit = $input->param('add_multiple_copies_submit');
328 my $number_of_copies = $input->param('number_of_copies');
330 if (C4::Context->preference('autoBarcode') eq 'incremental') {
331 $record = _increment_barcode($record, $frameworkcode);
334 my $addedolditem = TransformMarcToKoha($dbh,$record);
336 # If we have to add or add & duplicate, we add the item
337 if ($add_submit || $add_duplicate_submit) {
338 # check for item barcode # being unique
339 my $exist_itemnumber = get_item_from_barcode($addedolditem->{'barcode'});
340 push @errors,"barcode_not_unique" if($exist_itemnumber);
341 # if barcode exists, don't create, but report The problem.
342 unless ($exist_itemnumber) {
343 my ($oldbiblionumber,$oldbibnum,$oldbibitemnum) = AddItemFromMarc($record,$biblionumber);
344 set_item_default_location($oldbibitemnum);
346 $nextop = "additem";
347 if ($exist_itemnumber) {
348 $itemrecord = $record;
352 # If we have to add & duplicate
353 if ($add_duplicate_submit) {
354 $itemrecord = $record;
355 if (C4::Context->preference('autoBarcode') eq 'incremental') {
356 $itemrecord = _increment_barcode($itemrecord, $frameworkcode);
358 else {
359 # we have to clear the barcode field in the duplicate item record to make way for the new one generated by the javascript plugin
360 my ($tagfield,$tagsubfield) = &GetMarcFromKohaField("items.barcode",$frameworkcode);
361 my $fieldItem = $itemrecord->field($tagfield);
362 $itemrecord->delete_field($fieldItem);
363 $fieldItem->delete_subfields($tagsubfield);
364 $itemrecord->insert_fields_ordered($fieldItem);
368 # If we have to add multiple copies
369 if ($add_multiple_copies_submit) {
371 use C4::Barcodes;
372 my $barcodeobj = C4::Barcodes->new;
373 my $oldbarcode = $addedolditem->{'barcode'};
374 my ($tagfield,$tagsubfield) = &GetMarcFromKohaField("items.barcode",$frameworkcode);
376 # If there is a barcode and we can't find him new values, we can't add multiple copies
377 my $testbarcode = $barcodeobj->next_value($oldbarcode) if $barcodeobj;
378 if ($oldbarcode && !$testbarcode) {
380 push @errors, "no_next_barcode";
381 $itemrecord = $record;
383 } else {
384 # We add each item
386 # For the first iteration
387 my $barcodevalue = $oldbarcode;
388 my $exist_itemnumber;
391 for (my $i = 0; $i < $number_of_copies;) {
393 # If there is a barcode
394 if ($barcodevalue) {
396 # Getting a new barcode (if it is not the first iteration or the barcode we tried already exists)
397 $barcodevalue = $barcodeobj->next_value($oldbarcode) if ($i > 0 || $exist_itemnumber);
399 # Putting it into the record
400 if ($barcodevalue) {
401 $record->field($tagfield)->update($tagsubfield => $barcodevalue);
404 # Checking if the barcode already exists
405 $exist_itemnumber = get_item_from_barcode($barcodevalue);
408 # Adding the item
409 if (!$exist_itemnumber) {
410 my ($oldbiblionumber,$oldbibnum,$oldbibitemnum) = AddItemFromMarc($record,$biblionumber);
411 set_item_default_location($oldbibitemnum);
413 # We count the item only if it was really added
414 # That way, all items are added, even if there was some already existing barcodes
415 # FIXME : Please note that there is a risk of infinite loop here if we never find a suitable barcode
416 $i++;
419 # Preparing the next iteration
420 $oldbarcode = $barcodevalue;
422 undef($itemrecord);
425 if ($frameworkcode eq 'FA' && $input->param('borrowernumber')){
426 my $redirect_string = 'borrowernumber=' . uri_escape($input->param('borrowernumber')) .
427 '&barcode=' . uri_escape($input->param('barcode'));
428 $redirect_string .= '&duedatespec=' . uri_escape($input->param('duedatespec')) .
429 '&stickyduedate=1';
430 print $input->redirect("/cgi-bin/koha/circ/circulation.pl?" . $redirect_string);
431 exit;
435 #-------------------------------------------------------------------------------
436 } elsif ($op eq "edititem") {
437 #-------------------------------------------------------------------------------
438 # retrieve item if exist => then, it's a modif
439 $itemrecord = C4::Items::GetMarcItem($biblionumber,$itemnumber);
440 $nextop = "saveitem";
441 #-------------------------------------------------------------------------------
442 } elsif ($op eq "delitem") {
443 #-------------------------------------------------------------------------------
444 # check that there is no issue on this item before deletion.
445 $error = &DelItemCheck($dbh,$biblionumber,$itemnumber);
446 if($error == 1){
447 print $input->redirect("additem.pl?biblionumber=$biblionumber&frameworkcode=$frameworkcode");
448 }else{
449 push @errors,$error;
450 $nextop="additem";
452 #-------------------------------------------------------------------------------
453 } elsif ($op eq "delallitems") {
454 #-------------------------------------------------------------------------------
455 my @biblioitems = &GetBiblioItemByBiblioNumber($biblionumber);
456 my $errortest=0;
457 my $itemfail;
458 foreach my $biblioitem (@biblioitems) {
459 my $items = &GetItemsByBiblioitemnumber( $biblioitem->{biblioitemnumber} );
461 foreach my $item (@$items) {
462 $error =&DelItemCheck( $dbh, $biblionumber, $item->{itemnumber} );
463 $itemfail =$item;
464 if($error == 1){
465 next
467 else {
468 push @errors,$error;
469 $errortest++
472 if($errortest > 0){
473 $nextop="additem";
475 else {
476 print $input->redirect("/cgi-bin/koha/catalogue/moredetail.pl?biblionumber=$biblionumber");
477 exit;
480 #-------------------------------------------------------------------------------
481 } elsif ($op eq "saveitem") {
482 #-------------------------------------------------------------------------------
483 # rebuild
484 my @tags = $input->param('tag');
485 my @subfields = $input->param('subfield');
486 my @values = $input->param('field_value');
487 # build indicator hash.
488 my @ind_tag = $input->param('ind_tag');
489 my @indicator = $input->param('indicator');
490 # my $itemnumber = $input->param('itemnumber');
491 my $xml = TransformHtmlToXml(\@tags,\@subfields,\@values,\@indicator,\@ind_tag,'ITEM');
492 my $itemtosave=MARC::Record::new_from_xml($xml, 'UTF-8');
493 # MARC::Record builded => now, record in DB
494 # warn "R: ".$record->as_formatted;
495 # check that the barcode don't exist already
496 my $addedolditem = TransformMarcToKoha($dbh,$itemtosave);
497 my $exist_itemnumber = get_item_from_barcode($addedolditem->{'barcode'});
498 if ($exist_itemnumber && $exist_itemnumber != $itemnumber) {
499 push @errors,"barcode_not_unique";
500 } else {
501 my ($oldbiblionumber,$oldbibnum,$oldbibitemnum) = ModItemFromMarc($itemtosave,$biblionumber,$itemnumber);
502 $itemnumber="";
504 $nextop="additem";
508 #-------------------------------------------------------------------------------
509 # build screen with existing items. and "new" one
510 #-------------------------------------------------------------------------------
512 # now, build existiing item list
513 my $temp = GetMarcBiblio( $biblionumber );
514 #my @fields = $record->fields();
515 my %witness; #---- stores the list of subfields used at least once, with the "meaning" of the code
516 my @big_array;
517 #---- finds where items.itemnumber is stored
518 my ( $itemtagfield, $itemtagsubfield) = &GetMarcFromKohaField("items.itemnumber", $frameworkcode);
519 my ($branchtagfield, $branchtagsubfield) = &GetMarcFromKohaField("items.homebranch", $frameworkcode);
520 C4::Biblio::EmbedItemsInMarcBiblio($temp, $biblionumber);
521 my @fields = $temp->fields();
523 foreach my $field (@fields) {
524 next if ( $field->tag() < 10 );
526 my @subf = $field->subfields or (); # don't use ||, as that forces $field->subfelds to be interpreted in scalar context
527 my %this_row;
528 # loop through each subfield
529 my $i = 0;
530 foreach my $subfield (@subf){
531 my $subfieldcode = $subfield->[0];
532 my $subfieldvalue= $subfield->[1];
534 next if ($tagslib->{$field->tag()}->{$subfieldcode}->{tab} ne 10
535 && ($field->tag() ne $itemtagfield
536 && $subfieldcode ne $itemtagsubfield));
537 $witness{$subfieldcode} = $tagslib->{$field->tag()}->{$subfieldcode}->{lib} if ($tagslib->{$field->tag()}->{$subfieldcode}->{tab} eq 10);
538 if ($tagslib->{$field->tag()}->{$subfieldcode}->{tab} eq 10) {
539 $this_row{$subfieldcode} .= " | " if($this_row{$subfieldcode});
540 $this_row{$subfieldcode} .= GetAuthorisedValueDesc( $field->tag(),
541 $subfieldcode, $subfieldvalue, '', $tagslib)
542 || $subfieldvalue;
545 if (($field->tag eq $branchtagfield) && ($subfieldcode eq $branchtagsubfield) && C4::Context->preference("IndependantBranches")) {
546 #verifying rights
547 my $userenv = C4::Context->userenv();
548 unless (($userenv->{'flags'} == 1) or (($userenv->{'branch'} eq $subfieldvalue))){
549 $this_row{'nomod'} = 1;
552 $this_row{itemnumber} = $subfieldvalue if ($field->tag() eq $itemtagfield && $subfieldcode eq $itemtagsubfield);
554 if (%this_row) {
555 push(@big_array, \%this_row);
559 my ($holdingbrtagf,$holdingbrtagsubf) = &GetMarcFromKohaField("items.holdingbranch",$frameworkcode);
560 @big_array = sort {$a->{$holdingbrtagsubf} cmp $b->{$holdingbrtagsubf}} @big_array;
562 # now, construct template !
563 # First, the existing items for display
564 my @item_value_loop;
565 my @header_value_loop;
566 for my $row ( @big_array ) {
567 my %row_data;
568 my @item_fields = map +{ field => $_ || '' }, @$row{ sort keys(%witness) };
569 $row_data{item_value} = [ @item_fields ];
570 $row_data{itemnumber} = $row->{itemnumber};
571 #reporting this_row values
572 $row_data{'nomod'} = $row->{'nomod'};
573 push(@item_value_loop,\%row_data);
575 foreach my $subfield_code (sort keys(%witness)) {
576 my %header_value;
577 $header_value{header_value} = $witness{$subfield_code};
578 push(@header_value_loop, \%header_value);
581 # now, build the item form for entering a new item
582 my @loop_data =();
583 my $i=0;
585 my $pref_itemcallnumber = C4::Context->preference('itemcallnumber');
587 my $onlymine = C4::Context->preference('IndependantBranches') &&
588 C4::Context->userenv &&
589 C4::Context->userenv->{flags}!=1 &&
590 C4::Context->userenv->{branch};
591 my $branch = C4::Context->userenv->{branch};
592 if ($frameworkcode eq 'FA'){
593 $branch = $input->param('branch');
595 my $branches = GetBranchesLoop($branch,$onlymine); # build once ahead of time, instead of multiple times later.
597 # We generate form, from actuel record
598 @fields = ();
599 if($itemrecord){
600 foreach my $field ($itemrecord->fields()){
601 my $tag = $field->{_tag};
602 foreach my $subfield ( $field->subfields() ){
604 my $subfieldtag = $subfield->[0];
605 my $value = $subfield->[1];
606 my $subfieldlib = $tagslib->{$tag}->{$subfieldtag};
608 next if subfield_is_koha_internal_p($subfieldtag);
609 next if ($tagslib->{$tag}->{$subfieldtag}->{'tab'} ne "10");
611 my $subfield_data = generate_subfield_form($tag, $subfieldtag, $value, $tagslib, $subfieldlib, $branches, $today_iso, $biblionumber, $temp, \@loop_data, $i);
613 push @fields, "$tag$subfieldtag";
614 push (@loop_data, $subfield_data);
615 $i++;
620 # and now we add fields that are empty
622 foreach my $tag ( keys %{$tagslib}){
623 foreach my $subtag (keys %{$tagslib->{$tag}}){
624 next if subfield_is_koha_internal_p($subtag);
625 next if ($tagslib->{$tag}->{$subtag}->{'tab'} ne "10");
626 next if any { /^$tag$subtag$/ } @fields;
628 my @values = (undef);
629 @values = $itemrecord->field($tag)->subfield($subtag) if ($itemrecord && defined($itemrecord->field($tag)->subfield($subtag)));
630 for my $value (@values){
631 my $subfield_data = generate_subfield_form($tag, $subtag, $value, $tagslib, $tagslib->{$tag}->{$subtag}, $branches, $today_iso, $biblionumber, $temp, \@loop_data, $i);
632 push (@loop_data, $subfield_data);
633 $i++;
637 @loop_data = sort {$a->{subfield} cmp $b->{subfield} } @loop_data;
639 # what's the next op ? it's what we are not in : an add if we're editing, otherwise, and edit.
640 $template->param( title => $record->title() ) if ($record ne "-1");
641 $template->param(
642 biblionumber => $biblionumber,
643 title => $oldrecord->{title},
644 author => $oldrecord->{author},
645 item_loop => \@item_value_loop,
646 item_header_loop => \@header_value_loop,
647 item => \@loop_data,
648 itemnumber => $itemnumber,
649 itemtagfield => $itemtagfield,
650 itemtagsubfield => $itemtagsubfield,
651 op => $nextop,
652 opisadd => ($nextop eq "saveitem") ? 0 : 1,
653 C4::Search::enabled_staff_search_views,
656 if ($frameworkcode eq 'FA'){
657 $template->{VARS}->{'borrowernumber'}=$input->param('borrowernumber');
658 $template->{VARS}->{'barcode'}=$input->param('barcode');
659 $template->{VARS}->{'stickyduedate'}=$input->param('stickduedate');
660 $template->{VARS}->{'duedatespec'}=$input->param('duedatespec');
663 foreach my $error (@errors) {
664 $template->param($error => 1);
666 output_html_with_http_headers $input, $cookie, $template->output;