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 with
18 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
19 # Suite 330, Boston, MA 02111-1307 USA
28 use C4
::Koha
; # XXX subfield_is_koha_internal_p
29 use C4
::Branch
; # XXX subfield_is_koha_internal_p
36 my ($tagfield,$insubfield,$record) = @_;
39 foreach my $field ($record->field($tagfield)) {
40 my @subfields = $field->subfields();
41 foreach my $subfield (@subfields) {
42 if (@
$subfield[0] eq $insubfield) {
43 $result .= @
$subfield[1];
44 $indicator = $field->indicator(1).$field->indicator(2);
48 return($indicator,$result);
51 sub get_item_from_barcode
{
53 my $dbh=C4
::Context
->dbh;
55 my $rq=$dbh->prepare("SELECT itemnumber from items where items.barcode=?");
56 $rq->execute($barcode);
57 ($result)=$rq->fetchrow;
61 sub set_item_default_location
{
62 my $itemnumber = shift;
63 if ( C4
::Context
->preference('NewItemsDefaultLocation') ) {
64 my $item = GetItem
( $itemnumber );
65 $item->{'permanent_location'} = $item->{'location'};
66 $item->{'location'} = C4
::Context
->preference('NewItemsDefaultLocation');
67 ModItem
( $item, undef, $itemnumber);
72 my $dbh = C4
::Context
->dbh;
73 my $error = $input->param('error');
74 my $biblionumber = $input->param('biblionumber');
75 my $itemnumber = $input->param('itemnumber');
76 my $op = $input->param('op');
78 my $frameworkcode = &GetFrameworkCode
($biblionumber);
80 # Defining which userflag is needing according to the framework currently used
82 if (defined $input->param('frameworkcode')) {
83 $userflags = ($input->param('frameworkcode') eq 'FA') ?
"fast_cataloging" : "edit_catalogue";
86 if (not defined $userflags) {
87 $userflags = ($frameworkcode eq 'FA') ?
"fast_cataloging" : "edit_catalogue";
90 my ($template, $loggedinuser, $cookie)
91 = get_template_and_user
({template_name
=> "cataloguing/additem.tmpl",
95 flagsrequired
=> {editcatalogue
=> $userflags},
100 my $today_iso = C4
::Dates
->today('iso');
101 $template->param(today_iso
=> $today_iso);
103 my $tagslib = &GetMarcStructure
(1,$frameworkcode);
104 my $record = GetMarcBiblio
($biblionumber);
105 my $oldrecord = TransformMarcToKoha
($dbh,$record);
107 my $nextop="additem";
108 my @errors; # store errors found while checking data BEFORE saving item.
109 #-------------------------------------------------------------------------------
110 if ($op eq "additem") {
111 #-------------------------------------------------------------------------------
113 my @tags = $input->param('tag');
114 my @subfields = $input->param('subfield');
115 my @values = $input->param('field_value');
116 # build indicator hash.
117 my @ind_tag = $input->param('ind_tag');
118 my @indicator = $input->param('indicator');
119 my $xml = TransformHtmlToXml
(\
@tags,\
@subfields,\
@values,\
@indicator,\
@ind_tag, 'ITEM');
120 my $record = MARC
::Record
::new_from_xml
($xml, 'UTF-8');
123 my $add_submit = $input->param('add_submit');
124 my $add_duplicate_submit = $input->param('add_duplicate_submit');
125 my $add_multiple_copies_submit = $input->param('add_multiple_copies_submit');
126 my $number_of_copies = $input->param('number_of_copies');
128 # if autoBarcode is set to 'incremental', calculate barcode...
129 # NOTE: This code is subject to change in 3.2 with the implemenation of ajax based autobarcode code
130 # NOTE: 'incremental' is the ONLY autoBarcode option available to those not using javascript
131 if (C4
::Context
->preference('autoBarcode') eq 'incremental') {
132 my ($tagfield,$tagsubfield) = &GetMarcFromKohaField
("items.barcode",$frameworkcode);
133 unless ($record->field($tagfield)->subfield($tagsubfield)) {
134 my $sth_barcode = $dbh->prepare("select max(abs(barcode)) from items");
135 $sth_barcode->execute;
136 my ($newbarcode) = $sth_barcode->fetchrow;
138 # OK, we have the new barcode, now create the entry in MARC record
139 my $fieldItem = $record->field($tagfield);
140 $record->delete_field($fieldItem);
141 $fieldItem->add_subfields($tagsubfield => $newbarcode);
142 $record->insert_fields_ordered($fieldItem);
146 my $addedolditem = TransformMarcToKoha
($dbh,$record);
148 # If we have to add or add & duplicate, we add the item
149 if ($add_submit || $add_duplicate_submit) {
150 # check for item barcode # being unique
151 my $exist_itemnumber = get_item_from_barcode
($addedolditem->{'barcode'});
152 push @errors,"barcode_not_unique" if($exist_itemnumber);
153 # if barcode exists, don't create, but report The problem.
154 unless ($exist_itemnumber) {
155 my ($oldbiblionumber,$oldbibnum,$oldbibitemnum) = AddItemFromMarc
($record,$biblionumber);
156 set_item_default_location
($oldbibitemnum);
159 if ($exist_itemnumber) {
160 $itemrecord = $record;
164 # If we have to add & duplicate
165 if ($add_duplicate_submit) {
167 # We try to get the next barcode
169 my $barcodeobj = C4
::Barcodes
->new;
170 my $barcodevalue = $barcodeobj->next_value($addedolditem->{'barcode'}) if $barcodeobj;
171 my ($tagfield,$tagsubfield) = &GetMarcFromKohaField
("items.barcode",$frameworkcode);
172 if ($record->field($tagfield)->subfield($tagsubfield)) {
173 # If we got the next codebar value, we put it in the record
175 $record->field($tagfield)->update($tagsubfield => $barcodevalue);
176 # If not, we delete the recently inserted barcode from the record (so the user can input a barcode himself)
178 $record->field($tagfield)->update($tagsubfield => '');
181 $itemrecord = $record;
184 # If we have to add multiple copies
185 if ($add_multiple_copies_submit) {
188 my $barcodeobj = C4
::Barcodes
->new;
189 my $oldbarcode = $addedolditem->{'barcode'};
190 my ($tagfield,$tagsubfield) = &GetMarcFromKohaField
("items.barcode",$frameworkcode);
192 # If there is a barcode and we can't find him new values, we can't add multiple copies
193 my $testbarcode = $barcodeobj->next_value($oldbarcode) if $barcodeobj;
194 if ($oldbarcode && !$testbarcode) {
196 push @errors, "no_next_barcode";
197 $itemrecord = $record;
202 # For the first iteration
203 my $barcodevalue = $oldbarcode;
204 my $exist_itemnumber;
207 for (my $i = 0; $i < $number_of_copies;) {
209 # If there is a barcode
212 # Getting a new barcode (if it is not the first iteration or the barcode we tried already exists)
213 $barcodevalue = $barcodeobj->next_value($oldbarcode) if ($i > 0 || $exist_itemnumber);
215 # Putting it into the record
217 $record->field($tagfield)->update($tagsubfield => $barcodevalue);
220 # Checking if the barcode already exists
221 $exist_itemnumber = get_item_from_barcode
($barcodevalue);
225 if (!$exist_itemnumber) {
226 my ($oldbiblionumber,$oldbibnum,$oldbibitemnum) = AddItemFromMarc
($record,$biblionumber);
227 set_item_default_location
($oldbibitemnum);
229 # We count the item only if it was really added
230 # That way, all items are added, even if there was some already existing barcodes
231 # FIXME : Please note that there is a risk of infinite loop here if we never find a suitable barcode
235 # Preparing the next iteration
236 $oldbarcode = $barcodevalue;
243 #-------------------------------------------------------------------------------
244 } elsif ($op eq "edititem") {
245 #-------------------------------------------------------------------------------
246 # retrieve item if exist => then, it's a modif
247 $itemrecord = C4
::Items
::GetMarcItem
($biblionumber,$itemnumber);
248 $nextop = "saveitem";
249 #-------------------------------------------------------------------------------
250 } elsif ($op eq "delitem") {
251 #-------------------------------------------------------------------------------
252 # check that there is no issue on this item before deletion.
253 $error = &DelItemCheck
($dbh,$biblionumber,$itemnumber);
255 print $input->redirect("additem.pl?biblionumber=$biblionumber&frameworkcode=$frameworkcode");
260 #-------------------------------------------------------------------------------
261 } elsif ($op eq "delallitems") {
262 #-------------------------------------------------------------------------------
263 my @biblioitems = &GetBiblioItemByBiblioNumber
($biblionumber);
264 foreach my $biblioitem (@biblioitems){
265 my $items = &GetItemsByBiblioitemnumber
($biblioitem->{biblioitemnumber
});
267 foreach my $item (@
$items){
268 &DelItem
($dbh,$biblionumber,$item->{itemnumber
});
271 #-------------------------------------------------------------------------------
272 } elsif ($op eq "saveitem") {
273 #-------------------------------------------------------------------------------
275 my @tags = $input->param('tag');
276 my @subfields = $input->param('subfield');
277 my @values = $input->param('field_value');
278 # build indicator hash.
279 my @ind_tag = $input->param('ind_tag');
280 my @indicator = $input->param('indicator');
281 # my $itemnumber = $input->param('itemnumber');
282 my $xml = TransformHtmlToXml
(\
@tags,\
@subfields,\
@values,\
@indicator,\
@ind_tag,'ITEM');
283 my $itemtosave=MARC
::Record
::new_from_xml
($xml, 'UTF-8');
284 # MARC::Record builded => now, record in DB
285 # warn "R: ".$record->as_formatted;
286 # check that the barcode don't exist already
287 my $addedolditem = TransformMarcToKoha
($dbh,$itemtosave);
288 my $exist_itemnumber = get_item_from_barcode
($addedolditem->{'barcode'});
289 if ($exist_itemnumber && $exist_itemnumber != $itemnumber) {
290 push @errors,"barcode_not_unique";
292 my ($oldbiblionumber,$oldbibnum,$oldbibitemnum) = ModItemFromMarc
($itemtosave,$biblionumber,$itemnumber);
299 #-------------------------------------------------------------------------------
300 # build screen with existing items. and "new" one
301 #-------------------------------------------------------------------------------
303 # now, build existiing item list
304 my $temp = GetMarcBiblio
( $biblionumber );
305 my @fields = $temp->fields();
306 #my @fields = $record->fields();
307 my %witness; #---- stores the list of subfields used at least once, with the "meaning" of the code
309 #---- finds where items.itemnumber is stored
310 my ( $itemtagfield, $itemtagsubfield) = &GetMarcFromKohaField
("items.itemnumber", $frameworkcode);
311 my ($branchtagfield, $branchtagsubfield) = &GetMarcFromKohaField
("items.homebranch", $frameworkcode);
313 foreach my $field (@fields) {
314 next if ($field->tag()<10);
315 my @subf = $field->subfields or (); # don't use ||, as that forces $field->subfelds to be interpreted in scalar context
317 # loop through each subfield
318 for my $i (0..$#subf) {
319 next if ($tagslib->{$field->tag()}->{$subf[$i][0]}->{tab
} ne 10
320 && ($field->tag() ne $itemtagfield
321 && $subf[$i][0] ne $itemtagsubfield));
323 $witness{$subf[$i][0]} = $tagslib->{$field->tag()}->{$subf[$i][0]}->{lib
} if ($tagslib->{$field->tag()}->{$subf[$i][0]}->{tab
} eq 10);
324 if ($tagslib->{$field->tag()}->{$subf[$i][0]}->{tab
} eq 10) {
325 $this_row{$subf[$i][0]}=GetAuthorisedValueDesc
( $field->tag(),
326 $subf[$i][0], $subf[$i][1], '', $tagslib)
330 if (($field->tag eq $branchtagfield) && ($subf[$i][$0] eq $branchtagsubfield) && C4
::Context
->preference("IndependantBranches")) {
332 my $userenv = C4
::Context
->userenv();
333 unless (($userenv->{'flags'} == 1) or (($userenv->{'branch'} eq $subf[$i][1]))){
334 $this_row{'nomod'}=1;
337 $this_row{itemnumber
} = $subf[$i][1] if ($field->tag() eq $itemtagfield && $subf[$i][0] eq $itemtagsubfield);
340 push(@big_array, \
%this_row);
344 my ($holdingbrtagf,$holdingbrtagsubf) = &GetMarcFromKohaField
("items.holdingbranch",$frameworkcode);
345 @big_array = sort {$a->{$holdingbrtagsubf} cmp $b->{$holdingbrtagsubf}} @big_array;
347 # now, construct template !
348 # First, the existing items for display
350 my @header_value_loop;
351 for my $row ( @big_array ) {
353 my @item_fields = map +{ field
=> $_ || '' }, @
$row{ sort keys(%witness) };
354 $row_data{item_value
} = [ @item_fields ];
355 $row_data{itemnumber
} = $row->{itemnumber
};
356 #reporting this_row values
357 $row_data{'nomod'} = $row->{'nomod'};
358 push(@item_value_loop,\
%row_data);
360 foreach my $subfield_code (sort keys(%witness)) {
362 $header_value{header_value
} = $witness{$subfield_code};
363 push(@header_value_loop, \
%header_value);
366 # now, build the item form for entering a new item
370 my $branches = GetBranchesLoop
(); # build once ahead of time, instead of multiple times later.
371 my $pref_itemcallnumber = C4
::Context
->preference('itemcallnumber');
373 # Getting the fields where the item location is
374 my ($location_field, $location_subfield) = GetMarcFromKohaField
('items.location', $frameworkcode);
376 # Getting the name of the authorised values' category for item location
377 my $item_location_category = $tagslib->{$location_field}->{$location_subfield}->{'authorised_value'};
379 foreach my $tag (sort keys %{$tagslib}) {
380 # loop through each subfield
381 foreach my $subfield (sort keys %{$tagslib->{$tag}}) {
382 next if subfield_is_koha_internal_p
($subfield);
383 next if ($tagslib->{$tag}->{$subfield}->{'tab'} ne "10");
386 my $index_subfield = int(rand(1000000));
387 if ($subfield eq '@'){
388 $subfield_data{id
} = "tag_".$tag."_subfield_00_".$index_subfield;
390 $subfield_data{id
} = "tag_".$tag."_subfield_".$subfield."_".$index_subfield;
392 $subfield_data{tag
} = $tag;
393 $subfield_data{subfield
} = $subfield;
394 $subfield_data{random
} = int(rand(1000000)); # why do we need 2 different randoms?
395 # $subfield_data{marc_lib} = $tagslib->{$tag}->{$subfield}->{lib};
396 $subfield_data{marc_lib
} ="<span id=\"error$i\" title=\"".$tagslib->{$tag}->{$subfield}->{lib
}."\">".$tagslib->{$tag}->{$subfield}->{lib
}."</span>";
397 $subfield_data{mandatory
} = $tagslib->{$tag}->{$subfield}->{mandatory
};
398 $subfield_data{repeatable
} = $tagslib->{$tag}->{$subfield}->{repeatable
};
400 ($x,$value) = find_value
($tag,$subfield,$itemrecord) if ($itemrecord);
401 $value =~ s/"/"/g;
403 $value = $tagslib->{$tag}->{$subfield}->{defaultvalue
};
404 # get today date & replace YYYY, MM, DD if provided in the default value
405 my ( $year, $month, $day ) = split ',', $today_iso; # FIXME: iso dates don't have commas!
406 $value =~ s/YYYY/$year/g;
407 $value =~ s/MM/$month/g;
408 $value =~ s/DD/$day/g;
410 $subfield_data{visibility
} = "display:none;" if (($tagslib->{$tag}->{$subfield}->{hidden
} > 4) || ($tagslib->{$tag}->{$subfield}->{hidden
} < -4));
411 # testing branch value if IndependantBranches.
412 if (!$value && $tagslib->{$tag}->{$subfield}->{kohafield
} eq 'items.itemcallnumber' && $pref_itemcallnumber) {
413 my $CNtag = substr($pref_itemcallnumber, 0, 3);
414 my $CNsubfield = substr($pref_itemcallnumber, 3, 1);
415 my $CNsubfield2 = substr($pref_itemcallnumber, 4, 1);
416 my $temp2 = $temp->field($CNtag);
418 $value = ($temp2->subfield($CNsubfield)).' '.($temp2->subfield($CNsubfield2));
419 #remove any trailing space incase one subfield is used
420 $value =~ s/^\s+|\s+$//g;
424 my $attributes_no_value = qq(tabindex
="1" id
="$subfield_data{id}" name
="field_value" class="input_marceditor" size
="67" maxlength
="255" );
425 my $attributes = qq($attributes_no_value value
="$value" );
426 if ( $tagslib->{$tag}->{$subfield}->{authorised_value
} ) {
427 my @authorised_values;
429 # builds list, depending on authorised value...
431 if ( $tagslib->{$tag}->{$subfield}->{authorised_value
} eq "branches" ) {
432 foreach my $thisbranch (@
$branches) {
433 push @authorised_values, $thisbranch->{value
};
434 $authorised_lib{$thisbranch->{value
}} = $thisbranch->{branchname
};
435 # $value = $thisbranch->{value} if $thisbranch->{selected};
438 elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value
} eq "itemtypes" ) {
439 push @authorised_values, "" unless ( $tagslib->{$tag}->{$subfield}->{mandatory
} );
440 my $sth = $dbh->prepare("select itemtype,description from itemtypes order by description");
442 while ( my ( $itemtype, $description ) = $sth->fetchrow_array ) {
443 push @authorised_values, $itemtype;
444 $authorised_lib{$itemtype} = $description;
448 my $itype_sth = $dbh->prepare("SELECT itemtype FROM biblioitems WHERE biblionumber = ?");
449 $itype_sth->execute( $biblionumber );
450 ( $value ) = $itype_sth->fetchrow_array;
455 elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value
} eq "cn_source" ) {
456 push @authorised_values, "" unless ( $tagslib->{$tag}->{$subfield}->{mandatory
} );
458 my $class_sources = GetClassSources
();
459 my $default_source = C4
::Context
->preference("DefaultClassificationSource");
461 foreach my $class_source (sort keys %$class_sources) {
462 next unless $class_sources->{$class_source}->{'used'} or
463 ($value and $class_source eq $value) or
464 ($class_source eq $default_source);
465 push @authorised_values, $class_source;
466 $authorised_lib{$class_source} = $class_sources->{$class_source}->{'description'};
468 $value = $default_source unless ($value);
470 #---- "true" authorised value
473 push @authorised_values, "" unless ( $tagslib->{$tag}->{$subfield}->{mandatory
} );
475 # Are we dealing with item location ?
476 my $item_location = ($tagslib->{$tag}->{$subfield}->{authorised_value
} eq $item_location_category) ?
1 : 0;
478 # If so, we sort by authorised_value, else by libelle
479 my $orderby = $item_location ?
'authorised_value' : 'lib';
481 my $authorised_values_sth = $dbh->prepare("SELECT authorised_value,lib FROM authorised_values WHERE category=? ORDER BY $orderby");
483 $authorised_values_sth->execute( $tagslib->{$tag}->{$subfield}->{authorised_value
});
486 while ( my ( $value, $lib ) = $authorised_values_sth->fetchrow_array ) {
487 push @authorised_values, $value;
488 if ($tagslib->{$tag}->{$subfield}->{authorised_value
} eq $item_location_category) {
489 $authorised_lib{$value} = $value . " - " . $lib;
491 $authorised_lib{$value} = $lib;
494 # For item location, we show the code and the libelle
495 $authorised_lib{$value} = ($item_location) ?
$value . " - " . $lib : $lib;
498 $subfield_data{marc_value
} =CGI
::scrolling_list
( # FIXME: factor out scrolling_list
499 -name
=> "field_value",
500 -values => \
@authorised_values,
502 -labels
=> \
%authorised_lib,
507 -id
=> "tag_".$tag."_subfield_".$subfield."_".$index_subfield,
508 -class => "input_marceditor",
510 # it's a thesaurus / authority field
512 elsif ( $tagslib->{$tag}->{$subfield}->{authtypecode
} ) {
513 $subfield_data{marc_value
} = "<input type=\"text\" $attributes />
514 <a href=\"#\" class=\"buttonDot\"
515 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 # it's a plugin field
519 elsif ( $tagslib->{$tag}->{$subfield}->{value_builder
} ) {
521 my $plugin = C4
::Context
->intranetdir . "/cataloguing/value_builder/" . $tagslib->{$tag}->{$subfield}->{'value_builder'};
523 my $extended_param = plugin_parameters
( $dbh, $temp, $tagslib, $subfield_data{id
}, \
@loop_data );
524 my ( $function_name, $javascript ) = plugin_javascript
( $dbh, $temp, $tagslib, $subfield_data{id
}, \
@loop_data );
525 $subfield_data{marc_value
} = qq[<input
$attributes
526 onfocus
="Focus$function_name($subfield_data{random}, '$subfield_data{id}');"
527 onblur
=" Blur$function_name($subfield_data{random}, '$subfield_data{id}');" />
528 <a href
="#" class="buttonDot" onclick
="Clic$function_name('$subfield_data{id}'); return false;" title
="Tag Editor">...</a
>
531 warn "Plugin Failed: $plugin";
532 $subfield_data{marc_value
} = "<input $attributes />"; # supply default input form
535 elsif ( $tag eq '' ) { # it's an hidden field
536 $subfield_data{marc_value
} = qq(<input type
="hidden" $attributes />);
538 elsif ( $tagslib->{$tag}->{$subfield}->{'hidden'} ) { # FIXME: shouldn't input type be "hidden" ?
539 $subfield_data{marc_value
} = qq(<input type
="text" $attributes />);
541 elsif ( length($value) > 100
542 or (C4
::Context
->preference("marcflavour") eq "UNIMARC" and
543 300 <= $tag && $tag < 400 && $subfield eq 'a' )
544 or (C4
::Context
->preference("marcflavour") eq "MARC21" and
545 500 <= $tag && $tag < 600 )
547 # oversize field (textarea)
548 $subfield_data{marc_value
} = "<textarea $attributes_no_value>$value</textarea>\n";
550 # it's a standard field
551 $subfield_data{marc_value
} = "<input $attributes />";
553 # $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\">";
554 push (@loop_data, \
%subfield_data);
559 # what's the next op ? it's what we are not in : an add if we're editing, otherwise, and edit.
560 $template->param( title
=> $record->title() ) if ($record ne "-1");
562 biblionumber
=> $biblionumber,
563 title
=> $oldrecord->{title
},
564 author
=> $oldrecord->{author
},
565 item_loop
=> \
@item_value_loop,
566 item_header_loop
=> \
@header_value_loop,
568 itemnumber
=> $itemnumber,
569 itemtagfield
=> $itemtagfield,
570 itemtagsubfield
=> $itemtagsubfield,
572 opisadd
=> ($nextop eq "saveitem") ?
0 : 1,
573 C4
::Search
::enabled_staff_search_views
,
575 foreach my $error (@errors) {
576 $template->param($error => 1);
578 output_html_with_http_headers
$input, $cookie, $template->output;