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;
62 my $dbh = C4::Context->dbh;
63 my $error = $input->param('error');
64 my $biblionumber = $input->param('biblionumber');
65 my $itemnumber = $input->param('itemnumber');
66 my $op = $input->param('op');
68 my ($template, $loggedinuser, $cookie)
69 = get_template_and_user({template_name => "cataloguing/additem.tmpl",
73 flagsrequired => {editcatalogue => 1},
77 my $frameworkcode = &GetFrameworkCode($biblionumber);
79 my $today_iso = C4::Dates->today('iso');
80 $template->param(today_iso => $today_iso);
82 my $tagslib = &GetMarcStructure(1,$frameworkcode);
83 my $record = GetMarcBiblio($biblionumber);
84 my $oldrecord = TransformMarcToKoha($dbh,$record);
87 my @errors; # store errors found while checking data BEFORE saving item.
88 #-------------------------------------------------------------------------------
89 if ($op eq "additem") {
90 #-------------------------------------------------------------------------------
92 my @tags = $input->param('tag');
93 my @subfields = $input->param('subfield');
94 my @values = $input->param('field_value');
95 # build indicator hash.
96 my @ind_tag = $input->param('ind_tag');
97 my @indicator = $input->param('indicator');
98 my $xml = TransformHtmlToXml(\@tags,\@subfields,\@values,\@indicator,\@ind_tag, 'ITEM');
99 my $record = MARC::Record::new_from_xml($xml, 'UTF-8');
102 my $add_submit = $input->param('add_submit');
103 my $add_duplicate_submit = $input->param('add_duplicate_submit');
104 my $add_multiple_copies_submit = $input->param('add_multiple_copies_submit');
105 my $number_of_copies = $input->param('number_of_copies');
107 # if autoBarcode is set to 'incremental', calculate barcode...
108 # NOTE: This code is subject to change in 3.2 with the implemenation of ajax based autobarcode code
109 # NOTE: 'incremental' is the ONLY autoBarcode option available to those not using javascript
110 if (C4::Context->preference('autoBarcode') eq 'incremental') {
111 my ($tagfield,$tagsubfield) = &GetMarcFromKohaField("items.barcode",$frameworkcode);
112 unless ($record->field($tagfield)->subfield($tagsubfield)) {
113 my $sth_barcode = $dbh->prepare("select max(abs(barcode)) from items");
114 $sth_barcode->execute;
115 my ($newbarcode) = $sth_barcode->fetchrow;
117 # OK, we have the new barcode, now create the entry in MARC record
118 my $fieldItem = $record->field($tagfield);
119 $record->delete_field($fieldItem);
120 $fieldItem->add_subfields($tagsubfield => $newbarcode);
121 $record->insert_fields_ordered($fieldItem);
125 my $addedolditem = TransformMarcToKoha($dbh,$record);
127 # If we have to add or add & duplicate, we add the item
128 if ($add_submit || $add_duplicate_submit) {
129 # check for item barcode # being unique
130 my $exist_itemnumber = get_item_from_barcode($addedolditem->{'barcode'});
131 push @errors,"barcode_not_unique" if($exist_itemnumber);
132 # if barcode exists, don't create, but report The problem.
133 my ($oldbiblionumber,$oldbibnum,$oldbibitemnum) = AddItemFromMarc($record,$biblionumber) unless ($exist_itemnumber);
135 if ($exist_itemnumber) {
136 $itemrecord = $record;
140 # If we have to add & duplicate
141 if ($add_duplicate_submit) {
143 # We try to get the next barcode
145 my $barcodeobj = C4::Barcodes->new;
146 my $barcodevalue = $barcodeobj->next_value($addedolditem->{'barcode'}) if $barcodeobj;
147 my ($tagfield,$tagsubfield) = &GetMarcFromKohaField("items.barcode",$frameworkcode);
148 if ($record->field($tagfield)->subfield($tagsubfield)) {
149 # If we got the next codebar value, we put it in the record
151 $record->field($tagfield)->update($tagsubfield => $barcodevalue);
152 # If not, we delete the recently inserted barcode from the record (so the user can input a barcode himself)
154 $record->field($tagfield)->update($tagsubfield => '');
157 $itemrecord = $record;
160 # If we have to add multiple copies
161 if ($add_multiple_copies_submit) {
164 my $barcodeobj = C4::Barcodes->new;
165 my $oldbarcode = $addedolditem->{'barcode'};
166 my ($tagfield,$tagsubfield) = &GetMarcFromKohaField("items.barcode",$frameworkcode);
168 # If there is a barcode and we can't find him new values, we can't add multiple copies
169 my $testbarcode = $barcodeobj->next_value($oldbarcode) if $barcodeobj;
170 if ($oldbarcode && !$testbarcode) {
172 push @errors, "no_next_barcode";
173 $itemrecord = $record;
178 # For the first iteration
179 my $barcodevalue = $oldbarcode;
180 my $exist_itemnumber;
183 for (my $i = 0; $i < $number_of_copies;) {
185 # If there is a barcode
188 # Getting a new barcode (if it is not the first iteration or the barcode we tried already exists)
189 $barcodevalue = $barcodeobj->next_value($oldbarcode) if ($i > 0 || $exist_itemnumber);
191 # Putting it into the record
193 $record->field($tagfield)->update($tagsubfield => $barcodevalue);
196 # Checking if the barcode already exists
197 $exist_itemnumber = get_item_from_barcode($barcodevalue);
201 my ($oldbiblionumber,$oldbibnum,$oldbibitemnum) = AddItemFromMarc($record,$biblionumber) unless ($exist_itemnumber);
203 # We count the item only if it was really added
204 # That way, all items are added, even if there was some already existing barcodes
205 # FIXME : Please note that there is a risk of infinite loop here if we never find a suitable barcode
206 $i++ unless ($exist_itemnumber);
208 # Preparing the next iteration
209 $oldbarcode = $barcodevalue;
216 #-------------------------------------------------------------------------------
217 } elsif ($op eq "edititem") {
218 #-------------------------------------------------------------------------------
219 # retrieve item if exist => then, it's a modif
220 $itemrecord = C4::Items::GetMarcItem($biblionumber,$itemnumber);
221 $nextop = "saveitem";
222 #-------------------------------------------------------------------------------
223 } elsif ($op eq "delitem") {
224 #-------------------------------------------------------------------------------
225 # check that there is no issue on this item before deletion.
226 my $sth=$dbh->prepare("select * from issues i where i.itemnumber=?");
227 $sth->execute($itemnumber);
228 my $onloan=$sth->fetchrow;
232 push @errors,"book_on_loan";
234 # check it doesnt have a waiting reserve
235 $sth=$dbh->prepare("SELECT * FROM reserves WHERE found = 'W' AND itemnumber = ?");
236 $sth->execute($itemnumber);
237 my $reserve=$sth->fetchrow;
239 &DelItem($dbh,$biblionumber,$itemnumber);
240 print $input->redirect("additem.pl?biblionumber=$biblionumber&frameworkcode=$frameworkcode");
243 push @errors,"book_reserved";
245 #-------------------------------------------------------------------------------
246 } elsif ($op eq "saveitem") {
247 #-------------------------------------------------------------------------------
249 my @tags = $input->param('tag');
250 my @subfields = $input->param('subfield');
251 my @values = $input->param('field_value');
252 # build indicator hash.
253 my @ind_tag = $input->param('ind_tag');
254 my @indicator = $input->param('indicator');
255 # my $itemnumber = $input->param('itemnumber');
256 my $xml = TransformHtmlToXml(\@tags,\@subfields,\@values,\@indicator,\@ind_tag,'ITEM');
257 my $itemtosave=MARC::Record::new_from_xml($xml, 'UTF-8');
258 # MARC::Record builded => now, record in DB
259 # warn "R: ".$record->as_formatted;
260 # check that the barcode don't exist already
261 my $addedolditem = TransformMarcToKoha($dbh,$itemtosave);
262 my $exist_itemnumber = get_item_from_barcode($addedolditem->{'barcode'});
263 if ($exist_itemnumber && $exist_itemnumber != $itemnumber) {
264 push @errors,"barcode_not_unique";
266 my ($oldbiblionumber,$oldbibnum,$oldbibitemnum) = ModItemFromMarc($itemtosave,$biblionumber,$itemnumber);
273 #-------------------------------------------------------------------------------
274 # build screen with existing items. and "new" one
275 #-------------------------------------------------------------------------------
277 # now, build existiing item list
278 my $temp = GetMarcBiblio( $biblionumber );
279 my @fields = $temp->fields();
280 #my @fields = $record->fields();
281 my %witness; #---- stores the list of subfields used at least once, with the "meaning" of the code
283 #---- finds where items.itemnumber is stored
284 my ( $itemtagfield, $itemtagsubfield) = &GetMarcFromKohaField("items.itemnumber", $frameworkcode);
285 my ($branchtagfield, $branchtagsubfield) = &GetMarcFromKohaField("items.homebranch", $frameworkcode);
287 foreach my $field (@fields) {
288 next if ($field->tag()<10);
289 my @subf = $field->subfields or (); # don't use ||, as that forces $field->subfelds to be interpreted in scalar context
291 # loop through each subfield
292 for my $i (0..$#subf) {
293 next if ($tagslib->{$field->tag()}->{$subf[$i][0]}->{tab} ne 10
294 && ($field->tag() ne $itemtagfield
295 && $subf[$i][0] ne $itemtagsubfield));
297 $witness{$subf[$i][0]} = $tagslib->{$field->tag()}->{$subf[$i][0]}->{lib} if ($tagslib->{$field->tag()}->{$subf[$i][0]}->{tab} eq 10);
298 if ($tagslib->{$field->tag()}->{$subf[$i][0]}->{tab} eq 10) {
299 $this_row{$subf[$i][0]}=GetAuthorisedValueDesc( $field->tag(),
300 $subf[$i][0], $subf[$i][1], '', $tagslib)
304 if (($field->tag eq $branchtagfield) && ($subf[$i][$0] eq $branchtagsubfield) && C4::Context->preference("IndependantBranches")) {
306 my $userenv = C4::Context->userenv();
307 unless (($userenv->{'flags'} == 1) or (($userenv->{'branch'} eq $subf[$i][1]))){
308 $this_row{'nomod'}=1;
311 $this_row{itemnumber} = $subf[$i][1] if ($field->tag() eq $itemtagfield && $subf[$i][0] eq $itemtagsubfield);
314 push(@big_array, \%this_row);
318 my ($holdingbrtagf,$holdingbrtagsubf) = &GetMarcFromKohaField("items.holdingbranch",$frameworkcode);
319 @big_array = sort {$a->{$holdingbrtagsubf} cmp $b->{$holdingbrtagsubf}} @big_array;
321 # now, construct template !
322 # First, the existing items for display
324 my @header_value_loop;
325 for my $row ( @big_array ) {
327 my @item_fields = map +{ field => $_ || '' }, @$row{ sort keys(%witness) };
328 $row_data{item_value} = [ @item_fields ];
329 $row_data{itemnumber} = $row->{itemnumber};
330 #reporting this_row values
331 $row_data{'nomod'} = $row->{'nomod'};
332 push(@item_value_loop,\%row_data);
334 foreach my $subfield_code (sort keys(%witness)) {
336 $header_value{header_value} = $witness{$subfield_code};
337 push(@header_value_loop, \%header_value);
340 # now, build the item form for entering a new item
343 my $authorised_values_sth = $dbh->prepare("SELECT authorised_value,lib FROM authorised_values WHERE category=? ORDER BY lib");
345 my $branches = GetBranchesLoop(); # build once ahead of time, instead of multiple times later.
346 my $pref_itemcallnumber = C4::Context->preference('itemcallnumber');
348 foreach my $tag (sort keys %{$tagslib}) {
349 # loop through each subfield
350 foreach my $subfield (sort keys %{$tagslib->{$tag}}) {
351 next if subfield_is_koha_internal_p($subfield);
352 next if ($tagslib->{$tag}->{$subfield}->{'tab'} ne "10");
355 my $index_subfield = int(rand(1000000));
356 if ($subfield eq '@'){
357 $subfield_data{id} = "tag_".$tag."_subfield_00_".$index_subfield;
359 $subfield_data{id} = "tag_".$tag."_subfield_".$subfield."_".$index_subfield;
361 $subfield_data{tag} = $tag;
362 $subfield_data{subfield} = $subfield;
363 $subfield_data{random} = int(rand(1000000)); # why do we need 2 different randoms?
364 # $subfield_data{marc_lib} = $tagslib->{$tag}->{$subfield}->{lib};
365 $subfield_data{marc_lib} ="<span id=\"error$i\" title=\"".$tagslib->{$tag}->{$subfield}->{lib}."\">".$tagslib->{$tag}->{$subfield}->{lib}."</span>";
366 $subfield_data{mandatory} = $tagslib->{$tag}->{$subfield}->{mandatory};
367 $subfield_data{repeatable} = $tagslib->{$tag}->{$subfield}->{repeatable};
369 ($x,$value) = find_value($tag,$subfield,$itemrecord) if ($itemrecord);
370 $value =~ s/"/"/g;
372 $value = $tagslib->{$tag}->{$subfield}->{defaultvalue};
373 # get today date & replace YYYY, MM, DD if provided in the default value
374 my ( $year, $month, $day ) = split ',', $today_iso; # FIXME: iso dates don't have commas!
375 $value =~ s/YYYY/$year/g;
376 $value =~ s/MM/$month/g;
377 $value =~ s/DD/$day/g;
379 $subfield_data{visibility} = "display:none;" if (($tagslib->{$tag}->{$subfield}->{hidden} > 4) || ($tagslib->{$tag}->{$subfield}->{hidden} < -4));
380 # testing branch value if IndependantBranches.
381 if (!$value && $tagslib->{$tag}->{$subfield}->{kohafield} eq 'items.itemcallnumber' && $pref_itemcallnumber) {
382 my $CNtag = substr($pref_itemcallnumber, 0, 3);
383 my $CNsubfield = substr($pref_itemcallnumber, 3, 1);
384 my $CNsubfield2 = substr($pref_itemcallnumber, 4, 1);
385 my $temp2 = $temp->field($CNtag);
387 $value = ($temp2->subfield($CNsubfield)).' '.($temp2->subfield($CNsubfield2));
388 #remove any trailing space incase one subfield is used
389 $value =~ s/^\s+|\s+$//g;
393 my $attributes_no_value = qq(tabindex="1" id="$subfield_data{id}" name="field_value" class="input_marceditor" size="67" maxlength="255" );
394 my $attributes = qq($attributes_no_value value="$value" );
395 if ( $tagslib->{$tag}->{$subfield}->{authorised_value} ) {
396 my @authorised_values;
398 # builds list, depending on authorised value...
400 if ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "branches" ) {
401 foreach my $thisbranch (@$branches) {
402 push @authorised_values, $thisbranch->{value};
403 $authorised_lib{$thisbranch->{value}} = $thisbranch->{branchname};
404 $value = $thisbranch->{value} if $thisbranch->{selected};
407 elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes" ) {
408 push @authorised_values, "" unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
409 my $sth = $dbh->prepare("select itemtype,description from itemtypes order by description");
411 while ( my ( $itemtype, $description ) = $sth->fetchrow_array ) {
412 push @authorised_values, $itemtype;
413 $authorised_lib{$itemtype} = $description;
417 my $itype_sth = $dbh->prepare("SELECT itemtype FROM biblioitems WHERE biblionumber = ?");
418 $itype_sth->execute( $biblionumber );
419 ( $value ) = $itype_sth->fetchrow_array;
424 elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "cn_source" ) {
425 push @authorised_values, "" unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
427 my $class_sources = GetClassSources();
428 my $default_source = C4::Context->preference("DefaultClassificationSource");
430 foreach my $class_source (sort keys %$class_sources) {
431 next unless $class_sources->{$class_source}->{'used'} or
432 ($value and $class_source eq $value) or
433 ($class_source eq $default_source);
434 push @authorised_values, $class_source;
435 $authorised_lib{$class_source} = $class_sources->{$class_source}->{'description'};
437 $value = $default_source unless ($value);
439 #---- "true" authorised value
442 push @authorised_values, "" unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
443 $authorised_values_sth->execute( $tagslib->{$tag}->{$subfield}->{authorised_value} );
444 while ( my ( $value, $lib ) = $authorised_values_sth->fetchrow_array ) {
445 push @authorised_values, $value;
446 $authorised_lib{$value} = $lib;
449 $subfield_data{marc_value} =CGI::scrolling_list( # FIXME: factor out scrolling_list
450 -name => "field_value",
451 -values => \@authorised_values,
453 -labels => \%authorised_lib,
458 -id => "tag_".$tag."_subfield_".$subfield."_".$index_subfield,
459 -class => "input_marceditor",
461 # it's a thesaurus / authority field
463 elsif ( $tagslib->{$tag}->{$subfield}->{authtypecode} ) {
464 $subfield_data{marc_value} = "<input type=\"text\" $attributes />
465 <a href=\"#\" class=\"buttonDot\"
466 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>
468 # it's a plugin field
470 elsif ( $tagslib->{$tag}->{$subfield}->{value_builder} ) {
472 my $plugin = C4::Context->intranetdir . "/cataloguing/value_builder/" . $tagslib->{$tag}->{$subfield}->{'value_builder'};
474 my $extended_param = plugin_parameters( $dbh, $temp, $tagslib, $subfield_data{id}, \@loop_data );
475 my ( $function_name, $javascript ) = plugin_javascript( $dbh, $temp, $tagslib, $subfield_data{id}, \@loop_data );
476 $subfield_data{marc_value} = qq[<input $attributes
477 onfocus="Focus$function_name($subfield_data{random}, '$subfield_data{id}');"
478 onblur=" Blur$function_name($subfield_data{random}, '$subfield_data{id}');" />
479 <a href="#" class="buttonDot" onclick="Clic$function_name('$subfield_data{id}'); return false;" title="Tag Editor">...</a>
482 warn "Plugin Failed: $plugin";
483 $subfield_data{marc_value} = "<input $attributes />"; # supply default input form
486 elsif ( $tag eq '' ) { # it's an hidden field
487 $subfield_data{marc_value} = qq(<input type="hidden" $attributes />);
489 elsif ( $tagslib->{$tag}->{$subfield}->{'hidden'} ) { # FIXME: shouldn't input type be "hidden" ?
490 $subfield_data{marc_value} = qq(<input type="text" $attributes />);
492 elsif ( length($value) > 100
493 or (C4::Context->preference("marcflavour") eq "UNIMARC" and
494 300 <= $tag && $tag < 400 && $subfield eq 'a' )
495 or (C4::Context->preference("marcflavour") eq "MARC21" and
496 500 <= $tag && $tag < 600 )
498 # oversize field (textarea)
499 $subfield_data{marc_value} = "<textarea $attributes_no_value>$value</textarea>\n";
501 # it's a standard field
502 $subfield_data{marc_value} = "<input $attributes />";
504 # $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\">";
505 push (@loop_data, \%subfield_data);
510 # what's the next op ? it's what we are not in : an add if we're editing, otherwise, and edit.
511 $template->param( title => $record->title() ) if ($record ne "-1");
513 biblionumber => $biblionumber,
514 title => $oldrecord->{title},
515 author => $oldrecord->{author},
516 item_loop => \@item_value_loop,
517 item_header_loop => \@header_value_loop,
519 itemnumber => $itemnumber,
520 itemtagfield => $itemtagfield,
521 itemtagsubfield => $itemtagsubfield,
523 opisadd => ($nextop eq "saveitem") ? 0 : 1,
525 foreach my $error (@errors) {
526 $template->param($error => 1);
528 output_html_with_http_headers $input, $cookie, $template->output;