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>.
38 use List
::MoreUtils qw
/any/;
40 use Storable
qw(thaw freeze);
47 our $dbh = C4
::Context
->dbh;
50 my ($tagfield,$insubfield,$record) = @_;
53 foreach my $field ($record->field($tagfield)) {
54 my @subfields = $field->subfields();
55 foreach my $subfield (@subfields) {
56 if (@
$subfield[0] eq $insubfield) {
57 $result .= @
$subfield[1];
58 $indicator = $field->indicator(1).$field->indicator(2);
62 return($indicator,$result);
65 sub get_item_from_barcode
{
67 my $dbh=C4
::Context
->dbh;
69 my $rq=$dbh->prepare("SELECT itemnumber from items where items.barcode=?");
70 $rq->execute($barcode);
71 ($result)=$rq->fetchrow;
75 sub set_item_default_location
{
76 my $itemnumber = shift;
77 my $item = GetItem
( $itemnumber );
78 if ( C4
::Context
->preference('NewItemsDefaultLocation') ) {
79 $item->{'permanent_location'} = $item->{'location'};
80 $item->{'location'} = C4
::Context
->preference('NewItemsDefaultLocation');
81 ModItem
( $item, undef, $itemnumber);
84 $item->{'permanent_location'} = $item->{'location'} if !defined($item->{'permanent_location'});
85 ModItem
( $item, undef, $itemnumber);
89 # NOTE: This code is subject to change in the future with the implemenation of ajax based autobarcode code
90 # NOTE: 'incremental' is the ONLY autoBarcode option available to those not using javascript
91 sub _increment_barcode
{
92 my ($record, $frameworkcode) = @_;
93 my ($tagfield,$tagsubfield) = &GetMarcFromKohaField
("items.barcode",$frameworkcode);
94 unless ($record->field($tagfield)->subfield($tagsubfield)) {
95 my $sth_barcode = $dbh->prepare("select max(abs(barcode)) from items");
96 $sth_barcode->execute;
97 my ($newbarcode) = $sth_barcode->fetchrow;
99 # OK, we have the new barcode, now create the entry in MARC record
100 my $fieldItem = $record->field($tagfield);
101 $record->delete_field($fieldItem);
102 $fieldItem->add_subfields($tagsubfield => $newbarcode);
103 $record->insert_fields_ordered($fieldItem);
109 sub generate_subfield_form
{
110 my ($tag, $subfieldtag, $value, $tagslib,$subfieldlib, $branches, $biblionumber, $temp, $loop_data, $i, $restrictededition) = @_;
112 my $frameworkcode = &GetFrameworkCode
($biblionumber);
115 my $dbh = C4
::Context
->dbh;
117 my $index_subfield = int(rand(1000000));
118 if ($subfieldtag eq '@'){
119 $subfield_data{id
} = "tag_".$tag."_subfield_00_".$index_subfield;
121 $subfield_data{id
} = "tag_".$tag."_subfield_".$subfieldtag."_".$index_subfield;
124 $subfield_data{tag
} = $tag;
125 $subfield_data{subfield
} = $subfieldtag;
126 $subfield_data{marc_lib
} ="<span id=\"error$i\" title=\"".$subfieldlib->{lib
}."\">".$subfieldlib->{lib
}."</span>";
127 $subfield_data{mandatory
} = $subfieldlib->{mandatory
};
128 $subfield_data{repeatable
} = $subfieldlib->{repeatable
};
129 $subfield_data{maxlength
} = $subfieldlib->{maxlength
};
131 if ( ! defined( $value ) || $value eq '') {
132 $value = $subfieldlib->{defaultvalue
};
134 # get today date & replace <<YYYY>>, <<MM>>, <<DD>> if provided in the default value
135 my $today_dt = dt_from_string
;
136 my $year = $today_dt->strftime('%Y');
137 my $month = $today_dt->strftime('%m');
138 my $day = $today_dt->strftime('%d');
139 $value =~ s/<<YYYY>>/$year/g;
140 $value =~ s/<<MM>>/$month/g;
141 $value =~ s/<<DD>>/$day/g;
142 # And <<USER>> with surname (?)
143 my $username=(C4
::Context
->userenv?C4
::Context
->userenv->{'surname'}:"superlibrarian");
144 $value=~s/<<USER>>/$username/g;
147 $value =~ s/"/"/g;
150 $subfield_data{visibility
} = "display:none;" if (($subfieldlib->{hidden
} > 4) || ($subfieldlib->{hidden
} <= -4));
152 my $pref_itemcallnumber = C4
::Context
->preference('itemcallnumber');
153 if (!$value && $subfieldlib->{kohafield
} eq 'items.itemcallnumber' && $pref_itemcallnumber) {
154 my $CNtag = substr($pref_itemcallnumber, 0, 3);
155 my $CNsubfield = substr($pref_itemcallnumber, 3, 1);
156 my $CNsubfield2 = substr($pref_itemcallnumber, 4, 1);
157 my $temp2 = $temp->field($CNtag);
159 $value = join ' ', $temp2->subfield($CNsubfield) || q{}, $temp2->subfield($CNsubfield2) || q{};
160 #remove any trailing space incase one subfield is used
161 $value =~ s/^\s+|\s+$//g;
165 if ($frameworkcode eq 'FA' && $subfieldlib->{kohafield
} eq 'items.barcode' && !$value){
167 $value = $input->param('barcode');
170 if ( $subfieldlib->{authorised_value
} ) {
171 my @authorised_values;
173 # builds list, depending on authorised value...
174 if ( $subfieldlib->{authorised_value
} eq "branches" ) {
175 foreach my $thisbranch (@
$branches) {
176 push @authorised_values, $thisbranch->{branchcode
};
177 $authorised_lib{$thisbranch->{branchcode
}} = $thisbranch->{branchname
};
178 $value = $thisbranch->{branchcode
} if $thisbranch->{selected
} && !$value;
181 elsif ( $subfieldlib->{authorised_value
} eq "itemtypes" ) {
182 push @authorised_values, "";
183 my $itemtypes = Koha
::ItemTypes
->search_with_localization;
184 while ( my $itemtype = $itemtypes->next ) {
185 push @authorised_values, $itemtype->itemtype;
186 $authorised_lib{$itemtype->itemtype} = $itemtype->translated_description;
190 my $itype_sth = $dbh->prepare("SELECT itemtype FROM biblioitems WHERE biblionumber = ?");
191 $itype_sth->execute( $biblionumber );
192 ( $value ) = $itype_sth->fetchrow_array;
197 elsif ( $subfieldlib->{authorised_value
} eq "cn_source" ) {
198 push @authorised_values, "";
200 my $class_sources = GetClassSources
();
201 my $default_source = C4
::Context
->preference("DefaultClassificationSource");
203 foreach my $class_source (sort keys %$class_sources) {
204 next unless $class_sources->{$class_source}->{'used'} or
205 ($value and $class_source eq $value) or
206 ($class_source eq $default_source);
207 push @authorised_values, $class_source;
208 $authorised_lib{$class_source} = $class_sources->{$class_source}->{'description'};
210 $value = $default_source unless ($value);
212 #---- "true" authorised value
215 push @authorised_values, qq{};
216 my $av = GetAuthorisedValues
( $subfieldlib->{authorised_value
} );
218 push @authorised_values, $r->{authorised_value
};
219 $authorised_lib{$r->{authorised_value
}} = $r->{lib
};
223 if ( $subfieldlib->{hidden
} > 4 or $subfieldlib->{hidden
} <= -4 ) {
224 $subfield_data{marc_value
} = {
226 id
=> $subfield_data{id
},
227 maxlength
=> $subfield_data{maxlength
},
232 $subfield_data{marc_value
} = {
234 id
=> "tag_".$tag."_subfield_".$subfieldtag."_".$index_subfield,
235 values => \
@authorised_values,
236 labels
=> \
%authorised_lib,
241 # it's a thesaurus / authority field
242 elsif ( $subfieldlib->{authtypecode
} ) {
243 $subfield_data{marc_value
} = {
245 id
=> $subfield_data{id
},
246 maxlength
=> $subfield_data{maxlength
},
248 authtypecode
=> $subfieldlib->{authtypecode
},
251 # it's a plugin field
252 elsif ( $subfieldlib->{value_builder
} ) { # plugin
253 require Koha
::FrameworkPlugin
;
254 my $plugin = Koha
::FrameworkPlugin
->new({
255 name
=> $subfieldlib->{'value_builder'},
258 my $pars= { dbh
=> $dbh, record
=> $temp, tagslib
=>$tagslib,
259 id
=> $subfield_data{id
}, tabloop
=> $loop_data };
260 $plugin->build( $pars );
261 if( !$plugin->errstr ) {
262 my $class= 'buttonDot'. ( $plugin->noclick?
' disabled': '' );
263 $subfield_data{marc_value
} = {
264 type
=> 'text_plugin',
265 id
=> $subfield_data{id
},
266 maxlength
=> $subfield_data{maxlength
},
269 nopopup
=> $plugin->noclick,
270 javascript
=> $plugin->javascript,
273 warn $plugin->errstr;
274 $subfield_data{marc_value
} = {
276 id
=> $subfield_data{id
},
277 maxlength
=> $subfield_data{maxlength
},
279 }; # supply default input form
282 elsif ( $tag eq '' ) { # it's an hidden field
283 $subfield_data{marc_value
} = {
285 id
=> $subfield_data{id
},
286 maxlength
=> $subfield_data{maxlength
},
290 elsif ( $subfieldlib->{'hidden'} ) { # FIXME: shouldn't input type be "hidden" ?
291 $subfield_data{marc_value
} = {
293 id
=> $subfield_data{id
},
294 maxlength
=> $subfield_data{maxlength
},
300 $value and length($value) > 100
303 C4
::Context
->preference("marcflavour") eq "UNIMARC"
304 and 300 <= $tag && $tag < 400 && $subfieldtag eq 'a'
307 C4
::Context
->preference("marcflavour") eq "MARC21"
308 and 500 <= $tag && $tag < 600
311 # oversize field (textarea)
312 $subfield_data{marc_value
} = {
314 id
=> $subfield_data{id
},
318 # it's a standard field
319 $subfield_data{marc_value
} = {
321 id
=> $subfield_data{id
},
322 maxlength
=> $subfield_data{maxlength
},
327 # Getting list of subfields to keep when restricted editing is enabled
328 my $subfieldsToAllowForRestrictedEditing = C4
::Context
->preference('SubfieldsToAllowForRestrictedEditing');
329 my $allowAllSubfields = (
330 not defined $subfieldsToAllowForRestrictedEditing
331 or $subfieldsToAllowForRestrictedEditing eq q
||
333 my @subfieldsToAllow = split(/ /, $subfieldsToAllowForRestrictedEditing);
335 # If we're on restricted editing, and our field is not in the list of subfields to allow,
336 # then it is read-only
337 $subfield_data{marc_value
}->{readonly
} = (
338 not $allowAllSubfields
339 and $restrictededition
340 and !grep { $tag . '$' . $subfieldtag eq $_ } @subfieldsToAllow
343 return \
%subfield_data;
346 # Removes some subfields when prefilling items
347 # This function will remove any subfield that is not in the SubfieldsToUseWhenPrefill syspref
348 sub removeFieldsForPrefill
{
353 my ($tag, $subtag) = GetMarcFromKohaField
("items.barcode", '');
355 # Getting list of subfields to keep
356 my $subfieldsToUseWhenPrefill = C4
::Context
->preference('SubfieldsToUseWhenPrefill');
358 # Removing subfields that are not in the syspref
359 if ($tag && $subfieldsToUseWhenPrefill) {
360 my $field = $item->field($tag);
361 my @subfieldsToUse= split(/ /,$subfieldsToUseWhenPrefill);
362 foreach my $subfield ($field->subfields()) {
363 if (!grep { $subfield->[0] eq $_ } @subfieldsToUse) {
364 $field->delete_subfield(code
=> $subfield->[0]);
375 my $error = $input->param('error');
376 my $biblionumber = $input->param('biblionumber');
377 my $itemnumber = $input->param('itemnumber');
378 my $op = $input->param('op') || q{};
379 my $hostitemnumber = $input->param('hostitemnumber');
380 my $marcflavour = C4
::Context
->preference("marcflavour");
381 my $searchid = $input->param('searchid');
382 # fast cataloguing datas
383 my $fa_circborrowernumber = $input->param('circborrowernumber');
384 my $fa_barcode = $input->param('barcode');
385 my $fa_branch = $input->param('branch');
386 my $fa_stickyduedate = $input->param('stickyduedate');
387 my $fa_duedatespec = $input->param('duedatespec');
389 my $frameworkcode = &GetFrameworkCode
($biblionumber);
391 # Defining which userflag is needing according to the framework currently used
393 if (defined $input->param('frameworkcode')) {
394 $userflags = ($input->param('frameworkcode') eq 'FA') ?
"fast_cataloging" : "edit_items";
397 if (not defined $userflags) {
398 $userflags = ($frameworkcode eq 'FA') ?
"fast_cataloging" : "edit_items";
401 my ($template, $loggedinuser, $cookie)
402 = get_template_and_user
({template_name
=> "cataloguing/additem.tt",
405 authnotrequired
=> 0,
406 flagsrequired
=> {editcatalogue
=> $userflags},
411 # Does the user have a restricted item editing permission?
412 my $uid = Koha
::Patrons
->find( $loggedinuser )->userid;
413 my $restrictededition = $uid ? haspermission
($uid, {'editcatalogue' => 'edit_items_restricted'}) : undef;
414 # In case user is a superlibrarian, editing is not restricted
415 $restrictededition = 0 if ($restrictededition != 0 && C4
::Context
->IsSuperLibrarian());
416 # In case user has fast cataloging permission (and we're in fast cataloging), editing is not restricted
417 $restrictededition = 0 if ($restrictededition != 0 && $frameworkcode eq 'FA' && haspermission
($uid, {'editcatalogue' => 'fast_cataloging'}));
419 my $tagslib = &GetMarcStructure
(1,$frameworkcode);
420 my $record = GetMarcBiblio
({ biblionumber
=> $biblionumber });
421 my $oldrecord = TransformMarcToKoha
($record);
423 my $nextop="additem";
424 my @errors; # store errors found while checking data BEFORE saving item.
426 # Getting last created item cookie
427 my $prefillitem = C4
::Context
->preference('PrefillItem');
429 my $cookieitemrecord;
431 my $lastitemcookie = $input->cookie('LastCreatedItem');
432 if ($lastitemcookie) {
433 $lastitemcookie = uri_unescape
($lastitemcookie);
435 if ( thaw
($lastitemcookie) ) {
436 $cookieitemrecord = thaw
($lastitemcookie);
437 $cookieitemrecord = removeFieldsForPrefill
($cookieitemrecord);
441 $lastitemcookie = 'undef' unless $lastitemcookie;
442 warn "Storable::thaw failed to thaw LastCreatedItem-cookie. Cookie value '$lastitemcookie'. Caught error follows: '$@'";
447 #-------------------------------------------------------------------------------
448 if ($op eq "additem") {
450 #-------------------------------------------------------------------------------
452 my @tags = $input->multi_param('tag');
453 my @subfields = $input->multi_param('subfield');
454 my @values = $input->multi_param('field_value');
455 # build indicator hash.
456 my @ind_tag = $input->multi_param('ind_tag');
457 my @indicator = $input->multi_param('indicator');
458 my $xml = TransformHtmlToXml
(\
@tags,\
@subfields,\
@values,\
@indicator,\
@ind_tag, 'ITEM');
459 my $record = MARC
::Record
::new_from_xml
($xml, 'UTF-8');
462 my $add_submit = $input->param('add_submit');
463 my $add_duplicate_submit = $input->param('add_duplicate_submit');
464 my $add_multiple_copies_submit = $input->param('add_multiple_copies_submit');
465 my $number_of_copies = $input->param('number_of_copies');
467 # This is a bit tricky : if there is a cookie for the last created item and
468 # we just added an item, the cookie value is not correct yet (it will be updated
469 # next page). To prevent the form from being filled with outdated values, we
470 # force the use of "add and duplicate" feature, so the form will be filled with
472 $add_duplicate_submit = 1 if ($prefillitem);
475 # if autoBarcode is set to 'incremental', calculate barcode...
476 if ( C4
::Context
->preference('autoBarcode') eq 'incremental' ) {
477 $record = _increment_barcode
($record, $frameworkcode);
480 my $addedolditem = TransformMarcToKoha
( $record );
482 # If we have to add or add & duplicate, we add the item
483 if ( $add_submit || $add_duplicate_submit ) {
485 # check for item barcode # being unique
486 my $exist_itemnumber = get_item_from_barcode
( $addedolditem->{'barcode'} );
487 push @errors, "barcode_not_unique" if ($exist_itemnumber);
489 # if barcode exists, don't create, but report The problem.
490 unless ($exist_itemnumber) {
491 my ( $oldbiblionumber, $oldbibnum, $oldbibitemnum ) = AddItemFromMarc
( $record, $biblionumber );
492 set_item_default_location
($oldbibitemnum);
494 # Pushing the last created item cookie back
495 if ($prefillitem && defined $record) {
496 my $itemcookie = $input->cookie(
497 -name
=> 'LastCreatedItem',
498 # We uri_escape the whole freezed structure so we're sure we won't have any encoding problems
499 -value
=> uri_escape_utf8
( freeze
( $record ) ),
504 $cookie = [ $cookie, $itemcookie ];
509 if ($exist_itemnumber) {
510 $itemrecord = $record;
514 # If we have to add & duplicate
515 if ($add_duplicate_submit) {
516 $itemrecord = $record;
517 if (C4
::Context
->preference('autoBarcode') eq 'incremental') {
518 $itemrecord = _increment_barcode
($itemrecord, $frameworkcode);
521 # we have to clear the barcode field in the duplicate item record to make way for the new one generated by the javascript plugin
522 my ($tagfield,$tagsubfield) = &GetMarcFromKohaField
("items.barcode",$frameworkcode);
523 my $fieldItem = $itemrecord->field($tagfield);
524 $itemrecord->delete_field($fieldItem);
525 $fieldItem->delete_subfields($tagsubfield);
526 $itemrecord->insert_fields_ordered($fieldItem);
528 $itemrecord = removeFieldsForPrefill
($itemrecord) if ($prefillitem);
531 # If we have to add multiple copies
532 if ($add_multiple_copies_submit) {
535 my $barcodeobj = C4
::Barcodes
->new;
536 my $oldbarcode = $addedolditem->{'barcode'};
537 my ($tagfield,$tagsubfield) = &GetMarcFromKohaField
("items.barcode",$frameworkcode);
539 # If there is a barcode and we can't find their new values, we can't add multiple copies
541 $testbarcode = $barcodeobj->next_value($oldbarcode) if $barcodeobj;
542 if ($oldbarcode && !$testbarcode) {
544 push @errors, "no_next_barcode";
545 $itemrecord = $record;
550 # For the first iteration
551 my $barcodevalue = $oldbarcode;
552 my $exist_itemnumber;
555 for (my $i = 0; $i < $number_of_copies;) {
557 # If there is a barcode
560 # Getting a new barcode (if it is not the first iteration or the barcode we tried already exists)
561 $barcodevalue = $barcodeobj->next_value($oldbarcode) if ($i > 0 || $exist_itemnumber);
563 # Putting it into the record
565 $record->field($tagfield)->update($tagsubfield => $barcodevalue);
568 # Checking if the barcode already exists
569 $exist_itemnumber = get_item_from_barcode
($barcodevalue);
573 if (!$exist_itemnumber) {
574 my ($oldbiblionumber,$oldbibnum,$oldbibitemnum) = AddItemFromMarc
($record,$biblionumber);
575 set_item_default_location
($oldbibitemnum);
577 # We count the item only if it was really added
578 # That way, all items are added, even if there was some already existing barcodes
579 # FIXME : Please note that there is a risk of infinite loop here if we never find a suitable barcode
583 # Preparing the next iteration
584 $oldbarcode = $barcodevalue;
589 if ($frameworkcode eq 'FA' && $fa_circborrowernumber){
590 print $input->redirect(
591 '/cgi-bin/koha/circ/circulation.pl?'
592 .'borrowernumber='.$fa_circborrowernumber
593 .'&barcode='.uri_escape_utf8
($fa_barcode)
594 .'&duedatespec='.$fa_duedatespec
601 #-------------------------------------------------------------------------------
602 } elsif ($op eq "edititem") {
603 #-------------------------------------------------------------------------------
604 # retrieve item if exist => then, it's a modif
605 $itemrecord = C4
::Items
::GetMarcItem
($biblionumber,$itemnumber);
606 $nextop = "saveitem";
607 #-------------------------------------------------------------------------------
608 } elsif ($op eq "dupeitem") {
609 #-------------------------------------------------------------------------------
610 # retrieve item if exist => then, it's a modif
611 $itemrecord = C4
::Items
::GetMarcItem
($biblionumber,$itemnumber);
612 if (C4
::Context
->preference('autoBarcode') eq 'incremental') {
613 $itemrecord = _increment_barcode
($itemrecord, $frameworkcode);
616 # we have to clear the barcode field in the duplicate item record to make way for the new one generated by the javascript plugin
617 my ($tagfield,$tagsubfield) = &GetMarcFromKohaField
("items.barcode",$frameworkcode);
618 my $fieldItem = $itemrecord->field($tagfield);
619 $itemrecord->delete_field($fieldItem);
620 $fieldItem->delete_subfields($tagsubfield);
621 $itemrecord->insert_fields_ordered($fieldItem);
624 #check for hidden subfield and remove them for the duplicated item
625 foreach my $field ($itemrecord->fields()){
626 my $tag = $field->{_tag
};
627 foreach my $subfield ($field->subfields()){
628 my $subfieldtag = $subfield->[0];
629 if ($tagslib->{$tag}->{$subfieldtag}->{'tab'} ne "10"
630 || abs($tagslib->{$tag}->{$subfieldtag}->{hidden
})>4 ){
631 my $fieldItem = $itemrecord->field($tag);
632 $itemrecord->delete_field($fieldItem);
633 $fieldItem->delete_subfields($subfieldtag);
634 $itemrecord->insert_fields_ordered($fieldItem);
639 $itemrecord = removeFieldsForPrefill
($itemrecord) if ($prefillitem);
641 #-------------------------------------------------------------------------------
642 } elsif ($op eq "delitem") {
643 #-------------------------------------------------------------------------------
644 # check that there is no issue on this item before deletion.
645 $error = &DelItemCheck
( $biblionumber,$itemnumber);
647 print $input->redirect("additem.pl?biblionumber=$biblionumber&frameworkcode=$frameworkcode&searchid=$searchid");
652 #-------------------------------------------------------------------------------
653 } elsif ($op eq "delallitems") {
654 #-------------------------------------------------------------------------------
655 my @itemnumbers = Koha
::Items
->search({ biblionumber
=> $biblionumber })->get_column('itemnumber');
656 foreach my $itemnumber ( @itemnumbers ) {
657 $error = C4
::Items
::DelItemCheck
( $biblionumber, $itemnumber );
658 next if $error == 1; # Means ok
664 my $defaultview = C4
::Context
->preference('IntranetBiblioDefaultView');
665 my $views = { C4
::Search
::enabled_staff_search_views
};
666 if ($defaultview eq 'isbd' && $views->{can_view_ISBD
}) {
667 print $input->redirect("/cgi-bin/koha/catalogue/ISBDdetail.pl?biblionumber=$biblionumber&searchid=$searchid");
668 } elsif ($defaultview eq 'marc' && $views->{can_view_MARC
}) {
669 print $input->redirect("/cgi-bin/koha/catalogue/MARCdetail.pl?biblionumber=$biblionumber&searchid=$searchid");
670 } elsif ($defaultview eq 'labeled_marc' && $views->{can_view_labeledMARC
}) {
671 print $input->redirect("/cgi-bin/koha/catalogue/labeledMARCdetail.pl?biblionumber=$biblionumber&searchid=$searchid");
673 print $input->redirect("/cgi-bin/koha/catalogue/detail.pl?biblionumber=$biblionumber&searchid=$searchid");
677 #-------------------------------------------------------------------------------
678 } elsif ($op eq "saveitem") {
679 #-------------------------------------------------------------------------------
681 my @tags = $input->multi_param('tag');
682 my @subfields = $input->multi_param('subfield');
683 my @values = $input->multi_param('field_value');
684 # build indicator hash.
685 my @ind_tag = $input->multi_param('ind_tag');
686 my @indicator = $input->multi_param('indicator');
687 # my $itemnumber = $input->param('itemnumber');
688 my $xml = TransformHtmlToXml
(\
@tags,\
@subfields,\
@values,\
@indicator,\
@ind_tag,'ITEM');
689 my $itemtosave=MARC
::Record
::new_from_xml
($xml, 'UTF-8');
690 # MARC::Record builded => now, record in DB
691 # warn "R: ".$record->as_formatted;
692 # check that the barcode don't exist already
693 my $addedolditem = TransformMarcToKoha
($itemtosave);
694 my $exist_itemnumber = get_item_from_barcode
($addedolditem->{'barcode'});
695 if ($exist_itemnumber && $exist_itemnumber != $itemnumber) {
696 push @errors,"barcode_not_unique";
698 my $item = GetItem
( $itemnumber );
699 my $newitem = ModItemFromMarc
($itemtosave, $biblionumber, $itemnumber);
701 my $olditemlost = $item->{itemlost
};
702 my $newitemlost = $newitem->{itemlost
};
703 LostItem
( $item->{itemnumber
}, 'additem' )
704 if $newitemlost && $newitemlost ge '1' && !$olditemlost;
707 } elsif ($op eq "delinkitem"){
708 my $analyticfield = '773';
709 if ($marcflavour eq 'MARC21' || $marcflavour eq 'NORMARC'){
710 $analyticfield = '773';
711 } elsif ($marcflavour eq 'UNIMARC') {
712 $analyticfield = '461';
714 foreach my $field ($record->field($analyticfield)){
715 if ($field->subfield('9') eq $hostitemnumber){
716 $record->delete_field($field);
720 my $modbibresult = ModBiblio
($record, $biblionumber,'');
724 #-------------------------------------------------------------------------------
725 # build screen with existing items. and "new" one
726 #-------------------------------------------------------------------------------
728 # now, build existiing item list
729 my $temp = GetMarcBiblio
({ biblionumber
=> $biblionumber });
730 #my @fields = $record->fields();
733 my %witness; #---- stores the list of subfields used at least once, with the "meaning" of the code
735 #---- finds where items.itemnumber is stored
736 my ( $itemtagfield, $itemtagsubfield) = &GetMarcFromKohaField
("items.itemnumber", $frameworkcode);
737 my ($branchtagfield, $branchtagsubfield) = &GetMarcFromKohaField
("items.homebranch", $frameworkcode);
738 C4
::Biblio
::EmbedItemsInMarcBiblio
($temp, $biblionumber);
739 my @fields = $temp->fields();
743 if ( C4
::Context
->preference('EasyAnalyticalRecords') ) {
744 my $analyticfield = '773';
745 if ($marcflavour eq 'MARC21' || $marcflavour eq 'NORMARC') {
746 $analyticfield = '773';
747 } elsif ($marcflavour eq 'UNIMARC') {
748 $analyticfield = '461';
750 foreach my $hostfield ($temp->field($analyticfield)){
751 my $hostbiblionumber = $hostfield->subfield('0');
752 if ($hostbiblionumber){
753 my $hostrecord = GetMarcBiblio
({
754 biblionumber
=> $hostbiblionumber,
757 my ($itemfield, undef) = GetMarcFromKohaField
( 'items.itemnumber', GetFrameworkCode
($hostbiblionumber) );
758 foreach my $hostitem ($hostrecord->field($itemfield)){
759 if ($hostitem->subfield('9') eq $hostfield->subfield('9')){
760 push (@fields, $hostitem);
761 push (@hostitemnumbers, $hostfield->subfield('9'));
770 foreach my $field (@fields) {
771 next if ( $field->tag() < 10 );
773 my @subf = $field->subfields or (); # don't use ||, as that forces $field->subfelds to be interpreted in scalar context
775 # loop through each subfield
777 foreach my $subfield (@subf){
778 my $subfieldcode = $subfield->[0];
779 my $subfieldvalue= $subfield->[1];
781 next if ($tagslib->{$field->tag()}->{$subfieldcode}->{tab
} ne 10
782 && ($field->tag() ne $itemtagfield
783 && $subfieldcode ne $itemtagsubfield));
784 $witness{$subfieldcode} = $tagslib->{$field->tag()}->{$subfieldcode}->{lib
} if ($tagslib->{$field->tag()}->{$subfieldcode}->{tab
} eq 10);
785 if ($tagslib->{$field->tag()}->{$subfieldcode}->{tab
} eq 10) {
786 $this_row{$subfieldcode} .= " | " if($this_row{$subfieldcode});
787 $this_row{$subfieldcode} .= GetAuthorisedValueDesc
( $field->tag(),
788 $subfieldcode, $subfieldvalue, '', $tagslib)
792 if (($field->tag eq $branchtagfield) && ($subfieldcode eq $branchtagsubfield) && C4
::Context
->preference("IndependentBranches")) {
794 my $userenv = C4
::Context
->userenv();
795 unless (C4
::Context
->IsSuperLibrarian() or (($userenv->{'branch'} eq $subfieldvalue))){
796 $this_row{'nomod'} = 1;
799 $this_row{itemnumber
} = $subfieldvalue if ($field->tag() eq $itemtagfield && $subfieldcode eq $itemtagsubfield);
801 if ( C4
::Context
->preference('EasyAnalyticalRecords') ) {
802 foreach my $hostitemnumber (@hostitemnumbers) {
803 my $item = Koha
::Items
->find( $hostitemnumber );
804 if ($this_row{itemnumber
} eq $hostitemnumber) {
805 $this_row{hostitemflag
} = 1;
806 $this_row{hostbiblionumber
}= $item->biblio->biblionumber;
813 push(@big_array, \
%this_row);
817 my ($holdingbrtagf,$holdingbrtagsubf) = &GetMarcFromKohaField
("items.holdingbranch",$frameworkcode);
818 @big_array = sort {$a->{$holdingbrtagsubf} cmp $b->{$holdingbrtagsubf}} @big_array;
820 # now, construct template !
821 # First, the existing items for display
823 my @header_value_loop;
824 for my $row ( @big_array ) {
826 my @item_fields = map +{ field
=> $_ || '' }, @
$row{ sort keys(%witness) };
827 $row_data{item_value
} = [ @item_fields ];
828 $row_data{itemnumber
} = $row->{itemnumber
};
829 #reporting this_row values
830 $row_data{'nomod'} = $row->{'nomod'};
831 $row_data{'hostitemflag'} = $row->{'hostitemflag'};
832 $row_data{'hostbiblionumber'} = $row->{'hostbiblionumber'};
833 # $row_data{'countanalytics'} = $row->{'countanalytics'};
834 push(@item_value_loop,\
%row_data);
836 foreach my $subfield_code (sort keys(%witness)) {
838 $header_value{header_value
} = $witness{$subfield_code};
840 my $subfieldlib = $tagslib->{$itemtagfield}->{$subfield_code};
841 my $kohafield = $subfieldlib->{kohafield
};
842 if ( $kohafield && $kohafield =~ /items.(.+)/ ) {
843 $header_value{column_name
} = $1;
846 push(@header_value_loop, \
%header_value);
849 # now, build the item form for entering a new item
853 my $pref_itemcallnumber = C4
::Context
->preference('itemcallnumber');
855 my $branch = $input->param('branch') || C4
::Context
->userenv->{branch
};
856 my $libraries = Koha
::Libraries
->search({}, { order_by
=> ['branchname'] })->unblessed;# build once ahead of time, instead of multiple times later.
857 for my $library ( @
$libraries ) {
858 $library->{selected
} = 1 if $library->{branchcode
} eq $branch
861 # We generate form, from actuel record
864 foreach my $field ($itemrecord->fields()){
865 my $tag = $field->{_tag
};
866 foreach my $subfield ( $field->subfields() ){
868 my $subfieldtag = $subfield->[0];
869 my $value = $subfield->[1];
870 my $subfieldlib = $tagslib->{$tag}->{$subfieldtag};
872 next if ($tagslib->{$tag}->{$subfieldtag}->{'tab'} ne "10");
874 my $subfield_data = generate_subfield_form
($tag, $subfieldtag, $value, $tagslib, $subfieldlib, $libraries, $biblionumber, $temp, \
@loop_data, $i, $restrictededition);
875 push @fields, "$tag$subfieldtag";
876 push (@loop_data, $subfield_data);
882 # and now we add fields that are empty
884 # Using last created item if it exists
886 $itemrecord = $cookieitemrecord if ($prefillitem and not $justaddeditem and $op ne "edititem");
888 # We generate form, and fill with values if defined
889 foreach my $tag ( keys %{$tagslib}){
890 foreach my $subtag (keys %{$tagslib->{$tag}}){
891 next if IsMarcStructureInternal
($tagslib->{$tag}{$subtag});
892 next if ($tagslib->{$tag}->{$subtag}->{'tab'} ne "10");
893 next if any
{ /^$tag$subtag$/ } @fields;
895 my @values = (undef);
896 @values = $itemrecord->field($tag)->subfield($subtag) if ($itemrecord && defined($itemrecord->field($tag)) && defined($itemrecord->field($tag)->subfield($subtag)));
897 for my $value (@values){
898 my $subfield_data = generate_subfield_form
($tag, $subtag, $value, $tagslib, $tagslib->{$tag}->{$subtag}, $libraries, $biblionumber, $temp, \
@loop_data, $i, $restrictededition);
899 push (@loop_data, $subfield_data);
904 @loop_data = sort {$a->{subfield
} cmp $b->{subfield
} } @loop_data;
906 my $item = Koha
::Items
->find($itemnumber); # We certainly want to fetch it earlier
908 # what's the next op ? it's what we are not in : an add if we're editing, otherwise, and edit.
910 biblionumber
=> $biblionumber,
911 title
=> $oldrecord->{title
},
912 author
=> $oldrecord->{author
},
913 item_loop
=> \
@item_value_loop,
914 item_header_loop
=> \
@header_value_loop,
916 itemnumber
=> $itemnumber,
917 barcode
=> $item ?
$item->barcode : undef,
918 itemtagfield
=> $itemtagfield,
919 itemtagsubfield
=> $itemtagsubfield,
921 opisadd
=> ($nextop eq "saveitem") ?
0 : 1,
922 popup
=> scalar $input->param('popup') ?
1: 0,
923 C4
::Search
::enabled_staff_search_views
,
925 $template->{'VARS'}->{'searchid'} = $searchid;
927 if ($frameworkcode eq 'FA'){
928 # fast cataloguing datas
930 'circborrowernumber' => $fa_circborrowernumber,
931 'barcode' => $fa_barcode,
932 'branch' => $fa_branch,
933 'stickyduedate' => $fa_stickyduedate,
934 'duedatespec' => $fa_duedatespec,
938 foreach my $error (@errors) {
939 $template->param($error => 1);
941 output_html_with_http_headers
$input, $cookie, $template->output;