Merge branch 'bug2505_patches' of git://git.catalyst.net.nz/koha into to-push
[koha.git] / cataloguing / additem.pl
1 #!/usr/bin/perl
2
3
4 # Copyright 2000-2002 Katipo Communications
5 #
6 # This file is part of Koha.
7 #
8 # Koha is free software; you can redistribute it and/or modify it under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
11 # version.
12 #
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License along
18 # with Koha; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20
21 use strict;
22 #use warnings; FIXME - Bug 2505
23 use CGI;
24 use C4::Auth;
25 use C4::Output;
26 use C4::Biblio;
27 use C4::Items;
28 use C4::Context;
29 use C4::Koha; # XXX subfield_is_koha_internal_p
30 use C4::Branch; # XXX subfield_is_koha_internal_p
31 use C4::ClassSource;
32 use C4::Dates;
33
34 use MARC::File::XML;
35
36 sub find_value {
37     my ($tagfield,$insubfield,$record) = @_;
38     my $result;
39     my $indicator;
40     foreach my $field ($record->field($tagfield)) {
41         my @subfields = $field->subfields();
42         foreach my $subfield (@subfields) {
43             if (@$subfield[0] eq $insubfield) {
44                 $result .= @$subfield[1];
45                 $indicator = $field->indicator(1).$field->indicator(2);
46             }
47         }
48     }
49     return($indicator,$result);
50 }
51
52 sub get_item_from_barcode {
53     my ($barcode)=@_;
54     my $dbh=C4::Context->dbh;
55     my $result;
56     my $rq=$dbh->prepare("SELECT itemnumber from items where items.barcode=?");
57     $rq->execute($barcode);
58     ($result)=$rq->fetchrow;
59     return($result);
60 }
61
62 sub set_item_default_location {
63     my $itemnumber = shift;
64     if ( C4::Context->preference('NewItemsDefaultLocation') ) {
65         my $item = GetItem( $itemnumber );
66         $item->{'permanent_location'} = $item->{'location'};
67         $item->{'location'} = C4::Context->preference('NewItemsDefaultLocation');
68         ModItem( $item, undef, $itemnumber);
69     }
70 }
71
72 my $input = new CGI;
73 my $dbh = C4::Context->dbh;
74 my $error        = $input->param('error');
75 my $biblionumber = $input->param('biblionumber');
76 my $itemnumber   = $input->param('itemnumber');
77 my $op           = $input->param('op');
78
79 my $frameworkcode = &GetFrameworkCode($biblionumber);
80
81 # Defining which userflag is needing according to the framework currently used
82 my $userflags;
83 if (defined $input->param('frameworkcode')) {
84     $userflags = ($input->param('frameworkcode') eq 'FA') ? "fast_cataloging" : "edit_catalogue";
85 }
86
87 if (not defined $userflags) {
88     $userflags = ($frameworkcode eq 'FA') ? "fast_cataloging" : "edit_catalogue";
89 }
90
91 my ($template, $loggedinuser, $cookie)
92     = get_template_and_user({template_name => "cataloguing/additem.tmpl",
93                  query => $input,
94                  type => "intranet",
95                  authnotrequired => 0,
96                  flagsrequired => {editcatalogue => $userflags},
97                  debug => 1,
98                  });
99
100
101 my $today_iso = C4::Dates->today('iso');
102 $template->param(today_iso => $today_iso);
103
104 my $tagslib = &GetMarcStructure(1,$frameworkcode);
105 my $record = GetMarcBiblio($biblionumber);
106 my $oldrecord = TransformMarcToKoha($dbh,$record);
107 my $itemrecord;
108 my $nextop="additem";
109 my @errors; # store errors found while checking data BEFORE saving item.
110 #-------------------------------------------------------------------------------
111 if ($op eq "additem") {
112 #-------------------------------------------------------------------------------
113     # rebuild
114     my @tags      = $input->param('tag');
115     my @subfields = $input->param('subfield');
116     my @values    = $input->param('field_value');
117     # build indicator hash.
118     my @ind_tag   = $input->param('ind_tag');
119     my @indicator = $input->param('indicator');
120     my $xml = TransformHtmlToXml(\@tags,\@subfields,\@values,\@indicator,\@ind_tag, 'ITEM');
121     my $record = MARC::Record::new_from_xml($xml, 'UTF-8');
122
123     # type of add
124     my $add_submit                 = $input->param('add_submit');
125     my $add_duplicate_submit       = $input->param('add_duplicate_submit');
126     my $add_multiple_copies_submit = $input->param('add_multiple_copies_submit');
127     my $number_of_copies           = $input->param('number_of_copies');
128
129     # if autoBarcode is set to 'incremental', calculate barcode...
130         # NOTE: This code is subject to change in 3.2 with the implemenation of ajax based autobarcode code
131         # NOTE: 'incremental' is the ONLY autoBarcode option available to those not using javascript
132     if (C4::Context->preference('autoBarcode') eq 'incremental') {
133         my ($tagfield,$tagsubfield) = &GetMarcFromKohaField("items.barcode",$frameworkcode);
134         unless ($record->field($tagfield)->subfield($tagsubfield)) {
135             my $sth_barcode = $dbh->prepare("select max(abs(barcode)) from items");
136             $sth_barcode->execute;
137             my ($newbarcode) = $sth_barcode->fetchrow;
138             $newbarcode++;
139             # OK, we have the new barcode, now create the entry in MARC record
140             my $fieldItem = $record->field($tagfield);
141             $record->delete_field($fieldItem);
142             $fieldItem->add_subfields($tagsubfield => $newbarcode);
143             $record->insert_fields_ordered($fieldItem);
144         }
145     }
146
147     my $addedolditem = TransformMarcToKoha($dbh,$record);
148
149     # If we have to add or add & duplicate, we add the item
150     if ($add_submit || $add_duplicate_submit) {
151         # check for item barcode # being unique
152         my $exist_itemnumber = get_item_from_barcode($addedolditem->{'barcode'});
153         push @errors,"barcode_not_unique" if($exist_itemnumber);
154         # if barcode exists, don't create, but report The problem.
155     unless ($exist_itemnumber) {
156             my ($oldbiblionumber,$oldbibnum,$oldbibitemnum) = AddItemFromMarc($record,$biblionumber);
157         set_item_default_location($oldbibitemnum);
158     }
159         $nextop = "additem";
160         if ($exist_itemnumber) {
161             $itemrecord = $record;
162         }
163     }
164
165     # If we have to add & duplicate
166     if ($add_duplicate_submit) {
167
168         # We try to get the next barcode
169         use C4::Barcodes;
170         my $barcodeobj = C4::Barcodes->new;
171         my $barcodevalue = $barcodeobj->next_value($addedolditem->{'barcode'}) if $barcodeobj;
172         my ($tagfield,$tagsubfield) = &GetMarcFromKohaField("items.barcode",$frameworkcode);
173         if ($record->field($tagfield)->subfield($tagsubfield)) {
174             # If we got the next codebar value, we put it in the record
175             if ($barcodevalue) {
176                 $record->field($tagfield)->update($tagsubfield => $barcodevalue);
177             # If not, we delete the recently inserted barcode from the record (so the user can input a barcode himself)
178             } else {
179                 $record->field($tagfield)->update($tagsubfield => '');
180             }
181         }
182         $itemrecord = $record;
183     }
184
185     # If we have to add multiple copies
186     if ($add_multiple_copies_submit) {
187
188         use C4::Barcodes;
189         my $barcodeobj = C4::Barcodes->new;
190         my $oldbarcode = $addedolditem->{'barcode'};
191         my ($tagfield,$tagsubfield) = &GetMarcFromKohaField("items.barcode",$frameworkcode);
192
193         # If there is a barcode and we can't find him new values, we can't add multiple copies
194         my $testbarcode = $barcodeobj->next_value($oldbarcode) if $barcodeobj;
195         if ($oldbarcode && !$testbarcode) {
196
197             push @errors, "no_next_barcode";
198             $itemrecord = $record;
199
200         } else {
201         # We add each item
202
203             # For the first iteration
204             my $barcodevalue = $oldbarcode;
205             my $exist_itemnumber;
206
207
208             for (my $i = 0; $i < $number_of_copies;) {
209
210                 # If there is a barcode
211                 if ($barcodevalue) {
212
213                     # Getting a new barcode (if it is not the first iteration or the barcode we tried already exists)
214                     $barcodevalue = $barcodeobj->next_value($oldbarcode) if ($i > 0 || $exist_itemnumber);
215
216                     # Putting it into the record
217                     if ($barcodevalue) {
218                         $record->field($tagfield)->update($tagsubfield => $barcodevalue);
219                     }
220
221                     # Checking if the barcode already exists
222                     $exist_itemnumber = get_item_from_barcode($barcodevalue);
223                 }
224
225                 # Adding the item
226         if (!$exist_itemnumber) {
227             my ($oldbiblionumber,$oldbibnum,$oldbibitemnum) = AddItemFromMarc($record,$biblionumber);
228             set_item_default_location($oldbibitemnum);
229
230             # We count the item only if it was really added
231             # That way, all items are added, even if there was some already existing barcodes
232             # FIXME : Please note that there is a risk of infinite loop here if we never find a suitable barcode
233             $i++;
234         }
235
236                 # Preparing the next iteration
237                 $oldbarcode = $barcodevalue;
238             }
239             undef($itemrecord);
240         }
241     }
242
243
244 #-------------------------------------------------------------------------------
245 } elsif ($op eq "edititem") {
246 #-------------------------------------------------------------------------------
247 # retrieve item if exist => then, it's a modif
248     $itemrecord = C4::Items::GetMarcItem($biblionumber,$itemnumber);
249     $nextop = "saveitem";
250 #-------------------------------------------------------------------------------
251 } elsif ($op eq "delitem") {
252 #-------------------------------------------------------------------------------
253     # check that there is no issue on this item before deletion.
254     $error = &DelItemCheck($dbh,$biblionumber,$itemnumber);
255     if($error == 1){
256         print $input->redirect("additem.pl?biblionumber=$biblionumber&frameworkcode=$frameworkcode");
257     }else{
258         push @errors,$error;
259         $nextop="additem";
260     }
261 #-------------------------------------------------------------------------------
262 } elsif ($op eq "delallitems") {
263 #-------------------------------------------------------------------------------
264     my @biblioitems = &GetBiblioItemByBiblioNumber($biblionumber);
265     foreach my $biblioitem (@biblioitems){
266         my $items = &GetItemsByBiblioitemnumber($biblioitem->{biblioitemnumber});
267
268         foreach my $item (@$items){
269             &DelItem($dbh,$biblionumber,$item->{itemnumber});
270         }
271         }
272 #-------------------------------------------------------------------------------
273 } elsif ($op eq "saveitem") {
274 #-------------------------------------------------------------------------------
275     # rebuild
276     my @tags      = $input->param('tag');
277     my @subfields = $input->param('subfield');
278     my @values    = $input->param('field_value');
279     # build indicator hash.
280     my @ind_tag   = $input->param('ind_tag');
281     my @indicator = $input->param('indicator');
282     # my $itemnumber = $input->param('itemnumber');
283     my $xml = TransformHtmlToXml(\@tags,\@subfields,\@values,\@indicator,\@ind_tag,'ITEM');
284     my $itemtosave=MARC::Record::new_from_xml($xml, 'UTF-8');
285     # MARC::Record builded => now, record in DB
286     # warn "R: ".$record->as_formatted;
287     # check that the barcode don't exist already
288     my $addedolditem = TransformMarcToKoha($dbh,$itemtosave);
289     my $exist_itemnumber = get_item_from_barcode($addedolditem->{'barcode'});
290     if ($exist_itemnumber && $exist_itemnumber != $itemnumber) {
291         push @errors,"barcode_not_unique";
292     } else {
293         my ($oldbiblionumber,$oldbibnum,$oldbibitemnum) = ModItemFromMarc($itemtosave,$biblionumber,$itemnumber);
294         $itemnumber="";
295     }
296     $nextop="additem";
297 }
298
299 #
300 #-------------------------------------------------------------------------------
301 # build screen with existing items. and "new" one
302 #-------------------------------------------------------------------------------
303
304 # now, build existiing item list
305 my $temp = GetMarcBiblio( $biblionumber );
306 my @fields = $temp->fields();
307 #my @fields = $record->fields();
308 my %witness; #---- stores the list of subfields used at least once, with the "meaning" of the code
309 my @big_array;
310 #---- finds where items.itemnumber is stored
311 my (  $itemtagfield,   $itemtagsubfield) = &GetMarcFromKohaField("items.itemnumber", $frameworkcode);
312 my ($branchtagfield, $branchtagsubfield) = &GetMarcFromKohaField("items.homebranch", $frameworkcode);
313
314 foreach my $field (@fields) {
315     next if ($field->tag()<10);
316     my @subf = $field->subfields or (); # don't use ||, as that forces $field->subfelds to be interpreted in scalar context
317     my %this_row;
318 # loop through each subfield
319     for my $i (0..$#subf) {
320         next if ($tagslib->{$field->tag()}->{$subf[$i][0]}->{tab} ne 10 
321                 && ($field->tag() ne $itemtagfield 
322                 && $subf[$i][0]   ne $itemtagsubfield));
323
324         $witness{$subf[$i][0]} = $tagslib->{$field->tag()}->{$subf[$i][0]}->{lib} if ($tagslib->{$field->tag()}->{$subf[$i][0]}->{tab}  eq 10);
325                 if ($tagslib->{$field->tag()}->{$subf[$i][0]}->{tab}  eq 10) {
326                 $this_row{$subf[$i][0]}=GetAuthorisedValueDesc( $field->tag(),
327                         $subf[$i][0], $subf[$i][1], '', $tagslib) 
328                                                 || $subf[$i][1];
329                 }
330
331         if (($field->tag eq $branchtagfield) && ($subf[$i][$0] eq $branchtagsubfield) && C4::Context->preference("IndependantBranches")) {
332             #verifying rights
333             my $userenv = C4::Context->userenv();
334             unless (($userenv->{'flags'} == 1) or (($userenv->{'branch'} eq $subf[$i][1]))){
335                     $this_row{'nomod'}=1;
336             }
337         }
338         $this_row{itemnumber} = $subf[$i][1] if ($field->tag() eq $itemtagfield && $subf[$i][0] eq $itemtagsubfield);
339     }
340     if (%this_row) {
341         push(@big_array, \%this_row);
342     }
343 }
344
345 my ($holdingbrtagf,$holdingbrtagsubf) = &GetMarcFromKohaField("items.holdingbranch",$frameworkcode);
346 @big_array = sort {$a->{$holdingbrtagsubf} cmp $b->{$holdingbrtagsubf}} @big_array;
347
348 # now, construct template !
349 # First, the existing items for display
350 my @item_value_loop;
351 my @header_value_loop;
352 for my $row ( @big_array ) {
353     my %row_data;
354     my @item_fields = map +{ field => $_ || '' }, @$row{ sort keys(%witness) };
355     $row_data{item_value} = [ @item_fields ];
356     $row_data{itemnumber} = $row->{itemnumber};
357     #reporting this_row values
358     $row_data{'nomod'} = $row->{'nomod'};
359     push(@item_value_loop,\%row_data);
360 }
361 foreach my $subfield_code (sort keys(%witness)) {
362     my %header_value;
363     $header_value{header_value} = $witness{$subfield_code};
364     push(@header_value_loop, \%header_value);
365 }
366
367 # now, build the item form for entering a new item
368 my @loop_data =();
369 my $i=0;
370
371 my $branches = GetBranchesLoop();  # build once ahead of time, instead of multiple times later.
372 my $pref_itemcallnumber = C4::Context->preference('itemcallnumber');
373
374 # Getting the fields where the item location is
375 my ($location_field, $location_subfield) = GetMarcFromKohaField('items.location', $frameworkcode);
376
377 # Getting the name of the authorised values' category for item location
378 my $item_location_category = $tagslib->{$location_field}->{$location_subfield}->{'authorised_value'};
379
380 foreach my $tag (sort keys %{$tagslib}) {
381 # loop through each subfield
382   foreach my $subfield (sort keys %{$tagslib->{$tag}}) {
383     next if subfield_is_koha_internal_p($subfield);
384     next if ($tagslib->{$tag}->{$subfield}->{'tab'} ne "10");
385     my %subfield_data;
386  
387     my $index_subfield = int(rand(1000000)); 
388     if ($subfield eq '@'){
389         $subfield_data{id} = "tag_".$tag."_subfield_00_".$index_subfield;
390     } else {
391         $subfield_data{id} = "tag_".$tag."_subfield_".$subfield."_".$index_subfield;
392     }
393     $subfield_data{tag}        = $tag;
394     $subfield_data{subfield}   = $subfield;
395     $subfield_data{random}     = int(rand(1000000));    # why do we need 2 different randoms?
396 #   $subfield_data{marc_lib}   = $tagslib->{$tag}->{$subfield}->{lib};
397     $subfield_data{marc_lib}   ="<span id=\"error$i\" title=\"".$tagslib->{$tag}->{$subfield}->{lib}."\">".$tagslib->{$tag}->{$subfield}->{lib}."</span>";
398     $subfield_data{mandatory}  = $tagslib->{$tag}->{$subfield}->{mandatory};
399     $subfield_data{repeatable} = $tagslib->{$tag}->{$subfield}->{repeatable};
400     my ($x,$value);
401     ($x,$value) = find_value($tag,$subfield,$itemrecord) if ($itemrecord);
402     $value =~ s/"/&quot;/g;
403     unless ($value) {
404         $value = $tagslib->{$tag}->{$subfield}->{defaultvalue};
405         # get today date & replace YYYY, MM, DD if provided in the default value
406         my ( $year, $month, $day ) = split ',', $today_iso;     # FIXME: iso dates don't have commas!
407         $value =~ s/YYYY/$year/g;
408         $value =~ s/MM/$month/g;
409         $value =~ s/DD/$day/g;
410     }
411     $subfield_data{visibility} = "display:none;" if (($tagslib->{$tag}->{$subfield}->{hidden} > 4) || ($tagslib->{$tag}->{$subfield}->{hidden} < -4));
412     # testing branch value if IndependantBranches.
413     if (!$value && $tagslib->{$tag}->{$subfield}->{kohafield} eq 'items.itemcallnumber' && $pref_itemcallnumber) {
414         my $CNtag       = substr($pref_itemcallnumber, 0, 3);
415         my $CNsubfield  = substr($pref_itemcallnumber, 3, 1);
416         my $CNsubfield2 = substr($pref_itemcallnumber, 4, 1);
417         my $temp2 = $temp->field($CNtag);
418         if ($temp2) {
419             $value = ($temp2->subfield($CNsubfield)).' '.($temp2->subfield($CNsubfield2));
420             #remove any trailing space incase one subfield is used
421             $value =~ s/^\s+|\s+$//g;
422         }
423     }
424
425     my $attributes_no_value = qq(id="$subfield_data{id}" name="field_value" class="input_marceditor" size="67" maxlength="255" );
426     my $attributes          = qq($attributes_no_value value="$value" );
427     if ( $tagslib->{$tag}->{$subfield}->{authorised_value} ) {
428       my @authorised_values;
429       my %authorised_lib;
430       # builds list, depending on authorised value...
431   
432       if ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "branches" ) {
433           foreach my $thisbranch (@$branches) {
434               push @authorised_values, $thisbranch->{value};
435               $authorised_lib{$thisbranch->{value}} = $thisbranch->{branchname};
436              # $value = $thisbranch->{value} if $thisbranch->{selected};
437           }
438       }
439       elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes" ) {
440           push @authorised_values, "" unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
441           my $sth = $dbh->prepare("select itemtype,description from itemtypes order by description");
442           $sth->execute;
443           while ( my ( $itemtype, $description ) = $sth->fetchrow_array ) {
444               push @authorised_values, $itemtype;
445               $authorised_lib{$itemtype} = $description;
446           }
447
448           unless ( $value ) {
449               my $itype_sth = $dbh->prepare("SELECT itemtype FROM biblioitems WHERE biblionumber = ?");
450               $itype_sth->execute( $biblionumber );
451               ( $value ) = $itype_sth->fetchrow_array;
452           }
453   
454           #---- class_sources
455       }
456       elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "cn_source" ) {
457           push @authorised_values, "" unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
458             
459           my $class_sources = GetClassSources();
460           my $default_source = C4::Context->preference("DefaultClassificationSource");
461           
462           foreach my $class_source (sort keys %$class_sources) {
463               next unless $class_sources->{$class_source}->{'used'} or
464                           ($value and $class_source eq $value)      or
465                           ($class_source eq $default_source);
466               push @authorised_values, $class_source;
467               $authorised_lib{$class_source} = $class_sources->{$class_source}->{'description'};
468           }
469                   $value = $default_source unless ($value);
470
471           #---- "true" authorised value
472       }
473       else {
474           push @authorised_values, "" unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
475
476           # Are we dealing with item location ?
477           my $item_location = ($tagslib->{$tag}->{$subfield}->{authorised_value} eq $item_location_category) ? 1 : 0;
478
479           # If so, we sort by authorised_value, else by libelle
480           my $orderby = $item_location ? 'authorised_value' : 'lib';
481
482           my $authorised_values_sth = $dbh->prepare("SELECT authorised_value,lib FROM authorised_values WHERE category=? ORDER BY $orderby");
483
484           $authorised_values_sth->execute( $tagslib->{$tag}->{$subfield}->{authorised_value});
485
486
487           while ( my ( $value, $lib ) = $authorised_values_sth->fetchrow_array ) {
488             push @authorised_values, $value;
489                 if ($tagslib->{$tag}->{$subfield}->{authorised_value} eq $item_location_category) {
490                                 $authorised_lib{$value} = $value . " - " . $lib;
491                 } else {
492                                 $authorised_lib{$value} = $lib;
493                 }
494
495                 # For item location, we show the code and the libelle
496                 $authorised_lib{$value} = ($item_location) ? $value . " - " . $lib : $lib;
497           }
498       }
499       $subfield_data{marc_value} =CGI::scrolling_list(      # FIXME: factor out scrolling_list
500           -name     => "field_value",
501           -values   => \@authorised_values,
502           -default  => $value,
503           -labels   => \%authorised_lib,
504           -override => 1,
505           -size     => 1,
506           -multiple => 0,
507          # -tabindex => 1,
508           -id       => "tag_".$tag."_subfield_".$subfield."_".$index_subfield,
509           -class    => "input_marceditor",
510       );
511     # it's a thesaurus / authority field
512     }
513     elsif ( $tagslib->{$tag}->{$subfield}->{authtypecode} ) {
514         $subfield_data{marc_value} = "<input type=\"text\" $attributes />
515             <a href=\"#\" class=\"buttonDot\"
516                 onclick=\"Dopop('/cgi-bin/koha/authorities/auth_finder.pl?authtypecode=".$tagslib->{$tag}->{$subfield}->{authtypecode}."&index=$subfield_data{id}','$subfield_data{id}'); return false;\" title=\"Tag Editor\">...</a>
517     ";
518     # it's a plugin field
519     }
520     elsif ( $tagslib->{$tag}->{$subfield}->{value_builder} ) {
521         # opening plugin
522         my $plugin = C4::Context->intranetdir . "/cataloguing/value_builder/" . $tagslib->{$tag}->{$subfield}->{'value_builder'};
523         if (do $plugin) {
524             my $extended_param = plugin_parameters( $dbh, $temp, $tagslib, $subfield_data{id}, \@loop_data );
525             my ( $function_name, $javascript ) = plugin_javascript( $dbh, $temp, $tagslib, $subfield_data{id}, \@loop_data );
526             $subfield_data{marc_value} = qq[<input $attributes
527                 onfocus="Focus$function_name($subfield_data{random}, '$subfield_data{id}');"
528                  onblur=" Blur$function_name($subfield_data{random}, '$subfield_data{id}');" />
529                 <a href="#" class="buttonDot" onclick="Clic$function_name('$subfield_data{id}'); return false;" title="Tag Editor">...</a>
530                 $javascript];
531         } else {
532             warn "Plugin Failed: $plugin";
533             $subfield_data{marc_value} = "<input $attributes />"; # supply default input form
534         }
535     }
536     elsif ( $tag eq '' ) {       # it's an hidden field
537         $subfield_data{marc_value} = qq(<input type="hidden" $attributes />);
538     }
539     elsif ( $tagslib->{$tag}->{$subfield}->{'hidden'} ) {   # FIXME: shouldn't input type be "hidden" ?
540         $subfield_data{marc_value} = qq(<input type="text" $attributes />);
541     }
542     elsif ( length($value) > 100
543             or (C4::Context->preference("marcflavour") eq "UNIMARC" and
544                   300 <= $tag && $tag < 400 && $subfield eq 'a' )
545             or (C4::Context->preference("marcflavour") eq "MARC21"  and
546                   500 <= $tag && $tag < 600                     )
547           ) {
548         # oversize field (textarea)
549         $subfield_data{marc_value} = "<textarea $attributes_no_value>$value</textarea>\n";
550     } else {
551         # it's a standard field
552          $subfield_data{marc_value} = "<input $attributes />";
553     }
554 #   $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\">";
555     push (@loop_data, \%subfield_data);
556     $i++
557   }
558 }
559
560 # what's the next op ? it's what we are not in : an add if we're editing, otherwise, and edit.
561 $template->param( title => $record->title() ) if ($record ne "-1");
562 $template->param(
563     biblionumber => $biblionumber,
564     title        => $oldrecord->{title},
565     author       => $oldrecord->{author},
566     item_loop        => \@item_value_loop,
567     item_header_loop => \@header_value_loop,
568     item             => \@loop_data,
569     itemnumber       => $itemnumber,
570     itemtagfield     => $itemtagfield,
571     itemtagsubfield  => $itemtagsubfield,
572     op      => $nextop,
573     opisadd => ($nextop eq "saveitem") ? 0 : 1,
574     C4::Search::enabled_staff_search_views,
575 );
576 foreach my $error (@errors) {
577     $template->param($error => 1);
578 }
579 output_html_with_http_headers $input, $cookie, $template->output;