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
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.
23 #use warnings; FIXME - Bug 2505
31 use C4::Koha; # XXX subfield_is_koha_internal_p
32 use C4::Branch; # XXX subfield_is_koha_internal_p
35 use List::MoreUtils qw/any/;
37 use Storable qw(thaw freeze);
44 our $dbh = C4::Context->dbh;
47 my ($tagfield,$insubfield,$record) = @_;
50 foreach my $field ($record->field($tagfield)) {
51 my @subfields = $field->subfields();
52 foreach my $subfield (@subfields) {
53 if (@$subfield[0] eq $insubfield) {
54 $result .= @$subfield[1];
55 $indicator = $field->indicator(1).$field->indicator(2);
59 return($indicator,$result);
62 sub get_item_from_barcode {
64 my $dbh=C4::Context->dbh;
66 my $rq=$dbh->prepare("SELECT itemnumber from items where items.barcode=?");
67 $rq->execute($barcode);
68 ($result)=$rq->fetchrow;
72 sub set_item_default_location {
73 my $itemnumber = shift;
74 my $item = GetItem( $itemnumber );
75 if ( C4::Context->preference('NewItemsDefaultLocation') ) {
76 $item->{'permanent_location'} = $item->{'location'};
77 $item->{'location'} = C4::Context->preference('NewItemsDefaultLocation');
78 ModItem( $item, undef, $itemnumber);
81 $item->{'permanent_location'} = $item->{'location'} if !defined($item->{'permanent_location'});
82 ModItem( $item, undef, $itemnumber);
86 # NOTE: This code is subject to change in the future with the implemenation of ajax based autobarcode code
87 # NOTE: 'incremental' is the ONLY autoBarcode option available to those not using javascript
88 sub _increment_barcode {
89 my ($record, $frameworkcode) = @_;
90 my ($tagfield,$tagsubfield) = &GetMarcFromKohaField("items.barcode",$frameworkcode);
91 unless ($record->field($tagfield)->subfield($tagsubfield)) {
92 my $sth_barcode = $dbh->prepare("select max(abs(barcode)) from items");
93 $sth_barcode->execute;
94 my ($newbarcode) = $sth_barcode->fetchrow;
96 # OK, we have the new barcode, now create the entry in MARC record
97 my $fieldItem = $record->field($tagfield);
98 $record->delete_field($fieldItem);
99 $fieldItem->add_subfields($tagsubfield => $newbarcode);
100 $record->insert_fields_ordered($fieldItem);
106 sub generate_subfield_form {
107 my ($tag, $subfieldtag, $value, $tagslib,$subfieldlib, $branches, $today_iso, $biblionumber, $temp, $loop_data, $i, $limitededition) = @_;
109 my $frameworkcode = &GetFrameworkCode($biblionumber);
112 my $dbh = C4::Context->dbh;
114 my $index_subfield = int(rand(1000000));
115 if ($subfieldtag eq '@'){
116 $subfield_data{id} = "tag_".$tag."_subfield_00_".$index_subfield;
118 $subfield_data{id} = "tag_".$tag."_subfield_".$subfieldtag."_".$index_subfield;
121 $subfield_data{tag} = $tag;
122 $subfield_data{subfield} = $subfieldtag;
123 $subfield_data{random} = int(rand(1000000)); # why do we need 2 different randoms?
124 $subfield_data{marc_lib} ="<span id=\"error$i\" title=\"".$subfieldlib->{lib}."\">".$subfieldlib->{lib}."</span>";
125 $subfield_data{mandatory} = $subfieldlib->{mandatory};
126 $subfield_data{repeatable} = $subfieldlib->{repeatable};
127 $subfield_data{maxlength} = $subfieldlib->{maxlength};
129 $value =~ s/"/"/g;
130 if ( ! defined( $value ) || $value eq '') {
131 $value = $subfieldlib->{defaultvalue};
132 # get today date & replace YYYY, MM, DD if provided in the default value
133 my ( $year, $month, $day ) = split ',', $today_iso; # FIXME: iso dates don't have commas!
134 $value =~ s/YYYY/$year/g;
135 $value =~ s/MM/$month/g;
136 $value =~ s/DD/$day/g;
139 $subfield_data{visibility} = "display:none;" if (($subfieldlib->{hidden} > 4) || ($subfieldlib->{hidden} < -4));
141 my $pref_itemcallnumber = C4::Context->preference('itemcallnumber');
142 if (!$value && $subfieldlib->{kohafield} eq 'items.itemcallnumber' && $pref_itemcallnumber) {
143 my $CNtag = substr($pref_itemcallnumber, 0, 3);
144 my $CNsubfield = substr($pref_itemcallnumber, 3, 1);
145 my $CNsubfield2 = substr($pref_itemcallnumber, 4, 1);
146 my $temp2 = $temp->field($CNtag);
148 $value = ($temp2->subfield($CNsubfield)).' '.($temp2->subfield($CNsubfield2));
149 #remove any trailing space incase one subfield is used
150 $value =~ s/^\s+|\s+$//g;
154 if ($frameworkcode eq 'FA' && $subfieldlib->{kohafield} eq 'items.barcode' && !$value){
156 $value = $input->param('barcode');
158 my $attributes_no_value = qq(id="$subfield_data{id}" name="field_value" class="input_marceditor" size="50" maxlength="$subfield_data{maxlength}" );
159 my $attributes_no_value_textarea = qq(id="$subfield_data{id}" name="field_value" class="input_marceditor" rows="5" cols="64" );
161 # Getting list of subfields to keep when limited edition is enabled
162 my $subfieldsToAllowForLimitedEdition = C4::Context->preference('SubfieldsToAllowForLimitedEdition');
163 my @subfieldsToAllow = split(/ /, $subfieldsToAllowForLimitedEdition);
165 # If we're on limited edition, and our field is not in the list of subfields to allow,
166 # then it is read-only
167 $attributes_no_value .= 'readonly="readonly" '
170 and !grep { $tag . '$' . $subfieldtag eq $_ } @subfieldsToAllow
173 my $attributes = qq($attributes_no_value value="$value" );
175 if ( $subfieldlib->{authorised_value} ) {
176 my @authorised_values;
178 # builds list, depending on authorised value...
179 if ( $subfieldlib->{authorised_value} eq "branches" ) {
180 foreach my $thisbranch (@$branches) {
181 push @authorised_values, $thisbranch->{value};
182 $authorised_lib{$thisbranch->{value}} = $thisbranch->{branchname};
183 $value = $thisbranch->{value} if $thisbranch->{selected} && !$value;
186 elsif ( $subfieldlib->{authorised_value} eq "itemtypes" ) {
187 push @authorised_values, "" unless ( $subfieldlib->{mandatory} );
188 my $sth = $dbh->prepare("SELECT itemtype,description FROM itemtypes ORDER BY description");
190 while ( my ( $itemtype, $description ) = $sth->fetchrow_array ) {
191 push @authorised_values, $itemtype;
192 $authorised_lib{$itemtype} = $description;
196 my $itype_sth = $dbh->prepare("SELECT itemtype FROM biblioitems WHERE biblionumber = ?");
197 $itype_sth->execute( $biblionumber );
198 ( $value ) = $itype_sth->fetchrow_array;
203 elsif ( $subfieldlib->{authorised_value} eq "cn_source" ) {
204 push @authorised_values, "" unless ( $subfieldlib->{mandatory} );
206 my $class_sources = GetClassSources();
207 my $default_source = C4::Context->preference("DefaultClassificationSource");
209 foreach my $class_source (sort keys %$class_sources) {
210 next unless $class_sources->{$class_source}->{'used'} or
211 ($value and $class_source eq $value) or
212 ($class_source eq $default_source);
213 push @authorised_values, $class_source;
214 $authorised_lib{$class_source} = $class_sources->{$class_source}->{'description'};
216 $value = $default_source unless ($value);
218 #---- "true" authorised value
221 push @authorised_values, qq{} unless ( $subfieldlib->{mandatory} );
222 my $av = GetAuthorisedValues( $subfieldlib->{authorised_value} );
224 push @authorised_values, $r->{authorised_value};
225 $authorised_lib{$r->{authorised_value}} = $r->{lib};
229 if ($subfieldlib->{'hidden'}) {
230 $subfield_data{marc_value} = qq(<input type="hidden" $attributes /> $authorised_lib{$value});
234 -name => "field_value",
235 -values => \@authorised_values,
237 -labels => \%authorised_lib,
241 -id => "tag_".$tag."_subfield_".$subfieldtag."_".$index_subfield,
242 -class => "input_marceditor",
245 # If we're on limited edition, and our field is not in the list of subfields to allow,
246 # then it is read-only
247 push @scrparam, (-readonly => "readonly"), (-disabled => "disabled")
250 and !grep { $tag . '$' . $subfieldtag eq $_ } @subfieldsToAllow
252 $subfield_data{marc_value} =CGI::scrolling_list(@scrparam);
256 # it's a thesaurus / authority field
257 elsif ( $subfieldlib->{authtypecode} ) {
258 $subfield_data{marc_value} = "<input type=\"text\" $attributes />
259 <a href=\"#\" class=\"buttonDot\"
260 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>
263 # it's a plugin field
264 elsif ( $subfieldlib->{value_builder} ) {
266 my $plugin = C4::Context->intranetdir . "/cataloguing/value_builder/" . $subfieldlib->{'value_builder'};
268 my $extended_param = plugin_parameters( $dbh, $temp, $tagslib, $subfield_data{id}, $loop_data );
269 my ( $function_name, $javascript ) = plugin_javascript( $dbh, $temp, $tagslib, $subfield_data{id}, $loop_data );
270 my $change = index($javascript, 'function Change') > -1 ?
271 "return Change$function_name($subfield_data{random}, '$subfield_data{id}');" :
273 $subfield_data{marc_value} = qq[<input type="text" $attributes
274 onfocus="Focus$function_name($subfield_data{random}, '$subfield_data{id}');"
276 onblur=" Blur$function_name($subfield_data{random}, '$subfield_data{id}');" />
277 <a href="#" class="buttonDot" onclick="Clic$function_name('$subfield_data{id}'); return false;" title="Tag Editor">...</a>
280 warn "Plugin Failed: $plugin";
281 $subfield_data{marc_value} = "<input type=\"text\" $attributes />"; # supply default input form
284 elsif ( $tag eq '' ) { # it's an hidden field
285 $subfield_data{marc_value} = qq(<input type="hidden" $attributes />);
287 elsif ( $subfieldlib->{'hidden'} ) { # FIXME: shouldn't input type be "hidden" ?
288 $subfield_data{marc_value} = qq(<input type="text" $attributes />);
290 elsif ( length($value) > 100
291 or (C4::Context->preference("marcflavour") eq "UNIMARC" and
292 300 <= $tag && $tag < 400 && $subfieldtag eq 'a' )
293 or (C4::Context->preference("marcflavour") eq "MARC21" and
294 500 <= $tag && $tag < 600 )
296 # oversize field (textarea)
297 $subfield_data{marc_value} = "<textarea $attributes_no_value_textarea>$value</textarea>\n";
299 # it's a standard field
300 $subfield_data{marc_value} = "<input type=\"text\" $attributes />";
303 return \%subfield_data;
306 # Removes some subfields when prefilling items
307 # This function will remove any subfield that is not in the SubfieldsToUseWhenPrefill syspref
308 sub removeFieldsForPrefill {
313 my ($tag, $subtag) = GetMarcFromKohaField("items.barcode", '');
315 # Getting list of subfields to keep
316 my $subfieldsToUseWhenPrefill = C4::Context->preference('SubfieldsToUseWhenPrefill');
318 # Removing subfields that are not in the syspref
319 if ($tag && $subfieldsToUseWhenPrefill) {
320 my $field = $item->field($tag);
321 my @subfieldsToUse= split(/ /,$subfieldsToUseWhenPrefill);
322 foreach my $subfield ($field->subfields()) {
323 if (!grep { $subfield->[0] eq $_ } @subfieldsToUse) {
324 $field->delete_subfield(code => $subfield->[0]);
335 my $error = $input->param('error');
336 my $biblionumber = $input->param('biblionumber');
337 my $itemnumber = $input->param('itemnumber');
338 my $op = $input->param('op');
339 my $hostitemnumber = $input->param('hostitemnumber');
340 my $marcflavour = C4::Context->preference("marcflavour");
341 my $searchid = $input->param('searchid');
342 # fast cataloguing datas
343 my $fa_circborrowernumber = $input->param('circborrowernumber');
344 my $fa_barcode = $input->param('barcode');
345 my $fa_branch = $input->param('branch');
346 my $fa_stickyduedate = $input->param('stickyduedate');
347 my $fa_duedatespec = $input->param('duedatespec');
349 my $frameworkcode = &GetFrameworkCode($biblionumber);
351 # Defining which userflag is needing according to the framework currently used
353 if (defined $input->param('frameworkcode')) {
354 $userflags = ($input->param('frameworkcode') eq 'FA') ? "fast_cataloging" : "edit_items";
357 if (not defined $userflags) {
358 $userflags = ($frameworkcode eq 'FA') ? "fast_cataloging" : "edit_items";
361 my ($template, $loggedinuser, $cookie)
362 = get_template_and_user({template_name => "cataloguing/additem.tt",
365 authnotrequired => 0,
366 flagsrequired => {editcatalogue => $userflags},
371 # Does the user have a limited item edition permission?
372 my $uid = $loggedinuser ? GetMember( borrowernumber => $loggedinuser )->{userid} : undef;
373 my $limitededition = $uid ? haspermission($uid, {'editcatalogue' => 'limited_item_edition'}) : undef;
374 # In case user is a superlibrarian, edition is not limited
375 $limitededition = 0 if ($limitededition != 0 && $limitededition->{'superlibrarian'} eq 1);
377 my $today_iso = C4::Dates->today('iso');
378 my $tagslib = &GetMarcStructure(1,$frameworkcode);
379 my $record = GetMarcBiblio($biblionumber);
380 my $oldrecord = TransformMarcToKoha($dbh,$record);
382 my $nextop="additem";
383 my @errors; # store errors found while checking data BEFORE saving item.
385 # Getting last created item cookie
386 my $prefillitem = C4::Context->preference('PrefillItem');
388 my $cookieitemrecord;
390 my $lastitemcookie = $input->cookie('LastCreatedItem');
391 if ($lastitemcookie) {
392 $lastitemcookie = uri_unescape($lastitemcookie);
393 if ( thaw($lastitemcookie) ) {
394 $cookieitemrecord = thaw($lastitemcookie) ;
395 $cookieitemrecord = removeFieldsForPrefill($cookieitemrecord);
400 #-------------------------------------------------------------------------------
401 if ($op eq "additem") {
403 #-------------------------------------------------------------------------------
405 my @tags = $input->param('tag');
406 my @subfields = $input->param('subfield');
407 my @values = $input->param('field_value');
408 # build indicator hash.
409 my @ind_tag = $input->param('ind_tag');
410 my @indicator = $input->param('indicator');
411 my $xml = TransformHtmlToXml(\@tags,\@subfields,\@values,\@indicator,\@ind_tag, 'ITEM');
412 my $record = MARC::Record::new_from_xml($xml, 'UTF-8');
415 my $add_submit = $input->param('add_submit');
416 my $add_duplicate_submit = $input->param('add_duplicate_submit');
417 my $add_multiple_copies_submit = $input->param('add_multiple_copies_submit');
418 my $number_of_copies = $input->param('number_of_copies');
420 # This is a bit tricky : if there is a cookie for the last created item and
421 # we just added an item, the cookie value is not correct yet (it will be updated
422 # next page). To prevent the form from being filled with outdated values, we
423 # force the use of "add and duplicate" feature, so the form will be filled with
425 $add_duplicate_submit = 1 if ($prefillitem);
428 # if autoBarcode is set to 'incremental', calculate barcode...
429 if ( C4::Context->preference('autoBarcode') eq 'incremental' ) {
430 $record = _increment_barcode($record, $frameworkcode);
433 my $addedolditem = TransformMarcToKoha( $dbh, $record );
435 # If we have to add or add & duplicate, we add the item
436 if ( $add_submit || $add_duplicate_submit ) {
438 # check for item barcode # being unique
439 my $exist_itemnumber = get_item_from_barcode( $addedolditem->{'barcode'} );
440 push @errors, "barcode_not_unique" if ($exist_itemnumber);
442 # if barcode exists, don't create, but report The problem.
443 unless ($exist_itemnumber) {
444 my ( $oldbiblionumber, $oldbibnum, $oldbibitemnum ) = AddItemFromMarc( $record, $biblionumber );
445 set_item_default_location($oldbibitemnum);
447 # Pushing the last created item cookie back
448 if ($prefillitem && defined $record) {
449 my $itemcookie = $input->cookie(
450 -name => 'LastCreatedItem',
451 # We uri_escape the whole freezed structure so we're sure we won't have any encoding problems
452 -value => uri_escape_utf8( freeze( $record ) ),
457 $cookie = [ $cookie, $itemcookie ];
462 if ($exist_itemnumber) {
463 $itemrecord = $record;
467 # If we have to add & duplicate
468 if ($add_duplicate_submit) {
469 $itemrecord = $record;
470 if (C4::Context->preference('autoBarcode') eq 'incremental') {
471 $itemrecord = _increment_barcode($itemrecord, $frameworkcode);
474 # we have to clear the barcode field in the duplicate item record to make way for the new one generated by the javascript plugin
475 my ($tagfield,$tagsubfield) = &GetMarcFromKohaField("items.barcode",$frameworkcode);
476 my $fieldItem = $itemrecord->field($tagfield);
477 $itemrecord->delete_field($fieldItem);
478 $fieldItem->delete_subfields($tagsubfield);
479 $itemrecord->insert_fields_ordered($fieldItem);
481 $itemrecord = removeFieldsForPrefill($itemrecord) if ($prefillitem);
484 # If we have to add multiple copies
485 if ($add_multiple_copies_submit) {
488 my $barcodeobj = C4::Barcodes->new;
489 my $oldbarcode = $addedolditem->{'barcode'};
490 my ($tagfield,$tagsubfield) = &GetMarcFromKohaField("items.barcode",$frameworkcode);
492 # If there is a barcode and we can't find him new values, we can't add multiple copies
494 $testbarcode = $barcodeobj->next_value($oldbarcode) if $barcodeobj;
495 if ($oldbarcode && !$testbarcode) {
497 push @errors, "no_next_barcode";
498 $itemrecord = $record;
503 # For the first iteration
504 my $barcodevalue = $oldbarcode;
505 my $exist_itemnumber;
508 for (my $i = 0; $i < $number_of_copies;) {
510 # If there is a barcode
513 # Getting a new barcode (if it is not the first iteration or the barcode we tried already exists)
514 $barcodevalue = $barcodeobj->next_value($oldbarcode) if ($i > 0 || $exist_itemnumber);
516 # Putting it into the record
518 $record->field($tagfield)->update($tagsubfield => $barcodevalue);
521 # Checking if the barcode already exists
522 $exist_itemnumber = get_item_from_barcode($barcodevalue);
526 if (!$exist_itemnumber) {
527 my ($oldbiblionumber,$oldbibnum,$oldbibitemnum) = AddItemFromMarc($record,$biblionumber);
528 set_item_default_location($oldbibitemnum);
530 # We count the item only if it was really added
531 # That way, all items are added, even if there was some already existing barcodes
532 # FIXME : Please note that there is a risk of infinite loop here if we never find a suitable barcode
536 # Preparing the next iteration
537 $oldbarcode = $barcodevalue;
542 if ($frameworkcode eq 'FA' && $fa_circborrowernumber){
543 print $input->redirect(
544 '/cgi-bin/koha/circ/circulation.pl?'
545 .'borrowernumber='.$fa_circborrowernumber
546 .'&barcode='.uri_escape($fa_barcode)
547 .'&duedatespec='.$fa_duedatespec
554 #-------------------------------------------------------------------------------
555 } elsif ($op eq "edititem") {
556 #-------------------------------------------------------------------------------
557 # retrieve item if exist => then, it's a modif
558 $itemrecord = C4::Items::GetMarcItem($biblionumber,$itemnumber);
559 $nextop = "saveitem";
560 #-------------------------------------------------------------------------------
561 } elsif ($op eq "delitem") {
562 #-------------------------------------------------------------------------------
563 # check that there is no issue on this item before deletion.
564 $error = &DelItemCheck($dbh,$biblionumber,$itemnumber);
566 print $input->redirect("additem.pl?biblionumber=$biblionumber&frameworkcode=$frameworkcode&searchid=$searchid");
571 #-------------------------------------------------------------------------------
572 } elsif ($op eq "delallitems") {
573 #-------------------------------------------------------------------------------
574 my @biblioitems = &GetBiblioItemByBiblioNumber($biblionumber);
577 foreach my $biblioitem (@biblioitems) {
578 my $items = &GetItemsByBiblioitemnumber( $biblioitem->{biblioitemnumber} );
580 foreach my $item (@$items) {
581 $error =&DelItemCheck( $dbh, $biblionumber, $item->{itemnumber} );
595 my $defaultview = C4::Context->preference('IntranetBiblioDefaultView');
596 my $views = { C4::Search::enabled_staff_search_views };
597 if ($defaultview eq 'isbd' && $views->{can_view_ISBD}) {
598 print $input->redirect("/cgi-bin/koha/catalogue/ISBDdetail.pl?biblionumber=$biblionumber&searchid=$searchid");
599 } elsif ($defaultview eq 'marc' && $views->{can_view_MARC}) {
600 print $input->redirect("/cgi-bin/koha/catalogue/MARCdetail.pl?biblionumber=$biblionumber&searchid=$searchid");
601 } elsif ($defaultview eq 'labeled_marc' && $views->{can_view_labeledMARC}) {
602 print $input->redirect("/cgi-bin/koha/catalogue/labeledMARCdetail.pl?biblionumber=$biblionumber&searchid=$searchid");
604 print $input->redirect("/cgi-bin/koha/catalogue/detail.pl?biblionumber=$biblionumber&searchid=$searchid");
609 #-------------------------------------------------------------------------------
610 } elsif ($op eq "saveitem") {
611 #-------------------------------------------------------------------------------
613 my @tags = $input->param('tag');
614 my @subfields = $input->param('subfield');
615 my @values = $input->param('field_value');
616 # build indicator hash.
617 my @ind_tag = $input->param('ind_tag');
618 my @indicator = $input->param('indicator');
619 # my $itemnumber = $input->param('itemnumber');
620 my $xml = TransformHtmlToXml(\@tags,\@subfields,\@values,\@indicator,\@ind_tag,'ITEM');
621 my $itemtosave=MARC::Record::new_from_xml($xml, 'UTF-8');
622 # MARC::Record builded => now, record in DB
623 # warn "R: ".$record->as_formatted;
624 # check that the barcode don't exist already
625 my $addedolditem = TransformMarcToKoha($dbh,$itemtosave);
626 my $exist_itemnumber = get_item_from_barcode($addedolditem->{'barcode'});
627 if ($exist_itemnumber && $exist_itemnumber != $itemnumber) {
628 push @errors,"barcode_not_unique";
630 ModItemFromMarc($itemtosave,$biblionumber,$itemnumber);
633 my $item = GetItem( $itemnumber );
634 my $olditemlost = $item->{'itemlost'};
636 my ($lost_tag,$lost_subfield) = GetMarcFromKohaField("items.itemlost",'');
638 my $newitemlost = $itemtosave->subfield( $lost_tag, $lost_subfield );
639 if (($olditemlost eq '0' or $olditemlost eq '' ) and $newitemlost ge '1'){
640 LostItem($itemnumber,'MARK RETURNED');
643 } elsif ($op eq "delinkitem"){
644 my $analyticfield = '773';
645 if ($marcflavour eq 'MARC21' || $marcflavour eq 'NORMARC'){
646 $analyticfield = '773';
647 } elsif ($marcflavour eq 'UNIMARC') {
648 $analyticfield = '461';
650 foreach my $field ($record->field($analyticfield)){
651 if ($field->subfield('9') eq $hostitemnumber){
652 $record->delete_field($field);
656 my $modbibresult = ModBiblio($record, $biblionumber,'');
660 #-------------------------------------------------------------------------------
661 # build screen with existing items. and "new" one
662 #-------------------------------------------------------------------------------
664 # now, build existiing item list
665 my $temp = GetMarcBiblio( $biblionumber );
666 #my @fields = $record->fields();
669 my %witness; #---- stores the list of subfields used at least once, with the "meaning" of the code
671 #---- finds where items.itemnumber is stored
672 my ( $itemtagfield, $itemtagsubfield) = &GetMarcFromKohaField("items.itemnumber", $frameworkcode);
673 my ($branchtagfield, $branchtagsubfield) = &GetMarcFromKohaField("items.homebranch", $frameworkcode);
674 C4::Biblio::EmbedItemsInMarcBiblio($temp, $biblionumber);
675 my @fields = $temp->fields();
679 if ( C4::Context->preference('EasyAnalyticalRecords') ) {
680 my $analyticfield = '773';
681 if ($marcflavour eq 'MARC21' || $marcflavour eq 'NORMARC') {
682 $analyticfield = '773';
683 } elsif ($marcflavour eq 'UNIMARC') {
684 $analyticfield = '461';
686 foreach my $hostfield ($temp->field($analyticfield)){
687 my $hostbiblionumber = $hostfield->subfield('0');
688 if ($hostbiblionumber){
689 my $hostrecord = GetMarcBiblio($hostbiblionumber, 1);
691 my ($itemfield, undef) = GetMarcFromKohaField( 'items.itemnumber', GetFrameworkCode($hostbiblionumber) );
692 foreach my $hostitem ($hostrecord->field($itemfield)){
693 if ($hostitem->subfield('9') eq $hostfield->subfield('9')){
694 push (@fields, $hostitem);
695 push (@hostitemnumbers, $hostfield->subfield('9'));
704 foreach my $field (@fields) {
705 next if ( $field->tag() < 10 );
707 my @subf = $field->subfields or (); # don't use ||, as that forces $field->subfelds to be interpreted in scalar context
709 # loop through each subfield
711 foreach my $subfield (@subf){
712 my $subfieldcode = $subfield->[0];
713 my $subfieldvalue= $subfield->[1];
715 next if ($tagslib->{$field->tag()}->{$subfieldcode}->{tab} ne 10
716 && ($field->tag() ne $itemtagfield
717 && $subfieldcode ne $itemtagsubfield));
718 $witness{$subfieldcode} = $tagslib->{$field->tag()}->{$subfieldcode}->{lib} if ($tagslib->{$field->tag()}->{$subfieldcode}->{tab} eq 10);
719 if ($tagslib->{$field->tag()}->{$subfieldcode}->{tab} eq 10) {
720 $this_row{$subfieldcode} .= " | " if($this_row{$subfieldcode});
721 $this_row{$subfieldcode} .= GetAuthorisedValueDesc( $field->tag(),
722 $subfieldcode, $subfieldvalue, '', $tagslib)
726 if (($field->tag eq $branchtagfield) && ($subfieldcode eq $branchtagsubfield) && C4::Context->preference("IndependentBranches")) {
728 my $userenv = C4::Context->userenv();
729 unless (C4::Context->IsSuperLibrarian() or (($userenv->{'branch'} eq $subfieldvalue))){
730 $this_row{'nomod'} = 1;
733 $this_row{itemnumber} = $subfieldvalue if ($field->tag() eq $itemtagfield && $subfieldcode eq $itemtagsubfield);
735 if ( C4::Context->preference('EasyAnalyticalRecords') ) {
736 foreach my $hostitemnumber (@hostitemnumbers){
737 if ($this_row{itemnumber} eq $hostitemnumber){
738 $this_row{hostitemflag} = 1;
739 $this_row{hostbiblionumber}= GetBiblionumberFromItemnumber($hostitemnumber);
744 # my $countanalytics=GetAnalyticsCount($this_row{itemnumber});
745 # if ($countanalytics > 0){
746 # $this_row{countanalytics} = $countanalytics;
752 push(@big_array, \%this_row);
756 my ($holdingbrtagf,$holdingbrtagsubf) = &GetMarcFromKohaField("items.holdingbranch",$frameworkcode);
757 @big_array = sort {$a->{$holdingbrtagsubf} cmp $b->{$holdingbrtagsubf}} @big_array;
759 # now, construct template !
760 # First, the existing items for display
762 my @header_value_loop;
763 for my $row ( @big_array ) {
765 my @item_fields = map +{ field => $_ || '' }, @$row{ sort keys(%witness) };
766 $row_data{item_value} = [ @item_fields ];
767 $row_data{itemnumber} = $row->{itemnumber};
768 #reporting this_row values
769 $row_data{'nomod'} = $row->{'nomod'};
770 $row_data{'hostitemflag'} = $row->{'hostitemflag'};
771 $row_data{'hostbiblionumber'} = $row->{'hostbiblionumber'};
772 # $row_data{'countanalytics'} = $row->{'countanalytics'};
773 push(@item_value_loop,\%row_data);
775 foreach my $subfield_code (sort keys(%witness)) {
777 $header_value{header_value} = $witness{$subfield_code};
778 push(@header_value_loop, \%header_value);
781 # now, build the item form for entering a new item
785 my $pref_itemcallnumber = C4::Context->preference('itemcallnumber');
788 C4::Context->preference('IndependentBranches')
789 && C4::Context->userenv
790 && !C4::Context->IsSuperLibrarian()
791 && C4::Context->userenv->{branch};
792 my $branch = $input->param('branch') || C4::Context->userenv->{branch};
793 my $branches = GetBranchesLoop($branch,$onlymine); # build once ahead of time, instead of multiple times later.
795 # We generate form, from actuel record
798 foreach my $field ($itemrecord->fields()){
799 my $tag = $field->{_tag};
800 foreach my $subfield ( $field->subfields() ){
802 my $subfieldtag = $subfield->[0];
803 my $value = $subfield->[1];
804 my $subfieldlib = $tagslib->{$tag}->{$subfieldtag};
806 next if subfield_is_koha_internal_p($subfieldtag);
807 next if ($tagslib->{$tag}->{$subfieldtag}->{'tab'} ne "10");
809 my $subfield_data = generate_subfield_form($tag, $subfieldtag, $value, $tagslib, $subfieldlib, $branches, $today_iso, $biblionumber, $temp, \@loop_data, $i, $limitededition);
810 push @fields, "$tag$subfieldtag";
811 push (@loop_data, $subfield_data);
817 # and now we add fields that are empty
819 # Using last created item if it exists
821 $itemrecord = $cookieitemrecord if ($prefillitem and not $justaddeditem and $op ne "edititem");
823 # We generate form, and fill with values if defined
824 foreach my $tag ( keys %{$tagslib}){
825 foreach my $subtag (keys %{$tagslib->{$tag}}){
826 next if subfield_is_koha_internal_p($subtag);
827 next if ($tagslib->{$tag}->{$subtag}->{'tab'} ne "10");
828 next if any { /^$tag$subtag$/ } @fields;
830 my @values = (undef);
831 @values = $itemrecord->field($tag)->subfield($subtag) if ($itemrecord && defined($itemrecord->field($tag)) && defined($itemrecord->field($tag)->subfield($subtag)));
832 for my $value (@values){
833 my $subfield_data = generate_subfield_form($tag, $subtag, $value, $tagslib, $tagslib->{$tag}->{$subtag}, $branches, $today_iso, $biblionumber, $temp, \@loop_data, $i, $limitededition);
834 push (@loop_data, $subfield_data);
839 @loop_data = sort {$a->{subfield} cmp $b->{subfield} } @loop_data;
841 # what's the next op ? it's what we are not in : an add if we're editing, otherwise, and edit.
842 $template->param( title => $record->title() ) if ($record ne "-1");
844 biblionumber => $biblionumber,
845 title => $oldrecord->{title},
846 author => $oldrecord->{author},
847 item_loop => \@item_value_loop,
848 item_header_loop => \@header_value_loop,
850 itemnumber => $itemnumber,
851 barcode => GetBarcodeFromItemnumber($itemnumber),
852 itemtagfield => $itemtagfield,
853 itemtagsubfield => $itemtagsubfield,
855 opisadd => ($nextop eq "saveitem") ? 0 : 1,
856 popup => $input->param('popup') ? 1: 0,
857 C4::Search::enabled_staff_search_views,
859 $template->{'VARS'}->{'searchid'} = $searchid;
861 if ($frameworkcode eq 'FA'){
862 # fast cataloguing datas
864 'circborrowernumber' => $fa_circborrowernumber,
865 'barcode' => $fa_barcode,
866 'branch' => $fa_branch,
867 'stickyduedate' => $fa_stickyduedate,
868 'duedatespec' => $fa_duedatespec,
872 foreach my $error (@errors) {
873 $template->param($error => 1);
875 output_html_with_http_headers $input, $cookie, $template->output;