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 ($template, $loggedinuser, $cookie)
79 = get_template_and_user
({template_name
=> "cataloguing/additem.tmpl",
83 flagsrequired
=> {editcatalogue
=> 1},
87 my $frameworkcode = &GetFrameworkCode
($biblionumber);
89 my $today_iso = C4
::Dates
->today('iso');
90 $template->param(today_iso
=> $today_iso);
92 my $tagslib = &GetMarcStructure
(1,$frameworkcode);
93 my $record = GetMarcBiblio
($biblionumber);
94 my $oldrecord = TransformMarcToKoha
($dbh,$record);
97 my @errors; # store errors found while checking data BEFORE saving item.
98 #-------------------------------------------------------------------------------
99 if ($op eq "additem") {
100 #-------------------------------------------------------------------------------
102 my @tags = $input->param('tag');
103 my @subfields = $input->param('subfield');
104 my @values = $input->param('field_value');
105 # build indicator hash.
106 my @ind_tag = $input->param('ind_tag');
107 my @indicator = $input->param('indicator');
108 my $xml = TransformHtmlToXml
(\
@tags,\
@subfields,\
@values,\
@indicator,\
@ind_tag, 'ITEM');
109 my $record = MARC
::Record
::new_from_xml
($xml, 'UTF-8');
112 my $add_submit = $input->param('add_submit');
113 my $add_duplicate_submit = $input->param('add_duplicate_submit');
114 my $add_multiple_copies_submit = $input->param('add_multiple_copies_submit');
115 my $number_of_copies = $input->param('number_of_copies');
117 # if autoBarcode is set to 'incremental', calculate barcode...
118 # NOTE: This code is subject to change in 3.2 with the implemenation of ajax based autobarcode code
119 # NOTE: 'incremental' is the ONLY autoBarcode option available to those not using javascript
120 if (C4
::Context
->preference('autoBarcode') eq 'incremental') {
121 my ($tagfield,$tagsubfield) = &GetMarcFromKohaField
("items.barcode",$frameworkcode);
122 unless ($record->field($tagfield)->subfield($tagsubfield)) {
123 my $sth_barcode = $dbh->prepare("select max(abs(barcode)) from items");
124 $sth_barcode->execute;
125 my ($newbarcode) = $sth_barcode->fetchrow;
127 # OK, we have the new barcode, now create the entry in MARC record
128 my $fieldItem = $record->field($tagfield);
129 $record->delete_field($fieldItem);
130 $fieldItem->add_subfields($tagsubfield => $newbarcode);
131 $record->insert_fields_ordered($fieldItem);
135 my $addedolditem = TransformMarcToKoha
($dbh,$record);
137 # If we have to add or add & duplicate, we add the item
138 if ($add_submit || $add_duplicate_submit) {
139 # check for item barcode # being unique
140 my $exist_itemnumber = get_item_from_barcode
($addedolditem->{'barcode'});
141 push @errors,"barcode_not_unique" if($exist_itemnumber);
142 # if barcode exists, don't create, but report The problem.
143 unless ($exist_itemnumber) {
144 my ($oldbiblionumber,$oldbibnum,$oldbibitemnum) = AddItemFromMarc
($record,$biblionumber);
145 set_item_default_location
($oldbibitemnum);
148 if ($exist_itemnumber) {
149 $itemrecord = $record;
153 # If we have to add & duplicate
154 if ($add_duplicate_submit) {
156 # We try to get the next barcode
158 my $barcodeobj = C4
::Barcodes
->new;
159 my $barcodevalue = $barcodeobj->next_value($addedolditem->{'barcode'}) if $barcodeobj;
160 my ($tagfield,$tagsubfield) = &GetMarcFromKohaField
("items.barcode",$frameworkcode);
161 if ($record->field($tagfield)->subfield($tagsubfield)) {
162 # If we got the next codebar value, we put it in the record
164 $record->field($tagfield)->update($tagsubfield => $barcodevalue);
165 # If not, we delete the recently inserted barcode from the record (so the user can input a barcode himself)
167 $record->field($tagfield)->update($tagsubfield => '');
170 $itemrecord = $record;
173 # If we have to add multiple copies
174 if ($add_multiple_copies_submit) {
177 my $barcodeobj = C4
::Barcodes
->new;
178 my $oldbarcode = $addedolditem->{'barcode'};
179 my ($tagfield,$tagsubfield) = &GetMarcFromKohaField
("items.barcode",$frameworkcode);
181 # If there is a barcode and we can't find him new values, we can't add multiple copies
182 my $testbarcode = $barcodeobj->next_value($oldbarcode) if $barcodeobj;
183 if ($oldbarcode && !$testbarcode) {
185 push @errors, "no_next_barcode";
186 $itemrecord = $record;
191 # For the first iteration
192 my $barcodevalue = $oldbarcode;
193 my $exist_itemnumber;
196 for (my $i = 0; $i < $number_of_copies;) {
198 # If there is a barcode
201 # Getting a new barcode (if it is not the first iteration or the barcode we tried already exists)
202 $barcodevalue = $barcodeobj->next_value($oldbarcode) if ($i > 0 || $exist_itemnumber);
204 # Putting it into the record
206 $record->field($tagfield)->update($tagsubfield => $barcodevalue);
209 # Checking if the barcode already exists
210 $exist_itemnumber = get_item_from_barcode
($barcodevalue);
214 if (!$exist_itemnumber) {
215 my ($oldbiblionumber,$oldbibnum,$oldbibitemnum) = AddItemFromMarc
($record,$biblionumber);
216 set_item_default_location
($oldbibitemnum);
218 # We count the item only if it was really added
219 # That way, all items are added, even if there was some already existing barcodes
220 # FIXME : Please note that there is a risk of infinite loop here if we never find a suitable barcode
224 # Preparing the next iteration
225 $oldbarcode = $barcodevalue;
232 #-------------------------------------------------------------------------------
233 } elsif ($op eq "edititem") {
234 #-------------------------------------------------------------------------------
235 # retrieve item if exist => then, it's a modif
236 $itemrecord = C4
::Items
::GetMarcItem
($biblionumber,$itemnumber);
237 $nextop = "saveitem";
238 #-------------------------------------------------------------------------------
239 } elsif ($op eq "delitem") {
240 #-------------------------------------------------------------------------------
241 # check that there is no issue on this item before deletion.
242 $error = &DelItemCheck
($dbh,$biblionumber,$itemnumber);
244 print $input->redirect("additem.pl?biblionumber=$biblionumber&frameworkcode=$frameworkcode");
249 #-------------------------------------------------------------------------------
250 } elsif ($op eq "delallitems") {
251 #-------------------------------------------------------------------------------
252 my @biblioitems = &GetBiblioItemByBiblioNumber
($biblionumber);
253 foreach my $biblioitem (@biblioitems){
254 my $items = &GetItemsByBiblioitemnumber
($biblioitem->{biblioitemnumber
});
256 foreach my $item (@
$items){
257 &DelItem
($dbh,$biblionumber,$item->{itemnumber
});
260 #-------------------------------------------------------------------------------
261 } elsif ($op eq "saveitem") {
262 #-------------------------------------------------------------------------------
264 my @tags = $input->param('tag');
265 my @subfields = $input->param('subfield');
266 my @values = $input->param('field_value');
267 # build indicator hash.
268 my @ind_tag = $input->param('ind_tag');
269 my @indicator = $input->param('indicator');
270 # my $itemnumber = $input->param('itemnumber');
271 my $xml = TransformHtmlToXml
(\
@tags,\
@subfields,\
@values,\
@indicator,\
@ind_tag,'ITEM');
272 my $itemtosave=MARC
::Record
::new_from_xml
($xml, 'UTF-8');
273 # MARC::Record builded => now, record in DB
274 # warn "R: ".$record->as_formatted;
275 # check that the barcode don't exist already
276 my $addedolditem = TransformMarcToKoha
($dbh,$itemtosave);
277 my $exist_itemnumber = get_item_from_barcode
($addedolditem->{'barcode'});
278 if ($exist_itemnumber && $exist_itemnumber != $itemnumber) {
279 push @errors,"barcode_not_unique";
281 my ($oldbiblionumber,$oldbibnum,$oldbibitemnum) = ModItemFromMarc
($itemtosave,$biblionumber,$itemnumber);
288 #-------------------------------------------------------------------------------
289 # build screen with existing items. and "new" one
290 #-------------------------------------------------------------------------------
292 # now, build existiing item list
293 my $temp = GetMarcBiblio
( $biblionumber );
294 my @fields = $temp->fields();
295 #my @fields = $record->fields();
296 my %witness; #---- stores the list of subfields used at least once, with the "meaning" of the code
298 #---- finds where items.itemnumber is stored
299 my ( $itemtagfield, $itemtagsubfield) = &GetMarcFromKohaField
("items.itemnumber", $frameworkcode);
300 my ($branchtagfield, $branchtagsubfield) = &GetMarcFromKohaField
("items.homebranch", $frameworkcode);
302 foreach my $field (@fields) {
303 next if ($field->tag()<10);
304 my @subf = $field->subfields or (); # don't use ||, as that forces $field->subfelds to be interpreted in scalar context
306 # loop through each subfield
307 for my $i (0..$#subf) {
308 next if ($tagslib->{$field->tag()}->{$subf[$i][0]}->{tab
} ne 10
309 && ($field->tag() ne $itemtagfield
310 && $subf[$i][0] ne $itemtagsubfield));
312 $witness{$subf[$i][0]} = $tagslib->{$field->tag()}->{$subf[$i][0]}->{lib
} if ($tagslib->{$field->tag()}->{$subf[$i][0]}->{tab
} eq 10);
313 if ($tagslib->{$field->tag()}->{$subf[$i][0]}->{tab
} eq 10) {
314 $this_row{$subf[$i][0]}=GetAuthorisedValueDesc
( $field->tag(),
315 $subf[$i][0], $subf[$i][1], '', $tagslib)
319 if (($field->tag eq $branchtagfield) && ($subf[$i][$0] eq $branchtagsubfield) && C4
::Context
->preference("IndependantBranches")) {
321 my $userenv = C4
::Context
->userenv();
322 unless (($userenv->{'flags'} == 1) or (($userenv->{'branch'} eq $subf[$i][1]))){
323 $this_row{'nomod'}=1;
326 $this_row{itemnumber
} = $subf[$i][1] if ($field->tag() eq $itemtagfield && $subf[$i][0] eq $itemtagsubfield);
329 push(@big_array, \
%this_row);
333 my ($holdingbrtagf,$holdingbrtagsubf) = &GetMarcFromKohaField
("items.holdingbranch",$frameworkcode);
334 @big_array = sort {$a->{$holdingbrtagsubf} cmp $b->{$holdingbrtagsubf}} @big_array;
336 # now, construct template !
337 # First, the existing items for display
339 my @header_value_loop;
340 for my $row ( @big_array ) {
342 my @item_fields = map +{ field
=> $_ || '' }, @
$row{ sort keys(%witness) };
343 $row_data{item_value
} = [ @item_fields ];
344 $row_data{itemnumber
} = $row->{itemnumber
};
345 #reporting this_row values
346 $row_data{'nomod'} = $row->{'nomod'};
347 push(@item_value_loop,\
%row_data);
349 foreach my $subfield_code (sort keys(%witness)) {
351 $header_value{header_value
} = $witness{$subfield_code};
352 push(@header_value_loop, \
%header_value);
355 # now, build the item form for entering a new item
358 my $authorised_values_sth = $dbh->prepare("SELECT authorised_value,lib FROM authorised_values WHERE category=? ORDER BY lib");
360 my $branches = GetBranchesLoop
(); # build once ahead of time, instead of multiple times later.
361 my $pref_itemcallnumber = C4
::Context
->preference('itemcallnumber');
363 foreach my $tag (sort keys %{$tagslib}) {
364 # loop through each subfield
365 foreach my $subfield (sort keys %{$tagslib->{$tag}}) {
366 next if subfield_is_koha_internal_p
($subfield);
367 next if ($tagslib->{$tag}->{$subfield}->{'tab'} ne "10");
370 my $index_subfield = int(rand(1000000));
371 if ($subfield eq '@'){
372 $subfield_data{id
} = "tag_".$tag."_subfield_00_".$index_subfield;
374 $subfield_data{id
} = "tag_".$tag."_subfield_".$subfield."_".$index_subfield;
376 $subfield_data{tag
} = $tag;
377 $subfield_data{subfield
} = $subfield;
378 $subfield_data{random
} = int(rand(1000000)); # why do we need 2 different randoms?
379 # $subfield_data{marc_lib} = $tagslib->{$tag}->{$subfield}->{lib};
380 $subfield_data{marc_lib
} ="<span id=\"error$i\" title=\"".$tagslib->{$tag}->{$subfield}->{lib
}."\">".$tagslib->{$tag}->{$subfield}->{lib
}."</span>";
381 $subfield_data{mandatory
} = $tagslib->{$tag}->{$subfield}->{mandatory
};
382 $subfield_data{repeatable
} = $tagslib->{$tag}->{$subfield}->{repeatable
};
384 ($x,$value) = find_value
($tag,$subfield,$itemrecord) if ($itemrecord);
385 $value =~ s/"/"/g;
387 $value = $tagslib->{$tag}->{$subfield}->{defaultvalue
};
388 # get today date & replace YYYY, MM, DD if provided in the default value
389 my ( $year, $month, $day ) = split ',', $today_iso; # FIXME: iso dates don't have commas!
390 $value =~ s/YYYY/$year/g;
391 $value =~ s/MM/$month/g;
392 $value =~ s/DD/$day/g;
394 $subfield_data{visibility
} = "display:none;" if (($tagslib->{$tag}->{$subfield}->{hidden
} > 4) || ($tagslib->{$tag}->{$subfield}->{hidden
} < -4));
395 # testing branch value if IndependantBranches.
396 if (!$value && $tagslib->{$tag}->{$subfield}->{kohafield
} eq 'items.itemcallnumber' && $pref_itemcallnumber) {
397 my $CNtag = substr($pref_itemcallnumber, 0, 3);
398 my $CNsubfield = substr($pref_itemcallnumber, 3, 1);
399 my $CNsubfield2 = substr($pref_itemcallnumber, 4, 1);
400 my $temp2 = $temp->field($CNtag);
402 $value = ($temp2->subfield($CNsubfield)).' '.($temp2->subfield($CNsubfield2));
403 #remove any trailing space incase one subfield is used
404 $value =~ s/^\s+|\s+$//g;
408 my $attributes_no_value = qq(tabindex
="1" id
="$subfield_data{id}" name
="field_value" class="input_marceditor" size
="67" maxlength
="255" );
409 my $attributes = qq($attributes_no_value value
="$value" );
410 if ( $tagslib->{$tag}->{$subfield}->{authorised_value
} ) {
411 my @authorised_values;
413 # builds list, depending on authorised value...
415 if ( $tagslib->{$tag}->{$subfield}->{authorised_value
} eq "branches" ) {
416 foreach my $thisbranch (@
$branches) {
417 push @authorised_values, $thisbranch->{value
};
418 $authorised_lib{$thisbranch->{value
}} = $thisbranch->{branchname
};
419 $value = $thisbranch->{value
} if $thisbranch->{selected
};
422 elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value
} eq "itemtypes" ) {
423 push @authorised_values, "" unless ( $tagslib->{$tag}->{$subfield}->{mandatory
} );
424 my $sth = $dbh->prepare("select itemtype,description from itemtypes order by description");
426 while ( my ( $itemtype, $description ) = $sth->fetchrow_array ) {
427 push @authorised_values, $itemtype;
428 $authorised_lib{$itemtype} = $description;
432 my $itype_sth = $dbh->prepare("SELECT itemtype FROM biblioitems WHERE biblionumber = ?");
433 $itype_sth->execute( $biblionumber );
434 ( $value ) = $itype_sth->fetchrow_array;
439 elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value
} eq "cn_source" ) {
440 push @authorised_values, "" unless ( $tagslib->{$tag}->{$subfield}->{mandatory
} );
442 my $class_sources = GetClassSources
();
443 my $default_source = C4
::Context
->preference("DefaultClassificationSource");
445 foreach my $class_source (sort keys %$class_sources) {
446 next unless $class_sources->{$class_source}->{'used'} or
447 ($value and $class_source eq $value) or
448 ($class_source eq $default_source);
449 push @authorised_values, $class_source;
450 $authorised_lib{$class_source} = $class_sources->{$class_source}->{'description'};
452 $value = $default_source unless ($value);
454 #---- "true" authorised value
457 push @authorised_values, "" unless ( $tagslib->{$tag}->{$subfield}->{mandatory
} );
458 $authorised_values_sth->execute( $tagslib->{$tag}->{$subfield}->{authorised_value
} );
459 while ( my ( $value, $lib ) = $authorised_values_sth->fetchrow_array ) {
460 push @authorised_values, $value;
461 $authorised_lib{$value} = $lib;
464 $subfield_data{marc_value
} =CGI
::scrolling_list
( # FIXME: factor out scrolling_list
465 -name
=> "field_value",
466 -values => \
@authorised_values,
468 -labels
=> \
%authorised_lib,
473 -id
=> "tag_".$tag."_subfield_".$subfield."_".$index_subfield,
474 -class => "input_marceditor",
476 # it's a thesaurus / authority field
478 elsif ( $tagslib->{$tag}->{$subfield}->{authtypecode
} ) {
479 $subfield_data{marc_value
} = "<input type=\"text\" $attributes />
480 <a href=\"#\" class=\"buttonDot\"
481 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>
483 # it's a plugin field
485 elsif ( $tagslib->{$tag}->{$subfield}->{value_builder
} ) {
487 my $plugin = C4
::Context
->intranetdir . "/cataloguing/value_builder/" . $tagslib->{$tag}->{$subfield}->{'value_builder'};
489 my $extended_param = plugin_parameters
( $dbh, $temp, $tagslib, $subfield_data{id
}, \
@loop_data );
490 my ( $function_name, $javascript ) = plugin_javascript
( $dbh, $temp, $tagslib, $subfield_data{id
}, \
@loop_data );
491 $subfield_data{marc_value
} = qq[<input
$attributes
492 onfocus
="Focus$function_name($subfield_data{random}, '$subfield_data{id}');"
493 onblur
=" Blur$function_name($subfield_data{random}, '$subfield_data{id}');" />
494 <a href
="#" class="buttonDot" onclick
="Clic$function_name('$subfield_data{id}'); return false;" title
="Tag Editor">...</a
>
497 warn "Plugin Failed: $plugin";
498 $subfield_data{marc_value
} = "<input $attributes />"; # supply default input form
501 elsif ( $tag eq '' ) { # it's an hidden field
502 $subfield_data{marc_value
} = qq(<input type
="hidden" $attributes />);
504 elsif ( $tagslib->{$tag}->{$subfield}->{'hidden'} ) { # FIXME: shouldn't input type be "hidden" ?
505 $subfield_data{marc_value
} = qq(<input type
="text" $attributes />);
507 elsif ( length($value) > 100
508 or (C4
::Context
->preference("marcflavour") eq "UNIMARC" and
509 300 <= $tag && $tag < 400 && $subfield eq 'a' )
510 or (C4
::Context
->preference("marcflavour") eq "MARC21" and
511 500 <= $tag && $tag < 600 )
513 # oversize field (textarea)
514 $subfield_data{marc_value
} = "<textarea $attributes_no_value>$value</textarea>\n";
516 # it's a standard field
517 $subfield_data{marc_value
} = "<input $attributes />";
519 # $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\">";
520 push (@loop_data, \
%subfield_data);
525 # what's the next op ? it's what we are not in : an add if we're editing, otherwise, and edit.
526 $template->param( title
=> $record->title() ) if ($record ne "-1");
528 biblionumber
=> $biblionumber,
529 title
=> $oldrecord->{title
},
530 author
=> $oldrecord->{author
},
531 item_loop
=> \
@item_value_loop,
532 item_header_loop
=> \
@header_value_loop,
534 itemnumber
=> $itemnumber,
535 itemtagfield
=> $itemtagfield,
536 itemtagsubfield
=> $itemtagsubfield,
538 opisadd
=> ($nextop eq "saveitem") ?
0 : 1,
540 foreach my $error (@errors) {
541 $template->param($error => 1);
543 output_html_with_http_headers
$input, $cookie, $template->output;