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
37 my ($tagfield,$insubfield,$record) = @_;
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);
49 return($indicator,$result);
52 sub get_item_from_barcode
{
54 my $dbh=C4
::Context
->dbh;
56 my $rq=$dbh->prepare("SELECT itemnumber from items where items.barcode=?");
57 $rq->execute($barcode);
58 ($result)=$rq->fetchrow;
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);
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');
79 my $frameworkcode = &GetFrameworkCode
($biblionumber);
81 # Defining which userflag is needing according to the framework currently used
83 if (defined $input->param('frameworkcode')) {
84 $userflags = ($input->param('frameworkcode') eq 'FA') ?
"fast_cataloging" : "edit_catalogue";
87 if (not defined $userflags) {
88 $userflags = ($frameworkcode eq 'FA') ?
"fast_cataloging" : "edit_catalogue";
91 my ($template, $loggedinuser, $cookie)
92 = get_template_and_user
({template_name
=> "cataloguing/additem.tmpl",
96 flagsrequired
=> {editcatalogue
=> $userflags},
101 my $today_iso = C4
::Dates
->today('iso');
102 $template->param(today_iso
=> $today_iso);
104 my $tagslib = &GetMarcStructure
(1,$frameworkcode);
105 my $record = GetMarcBiblio
($biblionumber);
106 my $oldrecord = TransformMarcToKoha
($dbh,$record);
108 my $nextop="additem";
109 my @errors; # store errors found while checking data BEFORE saving item.
110 #-------------------------------------------------------------------------------
111 if ($op eq "additem") {
112 #-------------------------------------------------------------------------------
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');
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');
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;
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);
147 my $addedolditem = TransformMarcToKoha
($dbh,$record);
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);
160 if ($exist_itemnumber) {
161 $itemrecord = $record;
165 # If we have to add & duplicate
166 if ($add_duplicate_submit) {
168 # We try to get the next barcode
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
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)
179 $record->field($tagfield)->update($tagsubfield => '');
182 $itemrecord = $record;
185 # If we have to add multiple copies
186 if ($add_multiple_copies_submit) {
189 my $barcodeobj = C4
::Barcodes
->new;
190 my $oldbarcode = $addedolditem->{'barcode'};
191 my ($tagfield,$tagsubfield) = &GetMarcFromKohaField
("items.barcode",$frameworkcode);
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) {
197 push @errors, "no_next_barcode";
198 $itemrecord = $record;
203 # For the first iteration
204 my $barcodevalue = $oldbarcode;
205 my $exist_itemnumber;
208 for (my $i = 0; $i < $number_of_copies;) {
210 # If there is a barcode
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);
216 # Putting it into the record
218 $record->field($tagfield)->update($tagsubfield => $barcodevalue);
221 # Checking if the barcode already exists
222 $exist_itemnumber = get_item_from_barcode
($barcodevalue);
226 if (!$exist_itemnumber) {
227 my ($oldbiblionumber,$oldbibnum,$oldbibitemnum) = AddItemFromMarc
($record,$biblionumber);
228 set_item_default_location
($oldbibitemnum);
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
236 # Preparing the next iteration
237 $oldbarcode = $barcodevalue;
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);
256 print $input->redirect("additem.pl?biblionumber=$biblionumber&frameworkcode=$frameworkcode");
261 #-------------------------------------------------------------------------------
262 } elsif ($op eq "delallitems") {
263 #-------------------------------------------------------------------------------
264 my @biblioitems = &GetBiblioItemByBiblioNumber
($biblionumber);
265 foreach my $biblioitem (@biblioitems){
266 my $items = &GetItemsByBiblioitemnumber
($biblioitem->{biblioitemnumber
});
268 foreach my $item (@
$items){
269 &DelItem
($dbh,$biblionumber,$item->{itemnumber
});
272 #-------------------------------------------------------------------------------
273 } elsif ($op eq "saveitem") {
274 #-------------------------------------------------------------------------------
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";
293 my ($oldbiblionumber,$oldbibnum,$oldbibitemnum) = ModItemFromMarc
($itemtosave,$biblionumber,$itemnumber);
300 #-------------------------------------------------------------------------------
301 # build screen with existing items. and "new" one
302 #-------------------------------------------------------------------------------
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
310 #---- finds where items.itemnumber is stored
311 my ( $itemtagfield, $itemtagsubfield) = &GetMarcFromKohaField
("items.itemnumber", $frameworkcode);
312 my ($branchtagfield, $branchtagsubfield) = &GetMarcFromKohaField
("items.homebranch", $frameworkcode);
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
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));
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)
331 if (($field->tag eq $branchtagfield) && ($subf[$i][$0] eq $branchtagsubfield) && C4
::Context
->preference("IndependantBranches")) {
333 my $userenv = C4
::Context
->userenv();
334 unless (($userenv->{'flags'} == 1) or (($userenv->{'branch'} eq $subf[$i][1]))){
335 $this_row{'nomod'}=1;
338 $this_row{itemnumber
} = $subf[$i][1] if ($field->tag() eq $itemtagfield && $subf[$i][0] eq $itemtagsubfield);
341 push(@big_array, \
%this_row);
345 my ($holdingbrtagf,$holdingbrtagsubf) = &GetMarcFromKohaField
("items.holdingbranch",$frameworkcode);
346 @big_array = sort {$a->{$holdingbrtagsubf} cmp $b->{$holdingbrtagsubf}} @big_array;
348 # now, construct template !
349 # First, the existing items for display
351 my @header_value_loop;
352 for my $row ( @big_array ) {
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);
361 foreach my $subfield_code (sort keys(%witness)) {
363 $header_value{header_value
} = $witness{$subfield_code};
364 push(@header_value_loop, \
%header_value);
367 # now, build the item form for entering a new item
371 my $branches = GetBranchesLoop
(); # build once ahead of time, instead of multiple times later.
372 my $pref_itemcallnumber = C4
::Context
->preference('itemcallnumber');
374 # Getting the fields where the item location is
375 my ($location_field, $location_subfield) = GetMarcFromKohaField
('items.location', $frameworkcode);
377 # Getting the name of the authorised values' category for item location
378 my $item_location_category = $tagslib->{$location_field}->{$location_subfield}->{'authorised_value'};
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");
387 my $index_subfield = int(rand(1000000));
388 if ($subfield eq '@'){
389 $subfield_data{id
} = "tag_".$tag."_subfield_00_".$index_subfield;
391 $subfield_data{id
} = "tag_".$tag."_subfield_".$subfield."_".$index_subfield;
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
};
401 ($x,$value) = find_value
($tag,$subfield,$itemrecord) if ($itemrecord);
402 $value =~ s/"/"/g;
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;
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);
419 $value = ($temp2->subfield($CNsubfield)).' '.($temp2->subfield($CNsubfield2));
420 #remove any trailing space incase one subfield is used
421 $value =~ s/^\s+|\s+$//g;
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;
430 # builds list, depending on authorised value...
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};
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");
443 while ( my ( $itemtype, $description ) = $sth->fetchrow_array ) {
444 push @authorised_values, $itemtype;
445 $authorised_lib{$itemtype} = $description;
449 my $itype_sth = $dbh->prepare("SELECT itemtype FROM biblioitems WHERE biblionumber = ?");
450 $itype_sth->execute( $biblionumber );
451 ( $value ) = $itype_sth->fetchrow_array;
456 elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value
} eq "cn_source" ) {
457 push @authorised_values, "" unless ( $tagslib->{$tag}->{$subfield}->{mandatory
} );
459 my $class_sources = GetClassSources
();
460 my $default_source = C4
::Context
->preference("DefaultClassificationSource");
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'};
469 $value = $default_source unless ($value);
471 #---- "true" authorised value
474 push @authorised_values, "" unless ( $tagslib->{$tag}->{$subfield}->{mandatory
} );
476 # Are we dealing with item location ?
477 my $item_location = ($tagslib->{$tag}->{$subfield}->{authorised_value
} eq $item_location_category) ?
1 : 0;
479 # If so, we sort by authorised_value, else by libelle
480 my $orderby = $item_location ?
'authorised_value' : 'lib';
482 my $authorised_values_sth = $dbh->prepare("SELECT authorised_value,lib FROM authorised_values WHERE category=? ORDER BY $orderby");
484 $authorised_values_sth->execute( $tagslib->{$tag}->{$subfield}->{authorised_value
});
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;
492 $authorised_lib{$value} = $lib;
495 # For item location, we show the code and the libelle
496 $authorised_lib{$value} = ($item_location) ?
$value . " - " . $lib : $lib;
499 $subfield_data{marc_value
} =CGI
::scrolling_list
( # FIXME: factor out scrolling_list
500 -name
=> "field_value",
501 -values => \
@authorised_values,
503 -labels
=> \
%authorised_lib,
508 -id
=> "tag_".$tag."_subfield_".$subfield."_".$index_subfield,
509 -class => "input_marceditor",
511 # it's a thesaurus / authority field
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>
518 # it's a plugin field
520 elsif ( $tagslib->{$tag}->{$subfield}->{value_builder
} ) {
522 my $plugin = C4
::Context
->intranetdir . "/cataloguing/value_builder/" . $tagslib->{$tag}->{$subfield}->{'value_builder'};
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
>
532 warn "Plugin Failed: $plugin";
533 $subfield_data{marc_value
} = "<input $attributes />"; # supply default input form
536 elsif ( $tag eq '' ) { # it's an hidden field
537 $subfield_data{marc_value
} = qq(<input type
="hidden" $attributes />);
539 elsif ( $tagslib->{$tag}->{$subfield}->{'hidden'} ) { # FIXME: shouldn't input type be "hidden" ?
540 $subfield_data{marc_value
} = qq(<input type
="text" $attributes />);
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 )
548 # oversize field (textarea)
549 $subfield_data{marc_value
} = "<textarea $attributes_no_value>$value</textarea>\n";
551 # it's a standard field
552 $subfield_data{marc_value
} = "<input $attributes />";
554 # $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\">";
555 push (@loop_data, \
%subfield_data);
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");
563 biblionumber
=> $biblionumber,
564 title
=> $oldrecord->{title
},
565 author
=> $oldrecord->{author
},
566 item_loop
=> \
@item_value_loop,
567 item_header_loop
=> \
@header_value_loop,
569 itemnumber
=> $itemnumber,
570 itemtagfield
=> $itemtagfield,
571 itemtagsubfield
=> $itemtagsubfield,
573 opisadd
=> ($nextop eq "saveitem") ?
0 : 1,
574 C4
::Search
::enabled_staff_search_views
,
576 foreach my $error (@errors) {
577 $template->param($error => 1);
579 output_html_with_http_headers
$input, $cookie, $template->output;