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