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
10 # under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 3 of the License, or
12 # (at your option) any later version.
14 # Koha is distributed in the hope that it will be useful, but
15 # WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with Koha; if not, see <http://www.gnu.org/licenses>.
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, $restrictededition) = @_;
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{marc_lib} ="<span id=\"error$i\" title=\"".$subfieldlib->{lib}."\">".$subfieldlib->{lib}."</span>";
124 $subfield_data{mandatory} = $subfieldlib->{mandatory};
125 $subfield_data{repeatable} = $subfieldlib->{repeatable};
126 $subfield_data{maxlength} = $subfieldlib->{maxlength};
128 $value =~ s/"/"/g;
129 if ( ! defined( $value ) || $value eq '') {
130 $value = $subfieldlib->{defaultvalue};
131 # get today date & replace YYYY, MM, DD if provided in the default value
132 my ( $year, $month, $day ) = split ',', $today_iso; # FIXME: iso dates don't have commas!
133 $value =~ s/YYYY/$year/g;
134 $value =~ s/MM/$month/g;
135 $value =~ s/DD/$day/g;
138 $subfield_data{visibility} = "display:none;" if (($subfieldlib->{hidden} > 4) || ($subfieldlib->{hidden} <= -4));
140 my $pref_itemcallnumber = C4::Context->preference('itemcallnumber');
141 if (!$value && $subfieldlib->{kohafield} eq 'items.itemcallnumber' && $pref_itemcallnumber) {
142 my $CNtag = substr($pref_itemcallnumber, 0, 3);
143 my $CNsubfield = substr($pref_itemcallnumber, 3, 1);
144 my $CNsubfield2 = substr($pref_itemcallnumber, 4, 1);
145 my $temp2 = $temp->field($CNtag);
147 $value = ($temp2->subfield($CNsubfield)).' '.($temp2->subfield($CNsubfield2));
148 #remove any trailing space incase one subfield is used
149 $value =~ s/^\s+|\s+$//g;
153 if ($frameworkcode eq 'FA' && $subfieldlib->{kohafield} eq 'items.barcode' && !$value){
155 $value = $input->param('barcode');
157 my $attributes_no_value = qq(id="$subfield_data{id}" name="field_value" class="input_marceditor" size="50" maxlength="$subfield_data{maxlength}" );
159 # Getting list of subfields to keep when restricted editing is enabled
160 my $subfieldsToAllowForRestrictedEditing = C4::Context->preference('SubfieldsToAllowForRestrictedEditing');
161 my $allowAllSubfields = (
162 not defined $subfieldsToAllowForRestrictedEditing
163 or $subfieldsToAllowForRestrictedEditing == q||
165 my @subfieldsToAllow = split(/ /, $subfieldsToAllowForRestrictedEditing);
167 # If we're on restricted editing, and our field is not in the list of subfields to allow,
168 # then it is read-only
169 $attributes_no_value .= 'readonly="readonly" '
171 not $allowAllSubfields
172 and $restrictededition
173 and !grep { $tag . '$' . $subfieldtag eq $_ } @subfieldsToAllow
176 my $attributes = qq($attributes_no_value value="$value" );
178 if ( $subfieldlib->{authorised_value} ) {
179 my @authorised_values;
181 # builds list, depending on authorised value...
182 if ( $subfieldlib->{authorised_value} eq "branches" ) {
183 foreach my $thisbranch (@$branches) {
184 push @authorised_values, $thisbranch->{value};
185 $authorised_lib{$thisbranch->{value}} = $thisbranch->{branchname};
186 $value = $thisbranch->{value} if $thisbranch->{selected} && !$value;
189 elsif ( $subfieldlib->{authorised_value} eq "itemtypes" ) {
190 push @authorised_values, "" unless ( $subfieldlib->{mandatory} );
191 my $sth = $dbh->prepare("SELECT itemtype,description FROM itemtypes ORDER BY description");
193 while ( my ( $itemtype, $description ) = $sth->fetchrow_array ) {
194 push @authorised_values, $itemtype;
195 $authorised_lib{$itemtype} = $description;
199 my $itype_sth = $dbh->prepare("SELECT itemtype FROM biblioitems WHERE biblionumber = ?");
200 $itype_sth->execute( $biblionumber );
201 ( $value ) = $itype_sth->fetchrow_array;
206 elsif ( $subfieldlib->{authorised_value} eq "cn_source" ) {
207 push @authorised_values, "" unless ( $subfieldlib->{mandatory} );
209 my $class_sources = GetClassSources();
210 my $default_source = C4::Context->preference("DefaultClassificationSource");
212 foreach my $class_source (sort keys %$class_sources) {
213 next unless $class_sources->{$class_source}->{'used'} or
214 ($value and $class_source eq $value) or
215 ($class_source eq $default_source);
216 push @authorised_values, $class_source;
217 $authorised_lib{$class_source} = $class_sources->{$class_source}->{'description'};
219 $value = $default_source unless ($value);
221 #---- "true" authorised value
224 push @authorised_values, qq{} unless ( $subfieldlib->{mandatory} );
225 my $av = GetAuthorisedValues( $subfieldlib->{authorised_value} );
227 push @authorised_values, $r->{authorised_value};
228 $authorised_lib{$r->{authorised_value}} = $r->{lib};
232 if ( $subfieldlib->{hidden} > 4 or $subfieldlib->{hidden} <= -4 ) {
233 $subfield_data{marc_value} = {
235 id => $subfield_data{id},
236 maxlength => $subfield_data{max_length},
241 $subfield_data{marc_value} = {
243 id => "tag_".$tag."_subfield_".$subfieldtag."_".$index_subfield,
244 values => \@authorised_values,
245 labels => \%authorised_lib,
248 # If we're on restricted editing, and our field is not in the list of subfields to allow,
249 # then it is read-only
250 $subfield_data{marc_value}->{readonlyselect} = (
251 not $allowAllSubfields
252 and $restrictededition
253 and !grep { $tag . '$' . $subfieldtag eq $_ } @subfieldsToAllow
257 # it's a thesaurus / authority field
258 elsif ( $subfieldlib->{authtypecode} ) {
259 $subfield_data{marc_value} = {
261 id => $subfield_data{id},
262 maxlength => $subfield_data{max_length},
264 authtypecode => $subfieldlib->{authtypecode},
267 # it's a plugin field
268 elsif ( $subfieldlib->{value_builder} ) { # plugin
269 require Koha::FrameworkPlugin;
270 my $plugin = Koha::FrameworkPlugin->new({
271 name => $subfieldlib->{'value_builder'},
274 my $pars= { dbh => $dbh, record => $temp, tagslib =>$tagslib,
275 id => $subfield_data{id}, tabloop => $loop_data };
276 $plugin->build( $pars );
277 if( !$plugin->errstr ) {
278 my $class= 'buttonDot'. ( $plugin->noclick? ' disabled': '' );
279 $subfield_data{marc_value} = {
280 type => 'text_plugin',
281 id => $subfield_data{id},
282 maxlength => $subfield_data{max_length},
285 nopopup => $plugin->noclick,
286 javascript => $plugin->javascript,
289 warn $plugin->errstr;
290 $subfield_data{marc_value} = {
292 id => $subfield_data{id},
293 maxlength => $subfield_data{max_length},
295 }; # supply default input form
298 elsif ( $tag eq '' ) { # it's an hidden field
299 $subfield_data{marc_value} = {
301 id => $subfield_data{id},
302 maxlength => $subfield_data{max_length},
306 elsif ( $subfieldlib->{'hidden'} ) { # FIXME: shouldn't input type be "hidden" ?
307 $subfield_data{marc_value} = {
309 id => $subfield_data{id},
310 maxlength => $subfield_data{max_length},
317 C4::Context->preference("marcflavour") eq "UNIMARC"
318 and 300 <= $tag && $tag < 400 && $subfieldtag eq 'a'
321 C4::Context->preference("marcflavour") eq "MARC21"
322 and 500 <= $tag && $tag < 600
325 # oversize field (textarea)
326 $subfield_data{marc_value} = {
328 id => $subfield_data{id},
332 # it's a standard field
333 $subfield_data{marc_value} = {
335 id => $subfield_data{id},
336 maxlength => $subfield_data{max_length},
341 return \%subfield_data;
344 # Removes some subfields when prefilling items
345 # This function will remove any subfield that is not in the SubfieldsToUseWhenPrefill syspref
346 sub removeFieldsForPrefill {
351 my ($tag, $subtag) = GetMarcFromKohaField("items.barcode", '');
353 # Getting list of subfields to keep
354 my $subfieldsToUseWhenPrefill = C4::Context->preference('SubfieldsToUseWhenPrefill');
356 # Removing subfields that are not in the syspref
357 if ($tag && $subfieldsToUseWhenPrefill) {
358 my $field = $item->field($tag);
359 my @subfieldsToUse= split(/ /,$subfieldsToUseWhenPrefill);
360 foreach my $subfield ($field->subfields()) {
361 if (!grep { $subfield->[0] eq $_ } @subfieldsToUse) {
362 $field->delete_subfield(code => $subfield->[0]);
373 my $error = $input->param('error');
374 my $biblionumber = $input->param('biblionumber');
375 my $itemnumber = $input->param('itemnumber');
376 my $op = $input->param('op');
377 my $hostitemnumber = $input->param('hostitemnumber');
378 my $marcflavour = C4::Context->preference("marcflavour");
379 my $searchid = $input->param('searchid');
380 # fast cataloguing datas
381 my $fa_circborrowernumber = $input->param('circborrowernumber');
382 my $fa_barcode = $input->param('barcode');
383 my $fa_branch = $input->param('branch');
384 my $fa_stickyduedate = $input->param('stickyduedate');
385 my $fa_duedatespec = $input->param('duedatespec');
387 my $frameworkcode = &GetFrameworkCode($biblionumber);
389 # Defining which userflag is needing according to the framework currently used
391 if (defined $input->param('frameworkcode')) {
392 $userflags = ($input->param('frameworkcode') eq 'FA') ? "fast_cataloging" : "edit_items";
395 if (not defined $userflags) {
396 $userflags = ($frameworkcode eq 'FA') ? "fast_cataloging" : "edit_items";
399 my ($template, $loggedinuser, $cookie)
400 = get_template_and_user({template_name => "cataloguing/additem.tt",
403 authnotrequired => 0,
404 flagsrequired => {editcatalogue => $userflags},
409 # Does the user have a restricted item editing permission?
410 my $uid = $loggedinuser ? GetMember( borrowernumber => $loggedinuser )->{userid} : undef;
411 my $restrictededition = $uid ? haspermission($uid, {'editcatalogue' => 'edit_items_restricted'}) : undef;
412 # In case user is a superlibrarian, editing is not restricted
413 $restrictededition = 0 if ($restrictededition != 0 && C4::Context->IsSuperLibrarian());
414 # In case user has fast cataloging permission (and we're in fast cataloging), editing is not restricted
415 $restrictededition = 0 if ($restrictededition != 0 && $frameworkcode eq 'FA' && haspermission($uid, {'editcatalogue' => 'fast_cataloging'}));
417 my $today_iso = C4::Dates->today('iso');
418 my $tagslib = &GetMarcStructure(1,$frameworkcode);
419 my $record = GetMarcBiblio($biblionumber);
420 my $oldrecord = TransformMarcToKoha($dbh,$record);
422 my $nextop="additem";
423 my @errors; # store errors found while checking data BEFORE saving item.
425 # Getting last created item cookie
426 my $prefillitem = C4::Context->preference('PrefillItem');
428 my $cookieitemrecord;
430 my $lastitemcookie = $input->cookie('LastCreatedItem');
431 if ($lastitemcookie) {
432 $lastitemcookie = uri_unescape($lastitemcookie);
433 if ( thaw($lastitemcookie) ) {
434 $cookieitemrecord = thaw($lastitemcookie) ;
435 $cookieitemrecord = removeFieldsForPrefill($cookieitemrecord);
440 #-------------------------------------------------------------------------------
441 if ($op eq "additem") {
443 #-------------------------------------------------------------------------------
445 my @tags = $input->param('tag');
446 my @subfields = $input->param('subfield');
447 my @values = $input->param('field_value');
448 # build indicator hash.
449 my @ind_tag = $input->param('ind_tag');
450 my @indicator = $input->param('indicator');
451 my $xml = TransformHtmlToXml(\@tags,\@subfields,\@values,\@indicator,\@ind_tag, 'ITEM');
452 my $record = MARC::Record::new_from_xml($xml, 'UTF-8');
455 my $add_submit = $input->param('add_submit');
456 my $add_duplicate_submit = $input->param('add_duplicate_submit');
457 my $add_multiple_copies_submit = $input->param('add_multiple_copies_submit');
458 my $number_of_copies = $input->param('number_of_copies');
460 # This is a bit tricky : if there is a cookie for the last created item and
461 # we just added an item, the cookie value is not correct yet (it will be updated
462 # next page). To prevent the form from being filled with outdated values, we
463 # force the use of "add and duplicate" feature, so the form will be filled with
465 $add_duplicate_submit = 1 if ($prefillitem);
468 # if autoBarcode is set to 'incremental', calculate barcode...
469 if ( C4::Context->preference('autoBarcode') eq 'incremental' ) {
470 $record = _increment_barcode($record, $frameworkcode);
473 my $addedolditem = TransformMarcToKoha( $dbh, $record );
475 # If we have to add or add & duplicate, we add the item
476 if ( $add_submit || $add_duplicate_submit ) {
478 # check for item barcode # being unique
479 my $exist_itemnumber = get_item_from_barcode( $addedolditem->{'barcode'} );
480 push @errors, "barcode_not_unique" if ($exist_itemnumber);
482 # if barcode exists, don't create, but report The problem.
483 unless ($exist_itemnumber) {
484 my ( $oldbiblionumber, $oldbibnum, $oldbibitemnum ) = AddItemFromMarc( $record, $biblionumber );
485 set_item_default_location($oldbibitemnum);
487 # Pushing the last created item cookie back
488 if ($prefillitem && defined $record) {
489 my $itemcookie = $input->cookie(
490 -name => 'LastCreatedItem',
491 # We uri_escape the whole freezed structure so we're sure we won't have any encoding problems
492 -value => uri_escape_utf8( freeze( $record ) ),
497 $cookie = [ $cookie, $itemcookie ];
502 if ($exist_itemnumber) {
503 $itemrecord = $record;
507 # If we have to add & duplicate
508 if ($add_duplicate_submit) {
509 $itemrecord = $record;
510 if (C4::Context->preference('autoBarcode') eq 'incremental') {
511 $itemrecord = _increment_barcode($itemrecord, $frameworkcode);
514 # we have to clear the barcode field in the duplicate item record to make way for the new one generated by the javascript plugin
515 my ($tagfield,$tagsubfield) = &GetMarcFromKohaField("items.barcode",$frameworkcode);
516 my $fieldItem = $itemrecord->field($tagfield);
517 $itemrecord->delete_field($fieldItem);
518 $fieldItem->delete_subfields($tagsubfield);
519 $itemrecord->insert_fields_ordered($fieldItem);
521 $itemrecord = removeFieldsForPrefill($itemrecord) if ($prefillitem);
524 # If we have to add multiple copies
525 if ($add_multiple_copies_submit) {
528 my $barcodeobj = C4::Barcodes->new;
529 my $oldbarcode = $addedolditem->{'barcode'};
530 my ($tagfield,$tagsubfield) = &GetMarcFromKohaField("items.barcode",$frameworkcode);
532 # If there is a barcode and we can't find him new values, we can't add multiple copies
534 $testbarcode = $barcodeobj->next_value($oldbarcode) if $barcodeobj;
535 if ($oldbarcode && !$testbarcode) {
537 push @errors, "no_next_barcode";
538 $itemrecord = $record;
543 # For the first iteration
544 my $barcodevalue = $oldbarcode;
545 my $exist_itemnumber;
548 for (my $i = 0; $i < $number_of_copies;) {
550 # If there is a barcode
553 # Getting a new barcode (if it is not the first iteration or the barcode we tried already exists)
554 $barcodevalue = $barcodeobj->next_value($oldbarcode) if ($i > 0 || $exist_itemnumber);
556 # Putting it into the record
558 $record->field($tagfield)->update($tagsubfield => $barcodevalue);
561 # Checking if the barcode already exists
562 $exist_itemnumber = get_item_from_barcode($barcodevalue);
566 if (!$exist_itemnumber) {
567 my ($oldbiblionumber,$oldbibnum,$oldbibitemnum) = AddItemFromMarc($record,$biblionumber);
568 set_item_default_location($oldbibitemnum);
570 # We count the item only if it was really added
571 # That way, all items are added, even if there was some already existing barcodes
572 # FIXME : Please note that there is a risk of infinite loop here if we never find a suitable barcode
576 # Preparing the next iteration
577 $oldbarcode = $barcodevalue;
582 if ($frameworkcode eq 'FA' && $fa_circborrowernumber){
583 print $input->redirect(
584 '/cgi-bin/koha/circ/circulation.pl?'
585 .'borrowernumber='.$fa_circborrowernumber
586 .'&barcode='.uri_escape_utf8($fa_barcode)
587 .'&duedatespec='.$fa_duedatespec
594 #-------------------------------------------------------------------------------
595 } elsif ($op eq "edititem") {
596 #-------------------------------------------------------------------------------
597 # retrieve item if exist => then, it's a modif
598 $itemrecord = C4::Items::GetMarcItem($biblionumber,$itemnumber);
599 $nextop = "saveitem";
600 #-------------------------------------------------------------------------------
601 } elsif ($op eq "delitem") {
602 #-------------------------------------------------------------------------------
603 # check that there is no issue on this item before deletion.
604 $error = &DelItemCheck($dbh,$biblionumber,$itemnumber);
606 print $input->redirect("additem.pl?biblionumber=$biblionumber&frameworkcode=$frameworkcode&searchid=$searchid");
611 #-------------------------------------------------------------------------------
612 } elsif ($op eq "delallitems") {
613 #-------------------------------------------------------------------------------
614 my @biblioitems = &GetBiblioItemByBiblioNumber($biblionumber);
617 foreach my $biblioitem (@biblioitems) {
618 my $items = &GetItemsByBiblioitemnumber( $biblioitem->{biblioitemnumber} );
620 foreach my $item (@$items) {
621 $error =&DelItemCheck( $dbh, $biblionumber, $item->{itemnumber} );
635 my $defaultview = C4::Context->preference('IntranetBiblioDefaultView');
636 my $views = { C4::Search::enabled_staff_search_views };
637 if ($defaultview eq 'isbd' && $views->{can_view_ISBD}) {
638 print $input->redirect("/cgi-bin/koha/catalogue/ISBDdetail.pl?biblionumber=$biblionumber&searchid=$searchid");
639 } elsif ($defaultview eq 'marc' && $views->{can_view_MARC}) {
640 print $input->redirect("/cgi-bin/koha/catalogue/MARCdetail.pl?biblionumber=$biblionumber&searchid=$searchid");
641 } elsif ($defaultview eq 'labeled_marc' && $views->{can_view_labeledMARC}) {
642 print $input->redirect("/cgi-bin/koha/catalogue/labeledMARCdetail.pl?biblionumber=$biblionumber&searchid=$searchid");
644 print $input->redirect("/cgi-bin/koha/catalogue/detail.pl?biblionumber=$biblionumber&searchid=$searchid");
649 #-------------------------------------------------------------------------------
650 } elsif ($op eq "saveitem") {
651 #-------------------------------------------------------------------------------
653 my @tags = $input->param('tag');
654 my @subfields = $input->param('subfield');
655 my @values = $input->param('field_value');
656 # build indicator hash.
657 my @ind_tag = $input->param('ind_tag');
658 my @indicator = $input->param('indicator');
659 # my $itemnumber = $input->param('itemnumber');
660 my $xml = TransformHtmlToXml(\@tags,\@subfields,\@values,\@indicator,\@ind_tag,'ITEM');
661 my $itemtosave=MARC::Record::new_from_xml($xml, 'UTF-8');
662 # MARC::Record builded => now, record in DB
663 # warn "R: ".$record->as_formatted;
664 # check that the barcode don't exist already
665 my $addedolditem = TransformMarcToKoha($dbh,$itemtosave);
666 my $exist_itemnumber = get_item_from_barcode($addedolditem->{'barcode'});
667 if ($exist_itemnumber && $exist_itemnumber != $itemnumber) {
668 push @errors,"barcode_not_unique";
670 ModItemFromMarc($itemtosave,$biblionumber,$itemnumber);
673 my $item = GetItem( $itemnumber );
674 my $olditemlost = $item->{'itemlost'};
676 my ($lost_tag,$lost_subfield) = GetMarcFromKohaField("items.itemlost",'');
678 my $newitemlost = $itemtosave->subfield( $lost_tag, $lost_subfield );
679 if (($olditemlost eq '0' or $olditemlost eq '' ) and $newitemlost ge '1'){
680 LostItem($itemnumber,'MARK RETURNED');
683 } elsif ($op eq "delinkitem"){
684 my $analyticfield = '773';
685 if ($marcflavour eq 'MARC21' || $marcflavour eq 'NORMARC'){
686 $analyticfield = '773';
687 } elsif ($marcflavour eq 'UNIMARC') {
688 $analyticfield = '461';
690 foreach my $field ($record->field($analyticfield)){
691 if ($field->subfield('9') eq $hostitemnumber){
692 $record->delete_field($field);
696 my $modbibresult = ModBiblio($record, $biblionumber,'');
700 #-------------------------------------------------------------------------------
701 # build screen with existing items. and "new" one
702 #-------------------------------------------------------------------------------
704 # now, build existiing item list
705 my $temp = GetMarcBiblio( $biblionumber );
706 #my @fields = $record->fields();
709 my %witness; #---- stores the list of subfields used at least once, with the "meaning" of the code
711 #---- finds where items.itemnumber is stored
712 my ( $itemtagfield, $itemtagsubfield) = &GetMarcFromKohaField("items.itemnumber", $frameworkcode);
713 my ($branchtagfield, $branchtagsubfield) = &GetMarcFromKohaField("items.homebranch", $frameworkcode);
714 C4::Biblio::EmbedItemsInMarcBiblio($temp, $biblionumber);
715 my @fields = $temp->fields();
719 if ( C4::Context->preference('EasyAnalyticalRecords') ) {
720 my $analyticfield = '773';
721 if ($marcflavour eq 'MARC21' || $marcflavour eq 'NORMARC') {
722 $analyticfield = '773';
723 } elsif ($marcflavour eq 'UNIMARC') {
724 $analyticfield = '461';
726 foreach my $hostfield ($temp->field($analyticfield)){
727 my $hostbiblionumber = $hostfield->subfield('0');
728 if ($hostbiblionumber){
729 my $hostrecord = GetMarcBiblio($hostbiblionumber, 1);
731 my ($itemfield, undef) = GetMarcFromKohaField( 'items.itemnumber', GetFrameworkCode($hostbiblionumber) );
732 foreach my $hostitem ($hostrecord->field($itemfield)){
733 if ($hostitem->subfield('9') eq $hostfield->subfield('9')){
734 push (@fields, $hostitem);
735 push (@hostitemnumbers, $hostfield->subfield('9'));
744 foreach my $field (@fields) {
745 next if ( $field->tag() < 10 );
747 my @subf = $field->subfields or (); # don't use ||, as that forces $field->subfelds to be interpreted in scalar context
749 # loop through each subfield
751 foreach my $subfield (@subf){
752 my $subfieldcode = $subfield->[0];
753 my $subfieldvalue= $subfield->[1];
755 next if ($tagslib->{$field->tag()}->{$subfieldcode}->{tab} ne 10
756 && ($field->tag() ne $itemtagfield
757 && $subfieldcode ne $itemtagsubfield));
758 $witness{$subfieldcode} = $tagslib->{$field->tag()}->{$subfieldcode}->{lib} if ($tagslib->{$field->tag()}->{$subfieldcode}->{tab} eq 10);
759 if ($tagslib->{$field->tag()}->{$subfieldcode}->{tab} eq 10) {
760 $this_row{$subfieldcode} .= " | " if($this_row{$subfieldcode});
761 $this_row{$subfieldcode} .= GetAuthorisedValueDesc( $field->tag(),
762 $subfieldcode, $subfieldvalue, '', $tagslib)
766 if (($field->tag eq $branchtagfield) && ($subfieldcode eq $branchtagsubfield) && C4::Context->preference("IndependentBranches")) {
768 my $userenv = C4::Context->userenv();
769 unless (C4::Context->IsSuperLibrarian() or (($userenv->{'branch'} eq $subfieldvalue))){
770 $this_row{'nomod'} = 1;
773 $this_row{itemnumber} = $subfieldvalue if ($field->tag() eq $itemtagfield && $subfieldcode eq $itemtagsubfield);
775 if ( C4::Context->preference('EasyAnalyticalRecords') ) {
776 foreach my $hostitemnumber (@hostitemnumbers){
777 if ($this_row{itemnumber} eq $hostitemnumber){
778 $this_row{hostitemflag} = 1;
779 $this_row{hostbiblionumber}= GetBiblionumberFromItemnumber($hostitemnumber);
784 # my $countanalytics=GetAnalyticsCount($this_row{itemnumber});
785 # if ($countanalytics > 0){
786 # $this_row{countanalytics} = $countanalytics;
792 push(@big_array, \%this_row);
796 my ($holdingbrtagf,$holdingbrtagsubf) = &GetMarcFromKohaField("items.holdingbranch",$frameworkcode);
797 @big_array = sort {$a->{$holdingbrtagsubf} cmp $b->{$holdingbrtagsubf}} @big_array;
799 # now, construct template !
800 # First, the existing items for display
802 my @header_value_loop;
803 for my $row ( @big_array ) {
805 my @item_fields = map +{ field => $_ || '' }, @$row{ sort keys(%witness) };
806 $row_data{item_value} = [ @item_fields ];
807 $row_data{itemnumber} = $row->{itemnumber};
808 #reporting this_row values
809 $row_data{'nomod'} = $row->{'nomod'};
810 $row_data{'hostitemflag'} = $row->{'hostitemflag'};
811 $row_data{'hostbiblionumber'} = $row->{'hostbiblionumber'};
812 # $row_data{'countanalytics'} = $row->{'countanalytics'};
813 push(@item_value_loop,\%row_data);
815 foreach my $subfield_code (sort keys(%witness)) {
817 $header_value{header_value} = $witness{$subfield_code};
818 push(@header_value_loop, \%header_value);
821 # now, build the item form for entering a new item
825 my $pref_itemcallnumber = C4::Context->preference('itemcallnumber');
828 C4::Context->preference('IndependentBranches')
829 && C4::Context->userenv
830 && !C4::Context->IsSuperLibrarian()
831 && C4::Context->userenv->{branch};
832 my $branch = $input->param('branch') || C4::Context->userenv->{branch};
833 my $branches = GetBranchesLoop($branch,$onlymine); # build once ahead of time, instead of multiple times later.
835 # We generate form, from actuel record
838 foreach my $field ($itemrecord->fields()){
839 my $tag = $field->{_tag};
840 foreach my $subfield ( $field->subfields() ){
842 my $subfieldtag = $subfield->[0];
843 my $value = $subfield->[1];
844 my $subfieldlib = $tagslib->{$tag}->{$subfieldtag};
846 next if subfield_is_koha_internal_p($subfieldtag);
847 next if ($tagslib->{$tag}->{$subfieldtag}->{'tab'} ne "10");
849 my $subfield_data = generate_subfield_form($tag, $subfieldtag, $value, $tagslib, $subfieldlib, $branches, $today_iso, $biblionumber, $temp, \@loop_data, $i, $restrictededition);
850 push @fields, "$tag$subfieldtag";
851 push (@loop_data, $subfield_data);
857 # and now we add fields that are empty
859 # Using last created item if it exists
861 $itemrecord = $cookieitemrecord if ($prefillitem and not $justaddeditem and $op ne "edititem");
863 # We generate form, and fill with values if defined
864 foreach my $tag ( keys %{$tagslib}){
865 foreach my $subtag (keys %{$tagslib->{$tag}}){
866 next if subfield_is_koha_internal_p($subtag);
867 next if ($tagslib->{$tag}->{$subtag}->{'tab'} ne "10");
868 next if any { /^$tag$subtag$/ } @fields;
870 my @values = (undef);
871 @values = $itemrecord->field($tag)->subfield($subtag) if ($itemrecord && defined($itemrecord->field($tag)) && defined($itemrecord->field($tag)->subfield($subtag)));
872 for my $value (@values){
873 my $subfield_data = generate_subfield_form($tag, $subtag, $value, $tagslib, $tagslib->{$tag}->{$subtag}, $branches, $today_iso, $biblionumber, $temp, \@loop_data, $i, $restrictededition);
874 push (@loop_data, $subfield_data);
879 @loop_data = sort {$a->{subfield} cmp $b->{subfield} } @loop_data;
881 # what's the next op ? it's what we are not in : an add if we're editing, otherwise, and edit.
883 biblionumber => $biblionumber,
884 title => $oldrecord->{title},
885 author => $oldrecord->{author},
886 item_loop => \@item_value_loop,
887 item_header_loop => \@header_value_loop,
889 itemnumber => $itemnumber,
890 barcode => GetBarcodeFromItemnumber($itemnumber),
891 itemtagfield => $itemtagfield,
892 itemtagsubfield => $itemtagsubfield,
894 opisadd => ($nextop eq "saveitem") ? 0 : 1,
895 popup => $input->param('popup') ? 1: 0,
896 C4::Search::enabled_staff_search_views,
898 $template->{'VARS'}->{'searchid'} = $searchid;
900 if ($frameworkcode eq 'FA'){
901 # fast cataloguing datas
903 'circborrowernumber' => $fa_circborrowernumber,
904 'barcode' => $fa_barcode,
905 'branch' => $fa_branch,
906 'stickyduedate' => $fa_stickyduedate,
907 'duedatespec' => $fa_duedatespec,
911 foreach my $error (@errors) {
912 $template->param($error => 1);
914 output_html_with_http_headers $input, $cookie, $template->output;