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 = Koha::Items->find($itemnumber);
78 if ( C4::Context->preference('NewItemsDefaultLocation') ) {
81 permanent_location => $item->location,
82 location => C4::Context->preference('NewItemsDefaultLocation')
89 ModItem( { permanent_location => $item->location }, undef, $itemnumber )
90 unless defined $item->permanent_location;
94 # NOTE: This code is subject to change in the future with the implemenation of ajax based autobarcode code
95 # NOTE: 'incremental' is the ONLY autoBarcode option available to those not using javascript
96 sub _increment_barcode {
97 my ($record, $frameworkcode) = @_;
98 my ($tagfield,$tagsubfield) = &GetMarcFromKohaField("items.barcode",$frameworkcode);
99 unless ($record->field($tagfield)->subfield($tagsubfield)) {
100 my $sth_barcode = $dbh->prepare("select max(abs(barcode)) from items");
101 $sth_barcode->execute;
102 my ($newbarcode) = $sth_barcode->fetchrow;
104 # OK, we have the new barcode, now create the entry in MARC record
105 my $fieldItem = $record->field($tagfield);
106 $record->delete_field($fieldItem);
107 $fieldItem->add_subfields($tagsubfield => $newbarcode);
108 $record->insert_fields_ordered($fieldItem);
114 sub generate_subfield_form {
115 my ($tag, $subfieldtag, $value, $tagslib,$subfieldlib, $branches, $biblionumber, $temp, $loop_data, $i, $restrictededition) = @_;
117 my $frameworkcode = &GetFrameworkCode($biblionumber);
120 my $dbh = C4::Context->dbh;
122 my $index_subfield = int(rand(1000000));
123 if ($subfieldtag eq '@'){
124 $subfield_data{id} = "tag_".$tag."_subfield_00_".$index_subfield;
126 $subfield_data{id} = "tag_".$tag."_subfield_".$subfieldtag."_".$index_subfield;
129 $subfield_data{tag} = $tag;
130 $subfield_data{subfield} = $subfieldtag;
131 $subfield_data{marc_lib} ="<span id=\"error$i\" title=\"".$subfieldlib->{lib}."\">".$subfieldlib->{lib}."</span>";
132 $subfield_data{mandatory} = $subfieldlib->{mandatory};
133 $subfield_data{repeatable} = $subfieldlib->{repeatable};
134 $subfield_data{maxlength} = $subfieldlib->{maxlength};
136 if ( ! defined( $value ) || $value eq '') {
137 $value = $subfieldlib->{defaultvalue};
139 # get today date & replace <<YYYY>>, <<MM>>, <<DD>> if provided in the default value
140 my $today_dt = dt_from_string;
141 my $year = $today_dt->strftime('%Y');
142 my $month = $today_dt->strftime('%m');
143 my $day = $today_dt->strftime('%d');
144 $value =~ s/<<YYYY>>/$year/g;
145 $value =~ s/<<MM>>/$month/g;
146 $value =~ s/<<DD>>/$day/g;
147 # And <<USER>> with surname (?)
148 my $username=(C4::Context->userenv?C4::Context->userenv->{'surname'}:"superlibrarian");
149 $value=~s/<<USER>>/$username/g;
153 $subfield_data{visibility} = "display:none;" if (($subfieldlib->{hidden} > 4) || ($subfieldlib->{hidden} <= -4));
155 my $pref_itemcallnumber = C4::Context->preference('itemcallnumber');
156 if (!$value && $subfieldlib->{kohafield} eq 'items.itemcallnumber' && $pref_itemcallnumber) {
157 my $CNtag = substr($pref_itemcallnumber, 0, 3);
158 my $CNsubfield = substr($pref_itemcallnumber, 3, 1);
159 my $CNsubfield2 = substr($pref_itemcallnumber, 4, 1);
160 my $temp2 = $temp->field($CNtag);
162 $value = join ' ', $temp2->subfield($CNsubfield) || q{}, $temp2->subfield($CNsubfield2) || q{};
163 #remove any trailing space incase one subfield is used
164 $value =~ s/^\s+|\s+$//g;
168 if ($frameworkcode eq 'FA' && $subfieldlib->{kohafield} eq 'items.barcode' && !$value){
170 $value = $input->param('barcode');
173 if ( $subfieldlib->{authorised_value} ) {
174 my @authorised_values;
176 # builds list, depending on authorised value...
177 if ( $subfieldlib->{authorised_value} eq "branches" ) {
178 foreach my $thisbranch (@$branches) {
179 push @authorised_values, $thisbranch->{branchcode};
180 $authorised_lib{$thisbranch->{branchcode}} = $thisbranch->{branchname};
181 $value = $thisbranch->{branchcode} if $thisbranch->{selected} && !$value;
184 elsif ( $subfieldlib->{authorised_value} eq "itemtypes" ) {
185 push @authorised_values, "";
186 my $itemtypes = Koha::ItemTypes->search_with_localization;
187 while ( my $itemtype = $itemtypes->next ) {
188 push @authorised_values, $itemtype->itemtype;
189 $authorised_lib{$itemtype->itemtype} = $itemtype->translated_description;
193 my $itype_sth = $dbh->prepare("SELECT itemtype FROM biblioitems WHERE biblionumber = ?");
194 $itype_sth->execute( $biblionumber );
195 ( $value ) = $itype_sth->fetchrow_array;
200 elsif ( $subfieldlib->{authorised_value} eq "cn_source" ) {
201 push @authorised_values, "";
203 my $class_sources = GetClassSources();
204 my $default_source = C4::Context->preference("DefaultClassificationSource");
206 foreach my $class_source (sort keys %$class_sources) {
207 next unless $class_sources->{$class_source}->{'used'} or
208 ($value and $class_source eq $value) or
209 ($class_source eq $default_source);
210 push @authorised_values, $class_source;
211 $authorised_lib{$class_source} = $class_sources->{$class_source}->{'description'};
213 $value = $default_source unless ($value);
215 #---- "true" authorised value
218 push @authorised_values, qq{};
219 my $av = GetAuthorisedValues( $subfieldlib->{authorised_value} );
221 push @authorised_values, $r->{authorised_value};
222 $authorised_lib{$r->{authorised_value}} = $r->{lib};
226 if ( $subfieldlib->{hidden} > 4 or $subfieldlib->{hidden} <= -4 ) {
227 $subfield_data{marc_value} = {
229 id => $subfield_data{id},
230 maxlength => $subfield_data{maxlength},
235 $subfield_data{marc_value} = {
237 id => "tag_".$tag."_subfield_".$subfieldtag."_".$index_subfield,
238 values => \@authorised_values,
239 labels => \%authorised_lib,
244 # it's a thesaurus / authority field
245 elsif ( $subfieldlib->{authtypecode} ) {
246 $subfield_data{marc_value} = {
248 id => $subfield_data{id},
249 maxlength => $subfield_data{maxlength},
251 authtypecode => $subfieldlib->{authtypecode},
254 # it's a plugin field
255 elsif ( $subfieldlib->{value_builder} ) { # plugin
256 require Koha::FrameworkPlugin;
257 my $plugin = Koha::FrameworkPlugin->new({
258 name => $subfieldlib->{'value_builder'},
261 my $pars= { dbh => $dbh, record => $temp, tagslib =>$tagslib,
262 id => $subfield_data{id}, tabloop => $loop_data };
263 $plugin->build( $pars );
264 if( !$plugin->errstr ) {
265 my $class= 'buttonDot'. ( $plugin->noclick? ' disabled': '' );
266 $subfield_data{marc_value} = {
267 type => 'text_plugin',
268 id => $subfield_data{id},
269 maxlength => $subfield_data{maxlength},
272 nopopup => $plugin->noclick,
273 javascript => $plugin->javascript,
276 warn $plugin->errstr;
277 $subfield_data{marc_value} = {
279 id => $subfield_data{id},
280 maxlength => $subfield_data{maxlength},
282 }; # supply default input form
285 elsif ( $tag eq '' ) { # it's an hidden field
286 $subfield_data{marc_value} = {
288 id => $subfield_data{id},
289 maxlength => $subfield_data{maxlength},
293 elsif ( $subfieldlib->{'hidden'} ) { # FIXME: shouldn't input type be "hidden" ?
294 $subfield_data{marc_value} = {
296 id => $subfield_data{id},
297 maxlength => $subfield_data{maxlength},
303 $value and length($value) > 100
306 C4::Context->preference("marcflavour") eq "UNIMARC"
307 and 300 <= $tag && $tag < 400 && $subfieldtag eq 'a'
310 C4::Context->preference("marcflavour") eq "MARC21"
311 and 500 <= $tag && $tag < 600
314 # oversize field (textarea)
315 $subfield_data{marc_value} = {
317 id => $subfield_data{id},
321 # it's a standard field
322 $subfield_data{marc_value} = {
324 id => $subfield_data{id},
325 maxlength => $subfield_data{maxlength},
330 # Getting list of subfields to keep when restricted editing is enabled
331 my $subfieldsToAllowForRestrictedEditing = C4::Context->preference('SubfieldsToAllowForRestrictedEditing');
332 my $allowAllSubfields = (
333 not defined $subfieldsToAllowForRestrictedEditing
334 or $subfieldsToAllowForRestrictedEditing eq q||
336 my @subfieldsToAllow = split(/ /, $subfieldsToAllowForRestrictedEditing);
338 # If we're on restricted editing, and our field is not in the list of subfields to allow,
339 # then it is read-only
340 $subfield_data{marc_value}->{readonly} = (
341 not $allowAllSubfields
342 and $restrictededition
343 and !grep { $tag . '$' . $subfieldtag eq $_ } @subfieldsToAllow
346 return \%subfield_data;
349 # Removes some subfields when prefilling items
350 # This function will remove any subfield that is not in the SubfieldsToUseWhenPrefill syspref
351 sub removeFieldsForPrefill {
356 my ($tag, $subtag) = GetMarcFromKohaField("items.barcode", '');
358 # Getting list of subfields to keep
359 my $subfieldsToUseWhenPrefill = C4::Context->preference('SubfieldsToUseWhenPrefill');
361 # Removing subfields that are not in the syspref
362 if ($tag && $subfieldsToUseWhenPrefill) {
363 my $field = $item->field($tag);
364 my @subfieldsToUse= split(/ /,$subfieldsToUseWhenPrefill);
365 foreach my $subfield ($field->subfields()) {
366 if (!grep { $subfield->[0] eq $_ } @subfieldsToUse) {
367 $field->delete_subfield(code => $subfield->[0]);
378 my $error = $input->param('error');
379 my $biblionumber = $input->param('biblionumber');
380 my $itemnumber = $input->param('itemnumber');
381 my $op = $input->param('op') || q{};
382 my $hostitemnumber = $input->param('hostitemnumber');
383 my $marcflavour = C4::Context->preference("marcflavour");
384 my $searchid = $input->param('searchid');
385 # fast cataloguing datas
386 my $fa_circborrowernumber = $input->param('circborrowernumber');
387 my $fa_barcode = $input->param('barcode');
388 my $fa_branch = $input->param('branch');
389 my $fa_stickyduedate = $input->param('stickyduedate');
390 my $fa_duedatespec = $input->param('duedatespec');
392 my $frameworkcode = &GetFrameworkCode($biblionumber);
394 # Defining which userflag is needing according to the framework currently used
396 if (defined $input->param('frameworkcode')) {
397 $userflags = ($input->param('frameworkcode') eq 'FA') ? "fast_cataloging" : "edit_items";
400 if (not defined $userflags) {
401 $userflags = ($frameworkcode eq 'FA') ? "fast_cataloging" : "edit_items";
404 my ($template, $loggedinuser, $cookie)
405 = get_template_and_user({template_name => "cataloguing/additem.tt",
408 authnotrequired => 0,
409 flagsrequired => {editcatalogue => $userflags},
414 # XXX dpavlin -- signatura zatvorenog spremista
415 sub ffzg_zs_callnumber {
417 my ($tagfield,$tagsubfield) = &GetMarcFromKohaField("items.itemcallnumber",$frameworkcode);
418 if ($record->field($tagfield)->subfield($tagsubfield) =~ m/^ZS#(\w\w)\s(\d+)-(\d+)$/ ) {
419 my ( $prefix, $min, $max ) = ( $1, $2, $3 );
422 my $sth = $dbh->prepare(qq{ update ffzg_zs_seq set current = current + 1 where name = ? });
423 $sth->execute( $prefix );
424 $sth = $dbh->prepare(qq{ select current from ffzg_zs_seq where name = ? });
425 $sth->execute( $prefix );
426 my ($itemcallnumber) = $sth->fetchrow;
429 warn "ZS: $prefix $min - $max => $itemcallnumber\n";
431 if ( $itemcallnumber < $min || $itemcallnumber > $max ) {
432 die "can't find next itemcallnumber for $prefix $min-$max, got: $itemcallnumber";
434 my $fieldItem = $record->field($tagfield);
435 $fieldItem->update($tagsubfield => $prefix . ' ' . $itemcallnumber);
441 # Does the user have a restricted item editing permission?
442 my $uid = Koha::Patrons->find( $loggedinuser )->userid;
443 my $restrictededition = $uid ? haspermission($uid, {'editcatalogue' => 'edit_items_restricted'}) : undef;
444 # In case user is a superlibrarian, editing is not restricted
445 $restrictededition = 0 if ($restrictededition != 0 && C4::Context->IsSuperLibrarian());
446 # In case user has fast cataloging permission (and we're in fast cataloging), editing is not restricted
447 $restrictededition = 0 if ($restrictededition != 0 && $frameworkcode eq 'FA' && haspermission($uid, {'editcatalogue' => 'fast_cataloging'}));
449 my $tagslib = &GetMarcStructure(1,$frameworkcode);
450 my $record = GetMarcBiblio({ biblionumber => $biblionumber });
451 my $oldrecord = TransformMarcToKoha($record);
453 my $nextop="additem";
454 my @errors; # store errors found while checking data BEFORE saving item.
456 # Getting last created item cookie
457 my $prefillitem = C4::Context->preference('PrefillItem');
459 my $cookieitemrecord;
461 my $lastitemcookie = $input->cookie('LastCreatedItem');
462 if ($lastitemcookie) {
463 $lastitemcookie = uri_unescape($lastitemcookie);
465 if ( thaw($lastitemcookie) ) {
466 $cookieitemrecord = thaw($lastitemcookie);
467 $cookieitemrecord = removeFieldsForPrefill($cookieitemrecord);
471 $lastitemcookie = 'undef' unless $lastitemcookie;
472 warn "Storable::thaw failed to thaw LastCreatedItem-cookie. Cookie value '$lastitemcookie'. Caught error follows: '$@'";
477 #-------------------------------------------------------------------------------
478 if ($op eq "additem") {
480 #-------------------------------------------------------------------------------
482 my @tags = $input->multi_param('tag');
483 my @subfields = $input->multi_param('subfield');
484 my @values = $input->multi_param('field_value');
485 # build indicator hash.
486 my @ind_tag = $input->multi_param('ind_tag');
487 my @indicator = $input->multi_param('indicator');
488 my $xml = TransformHtmlToXml(\@tags,\@subfields,\@values,\@indicator,\@ind_tag, 'ITEM');
489 my $record = MARC::Record::new_from_xml($xml, 'UTF-8');
492 my $add_submit = $input->param('add_submit');
493 my $add_duplicate_submit = $input->param('add_duplicate_submit');
494 my $add_multiple_copies_submit = $input->param('add_multiple_copies_submit');
495 my $number_of_copies = $input->param('number_of_copies');
497 # This is a bit tricky : if there is a cookie for the last created item and
498 # we just added an item, the cookie value is not correct yet (it will be updated
499 # next page). To prevent the form from being filled with outdated values, we
500 # force the use of "add and duplicate" feature, so the form will be filled with
502 $add_duplicate_submit = 1 if ($prefillitem);
505 # if autoBarcode is set to 'incremental', calculate barcode...
506 if ( C4::Context->preference('autoBarcode') eq 'incremental' ) {
507 $record = _increment_barcode($record, $frameworkcode);
510 my $addedolditem = TransformMarcToKoha( $record );
512 $record = ffzg_zs_callnumber( $record ); # XXX dpavlin
514 # If we have to add or add & duplicate, we add the item
515 if ( $add_submit || $add_duplicate_submit ) {
517 # check for item barcode # being unique
518 my $exist_itemnumber = get_item_from_barcode( $addedolditem->{'barcode'} );
519 push @errors, "barcode_not_unique" if ($exist_itemnumber);
521 # if barcode exists, don't create, but report The problem.
522 unless ($exist_itemnumber) {
523 my ( $oldbiblionumber, $oldbibnum, $oldbibitemnum ) = AddItemFromMarc( $record, $biblionumber );
524 set_item_default_location($oldbibitemnum);
526 # Pushing the last created item cookie back
527 if ($prefillitem && defined $record) {
528 my $itemcookie = $input->cookie(
529 -name => 'LastCreatedItem',
530 # We uri_escape the whole freezed structure so we're sure we won't have any encoding problems
531 -value => uri_escape_utf8( freeze( $record ) ),
536 $cookie = [ $cookie, $itemcookie ];
541 if ($exist_itemnumber) {
542 $itemrecord = $record;
546 # If we have to add & duplicate
547 if ($add_duplicate_submit) {
548 $itemrecord = $record;
549 if (C4::Context->preference('autoBarcode') eq 'incremental') {
550 $itemrecord = _increment_barcode($itemrecord, $frameworkcode);
553 # we have to clear the barcode field in the duplicate item record to make way for the new one generated by the javascript plugin
554 my ($tagfield,$tagsubfield) = &GetMarcFromKohaField("items.barcode",$frameworkcode);
555 my $fieldItem = $itemrecord->field($tagfield);
556 $itemrecord->delete_field($fieldItem);
557 $fieldItem->delete_subfields($tagsubfield);
558 $itemrecord->insert_fields_ordered($fieldItem);
560 $itemrecord = removeFieldsForPrefill($itemrecord) if ($prefillitem);
563 # If we have to add multiple copies
564 if ($add_multiple_copies_submit) {
567 my $barcodeobj = C4::Barcodes->new;
568 my $oldbarcode = $addedolditem->{'barcode'};
569 my ($tagfield,$tagsubfield) = &GetMarcFromKohaField("items.barcode",$frameworkcode);
571 # If there is a barcode and we can't find their new values, we can't add multiple copies
573 $testbarcode = $barcodeobj->next_value($oldbarcode) if $barcodeobj;
574 if ($oldbarcode && !$testbarcode) {
576 push @errors, "no_next_barcode";
577 $itemrecord = $record;
582 # For the first iteration
583 my $barcodevalue = $oldbarcode;
584 my $exist_itemnumber;
587 for (my $i = 0; $i < $number_of_copies;) {
589 # If there is a barcode
592 # Getting a new barcode (if it is not the first iteration or the barcode we tried already exists)
593 $barcodevalue = $barcodeobj->next_value($oldbarcode) if ($i > 0 || $exist_itemnumber);
595 # Putting it into the record
597 $record->field($tagfield)->update($tagsubfield => $barcodevalue);
600 # Checking if the barcode already exists
601 $exist_itemnumber = get_item_from_barcode($barcodevalue);
605 if (!$exist_itemnumber) {
606 my ($oldbiblionumber,$oldbibnum,$oldbibitemnum) = AddItemFromMarc($record,$biblionumber);
607 set_item_default_location($oldbibitemnum);
609 # We count the item only if it was really added
610 # That way, all items are added, even if there was some already existing barcodes
611 # FIXME : Please note that there is a risk of infinite loop here if we never find a suitable barcode
615 # Preparing the next iteration
616 $oldbarcode = $barcodevalue;
621 if ($frameworkcode eq 'FA' && $fa_circborrowernumber){
622 print $input->redirect(
623 '/cgi-bin/koha/circ/circulation.pl?'
624 .'borrowernumber='.$fa_circborrowernumber
625 .'&barcode='.uri_escape_utf8($fa_barcode)
626 .'&duedatespec='.$fa_duedatespec
633 #-------------------------------------------------------------------------------
634 } elsif ($op eq "edititem") {
635 #-------------------------------------------------------------------------------
636 # retrieve item if exist => then, it's a modif
637 $itemrecord = C4::Items::GetMarcItem($biblionumber,$itemnumber);
638 $nextop = "saveitem";
639 #-------------------------------------------------------------------------------
640 } elsif ($op eq "dupeitem") {
641 #-------------------------------------------------------------------------------
642 # retrieve item if exist => then, it's a modif
643 $itemrecord = C4::Items::GetMarcItem($biblionumber,$itemnumber);
644 if (C4::Context->preference('autoBarcode') eq 'incremental') {
645 $itemrecord = _increment_barcode($itemrecord, $frameworkcode);
648 # we have to clear the barcode field in the duplicate item record to make way for the new one generated by the javascript plugin
649 my ($tagfield,$tagsubfield) = &GetMarcFromKohaField("items.barcode",$frameworkcode);
650 my $fieldItem = $itemrecord->field($tagfield);
651 $itemrecord->delete_field($fieldItem);
652 $fieldItem->delete_subfields($tagsubfield);
653 $itemrecord->insert_fields_ordered($fieldItem);
656 #check for hidden subfield and remove them for the duplicated item
657 foreach my $field ($itemrecord->fields()){
658 my $tag = $field->{_tag};
659 foreach my $subfield ($field->subfields()){
660 my $subfieldtag = $subfield->[0];
661 if ($tagslib->{$tag}->{$subfieldtag}->{'tab'} ne "10"
662 || abs($tagslib->{$tag}->{$subfieldtag}->{hidden})>4 ){
663 my $fieldItem = $itemrecord->field($tag);
664 $itemrecord->delete_field($fieldItem);
665 $fieldItem->delete_subfields($subfieldtag);
666 $itemrecord->insert_fields_ordered($fieldItem);
671 $itemrecord = removeFieldsForPrefill($itemrecord) if ($prefillitem);
673 #-------------------------------------------------------------------------------
674 } elsif ($op eq "delitem") {
675 #-------------------------------------------------------------------------------
676 # check that there is no issue on this item before deletion.
677 $error = &DelItemCheck( $biblionumber,$itemnumber);
679 print $input->redirect("additem.pl?biblionumber=$biblionumber&frameworkcode=$frameworkcode&searchid=$searchid");
684 #-------------------------------------------------------------------------------
685 } elsif ($op eq "delallitems") {
686 #-------------------------------------------------------------------------------
687 my @itemnumbers = Koha::Items->search({ biblionumber => $biblionumber })->get_column('itemnumber');
688 foreach my $itemnumber ( @itemnumbers ) {
689 $error = C4::Items::DelItemCheck( $biblionumber, $itemnumber );
690 next if $error == 1; # Means ok
696 my $defaultview = C4::Context->preference('IntranetBiblioDefaultView');
697 my $views = { C4::Search::enabled_staff_search_views };
698 if ($defaultview eq 'isbd' && $views->{can_view_ISBD}) {
699 print $input->redirect("/cgi-bin/koha/catalogue/ISBDdetail.pl?biblionumber=$biblionumber&searchid=$searchid");
700 } elsif ($defaultview eq 'marc' && $views->{can_view_MARC}) {
701 print $input->redirect("/cgi-bin/koha/catalogue/MARCdetail.pl?biblionumber=$biblionumber&searchid=$searchid");
702 } elsif ($defaultview eq 'labeled_marc' && $views->{can_view_labeledMARC}) {
703 print $input->redirect("/cgi-bin/koha/catalogue/labeledMARCdetail.pl?biblionumber=$biblionumber&searchid=$searchid");
705 print $input->redirect("/cgi-bin/koha/catalogue/detail.pl?biblionumber=$biblionumber&searchid=$searchid");
709 #-------------------------------------------------------------------------------
710 } elsif ($op eq "saveitem") {
711 #-------------------------------------------------------------------------------
713 my @tags = $input->multi_param('tag');
714 my @subfields = $input->multi_param('subfield');
715 my @values = $input->multi_param('field_value');
716 # build indicator hash.
717 my @ind_tag = $input->multi_param('ind_tag');
718 my @indicator = $input->multi_param('indicator');
719 # my $itemnumber = $input->param('itemnumber');
720 my $xml = TransformHtmlToXml(\@tags,\@subfields,\@values,\@indicator,\@ind_tag,'ITEM');
721 my $itemtosave=MARC::Record::new_from_xml($xml, 'UTF-8');
722 # MARC::Record builded => now, record in DB
723 # warn "R: ".$record->as_formatted;
724 # check that the barcode don't exist already
726 $itemtosave = ffzg_zs_callnumber( $itemtosave ); # XXX dpavlin
728 my $addedolditem = TransformMarcToKoha($itemtosave);
729 my $exist_itemnumber = get_item_from_barcode($addedolditem->{'barcode'});
730 if ($exist_itemnumber && $exist_itemnumber != $itemnumber) {
731 push @errors,"barcode_not_unique";
733 my $item = Koha::Items->find($itemnumber );
734 my $newitem = ModItemFromMarc($itemtosave, $biblionumber, $itemnumber);
736 my $olditemlost = $item->itemlost;
737 my $newitemlost = $newitem->{itemlost};
738 LostItem( $item->itemnumber, 'additem' )
739 if $newitemlost && $newitemlost ge '1' && !$olditemlost;
742 } elsif ($op eq "delinkitem"){
743 my $analyticfield = '773';
744 if ($marcflavour eq 'MARC21' || $marcflavour eq 'NORMARC'){
745 $analyticfield = '773';
746 } elsif ($marcflavour eq 'UNIMARC') {
747 $analyticfield = '461';
749 foreach my $field ($record->field($analyticfield)){
750 if ($field->subfield('9') eq $hostitemnumber){
751 $record->delete_field($field);
755 my $modbibresult = ModBiblio($record, $biblionumber,'');
759 #-------------------------------------------------------------------------------
760 # build screen with existing items. and "new" one
761 #-------------------------------------------------------------------------------
763 # now, build existiing item list
764 my $temp = GetMarcBiblio({ biblionumber => $biblionumber });
765 #my @fields = $record->fields();
768 my %witness; #---- stores the list of subfields used at least once, with the "meaning" of the code
770 #---- finds where items.itemnumber is stored
771 my ( $itemtagfield, $itemtagsubfield) = &GetMarcFromKohaField("items.itemnumber", $frameworkcode);
772 my ($branchtagfield, $branchtagsubfield) = &GetMarcFromKohaField("items.homebranch", $frameworkcode);
773 C4::Biblio::EmbedItemsInMarcBiblio({
774 marc_record => $temp,
775 biblionumber => $biblionumber });
776 my @fields = $temp->fields();
780 if ( C4::Context->preference('EasyAnalyticalRecords') ) {
781 my $analyticfield = '773';
782 if ($marcflavour eq 'MARC21' || $marcflavour eq 'NORMARC') {
783 $analyticfield = '773';
784 } elsif ($marcflavour eq 'UNIMARC') {
785 $analyticfield = '461';
787 foreach my $hostfield ($temp->field($analyticfield)){
788 my $hostbiblionumber = $hostfield->subfield('0');
789 if ($hostbiblionumber){
790 my $hostrecord = GetMarcBiblio({
791 biblionumber => $hostbiblionumber,
794 my ($itemfield, undef) = GetMarcFromKohaField( 'items.itemnumber', GetFrameworkCode($hostbiblionumber) );
795 foreach my $hostitem ($hostrecord->field($itemfield)){
796 if ($hostitem->subfield('9') eq $hostfield->subfield('9')){
797 push (@fields, $hostitem);
798 push (@hostitemnumbers, $hostfield->subfield('9'));
807 foreach my $field (@fields) {
808 next if ( $field->tag() < 10 );
810 my @subf = $field->subfields or (); # don't use ||, as that forces $field->subfelds to be interpreted in scalar context
812 # loop through each subfield
814 foreach my $subfield (@subf){
815 my $subfieldcode = $subfield->[0];
816 my $subfieldvalue= $subfield->[1];
818 next if ($tagslib->{$field->tag()}->{$subfieldcode}->{tab} ne 10
819 && ($field->tag() ne $itemtagfield
820 && $subfieldcode ne $itemtagsubfield));
821 $witness{$subfieldcode} = $tagslib->{$field->tag()}->{$subfieldcode}->{lib} if ($tagslib->{$field->tag()}->{$subfieldcode}->{tab} eq 10);
822 if ($tagslib->{$field->tag()}->{$subfieldcode}->{tab} eq 10) {
823 $this_row{$subfieldcode} .= " | " if($this_row{$subfieldcode});
824 $this_row{$subfieldcode} .= GetAuthorisedValueDesc( $field->tag(),
825 $subfieldcode, $subfieldvalue, '', $tagslib)
829 if (($field->tag eq $branchtagfield) && ($subfieldcode eq $branchtagsubfield) && C4::Context->preference("IndependentBranches")) {
831 my $userenv = C4::Context->userenv();
832 unless (C4::Context->IsSuperLibrarian() or (($userenv->{'branch'} eq $subfieldvalue))){
833 $this_row{'nomod'} = 1;
836 $this_row{itemnumber} = $subfieldvalue if ($field->tag() eq $itemtagfield && $subfieldcode eq $itemtagsubfield);
838 if ( C4::Context->preference('EasyAnalyticalRecords') ) {
839 foreach my $hostitemnumber (@hostitemnumbers) {
840 my $item = Koha::Items->find( $hostitemnumber );
841 if ($this_row{itemnumber} eq $hostitemnumber) {
842 $this_row{hostitemflag} = 1;
843 $this_row{hostbiblionumber}= $item->biblio->biblionumber;
850 push(@big_array, \%this_row);
854 my ($holdingbrtagf,$holdingbrtagsubf) = &GetMarcFromKohaField("items.holdingbranch",$frameworkcode);
855 @big_array = sort {$a->{$holdingbrtagsubf} cmp $b->{$holdingbrtagsubf}} @big_array;
857 # now, construct template !
858 # First, the existing items for display
860 my @header_value_loop;
861 for my $row ( @big_array ) {
863 my @item_fields = map +{ field => $_ || '' }, @$row{ sort keys(%witness) };
864 $row_data{item_value} = [ @item_fields ];
865 $row_data{itemnumber} = $row->{itemnumber};
866 #reporting this_row values
867 $row_data{'nomod'} = $row->{'nomod'};
868 $row_data{'hostitemflag'} = $row->{'hostitemflag'};
869 $row_data{'hostbiblionumber'} = $row->{'hostbiblionumber'};
870 # $row_data{'countanalytics'} = $row->{'countanalytics'};
871 push(@item_value_loop,\%row_data);
873 foreach my $subfield_code (sort keys(%witness)) {
875 $header_value{header_value} = $witness{$subfield_code};
877 my $subfieldlib = $tagslib->{$itemtagfield}->{$subfield_code};
878 my $kohafield = $subfieldlib->{kohafield};
879 if ( $kohafield && $kohafield =~ /items.(.+)/ ) {
880 $header_value{column_name} = $1;
883 push(@header_value_loop, \%header_value);
886 # now, build the item form for entering a new item
890 my $pref_itemcallnumber = C4::Context->preference('itemcallnumber');
892 my $branch = $input->param('branch') || C4::Context->userenv->{branch};
893 my $libraries = Koha::Libraries->search({}, { order_by => ['branchname'] })->unblessed;# build once ahead of time, instead of multiple times later.
894 for my $library ( @$libraries ) {
895 $library->{selected} = 1 if $library->{branchcode} eq $branch
898 # We generate form, from actuel record
901 foreach my $field ($itemrecord->fields()){
902 my $tag = $field->{_tag};
903 foreach my $subfield ( $field->subfields() ){
905 my $subfieldtag = $subfield->[0];
906 my $value = $subfield->[1];
907 my $subfieldlib = $tagslib->{$tag}->{$subfieldtag};
909 next if ($tagslib->{$tag}->{$subfieldtag}->{'tab'} ne "10");
911 my $subfield_data = generate_subfield_form($tag, $subfieldtag, $value, $tagslib, $subfieldlib, $libraries, $biblionumber, $temp, \@loop_data, $i, $restrictededition);
912 push @fields, "$tag$subfieldtag";
913 push (@loop_data, $subfield_data);
919 # and now we add fields that are empty
921 # Using last created item if it exists
923 $itemrecord = $cookieitemrecord if ($prefillitem and not $justaddeditem and $op ne "edititem");
925 # We generate form, and fill with values if defined
926 foreach my $tag ( keys %{$tagslib}){
927 foreach my $subtag (keys %{$tagslib->{$tag}}){
928 next if IsMarcStructureInternal($tagslib->{$tag}{$subtag});
929 next if ($tagslib->{$tag}->{$subtag}->{'tab'} ne "10");
930 next if any { /^$tag$subtag$/ } @fields;
932 my @values = (undef);
933 @values = $itemrecord->field($tag)->subfield($subtag) if ($itemrecord && defined($itemrecord->field($tag)) && defined($itemrecord->field($tag)->subfield($subtag)));
934 for my $value (@values){
935 my $subfield_data = generate_subfield_form($tag, $subtag, $value, $tagslib, $tagslib->{$tag}->{$subtag}, $libraries, $biblionumber, $temp, \@loop_data, $i, $restrictededition);
936 push (@loop_data, $subfield_data);
941 @loop_data = sort {$a->{subfield} cmp $b->{subfield} } @loop_data;
943 my $item = Koha::Items->find($itemnumber); # We certainly want to fetch it earlier
945 # what's the next op ? it's what we are not in : an add if we're editing, otherwise, and edit.
947 biblionumber => $biblionumber,
948 title => $oldrecord->{title},
949 author => $oldrecord->{author},
950 item_loop => \@item_value_loop,
951 item_header_loop => \@header_value_loop,
953 itemnumber => $itemnumber,
954 barcode => $item ? $item->barcode : undef,
955 itemtagfield => $itemtagfield,
956 itemtagsubfield => $itemtagsubfield,
958 opisadd => ($nextop eq "saveitem") ? 0 : 1,
959 popup => scalar $input->param('popup') ? 1: 0,
960 C4::Search::enabled_staff_search_views,
962 $template->{'VARS'}->{'searchid'} = $searchid;
964 if ($frameworkcode eq 'FA'){
965 # fast cataloguing datas
967 'circborrowernumber' => $fa_circborrowernumber,
968 'barcode' => $fa_barcode,
969 'branch' => $fa_branch,
970 'stickyduedate' => $fa_stickyduedate,
971 'duedatespec' => $fa_duedatespec,
975 foreach my $error (@errors) {
976 $template->param($error => 1);
978 output_html_with_http_headers $input, $cookie, $template->output;