4 # Copyright 2000-2002 Katipo Communications
6 # This file is part of Koha.
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
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.
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.
22 #use warnings; FIXME - Bug 2505
29 use C4::Koha; # XXX subfield_is_koha_internal_p
30 use C4::Branch; # XXX subfield_is_koha_internal_p
36 my $dbh = C4::Context->dbh;
39 my ($tagfield,$insubfield,$record) = @_;
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);
51 return($indicator,$result);
54 sub get_item_from_barcode {
56 my $dbh=C4::Context->dbh;
58 my $rq=$dbh->prepare("SELECT itemnumber from items where items.barcode=?");
59 $rq->execute($barcode);
60 ($result)=$rq->fetchrow;
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);
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;
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);
95 my $error = $input->param('error');
96 my $biblionumber = $input->param('biblionumber');
97 my $itemnumber = $input->param('itemnumber');
98 my $op = $input->param('op');
100 my $frameworkcode = &GetFrameworkCode($biblionumber);
102 # Defining which userflag is needing according to the framework currently used
104 if (defined $input->param('frameworkcode')) {
105 $userflags = ($input->param('frameworkcode') eq 'FA') ? "fast_cataloging" : "edit_items";
108 if (not defined $userflags) {
109 $userflags = ($frameworkcode eq 'FA') ? "fast_cataloging" : "edit_items";
112 my ($template, $loggedinuser, $cookie)
113 = get_template_and_user({template_name => "cataloguing/additem.tmpl",
116 authnotrequired => 0,
117 flagsrequired => {editcatalogue => $userflags},
122 my $today_iso = C4::Dates->today('iso');
123 $template->param(today_iso => $today_iso);
125 my $tagslib = &GetMarcStructure(1,$frameworkcode);
126 my $record = GetMarcBiblio($biblionumber);
127 my $oldrecord = TransformMarcToKoha($dbh,$record);
129 my $nextop="additem";
130 my @errors; # store errors found while checking data BEFORE saving item.
131 #-------------------------------------------------------------------------------
132 if ($op eq "additem") {
133 #-------------------------------------------------------------------------------
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');
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');
150 if (C4::Context->preference('autoBarcode') eq 'incremental') {
151 $record = _increment_barcode($record, $frameworkcode);
154 my $addedolditem = TransformMarcToKoha($dbh,$record);
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);
167 if ($exist_itemnumber) {
168 $itemrecord = $record;
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);
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);
188 # If we have to add multiple copies
189 if ($add_multiple_copies_submit) {
192 my $barcodeobj = C4::Barcodes->new;
193 my $oldbarcode = $addedolditem->{'barcode'};
194 my ($tagfield,$tagsubfield) = &GetMarcFromKohaField("items.barcode",$frameworkcode);
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) {
200 push @errors, "no_next_barcode";
201 $itemrecord = $record;
206 # For the first iteration
207 my $barcodevalue = $oldbarcode;
208 my $exist_itemnumber;
211 for (my $i = 0; $i < $number_of_copies;) {
213 # If there is a barcode
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);
219 # Putting it into the record
221 $record->field($tagfield)->update($tagsubfield => $barcodevalue);
224 # Checking if the barcode already exists
225 $exist_itemnumber = get_item_from_barcode($barcodevalue);
229 if (!$exist_itemnumber) {
230 my ($oldbiblionumber,$oldbibnum,$oldbibitemnum) = AddItemFromMarc($record,$biblionumber);
231 set_item_default_location($oldbibitemnum);
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
239 # Preparing the next iteration
240 $oldbarcode = $barcodevalue;
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);
259 print $input->redirect("additem.pl?biblionumber=$biblionumber&frameworkcode=$frameworkcode");
264 #-------------------------------------------------------------------------------
265 } elsif ($op eq "delallitems") {
266 #-------------------------------------------------------------------------------
267 my @biblioitems = &GetBiblioItemByBiblioNumber($biblionumber);
268 foreach my $biblioitem (@biblioitems){
269 my $items = &GetItemsByBiblioitemnumber($biblioitem->{biblioitemnumber});
271 foreach my $item (@$items){
272 &DelItem($dbh,$biblionumber,$item->{itemnumber});
275 #-------------------------------------------------------------------------------
276 } elsif ($op eq "saveitem") {
277 #-------------------------------------------------------------------------------
279 my @tags = $input->param('tag');
280 my @subfields = $input->param('subfield');
281 my @values = $input->param('field_value');
282 # build indicator hash.
283 my @ind_tag = $input->param('ind_tag');
284 my @indicator = $input->param('indicator');
285 # my $itemnumber = $input->param('itemnumber');
286 my $xml = TransformHtmlToXml(\@tags,\@subfields,\@values,\@indicator,\@ind_tag,'ITEM');
287 my $itemtosave=MARC::Record::new_from_xml($xml, 'UTF-8');
288 # MARC::Record builded => now, record in DB
289 # warn "R: ".$record->as_formatted;
290 # check that the barcode don't exist already
291 my $addedolditem = TransformMarcToKoha($dbh,$itemtosave);
292 my $exist_itemnumber = get_item_from_barcode($addedolditem->{'barcode'});
293 if ($exist_itemnumber && $exist_itemnumber != $itemnumber) {
294 push @errors,"barcode_not_unique";
296 my ($oldbiblionumber,$oldbibnum,$oldbibitemnum) = ModItemFromMarc($itemtosave,$biblionumber,$itemnumber);
303 #-------------------------------------------------------------------------------
304 # build screen with existing items. and "new" one
305 #-------------------------------------------------------------------------------
307 # now, build existiing item list
308 my $temp = GetMarcBiblio( $biblionumber );
309 my @fields = $temp->fields();
310 #my @fields = $record->fields();
311 my %witness; #---- stores the list of subfields used at least once, with the "meaning" of the code
313 #---- finds where items.itemnumber is stored
314 my ( $itemtagfield, $itemtagsubfield) = &GetMarcFromKohaField("items.itemnumber", $frameworkcode);
315 my ($branchtagfield, $branchtagsubfield) = &GetMarcFromKohaField("items.homebranch", $frameworkcode);
317 foreach my $field (@fields) {
318 next if ($field->tag()<10);
319 my @subf = $field->subfields or (); # don't use ||, as that forces $field->subfelds to be interpreted in scalar context
321 # loop through each subfield
322 for my $i (0..$#subf) {
323 next if ($tagslib->{$field->tag()}->{$subf[$i][0]}->{tab} ne 10
324 && ($field->tag() ne $itemtagfield
325 && $subf[$i][0] ne $itemtagsubfield));
327 $witness{$subf[$i][0]} = $tagslib->{$field->tag()}->{$subf[$i][0]}->{lib} if ($tagslib->{$field->tag()}->{$subf[$i][0]}->{tab} eq 10);
328 if ($tagslib->{$field->tag()}->{$subf[$i][0]}->{tab} eq 10) {
329 $this_row{$subf[$i][0]}=GetAuthorisedValueDesc( $field->tag(),
330 $subf[$i][0], $subf[$i][1], '', $tagslib)
334 if (($field->tag eq $branchtagfield) && ($subf[$i][$0] eq $branchtagsubfield) && C4::Context->preference("IndependantBranches")) {
336 my $userenv = C4::Context->userenv();
337 unless (($userenv->{'flags'} == 1) or (($userenv->{'branch'} eq $subf[$i][1]))){
338 $this_row{'nomod'}=1;
341 $this_row{itemnumber} = $subf[$i][1] if ($field->tag() eq $itemtagfield && $subf[$i][0] eq $itemtagsubfield);
344 push(@big_array, \%this_row);
348 my ($holdingbrtagf,$holdingbrtagsubf) = &GetMarcFromKohaField("items.holdingbranch",$frameworkcode);
349 @big_array = sort {$a->{$holdingbrtagsubf} cmp $b->{$holdingbrtagsubf}} @big_array;
351 # now, construct template !
352 # First, the existing items for display
354 my @header_value_loop;
355 for my $row ( @big_array ) {
357 my @item_fields = map +{ field => $_ || '' }, @$row{ sort keys(%witness) };
358 $row_data{item_value} = [ @item_fields ];
359 $row_data{itemnumber} = $row->{itemnumber};
360 #reporting this_row values
361 $row_data{'nomod'} = $row->{'nomod'};
362 push(@item_value_loop,\%row_data);
364 foreach my $subfield_code (sort keys(%witness)) {
366 $header_value{header_value} = $witness{$subfield_code};
367 push(@header_value_loop, \%header_value);
370 # now, build the item form for entering a new item
374 my $branches = GetBranchesLoop(); # build once ahead of time, instead of multiple times later.
375 my $pref_itemcallnumber = C4::Context->preference('itemcallnumber');
377 # Getting the fields where the item location is
378 my ($location_field, $location_subfield) = GetMarcFromKohaField('items.location', $frameworkcode);
380 # Getting the name of the authorised values' category for item location
381 my $item_location_category = $tagslib->{$location_field}->{$location_subfield}->{'authorised_value'};
383 foreach my $tag (sort keys %{$tagslib}) {
384 # loop through each subfield
385 foreach my $subfield (sort keys %{$tagslib->{$tag}}) {
386 next if subfield_is_koha_internal_p($subfield);
387 next if ($tagslib->{$tag}->{$subfield}->{'tab'} ne "10");
390 my $index_subfield = int(rand(1000000));
391 if ($subfield eq '@'){
392 $subfield_data{id} = "tag_".$tag."_subfield_00_".$index_subfield;
394 $subfield_data{id} = "tag_".$tag."_subfield_".$subfield."_".$index_subfield;
396 $subfield_data{tag} = $tag;
397 $subfield_data{subfield} = $subfield;
398 $subfield_data{random} = int(rand(1000000)); # why do we need 2 different randoms?
399 # $subfield_data{marc_lib} = $tagslib->{$tag}->{$subfield}->{lib};
400 $subfield_data{marc_lib} ="<span id=\"error$i\" title=\"".$tagslib->{$tag}->{$subfield}->{lib}."\">".$tagslib->{$tag}->{$subfield}->{lib}."</span>";
401 $subfield_data{mandatory} = $tagslib->{$tag}->{$subfield}->{mandatory};
402 $subfield_data{repeatable} = $tagslib->{$tag}->{$subfield}->{repeatable};
404 ($x,$value) = find_value($tag,$subfield,$itemrecord) if ($itemrecord);
405 $value =~ s/"/"/g;
407 $value = $tagslib->{$tag}->{$subfield}->{defaultvalue};
408 # get today date & replace YYYY, MM, DD if provided in the default value
409 my ( $year, $month, $day ) = split ',', $today_iso; # FIXME: iso dates don't have commas!
410 $value =~ s/YYYY/$year/g;
411 $value =~ s/MM/$month/g;
412 $value =~ s/DD/$day/g;
414 $subfield_data{visibility} = "display:none;" if (($tagslib->{$tag}->{$subfield}->{hidden} > 4) || ($tagslib->{$tag}->{$subfield}->{hidden} < -4));
415 # testing branch value if IndependantBranches.
416 if (!$value && $tagslib->{$tag}->{$subfield}->{kohafield} eq 'items.itemcallnumber' && $pref_itemcallnumber) {
417 my $CNtag = substr($pref_itemcallnumber, 0, 3);
418 my $CNsubfield = substr($pref_itemcallnumber, 3, 1);
419 my $CNsubfield2 = substr($pref_itemcallnumber, 4, 1);
420 my $temp2 = $temp->field($CNtag);
422 $value = ($temp2->subfield($CNsubfield)).' '.($temp2->subfield($CNsubfield2));
423 #remove any trailing space incase one subfield is used
424 $value =~ s/^\s+|\s+$//g;
428 my $attributes_no_value = qq(id="$subfield_data{id}" name="field_value" class="input_marceditor" size="67" maxlength="255" );
429 my $attributes = qq($attributes_no_value value="$value" );
430 if ( $tagslib->{$tag}->{$subfield}->{authorised_value} ) {
431 my @authorised_values;
433 # builds list, depending on authorised value...
435 if ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "branches" ) {
436 foreach my $thisbranch (@$branches) {
437 push @authorised_values, $thisbranch->{value};
438 $authorised_lib{$thisbranch->{value}} = $thisbranch->{branchname};
439 # in edit item this is set to the data value otherwise use default
440 if ($op ne 'edititem' && $thisbranch->{selected} ) {
441 $value = $thisbranch->{value};
445 elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes" ) {
446 push @authorised_values, "" unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
447 my $sth = $dbh->prepare("select itemtype,description from itemtypes order by description");
449 while ( my ( $itemtype, $description ) = $sth->fetchrow_array ) {
450 push @authorised_values, $itemtype;
451 $authorised_lib{$itemtype} = $description;
455 my $itype_sth = $dbh->prepare("SELECT itemtype FROM biblioitems WHERE biblionumber = ?");
456 $itype_sth->execute( $biblionumber );
457 ( $value ) = $itype_sth->fetchrow_array;
462 elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "cn_source" ) {
463 push @authorised_values, "" unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
465 my $class_sources = GetClassSources();
466 my $default_source = C4::Context->preference("DefaultClassificationSource");
468 foreach my $class_source (sort keys %$class_sources) {
469 next unless $class_sources->{$class_source}->{'used'} or
470 ($value and $class_source eq $value) or
471 ($class_source eq $default_source);
472 push @authorised_values, $class_source;
473 $authorised_lib{$class_source} = $class_sources->{$class_source}->{'description'};
475 $value = $default_source unless ($value);
477 #---- "true" authorised value
480 push @authorised_values, "" unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
482 # Are we dealing with item location ?
483 my $item_location = ($tagslib->{$tag}->{$subfield}->{authorised_value} eq $item_location_category) ? 1 : 0;
485 # If so, we sort by authorised_value, else by libelle
486 my $orderby = $item_location ? 'authorised_value' : 'lib';
488 my $authorised_values_sth = $dbh->prepare("SELECT authorised_value,lib FROM authorised_values WHERE category=? ORDER BY $orderby");
490 $authorised_values_sth->execute( $tagslib->{$tag}->{$subfield}->{authorised_value});
493 while ( my ( $value, $lib ) = $authorised_values_sth->fetchrow_array ) {
494 push @authorised_values, $value;
495 if ($tagslib->{$tag}->{$subfield}->{authorised_value} eq $item_location_category) {
496 $authorised_lib{$value} = $value . " - " . $lib;
498 $authorised_lib{$value} = $lib;
501 # For item location, we show the code and the libelle
502 $authorised_lib{$value} = ($item_location) ? $value . " - " . $lib : $lib;
505 $subfield_data{marc_value} =CGI::scrolling_list( # FIXME: factor out scrolling_list
506 -name => "field_value",
507 -values => \@authorised_values,
509 -labels => \%authorised_lib,
514 -id => "tag_".$tag."_subfield_".$subfield."_".$index_subfield,
515 -class => "input_marceditor",
517 # it's a thesaurus / authority field
519 elsif ( $tagslib->{$tag}->{$subfield}->{authtypecode} ) {
520 $subfield_data{marc_value} = "<input type=\"text\" $attributes />
521 <a href=\"#\" class=\"buttonDot\"
522 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>
524 # it's a plugin field
526 elsif ( $tagslib->{$tag}->{$subfield}->{value_builder} ) {
528 my $plugin = C4::Context->intranetdir . "/cataloguing/value_builder/" . $tagslib->{$tag}->{$subfield}->{'value_builder'};
530 my $extended_param = plugin_parameters( $dbh, $temp, $tagslib, $subfield_data{id}, \@loop_data );
531 my ( $function_name, $javascript ) = plugin_javascript( $dbh, $temp, $tagslib, $subfield_data{id}, \@loop_data );
532 $subfield_data{marc_value} = qq[<input $attributes
533 onfocus="Focus$function_name($subfield_data{random}, '$subfield_data{id}');"
534 onblur=" Blur$function_name($subfield_data{random}, '$subfield_data{id}');" />
535 <a href="#" class="buttonDot" onclick="Clic$function_name('$subfield_data{id}'); return false;" title="Tag Editor">...</a>
538 warn "Plugin Failed: $plugin";
539 $subfield_data{marc_value} = "<input $attributes />"; # supply default input form
542 elsif ( $tag eq '' ) { # it's an hidden field
543 $subfield_data{marc_value} = qq(<input type="hidden" $attributes />);
545 elsif ( $tagslib->{$tag}->{$subfield}->{'hidden'} ) { # FIXME: shouldn't input type be "hidden" ?
546 $subfield_data{marc_value} = qq(<input type="text" $attributes />);
548 elsif ( length($value) > 100
549 or (C4::Context->preference("marcflavour") eq "UNIMARC" and
550 300 <= $tag && $tag < 400 && $subfield eq 'a' )
551 or (C4::Context->preference("marcflavour") eq "MARC21" and
552 500 <= $tag && $tag < 600 )
554 # oversize field (textarea)
555 $subfield_data{marc_value} = "<textarea $attributes_no_value>$value</textarea>\n";
557 # it's a standard field
558 $subfield_data{marc_value} = "<input $attributes />";
560 # $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\">";
561 push (@loop_data, \%subfield_data);
566 # what's the next op ? it's what we are not in : an add if we're editing, otherwise, and edit.
567 $template->param( title => $record->title() ) if ($record ne "-1");
569 biblionumber => $biblionumber,
570 title => $oldrecord->{title},
571 author => $oldrecord->{author},
572 item_loop => \@item_value_loop,
573 item_header_loop => \@header_value_loop,
575 itemnumber => $itemnumber,
576 itemtagfield => $itemtagfield,
577 itemtagsubfield => $itemtagsubfield,
579 opisadd => ($nextop eq "saveitem") ? 0 : 1,
580 C4::Search::enabled_staff_search_views,
582 foreach my $error (@errors) {
583 $template->param($error => 1);
585 output_html_with_http_headers $input, $cookie, $template->output;