Bug 9455: Remove unnecessary quoting from sql
[koha.git] / cataloguing / additem.pl
bloba8a8649369136fac578468c9f2a1d0f8ee16220d
1 #!/usr/bin/perl
3 # Copyright 2000-2002 Katipo Communications
4 # Copyright 2004-2010 BibLibre
5 # Parts Copyright Catalyst IT 2011
7 # This file is part of Koha.
9 # Koha is free software; you can redistribute it and/or modify it under the
10 # terms of the GNU General Public License as published by the Free Software
11 # Foundation; either version 2 of the License, or (at your option) any later
12 # version.
14 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License along
19 # with Koha; if not, write to the Free Software Foundation, Inc.,
20 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 use strict;
23 #use warnings; FIXME - Bug 2505
24 use CGI;
25 use C4::Auth;
26 use C4::Output;
27 use C4::Biblio;
28 use C4::Items;
29 use C4::Context;
30 use C4::Koha; # XXX subfield_is_koha_internal_p
31 use C4::Branch; # XXX subfield_is_koha_internal_p
32 use C4::ClassSource;
33 use C4::Dates;
34 use List::MoreUtils qw/any/;
35 use C4::Search;
36 use Storable qw(thaw freeze);
37 use URI::Escape;
40 use MARC::File::XML;
41 use URI::Escape;
43 our $dbh = C4::Context->dbh;
45 sub find_value {
46 my ($tagfield,$insubfield,$record) = @_;
47 my $result;
48 my $indicator;
49 foreach my $field ($record->field($tagfield)) {
50 my @subfields = $field->subfields();
51 foreach my $subfield (@subfields) {
52 if (@$subfield[0] eq $insubfield) {
53 $result .= @$subfield[1];
54 $indicator = $field->indicator(1).$field->indicator(2);
58 return($indicator,$result);
61 sub get_item_from_barcode {
62 my ($barcode)=@_;
63 my $dbh=C4::Context->dbh;
64 my $result;
65 my $rq=$dbh->prepare("SELECT itemnumber from items where items.barcode=?");
66 $rq->execute($barcode);
67 ($result)=$rq->fetchrow;
68 return($result);
71 sub set_item_default_location {
72 my $itemnumber = shift;
73 my $item = GetItem( $itemnumber );
74 if ( C4::Context->preference('NewItemsDefaultLocation') ) {
75 $item->{'permanent_location'} = $item->{'location'};
76 $item->{'location'} = C4::Context->preference('NewItemsDefaultLocation');
77 ModItem( $item, undef, $itemnumber);
79 else {
80 $item->{'permanent_location'} = $item->{'location'} if !defined($item->{'permanent_location'});
81 ModItem( $item, undef, $itemnumber);
85 # NOTE: This code is subject to change in the future with the implemenation of ajax based autobarcode code
86 # NOTE: 'incremental' is the ONLY autoBarcode option available to those not using javascript
87 sub _increment_barcode {
88 my ($record, $frameworkcode) = @_;
89 my ($tagfield,$tagsubfield) = &GetMarcFromKohaField("items.barcode",$frameworkcode);
90 unless ($record->field($tagfield)->subfield($tagsubfield)) {
91 my $sth_barcode = $dbh->prepare("select max(abs(barcode)) from items");
92 $sth_barcode->execute;
93 my ($newbarcode) = $sth_barcode->fetchrow;
94 $newbarcode++;
95 # OK, we have the new barcode, now create the entry in MARC record
96 my $fieldItem = $record->field($tagfield);
97 $record->delete_field($fieldItem);
98 $fieldItem->add_subfields($tagsubfield => $newbarcode);
99 $record->insert_fields_ordered($fieldItem);
101 return $record;
105 sub generate_subfield_form {
106 my ($tag, $subfieldtag, $value, $tagslib,$subfieldlib, $branches, $today_iso, $biblionumber, $temp, $loop_data, $i) = @_;
108 my $frameworkcode = &GetFrameworkCode($biblionumber);
109 my %subfield_data;
110 my $dbh = C4::Context->dbh;
112 my $index_subfield = int(rand(1000000));
113 if ($subfieldtag eq '@'){
114 $subfield_data{id} = "tag_".$tag."_subfield_00_".$index_subfield;
115 } else {
116 $subfield_data{id} = "tag_".$tag."_subfield_".$subfieldtag."_".$index_subfield;
119 $subfield_data{tag} = $tag;
120 $subfield_data{subfield} = $subfieldtag;
121 $subfield_data{random} = int(rand(1000000)); # why do we need 2 different randoms?
122 $subfield_data{marc_lib} ="<span id=\"error$i\" title=\"".$subfieldlib->{lib}."\">".$subfieldlib->{lib}."</span>";
123 $subfield_data{mandatory} = $subfieldlib->{mandatory};
124 $subfield_data{repeatable} = $subfieldlib->{repeatable};
125 $subfield_data{maxlength} = $subfieldlib->{maxlength};
127 $value =~ s/"/&quot;/g;
128 if ( ! defined( $value ) || $value eq '') {
129 $value = $subfieldlib->{defaultvalue};
130 # get today date & replace YYYY, MM, DD if provided in the default value
131 my ( $year, $month, $day ) = split ',', $today_iso; # FIXME: iso dates don't have commas!
132 $value =~ s/YYYY/$year/g;
133 $value =~ s/MM/$month/g;
134 $value =~ s/DD/$day/g;
137 $subfield_data{visibility} = "display:none;" if (($subfieldlib->{hidden} > 4) || ($subfieldlib->{hidden} < -4));
139 my $pref_itemcallnumber = C4::Context->preference('itemcallnumber');
140 if (!$value && $subfieldlib->{kohafield} eq 'items.itemcallnumber' && $pref_itemcallnumber) {
141 my $CNtag = substr($pref_itemcallnumber, 0, 3);
142 my $CNsubfield = substr($pref_itemcallnumber, 3, 1);
143 my $CNsubfield2 = substr($pref_itemcallnumber, 4, 1);
144 my $temp2 = $temp->field($CNtag);
145 if ($temp2) {
146 $value = ($temp2->subfield($CNsubfield)).' '.($temp2->subfield($CNsubfield2));
147 #remove any trailing space incase one subfield is used
148 $value =~ s/^\s+|\s+$//g;
152 if ($frameworkcode eq 'FA' && $subfieldlib->{kohafield} eq 'items.barcode' && !$value){
153 my $input = new CGI;
154 $value = $input->param('barcode');
156 my $attributes_no_value = qq(tabindex="1" id="$subfield_data{id}" name="field_value" class="input_marceditor" size="67" maxlength="$subfield_data{maxlength}" );
157 my $attributes = qq($attributes_no_value value="$value" );
159 if ( $subfieldlib->{authorised_value} ) {
160 my @authorised_values;
161 my %authorised_lib;
162 # builds list, depending on authorised value...
163 if ( $subfieldlib->{authorised_value} eq "branches" ) {
164 foreach my $thisbranch (@$branches) {
165 push @authorised_values, $thisbranch->{value};
166 $authorised_lib{$thisbranch->{value}} = $thisbranch->{branchname};
167 $value = $thisbranch->{value} if $thisbranch->{selected} && !$value;
170 elsif ( $subfieldlib->{authorised_value} eq "itemtypes" ) {
171 push @authorised_values, "" unless ( $subfieldlib->{mandatory} );
172 my $sth = $dbh->prepare("SELECT itemtype,description FROM itemtypes ORDER BY description");
173 $sth->execute;
174 while ( my ( $itemtype, $description ) = $sth->fetchrow_array ) {
175 push @authorised_values, $itemtype;
176 $authorised_lib{$itemtype} = $description;
179 unless ( $value ) {
180 my $itype_sth = $dbh->prepare("SELECT itemtype FROM biblioitems WHERE biblionumber = ?");
181 $itype_sth->execute( $biblionumber );
182 ( $value ) = $itype_sth->fetchrow_array;
185 #---- class_sources
187 elsif ( $subfieldlib->{authorised_value} eq "cn_source" ) {
188 push @authorised_values, "" unless ( $subfieldlib->{mandatory} );
190 my $class_sources = GetClassSources();
191 my $default_source = C4::Context->preference("DefaultClassificationSource");
193 foreach my $class_source (sort keys %$class_sources) {
194 next unless $class_sources->{$class_source}->{'used'} or
195 ($value and $class_source eq $value) or
196 ($class_source eq $default_source);
197 push @authorised_values, $class_source;
198 $authorised_lib{$class_source} = $class_sources->{$class_source}->{'description'};
200 $value = $default_source unless ($value);
202 #---- "true" authorised value
204 else {
205 push @authorised_values, qq{} unless ( $subfieldlib->{mandatory} );
206 my $av = GetAuthorisedValues( $subfieldlib->{authorised_value} );
207 for my $r ( @$av ) {
208 push @authorised_values, $r->{authorised_value};
209 $authorised_lib{$r->{authorised_value}} = $r->{lib};
213 if ($subfieldlib->{'hidden'}) {
214 $subfield_data{marc_value} = qq(<input type="hidden" $attributes /> $authorised_lib{$value});
216 else {
217 $subfield_data{marc_value} =CGI::scrolling_list( # FIXME: factor out scrolling_list
218 -name => "field_value",
219 -values => \@authorised_values,
220 -default => $value,
221 -labels => \%authorised_lib,
222 -override => 1,
223 -size => 1,
224 -multiple => 0,
225 -tabindex => 1,
226 -id => "tag_".$tag."_subfield_".$subfieldtag."_".$index_subfield,
227 -class => "input_marceditor",
232 # it's a thesaurus / authority field
233 elsif ( $subfieldlib->{authtypecode} ) {
234 $subfield_data{marc_value} = "<input type=\"text\" $attributes />
235 <a href=\"#\" class=\"buttonDot\"
236 onclick=\"Dopop('/cgi-bin/koha/authorities/auth_finder.pl?authtypecode=".$subfieldlib->{authtypecode}."&index=$subfield_data{id}','$subfield_data{id}'); return false;\" title=\"Tag Editor\">...</a>
239 # it's a plugin field
240 elsif ( $subfieldlib->{value_builder} ) {
241 # opening plugin
242 my $plugin = C4::Context->intranetdir . "/cataloguing/value_builder/" . $subfieldlib->{'value_builder'};
243 if (do $plugin) {
244 my $extended_param = plugin_parameters( $dbh, $temp, $tagslib, $subfield_data{id}, $loop_data );
245 my ( $function_name, $javascript ) = plugin_javascript( $dbh, $temp, $tagslib, $subfield_data{id}, $loop_data );
246 my $change = index($javascript, 'function Change') > -1 ?
247 "return Change$function_name($subfield_data{random}, '$subfield_data{id}');" :
248 'return 1;';
249 $subfield_data{marc_value} = qq[<input type="text" $attributes
250 onfocus="Focus$function_name($subfield_data{random}, '$subfield_data{id}');"
251 onchange=" $change"
252 onblur=" Blur$function_name($subfield_data{random}, '$subfield_data{id}');" />
253 <a href="#" class="buttonDot" onclick="Clic$function_name('$subfield_data{id}'); return false;" title="Tag Editor">...</a>
254 $javascript];
255 } else {
256 warn "Plugin Failed: $plugin";
257 $subfield_data{marc_value} = "<input type=\"text\" $attributes />"; # supply default input form
260 elsif ( $tag eq '' ) { # it's an hidden field
261 $subfield_data{marc_value} = qq(<input type="hidden" $attributes />);
263 elsif ( $subfieldlib->{'hidden'} ) { # FIXME: shouldn't input type be "hidden" ?
264 $subfield_data{marc_value} = qq(<input type="text" $attributes />);
266 elsif ( length($value) > 100
267 or (C4::Context->preference("marcflavour") eq "UNIMARC" and
268 300 <= $tag && $tag < 400 && $subfieldtag eq 'a' )
269 or (C4::Context->preference("marcflavour") eq "MARC21" and
270 500 <= $tag && $tag < 600 )
272 # oversize field (textarea)
273 $subfield_data{marc_value} = "<textarea $attributes_no_value>$value</textarea>\n";
274 } else {
275 # it's a standard field
276 $subfield_data{marc_value} = "<input type=\"text\" $attributes />";
279 return \%subfield_data;
282 # Removes some subfields when prefilling items
283 # This function will remove any subfield that is not in the SubfieldsToUseWhenPrefill syspref
284 sub removeFieldsForPrefill {
286 my $item = shift;
288 # Getting item tag
289 my ($tag, $subtag) = GetMarcFromKohaField("items.barcode", '');
291 # Getting list of subfields to keep
292 my $subfieldsToUseWhenPrefill = C4::Context->preference('SubfieldsToUseWhenPrefill');
294 # Removing subfields that are not in the syspref
295 if ($tag && $subfieldsToUseWhenPrefill) {
296 my $field = $item->field($tag);
297 my @subfieldsToUse= split(/ /,$subfieldsToUseWhenPrefill);
298 foreach my $subfield ($field->subfields()) {
299 if (!grep { $subfield->[0] eq $_ } @subfieldsToUse) {
300 $field->delete_subfield(code => $subfield->[0]);
306 return $item;
310 my $input = new CGI;
311 my $error = $input->param('error');
312 my $biblionumber = $input->param('biblionumber');
313 my $itemnumber = $input->param('itemnumber');
314 my $op = $input->param('op');
315 my $hostitemnumber = $input->param('hostitemnumber');
316 my $marcflavour = C4::Context->preference("marcflavour");
317 # fast cataloguing datas
318 my $fa_circborrowernumber = $input->param('circborrowernumber');
319 my $fa_barcode = $input->param('barcode');
320 my $fa_branch = $input->param('branch');
321 my $fa_stickyduedate = $input->param('stickyduedate');
322 my $fa_duedatespec = $input->param('duedatespec');
324 my $frameworkcode = &GetFrameworkCode($biblionumber);
326 # Defining which userflag is needing according to the framework currently used
327 my $userflags;
328 if (defined $input->param('frameworkcode')) {
329 $userflags = ($input->param('frameworkcode') eq 'FA') ? "fast_cataloging" : "edit_items";
332 if (not defined $userflags) {
333 $userflags = ($frameworkcode eq 'FA') ? "fast_cataloging" : "edit_items";
336 my ($template, $loggedinuser, $cookie)
337 = get_template_and_user({template_name => "cataloguing/additem.tt",
338 query => $input,
339 type => "intranet",
340 authnotrequired => 0,
341 flagsrequired => {editcatalogue => $userflags},
342 debug => 1,
346 my $today_iso = C4::Dates->today('iso');
347 my $tagslib = &GetMarcStructure(1,$frameworkcode);
348 my $record = GetMarcBiblio($biblionumber);
349 my $oldrecord = TransformMarcToKoha($dbh,$record);
350 my $itemrecord;
351 my $nextop="additem";
352 my @errors; # store errors found while checking data BEFORE saving item.
354 # Getting last created item cookie
355 my $prefillitem = C4::Context->preference('PrefillItem');
356 my $justaddeditem;
357 my $cookieitemrecord;
358 if ($prefillitem) {
359 my $lastitemcookie = $input->cookie('LastCreatedItem');
360 if ($lastitemcookie) {
361 $lastitemcookie = uri_unescape($lastitemcookie);
362 if ( thaw($lastitemcookie) ) {
363 $cookieitemrecord = thaw($lastitemcookie) ;
364 $cookieitemrecord = removeFieldsForPrefill($cookieitemrecord);
369 #-------------------------------------------------------------------------------
370 if ($op eq "additem") {
372 #-------------------------------------------------------------------------------
373 # rebuild
374 my @tags = $input->param('tag');
375 my @subfields = $input->param('subfield');
376 my @values = $input->param('field_value');
377 # build indicator hash.
378 my @ind_tag = $input->param('ind_tag');
379 my @indicator = $input->param('indicator');
380 my $xml = TransformHtmlToXml(\@tags,\@subfields,\@values,\@indicator,\@ind_tag, 'ITEM');
381 my $record = MARC::Record::new_from_xml($xml, 'UTF-8');
383 # type of add
384 my $add_submit = $input->param('add_submit');
385 my $add_duplicate_submit = $input->param('add_duplicate_submit');
386 my $add_multiple_copies_submit = $input->param('add_multiple_copies_submit');
387 my $number_of_copies = $input->param('number_of_copies');
389 # This is a bit tricky : if there is a cookie for the last created item and
390 # we just added an item, the cookie value is not correct yet (it will be updated
391 # next page). To prevent the form from being filled with outdated values, we
392 # force the use of "add and duplicate" feature, so the form will be filled with
393 # correct values.
394 $add_duplicate_submit = 1 if ($prefillitem);
395 $justaddeditem = 1;
397 # if autoBarcode is set to 'incremental', calculate barcode...
398 if ( C4::Context->preference('autoBarcode') eq 'incremental' ) {
399 $record = _increment_barcode($record, $frameworkcode);
403 if (C4::Context->preference('autoBarcode') eq 'incremental') {
404 $record = _increment_barcode($record, $frameworkcode);
407 my $addedolditem = TransformMarcToKoha( $dbh, $record );
409 # If we have to add or add & duplicate, we add the item
410 if ( $add_submit || $add_duplicate_submit ) {
412 # check for item barcode # being unique
413 my $exist_itemnumber = get_item_from_barcode( $addedolditem->{'barcode'} );
414 push @errors, "barcode_not_unique" if ($exist_itemnumber);
416 # if barcode exists, don't create, but report The problem.
417 unless ($exist_itemnumber) {
418 my ( $oldbiblionumber, $oldbibnum, $oldbibitemnum ) = AddItemFromMarc( $record, $biblionumber );
419 set_item_default_location($oldbibitemnum);
421 # Pushing the last created item cookie back
422 if ($prefillitem && defined $record) {
423 my $itemcookie = $input->cookie(
424 -name => 'LastCreatedItem',
425 # We uri_escape the whole freezed structure so we're sure we won't have any encoding problems
426 -value => uri_escape_utf8( freeze( $record ) ),
427 -HttpOnly => 1,
428 -expires => ''
431 $cookie = [ $cookie, $itemcookie ];
435 $nextop = "additem";
436 if ($exist_itemnumber) {
437 $itemrecord = $record;
441 # If we have to add & duplicate
442 if ($add_duplicate_submit) {
443 $itemrecord = $record;
444 if (C4::Context->preference('autoBarcode') eq 'incremental') {
445 $itemrecord = _increment_barcode($itemrecord, $frameworkcode);
447 else {
448 # we have to clear the barcode field in the duplicate item record to make way for the new one generated by the javascript plugin
449 my ($tagfield,$tagsubfield) = &GetMarcFromKohaField("items.barcode",$frameworkcode);
450 my $fieldItem = $itemrecord->field($tagfield);
451 $itemrecord->delete_field($fieldItem);
452 $fieldItem->delete_subfields($tagsubfield);
453 $itemrecord->insert_fields_ordered($fieldItem);
455 $itemrecord = removeFieldsForPrefill($itemrecord) if ($prefillitem);
458 # If we have to add multiple copies
459 if ($add_multiple_copies_submit) {
461 use C4::Barcodes;
462 my $barcodeobj = C4::Barcodes->new;
463 my $oldbarcode = $addedolditem->{'barcode'};
464 my ($tagfield,$tagsubfield) = &GetMarcFromKohaField("items.barcode",$frameworkcode);
466 # If there is a barcode and we can't find him new values, we can't add multiple copies
467 my $testbarcode;
468 $testbarcode = $barcodeobj->next_value($oldbarcode) if $barcodeobj;
469 if ($oldbarcode && !$testbarcode) {
471 push @errors, "no_next_barcode";
472 $itemrecord = $record;
474 } else {
475 # We add each item
477 # For the first iteration
478 my $barcodevalue = $oldbarcode;
479 my $exist_itemnumber;
482 for (my $i = 0; $i < $number_of_copies;) {
484 # If there is a barcode
485 if ($barcodevalue) {
487 # Getting a new barcode (if it is not the first iteration or the barcode we tried already exists)
488 $barcodevalue = $barcodeobj->next_value($oldbarcode) if ($i > 0 || $exist_itemnumber);
490 # Putting it into the record
491 if ($barcodevalue) {
492 $record->field($tagfield)->update($tagsubfield => $barcodevalue);
495 # Checking if the barcode already exists
496 $exist_itemnumber = get_item_from_barcode($barcodevalue);
499 # Adding the item
500 if (!$exist_itemnumber) {
501 my ($oldbiblionumber,$oldbibnum,$oldbibitemnum) = AddItemFromMarc($record,$biblionumber);
502 set_item_default_location($oldbibitemnum);
504 # We count the item only if it was really added
505 # That way, all items are added, even if there was some already existing barcodes
506 # FIXME : Please note that there is a risk of infinite loop here if we never find a suitable barcode
507 $i++;
510 # Preparing the next iteration
511 $oldbarcode = $barcodevalue;
513 undef($itemrecord);
516 if ($frameworkcode eq 'FA' && $fa_circborrowernumber){
517 print $input->redirect(
518 '/cgi-bin/koha/circ/circulation.pl?'
519 .'borrowernumber='.$fa_circborrowernumber
520 .'&barcode='.uri_escape($fa_barcode)
521 .'&duedatespec='.$fa_duedatespec
522 .'&stickyduedate=1'
524 exit;
528 #-------------------------------------------------------------------------------
529 } elsif ($op eq "edititem") {
530 #-------------------------------------------------------------------------------
531 # retrieve item if exist => then, it's a modif
532 $itemrecord = C4::Items::GetMarcItem($biblionumber,$itemnumber);
533 $nextop = "saveitem";
534 #-------------------------------------------------------------------------------
535 } elsif ($op eq "delitem") {
536 #-------------------------------------------------------------------------------
537 # check that there is no issue on this item before deletion.
538 $error = &DelItemCheck($dbh,$biblionumber,$itemnumber);
539 if($error == 1){
540 print $input->redirect("additem.pl?biblionumber=$biblionumber&frameworkcode=$frameworkcode");
541 }else{
542 push @errors,$error;
543 $nextop="additem";
545 #-------------------------------------------------------------------------------
546 } elsif ($op eq "delallitems") {
547 #-------------------------------------------------------------------------------
548 my @biblioitems = &GetBiblioItemByBiblioNumber($biblionumber);
549 my $errortest=0;
550 my $itemfail;
551 foreach my $biblioitem (@biblioitems) {
552 my $items = &GetItemsByBiblioitemnumber( $biblioitem->{biblioitemnumber} );
554 foreach my $item (@$items) {
555 $error =&DelItemCheck( $dbh, $biblionumber, $item->{itemnumber} );
556 $itemfail =$item;
557 if($error == 1){
558 next
560 else {
561 push @errors,$error;
562 $errortest++
565 if($errortest > 0){
566 $nextop="additem";
568 else {
569 my $defaultview = C4::Context->preference('IntranetBiblioDefaultView');
570 my $views = { C4::Search::enabled_staff_search_views };
571 if ($defaultview eq 'isbd' && $views->{can_view_ISBD}) {
572 print $input->redirect("/cgi-bin/koha/catalogue/ISBDdetail.pl?biblionumber=$biblionumber");
573 } elsif ($defaultview eq 'marc' && $views->{can_view_MARC}) {
574 print $input->redirect("/cgi-bin/koha/catalogue/MARCdetail.pl?biblionumber=$biblionumber");
575 } elsif ($defaultview eq 'labeled_marc' && $views->{can_view_labeledMARC}) {
576 print $input->redirect("/cgi-bin/koha/catalogue/labeledMARCdetail.pl?biblionumber=$biblionumber");
577 } else {
578 print $input->redirect("/cgi-bin/koha/catalogue/detail.pl?biblionumber=$biblionumber");
580 exit;
583 #-------------------------------------------------------------------------------
584 } elsif ($op eq "saveitem") {
585 #-------------------------------------------------------------------------------
586 # rebuild
587 my @tags = $input->param('tag');
588 my @subfields = $input->param('subfield');
589 my @values = $input->param('field_value');
590 # build indicator hash.
591 my @ind_tag = $input->param('ind_tag');
592 my @indicator = $input->param('indicator');
593 # my $itemnumber = $input->param('itemnumber');
594 my $xml = TransformHtmlToXml(\@tags,\@subfields,\@values,\@indicator,\@ind_tag,'ITEM');
595 my $itemtosave=MARC::Record::new_from_xml($xml, 'UTF-8');
596 # MARC::Record builded => now, record in DB
597 # warn "R: ".$record->as_formatted;
598 # check that the barcode don't exist already
599 my $addedolditem = TransformMarcToKoha($dbh,$itemtosave);
600 my $exist_itemnumber = get_item_from_barcode($addedolditem->{'barcode'});
601 if ($exist_itemnumber && $exist_itemnumber != $itemnumber) {
602 push @errors,"barcode_not_unique";
603 } else {
604 ModItemFromMarc($itemtosave,$biblionumber,$itemnumber);
605 $itemnumber="";
607 $nextop="additem";
608 } elsif ($op eq "delinkitem"){
609 my $analyticfield = '773';
610 if ($marcflavour eq 'MARC21' || $marcflavour eq 'NORMARC'){
611 $analyticfield = '773';
612 } elsif ($marcflavour eq 'UNIMARC') {
613 $analyticfield = '461';
615 foreach my $field ($record->field($analyticfield)){
616 if ($field->subfield('9') eq $hostitemnumber){
617 $record->delete_field($field);
618 last;
621 my $modbibresult = ModBiblio($record, $biblionumber,'');
625 #-------------------------------------------------------------------------------
626 # build screen with existing items. and "new" one
627 #-------------------------------------------------------------------------------
629 # now, build existiing item list
630 my $temp = GetMarcBiblio( $biblionumber );
631 #my @fields = $record->fields();
634 my %witness; #---- stores the list of subfields used at least once, with the "meaning" of the code
635 my @big_array;
636 #---- finds where items.itemnumber is stored
637 my ( $itemtagfield, $itemtagsubfield) = &GetMarcFromKohaField("items.itemnumber", $frameworkcode);
638 my ($branchtagfield, $branchtagsubfield) = &GetMarcFromKohaField("items.homebranch", $frameworkcode);
639 C4::Biblio::EmbedItemsInMarcBiblio($temp, $biblionumber);
640 my @fields = $temp->fields();
643 my @hostitemnumbers;
644 if ( C4::Context->preference('EasyAnalyticalRecords') ) {
645 my $analyticfield = '773';
646 if ($marcflavour eq 'MARC21' || $marcflavour eq 'NORMARC') {
647 $analyticfield = '773';
648 } elsif ($marcflavour eq 'UNIMARC') {
649 $analyticfield = '461';
651 foreach my $hostfield ($temp->field($analyticfield)){
652 my $hostbiblionumber = $hostfield->subfield('0');
653 if ($hostbiblionumber){
654 my $hostrecord = GetMarcBiblio($hostbiblionumber, 1);
655 if ($hostrecord) {
656 my ($itemfield, undef) = GetMarcFromKohaField( 'items.itemnumber', GetFrameworkCode($hostbiblionumber) );
657 foreach my $hostitem ($hostrecord->field($itemfield)){
658 if ($hostitem->subfield('9') eq $hostfield->subfield('9')){
659 push (@fields, $hostitem);
660 push (@hostitemnumbers, $hostfield->subfield('9'));
669 foreach my $field (@fields) {
670 next if ( $field->tag() < 10 );
672 my @subf = $field->subfields or (); # don't use ||, as that forces $field->subfelds to be interpreted in scalar context
673 my %this_row;
674 # loop through each subfield
675 my $i = 0;
676 foreach my $subfield (@subf){
677 my $subfieldcode = $subfield->[0];
678 my $subfieldvalue= $subfield->[1];
680 next if ($tagslib->{$field->tag()}->{$subfieldcode}->{tab} ne 10
681 && ($field->tag() ne $itemtagfield
682 && $subfieldcode ne $itemtagsubfield));
683 $witness{$subfieldcode} = $tagslib->{$field->tag()}->{$subfieldcode}->{lib} if ($tagslib->{$field->tag()}->{$subfieldcode}->{tab} eq 10);
684 if ($tagslib->{$field->tag()}->{$subfieldcode}->{tab} eq 10) {
685 $this_row{$subfieldcode} .= " | " if($this_row{$subfieldcode});
686 $this_row{$subfieldcode} .= GetAuthorisedValueDesc( $field->tag(),
687 $subfieldcode, $subfieldvalue, '', $tagslib)
688 || $subfieldvalue;
691 if (($field->tag eq $branchtagfield) && ($subfieldcode eq $branchtagsubfield) && C4::Context->preference("IndependantBranches")) {
692 #verifying rights
693 my $userenv = C4::Context->userenv();
694 unless (($userenv->{'flags'} == 1) or (($userenv->{'branch'} eq $subfieldvalue))){
695 $this_row{'nomod'} = 1;
698 $this_row{itemnumber} = $subfieldvalue if ($field->tag() eq $itemtagfield && $subfieldcode eq $itemtagsubfield);
700 if ( C4::Context->preference('EasyAnalyticalRecords') ) {
701 foreach my $hostitemnumber (@hostitemnumbers){
702 if ($this_row{itemnumber} eq $hostitemnumber){
703 $this_row{hostitemflag} = 1;
704 $this_row{hostbiblionumber}= GetBiblionumberFromItemnumber($hostitemnumber);
705 last;
709 # my $countanalytics=GetAnalyticsCount($this_row{itemnumber});
710 # if ($countanalytics > 0){
711 # $this_row{countanalytics} = $countanalytics;
716 if (%this_row) {
717 push(@big_array, \%this_row);
721 my ($holdingbrtagf,$holdingbrtagsubf) = &GetMarcFromKohaField("items.holdingbranch",$frameworkcode);
722 @big_array = sort {$a->{$holdingbrtagsubf} cmp $b->{$holdingbrtagsubf}} @big_array;
724 # now, construct template !
725 # First, the existing items for display
726 my @item_value_loop;
727 my @header_value_loop;
728 for my $row ( @big_array ) {
729 my %row_data;
730 my @item_fields = map +{ field => $_ || '' }, @$row{ sort keys(%witness) };
731 $row_data{item_value} = [ @item_fields ];
732 $row_data{itemnumber} = $row->{itemnumber};
733 #reporting this_row values
734 $row_data{'nomod'} = $row->{'nomod'};
735 $row_data{'hostitemflag'} = $row->{'hostitemflag'};
736 $row_data{'hostbiblionumber'} = $row->{'hostbiblionumber'};
737 # $row_data{'countanalytics'} = $row->{'countanalytics'};
738 push(@item_value_loop,\%row_data);
740 foreach my $subfield_code (sort keys(%witness)) {
741 my %header_value;
742 $header_value{header_value} = $witness{$subfield_code};
743 push(@header_value_loop, \%header_value);
746 # now, build the item form for entering a new item
747 my @loop_data =();
748 my $i=0;
750 my $pref_itemcallnumber = C4::Context->preference('itemcallnumber');
752 my $onlymine = C4::Context->preference('IndependantBranches') &&
753 C4::Context->userenv &&
754 C4::Context->userenv->{flags}!=1 &&
755 C4::Context->userenv->{branch};
756 my $branch = $input->param('branch') || C4::Context->userenv->{branch};
757 my $branches = GetBranchesLoop($branch,$onlymine); # build once ahead of time, instead of multiple times later.
759 # We generate form, from actuel record
760 @fields = ();
761 if($itemrecord){
762 foreach my $field ($itemrecord->fields()){
763 my $tag = $field->{_tag};
764 foreach my $subfield ( $field->subfields() ){
766 my $subfieldtag = $subfield->[0];
767 my $value = $subfield->[1];
768 my $subfieldlib = $tagslib->{$tag}->{$subfieldtag};
770 next if subfield_is_koha_internal_p($subfieldtag);
771 next if ($tagslib->{$tag}->{$subfieldtag}->{'tab'} ne "10");
773 my $subfield_data = generate_subfield_form($tag, $subfieldtag, $value, $tagslib, $subfieldlib, $branches, $today_iso, $biblionumber, $temp, \@loop_data, $i);
775 push @fields, "$tag$subfieldtag";
776 push (@loop_data, $subfield_data);
777 $i++;
782 # and now we add fields that are empty
784 # Using last created item if it exists
786 $itemrecord = $cookieitemrecord if ($prefillitem and not $justaddeditem and $op ne "edititem");
788 # We generate form, and fill with values if defined
789 foreach my $tag ( keys %{$tagslib}){
790 foreach my $subtag (keys %{$tagslib->{$tag}}){
791 next if subfield_is_koha_internal_p($subtag);
792 next if ($tagslib->{$tag}->{$subtag}->{'tab'} ne "10");
793 next if any { /^$tag$subtag$/ } @fields;
795 my @values = (undef);
796 @values = $itemrecord->field($tag)->subfield($subtag) if ($itemrecord && defined($itemrecord->field($tag)->subfield($subtag)));
797 for my $value (@values){
798 my $subfield_data = generate_subfield_form($tag, $subtag, $value, $tagslib, $tagslib->{$tag}->{$subtag}, $branches, $today_iso, $biblionumber, $temp, \@loop_data, $i);
799 push (@loop_data, $subfield_data);
800 $i++;
804 @loop_data = sort {$a->{subfield} cmp $b->{subfield} } @loop_data;
806 # what's the next op ? it's what we are not in : an add if we're editing, otherwise, and edit.
807 $template->param( title => $record->title() ) if ($record ne "-1");
808 $template->param(
809 biblionumber => $biblionumber,
810 title => $oldrecord->{title},
811 author => $oldrecord->{author},
812 item_loop => \@item_value_loop,
813 item_header_loop => \@header_value_loop,
814 item => \@loop_data,
815 itemnumber => $itemnumber,
816 barcode => GetBarcodeFromItemnumber($itemnumber),
817 itemtagfield => $itemtagfield,
818 itemtagsubfield => $itemtagsubfield,
819 op => $nextop,
820 opisadd => ($nextop eq "saveitem") ? 0 : 1,
821 popup => $input->param('popup') ? 1: 0,
822 C4::Search::enabled_staff_search_views,
825 if ($frameworkcode eq 'FA'){
826 # fast cataloguing datas
827 $template->param(
828 'circborrowernumber' => $fa_circborrowernumber,
829 'barcode' => $fa_barcode,
830 'branch' => $fa_branch,
831 'stickyduedate' => $fa_stickyduedate,
832 'duedatespec' => $fa_duedatespec,
836 foreach my $error (@errors) {
837 $template->param($error => 1);
839 output_html_with_http_headers $input, $cookie, $template->output;