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, $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 $today_iso = output_pref( { dt=>dt_from_string, dateonly => 1, dateformat => 'iso' } );
133 my ( $year, $month, $day ) = split ('-', $today_iso);
134 $value =~ s/YYYY/$year/g;
135 $value =~ s/MM/$month/g;
136 $value =~ s/DD/$day/g;
139 $subfield_data{visibility} = "display:none;" if (($subfieldlib->{hidden} > 4) || ($subfieldlib->{hidden} <= -4));
141 my $pref_itemcallnumber = C4::Context->preference('itemcallnumber');
142 if (!$value && $subfieldlib->{kohafield} eq 'items.itemcallnumber' && $pref_itemcallnumber) {
143 my $CNtag = substr($pref_itemcallnumber, 0, 3);
144 my $CNsubfield = substr($pref_itemcallnumber, 3, 1);
145 my $CNsubfield2 = substr($pref_itemcallnumber, 4, 1);
146 my $temp2 = $temp->field($CNtag);
148 $value = ($temp2->subfield($CNsubfield)).' '.($temp2->subfield($CNsubfield2));
149 #remove any trailing space incase one subfield is used
150 $value =~ s/^\s+|\s+$//g;
154 if ($frameworkcode eq 'FA' && $subfieldlib->{kohafield} eq 'items.barcode' && !$value){
156 $value = $input->param('barcode');
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 ( $subfieldlib->{authorised_value} ) {
168 my @authorised_values;
170 # builds list, depending on authorised value...
171 if ( $subfieldlib->{authorised_value} eq "branches" ) {
172 foreach my $thisbranch (@$branches) {
173 push @authorised_values, $thisbranch->{value};
174 $authorised_lib{$thisbranch->{value}} = $thisbranch->{branchname};
175 $value = $thisbranch->{value} if $thisbranch->{selected} && !$value;
178 elsif ( $subfieldlib->{authorised_value} eq "itemtypes" ) {
179 push @authorised_values, "" unless ( $subfieldlib->{mandatory} );
180 my $itemtypes = GetItemTypes( style => 'array' );
181 for my $itemtype ( @$itemtypes ) {
182 push @authorised_values, $itemtype->{itemtype};
183 $authorised_lib{$itemtype->{itemtype}} = $itemtype->{translated_description};
187 my $itype_sth = $dbh->prepare("SELECT itemtype FROM biblioitems WHERE biblionumber = ?");
188 $itype_sth->execute( $biblionumber );
189 ( $value ) = $itype_sth->fetchrow_array;
194 elsif ( $subfieldlib->{authorised_value} eq "cn_source" ) {
195 push @authorised_values, "" unless ( $subfieldlib->{mandatory} );
197 my $class_sources = GetClassSources();
198 my $default_source = C4::Context->preference("DefaultClassificationSource");
200 foreach my $class_source (sort keys %$class_sources) {
201 next unless $class_sources->{$class_source}->{'used'} or
202 ($value and $class_source eq $value) or
203 ($class_source eq $default_source);
204 push @authorised_values, $class_source;
205 $authorised_lib{$class_source} = $class_sources->{$class_source}->{'description'};
207 $value = $default_source unless ($value);
209 #---- "true" authorised value
212 push @authorised_values, qq{} unless ( $subfieldlib->{mandatory} );
213 my $av = GetAuthorisedValues( $subfieldlib->{authorised_value} );
215 push @authorised_values, $r->{authorised_value};
216 $authorised_lib{$r->{authorised_value}} = $r->{lib};
220 if ( $subfieldlib->{hidden} > 4 or $subfieldlib->{hidden} <= -4 ) {
221 $subfield_data{marc_value} = {
223 id => $subfield_data{id},
224 maxlength => $subfield_data{max_length},
229 $subfield_data{marc_value} = {
231 id => "tag_".$tag."_subfield_".$subfieldtag."_".$index_subfield,
232 values => \@authorised_values,
233 labels => \%authorised_lib,
236 # If we're on restricted editing, and our field is not in the list of subfields to allow,
237 # then it is read-only
238 $subfield_data{marc_value}->{readonlyselect} = (
239 not $allowAllSubfields
240 and $restrictededition
241 and !grep { $tag . '$' . $subfieldtag eq $_ } @subfieldsToAllow
245 # it's a thesaurus / authority field
246 elsif ( $subfieldlib->{authtypecode} ) {
247 $subfield_data{marc_value} = {
249 id => $subfield_data{id},
250 maxlength => $subfield_data{max_length},
252 authtypecode => $subfieldlib->{authtypecode},
255 # it's a plugin field
256 elsif ( $subfieldlib->{value_builder} ) { # plugin
257 require Koha::FrameworkPlugin;
258 my $plugin = Koha::FrameworkPlugin->new({
259 name => $subfieldlib->{'value_builder'},
262 my $pars= { dbh => $dbh, record => $temp, tagslib =>$tagslib,
263 id => $subfield_data{id}, tabloop => $loop_data };
264 $plugin->build( $pars );
265 if( !$plugin->errstr ) {
266 my $class= 'buttonDot'. ( $plugin->noclick? ' disabled': '' );
267 $subfield_data{marc_value} = {
268 type => 'text_plugin',
269 id => $subfield_data{id},
270 maxlength => $subfield_data{max_length},
273 nopopup => $plugin->noclick,
274 javascript => $plugin->javascript,
277 warn $plugin->errstr;
278 $subfield_data{marc_value} = {
280 id => $subfield_data{id},
281 maxlength => $subfield_data{max_length},
283 }; # supply default input form
286 elsif ( $tag eq '' ) { # it's an hidden field
287 $subfield_data{marc_value} = {
289 id => $subfield_data{id},
290 maxlength => $subfield_data{max_length},
294 elsif ( $subfieldlib->{'hidden'} ) { # FIXME: shouldn't input type be "hidden" ?
295 $subfield_data{marc_value} = {
297 id => $subfield_data{id},
298 maxlength => $subfield_data{max_length},
305 C4::Context->preference("marcflavour") eq "UNIMARC"
306 and 300 <= $tag && $tag < 400 && $subfieldtag eq 'a'
309 C4::Context->preference("marcflavour") eq "MARC21"
310 and 500 <= $tag && $tag < 600
313 # oversize field (textarea)
314 $subfield_data{marc_value} = {
316 id => $subfield_data{id},
320 # it's a standard field
321 $subfield_data{marc_value} = {
323 id => $subfield_data{id},
324 maxlength => $subfield_data{max_length},
329 return \%subfield_data;
332 # Removes some subfields when prefilling items
333 # This function will remove any subfield that is not in the SubfieldsToUseWhenPrefill syspref
334 sub removeFieldsForPrefill {
339 my ($tag, $subtag) = GetMarcFromKohaField("items.barcode", '');
341 # Getting list of subfields to keep
342 my $subfieldsToUseWhenPrefill = C4::Context->preference('SubfieldsToUseWhenPrefill');
344 # Removing subfields that are not in the syspref
345 if ($tag && $subfieldsToUseWhenPrefill) {
346 my $field = $item->field($tag);
347 my @subfieldsToUse= split(/ /,$subfieldsToUseWhenPrefill);
348 foreach my $subfield ($field->subfields()) {
349 if (!grep { $subfield->[0] eq $_ } @subfieldsToUse) {
350 $field->delete_subfield(code => $subfield->[0]);
361 my $error = $input->param('error');
362 my $biblionumber = $input->param('biblionumber');
363 my $itemnumber = $input->param('itemnumber');
364 my $op = $input->param('op');
365 my $hostitemnumber = $input->param('hostitemnumber');
366 my $marcflavour = C4::Context->preference("marcflavour");
367 my $searchid = $input->param('searchid');
368 # fast cataloguing datas
369 my $fa_circborrowernumber = $input->param('circborrowernumber');
370 my $fa_barcode = $input->param('barcode');
371 my $fa_branch = $input->param('branch');
372 my $fa_stickyduedate = $input->param('stickyduedate');
373 my $fa_duedatespec = $input->param('duedatespec');
375 my $frameworkcode = &GetFrameworkCode($biblionumber);
377 # Defining which userflag is needing according to the framework currently used
379 if (defined $input->param('frameworkcode')) {
380 $userflags = ($input->param('frameworkcode') eq 'FA') ? "fast_cataloging" : "edit_items";
383 if (not defined $userflags) {
384 $userflags = ($frameworkcode eq 'FA') ? "fast_cataloging" : "edit_items";
387 my ($template, $loggedinuser, $cookie)
388 = get_template_and_user({template_name => "cataloguing/additem.tt",
391 authnotrequired => 0,
392 flagsrequired => {editcatalogue => $userflags},
397 # XXX dpavlin -- signatura zatvorenog spremista
398 sub ffzg_zs_callnumber {
400 my ($tagfield,$tagsubfield) = &GetMarcFromKohaField("items.itemcallnumber",$frameworkcode);
401 if ($record->field($tagfield)->subfield($tagsubfield) =~ m/^ZS#(\w\w)\s(\d+)-(\d+)$/ ) {
402 my ( $prefix, $min, $max ) = ( $1, $2, $3 );
405 my $sth = $dbh->prepare(qq{ update ffzg_zs_seq set current = current + 1 where name = ? });
406 $sth->execute( $prefix );
407 $sth = $dbh->prepare(qq{ select current from ffzg_zs_seq where name = ? });
408 $sth->execute( $prefix );
409 my ($itemcallnumber) = $sth->fetchrow;
412 warn "ZS: $prefix $min - $max => $itemcallnumber\n";
414 if ( $itemcallnumber < $min || $itemcallnumber > $max ) {
415 die "can't find next itemcallnumber for $prefix $min-$max, got: $itemcallnumber";
417 my $fieldItem = $record->field($tagfield);
418 $fieldItem->update($tagsubfield => $prefix . ' ' . $itemcallnumber);
424 # Does the user have a restricted item editing permission?
425 my $uid = $loggedinuser ? GetMember( borrowernumber => $loggedinuser )->{userid} : undef;
426 my $restrictededition = $uid ? haspermission($uid, {'editcatalogue' => 'edit_items_restricted'}) : undef;
427 # In case user is a superlibrarian, editing is not restricted
428 $restrictededition = 0 if ($restrictededition != 0 && C4::Context->IsSuperLibrarian());
429 # In case user has fast cataloging permission (and we're in fast cataloging), editing is not restricted
430 $restrictededition = 0 if ($restrictededition != 0 && $frameworkcode eq 'FA' && haspermission($uid, {'editcatalogue' => 'fast_cataloging'}));
432 my $tagslib = &GetMarcStructure(1,$frameworkcode);
433 my $record = GetMarcBiblio($biblionumber);
434 my $oldrecord = TransformMarcToKoha($dbh,$record);
436 my $nextop="additem";
437 my @errors; # store errors found while checking data BEFORE saving item.
439 # Getting last created item cookie
440 my $prefillitem = C4::Context->preference('PrefillItem');
442 my $cookieitemrecord;
444 my $lastitemcookie = $input->cookie('LastCreatedItem');
445 if ($lastitemcookie) {
446 $lastitemcookie = uri_unescape($lastitemcookie);
447 if ( thaw($lastitemcookie) ) {
448 $cookieitemrecord = thaw($lastitemcookie) ;
449 $cookieitemrecord = removeFieldsForPrefill($cookieitemrecord);
454 #-------------------------------------------------------------------------------
455 if ($op eq "additem") {
457 #-------------------------------------------------------------------------------
459 my @tags = $input->param('tag');
460 my @subfields = $input->param('subfield');
461 my @values = $input->param('field_value');
462 # build indicator hash.
463 my @ind_tag = $input->param('ind_tag');
464 my @indicator = $input->param('indicator');
465 my $xml = TransformHtmlToXml(\@tags,\@subfields,\@values,\@indicator,\@ind_tag, 'ITEM');
466 my $record = MARC::Record::new_from_xml($xml, 'UTF-8');
469 my $add_submit = $input->param('add_submit');
470 my $add_duplicate_submit = $input->param('add_duplicate_submit');
471 my $add_multiple_copies_submit = $input->param('add_multiple_copies_submit');
472 my $number_of_copies = $input->param('number_of_copies');
474 # This is a bit tricky : if there is a cookie for the last created item and
475 # we just added an item, the cookie value is not correct yet (it will be updated
476 # next page). To prevent the form from being filled with outdated values, we
477 # force the use of "add and duplicate" feature, so the form will be filled with
479 $add_duplicate_submit = 1 if ($prefillitem);
482 # if autoBarcode is set to 'incremental', calculate barcode...
483 if ( C4::Context->preference('autoBarcode') eq 'incremental' ) {
484 $record = _increment_barcode($record, $frameworkcode);
487 my $addedolditem = TransformMarcToKoha( $dbh, $record );
489 $record = ffzg_zs_callnumber( $record ); # XXX dpavlin
491 # If we have to add or add & duplicate, we add the item
492 if ( $add_submit || $add_duplicate_submit ) {
494 # check for item barcode # being unique
495 my $exist_itemnumber = get_item_from_barcode( $addedolditem->{'barcode'} );
496 push @errors, "barcode_not_unique" if ($exist_itemnumber);
498 # if barcode exists, don't create, but report The problem.
499 unless ($exist_itemnumber) {
500 my ( $oldbiblionumber, $oldbibnum, $oldbibitemnum ) = AddItemFromMarc( $record, $biblionumber );
501 set_item_default_location($oldbibitemnum);
503 # Pushing the last created item cookie back
504 if ($prefillitem && defined $record) {
505 my $itemcookie = $input->cookie(
506 -name => 'LastCreatedItem',
507 # We uri_escape the whole freezed structure so we're sure we won't have any encoding problems
508 -value => uri_escape_utf8( freeze( $record ) ),
513 $cookie = [ $cookie, $itemcookie ];
518 if ($exist_itemnumber) {
519 $itemrecord = $record;
523 # If we have to add & duplicate
524 if ($add_duplicate_submit) {
525 $itemrecord = $record;
526 if (C4::Context->preference('autoBarcode') eq 'incremental') {
527 $itemrecord = _increment_barcode($itemrecord, $frameworkcode);
530 # we have to clear the barcode field in the duplicate item record to make way for the new one generated by the javascript plugin
531 my ($tagfield,$tagsubfield) = &GetMarcFromKohaField("items.barcode",$frameworkcode);
532 my $fieldItem = $itemrecord->field($tagfield);
533 $itemrecord->delete_field($fieldItem);
534 $fieldItem->delete_subfields($tagsubfield);
535 $itemrecord->insert_fields_ordered($fieldItem);
537 $itemrecord = removeFieldsForPrefill($itemrecord) if ($prefillitem);
540 # If we have to add multiple copies
541 if ($add_multiple_copies_submit) {
544 my $barcodeobj = C4::Barcodes->new;
545 my $oldbarcode = $addedolditem->{'barcode'};
546 my ($tagfield,$tagsubfield) = &GetMarcFromKohaField("items.barcode",$frameworkcode);
548 # If there is a barcode and we can't find him new values, we can't add multiple copies
550 $testbarcode = $barcodeobj->next_value($oldbarcode) if $barcodeobj;
551 if ($oldbarcode && !$testbarcode) {
553 push @errors, "no_next_barcode";
554 $itemrecord = $record;
559 # For the first iteration
560 my $barcodevalue = $oldbarcode;
561 my $exist_itemnumber;
564 for (my $i = 0; $i < $number_of_copies;) {
566 # If there is a barcode
569 # Getting a new barcode (if it is not the first iteration or the barcode we tried already exists)
570 $barcodevalue = $barcodeobj->next_value($oldbarcode) if ($i > 0 || $exist_itemnumber);
572 # Putting it into the record
574 $record->field($tagfield)->update($tagsubfield => $barcodevalue);
577 # Checking if the barcode already exists
578 $exist_itemnumber = get_item_from_barcode($barcodevalue);
582 if (!$exist_itemnumber) {
583 my ($oldbiblionumber,$oldbibnum,$oldbibitemnum) = AddItemFromMarc($record,$biblionumber);
584 set_item_default_location($oldbibitemnum);
586 # We count the item only if it was really added
587 # That way, all items are added, even if there was some already existing barcodes
588 # FIXME : Please note that there is a risk of infinite loop here if we never find a suitable barcode
592 # Preparing the next iteration
593 $oldbarcode = $barcodevalue;
598 if ($frameworkcode eq 'FA' && $fa_circborrowernumber){
599 print $input->redirect(
600 '/cgi-bin/koha/circ/circulation.pl?'
601 .'borrowernumber='.$fa_circborrowernumber
602 .'&barcode='.uri_escape_utf8($fa_barcode)
603 .'&duedatespec='.$fa_duedatespec
610 #-------------------------------------------------------------------------------
611 } elsif ($op eq "edititem") {
612 #-------------------------------------------------------------------------------
613 # retrieve item if exist => then, it's a modif
614 $itemrecord = C4::Items::GetMarcItem($biblionumber,$itemnumber);
615 $nextop = "saveitem";
616 #-------------------------------------------------------------------------------
617 } elsif ($op eq "dupeitem") {
618 #-------------------------------------------------------------------------------
619 # retrieve item if exist => then, it's a modif
620 $itemrecord = C4::Items::GetMarcItem($biblionumber,$itemnumber);
621 if (C4::Context->preference('autoBarcode') eq 'incremental') {
622 $itemrecord = _increment_barcode($itemrecord, $frameworkcode);
625 # we have to clear the barcode field in the duplicate item record to make way for the new one generated by the javascript plugin
626 my ($tagfield,$tagsubfield) = &GetMarcFromKohaField("items.barcode",$frameworkcode);
627 my $fieldItem = $itemrecord->field($tagfield);
628 $itemrecord->delete_field($fieldItem);
629 $fieldItem->delete_subfields($tagsubfield);
630 $itemrecord->insert_fields_ordered($fieldItem);
633 #check for hidden subfield and remove them for the duplicated item
634 foreach my $field ($itemrecord->fields()){
635 my $tag = $field->{_tag};
636 foreach my $subfield ($field->subfields()){
637 my $subfieldtag = $subfield->[0];
638 if (subfield_is_koha_internal_p($subfieldtag) || $tagslib->{$tag}->{$subfieldtag}->{'tab'} ne "10"
639 || abs($tagslib->{$tag}->{$subfieldtag}->{hidden})>4 ){
640 my $fieldItem = $itemrecord->field($tag);
641 $itemrecord->delete_field($fieldItem);
642 $fieldItem->delete_subfields($subfieldtag);
643 $itemrecord->insert_fields_ordered($fieldItem);
648 $itemrecord = removeFieldsForPrefill($itemrecord) if ($prefillitem);
650 #-------------------------------------------------------------------------------
651 } elsif ($op eq "delitem") {
652 #-------------------------------------------------------------------------------
653 # check that there is no issue on this item before deletion.
654 $error = &DelItemCheck($dbh,$biblionumber,$itemnumber);
656 print $input->redirect("additem.pl?biblionumber=$biblionumber&frameworkcode=$frameworkcode&searchid=$searchid");
661 #-------------------------------------------------------------------------------
662 } elsif ($op eq "delallitems") {
663 #-------------------------------------------------------------------------------
664 my @biblioitems = &GetBiblioItemByBiblioNumber($biblionumber);
667 foreach my $biblioitem (@biblioitems) {
668 my $items = &GetItemsByBiblioitemnumber( $biblioitem->{biblioitemnumber} );
670 foreach my $item (@$items) {
671 $error =&DelItemCheck( $dbh, $biblionumber, $item->{itemnumber} );
685 my $defaultview = C4::Context->preference('IntranetBiblioDefaultView');
686 my $views = { C4::Search::enabled_staff_search_views };
687 if ($defaultview eq 'isbd' && $views->{can_view_ISBD}) {
688 print $input->redirect("/cgi-bin/koha/catalogue/ISBDdetail.pl?biblionumber=$biblionumber&searchid=$searchid");
689 } elsif ($defaultview eq 'marc' && $views->{can_view_MARC}) {
690 print $input->redirect("/cgi-bin/koha/catalogue/MARCdetail.pl?biblionumber=$biblionumber&searchid=$searchid");
691 } elsif ($defaultview eq 'labeled_marc' && $views->{can_view_labeledMARC}) {
692 print $input->redirect("/cgi-bin/koha/catalogue/labeledMARCdetail.pl?biblionumber=$biblionumber&searchid=$searchid");
694 print $input->redirect("/cgi-bin/koha/catalogue/detail.pl?biblionumber=$biblionumber&searchid=$searchid");
699 #-------------------------------------------------------------------------------
700 } elsif ($op eq "saveitem") {
701 #-------------------------------------------------------------------------------
703 my @tags = $input->param('tag');
704 my @subfields = $input->param('subfield');
705 my @values = $input->param('field_value');
706 # build indicator hash.
707 my @ind_tag = $input->param('ind_tag');
708 my @indicator = $input->param('indicator');
709 # my $itemnumber = $input->param('itemnumber');
710 my $xml = TransformHtmlToXml(\@tags,\@subfields,\@values,\@indicator,\@ind_tag,'ITEM');
711 my $itemtosave=MARC::Record::new_from_xml($xml, 'UTF-8');
712 # MARC::Record builded => now, record in DB
713 # warn "R: ".$record->as_formatted;
714 # check that the barcode don't exist already
716 $itemtosave = ffzg_zs_callnumber( $itemtosave ); # XXX dpavlin
718 my $addedolditem = TransformMarcToKoha($dbh,$itemtosave);
719 my $exist_itemnumber = get_item_from_barcode($addedolditem->{'barcode'});
720 if ($exist_itemnumber && $exist_itemnumber != $itemnumber) {
721 push @errors,"barcode_not_unique";
723 ModItemFromMarc($itemtosave,$biblionumber,$itemnumber);
726 my $item = GetItem( $itemnumber );
727 my $olditemlost = $item->{'itemlost'};
729 my ($lost_tag,$lost_subfield) = GetMarcFromKohaField("items.itemlost",'');
731 my $newitemlost = $itemtosave->subfield( $lost_tag, $lost_subfield );
732 if (($olditemlost eq '0' or $olditemlost eq '' ) and $newitemlost ge '1'){
733 LostItem($itemnumber,'MARK RETURNED');
736 } elsif ($op eq "delinkitem"){
737 my $analyticfield = '773';
738 if ($marcflavour eq 'MARC21' || $marcflavour eq 'NORMARC'){
739 $analyticfield = '773';
740 } elsif ($marcflavour eq 'UNIMARC') {
741 $analyticfield = '461';
743 foreach my $field ($record->field($analyticfield)){
744 if ($field->subfield('9') eq $hostitemnumber){
745 $record->delete_field($field);
749 my $modbibresult = ModBiblio($record, $biblionumber,'');
753 #-------------------------------------------------------------------------------
754 # build screen with existing items. and "new" one
755 #-------------------------------------------------------------------------------
757 # now, build existiing item list
758 my $temp = GetMarcBiblio( $biblionumber );
759 #my @fields = $record->fields();
762 my %witness; #---- stores the list of subfields used at least once, with the "meaning" of the code
764 #---- finds where items.itemnumber is stored
765 my ( $itemtagfield, $itemtagsubfield) = &GetMarcFromKohaField("items.itemnumber", $frameworkcode);
766 my ($branchtagfield, $branchtagsubfield) = &GetMarcFromKohaField("items.homebranch", $frameworkcode);
767 C4::Biblio::EmbedItemsInMarcBiblio($temp, $biblionumber);
768 my @fields = $temp->fields();
772 if ( C4::Context->preference('EasyAnalyticalRecords') ) {
773 my $analyticfield = '773';
774 if ($marcflavour eq 'MARC21' || $marcflavour eq 'NORMARC') {
775 $analyticfield = '773';
776 } elsif ($marcflavour eq 'UNIMARC') {
777 $analyticfield = '461';
779 foreach my $hostfield ($temp->field($analyticfield)){
780 my $hostbiblionumber = $hostfield->subfield('0');
781 if ($hostbiblionumber){
782 my $hostrecord = GetMarcBiblio($hostbiblionumber, 1);
784 my ($itemfield, undef) = GetMarcFromKohaField( 'items.itemnumber', GetFrameworkCode($hostbiblionumber) );
785 foreach my $hostitem ($hostrecord->field($itemfield)){
786 if ($hostitem->subfield('9') eq $hostfield->subfield('9')){
787 push (@fields, $hostitem);
788 push (@hostitemnumbers, $hostfield->subfield('9'));
797 foreach my $field (@fields) {
798 next if ( $field->tag() < 10 );
800 my @subf = $field->subfields or (); # don't use ||, as that forces $field->subfelds to be interpreted in scalar context
802 # loop through each subfield
804 foreach my $subfield (@subf){
805 my $subfieldcode = $subfield->[0];
806 my $subfieldvalue= $subfield->[1];
808 next if ($tagslib->{$field->tag()}->{$subfieldcode}->{tab} ne 10
809 && ($field->tag() ne $itemtagfield
810 && $subfieldcode ne $itemtagsubfield));
811 $witness{$subfieldcode} = $tagslib->{$field->tag()}->{$subfieldcode}->{lib} if ($tagslib->{$field->tag()}->{$subfieldcode}->{tab} eq 10);
812 if ($tagslib->{$field->tag()}->{$subfieldcode}->{tab} eq 10) {
813 $this_row{$subfieldcode} .= " | " if($this_row{$subfieldcode});
814 $this_row{$subfieldcode} .= GetAuthorisedValueDesc( $field->tag(),
815 $subfieldcode, $subfieldvalue, '', $tagslib)
819 if (($field->tag eq $branchtagfield) && ($subfieldcode eq $branchtagsubfield) && C4::Context->preference("IndependentBranches")) {
821 my $userenv = C4::Context->userenv();
822 unless (C4::Context->IsSuperLibrarian() or (($userenv->{'branch'} eq $subfieldvalue))){
823 $this_row{'nomod'} = 1;
826 $this_row{itemnumber} = $subfieldvalue if ($field->tag() eq $itemtagfield && $subfieldcode eq $itemtagsubfield);
828 if ( C4::Context->preference('EasyAnalyticalRecords') ) {
829 foreach my $hostitemnumber (@hostitemnumbers){
830 if ($this_row{itemnumber} eq $hostitemnumber){
831 $this_row{hostitemflag} = 1;
832 $this_row{hostbiblionumber}= GetBiblionumberFromItemnumber($hostitemnumber);
837 # my $countanalytics=GetAnalyticsCount($this_row{itemnumber});
838 # if ($countanalytics > 0){
839 # $this_row{countanalytics} = $countanalytics;
845 push(@big_array, \%this_row);
849 my ($holdingbrtagf,$holdingbrtagsubf) = &GetMarcFromKohaField("items.holdingbranch",$frameworkcode);
850 @big_array = sort {$a->{$holdingbrtagsubf} cmp $b->{$holdingbrtagsubf}} @big_array;
852 # now, construct template !
853 # First, the existing items for display
855 my @header_value_loop;
856 for my $row ( @big_array ) {
858 my @item_fields = map +{ field => $_ || '' }, @$row{ sort keys(%witness) };
859 $row_data{item_value} = [ @item_fields ];
860 $row_data{itemnumber} = $row->{itemnumber};
861 #reporting this_row values
862 $row_data{'nomod'} = $row->{'nomod'};
863 $row_data{'hostitemflag'} = $row->{'hostitemflag'};
864 $row_data{'hostbiblionumber'} = $row->{'hostbiblionumber'};
865 # $row_data{'countanalytics'} = $row->{'countanalytics'};
866 push(@item_value_loop,\%row_data);
868 foreach my $subfield_code (sort keys(%witness)) {
870 $header_value{header_value} = $witness{$subfield_code};
872 my $subfieldlib = $tagslib->{$itemtagfield}->{$subfield_code};
873 my $kohafield = $subfieldlib->{kohafield};
874 if ( $kohafield && $kohafield =~ /items.(.+)/ ) {
875 $header_value{column_name} = $1;
878 push(@header_value_loop, \%header_value);
881 # now, build the item form for entering a new item
885 my $pref_itemcallnumber = C4::Context->preference('itemcallnumber');
888 C4::Context->preference('IndependentBranches')
889 && C4::Context->userenv
890 && !C4::Context->IsSuperLibrarian()
891 && C4::Context->userenv->{branch};
892 my $branch = $input->param('branch') || C4::Context->userenv->{branch};
893 my $branches = GetBranchesLoop($branch,$onlymine); # build once ahead of time, instead of multiple times later.
895 # We generate form, from actuel record
898 foreach my $field ($itemrecord->fields()){
899 my $tag = $field->{_tag};
900 foreach my $subfield ( $field->subfields() ){
902 my $subfieldtag = $subfield->[0];
903 my $value = $subfield->[1];
904 my $subfieldlib = $tagslib->{$tag}->{$subfieldtag};
906 next if subfield_is_koha_internal_p($subfieldtag);
907 next if ($tagslib->{$tag}->{$subfieldtag}->{'tab'} ne "10");
909 my $subfield_data = generate_subfield_form($tag, $subfieldtag, $value, $tagslib, $subfieldlib, $branches, $biblionumber, $temp, \@loop_data, $i, $restrictededition);
910 push @fields, "$tag$subfieldtag";
911 push (@loop_data, $subfield_data);
917 # and now we add fields that are empty
919 # Using last created item if it exists
921 $itemrecord = $cookieitemrecord if ($prefillitem and not $justaddeditem and $op ne "edititem");
923 # We generate form, and fill with values if defined
924 foreach my $tag ( keys %{$tagslib}){
925 foreach my $subtag (keys %{$tagslib->{$tag}}){
926 next if subfield_is_koha_internal_p($subtag);
927 next if ($tagslib->{$tag}->{$subtag}->{'tab'} ne "10");
928 next if any { /^$tag$subtag$/ } @fields;
930 my @values = (undef);
931 @values = $itemrecord->field($tag)->subfield($subtag) if ($itemrecord && defined($itemrecord->field($tag)) && defined($itemrecord->field($tag)->subfield($subtag)));
932 for my $value (@values){
933 my $subfield_data = generate_subfield_form($tag, $subtag, $value, $tagslib, $tagslib->{$tag}->{$subtag}, $branches, $biblionumber, $temp, \@loop_data, $i, $restrictededition);
934 push (@loop_data, $subfield_data);
939 @loop_data = sort {$a->{subfield} cmp $b->{subfield} } @loop_data;
941 # what's the next op ? it's what we are not in : an add if we're editing, otherwise, and edit.
943 biblionumber => $biblionumber,
944 title => $oldrecord->{title},
945 author => $oldrecord->{author},
946 item_loop => \@item_value_loop,
947 item_header_loop => \@header_value_loop,
949 itemnumber => $itemnumber,
950 barcode => GetBarcodeFromItemnumber($itemnumber),
951 itemtagfield => $itemtagfield,
952 itemtagsubfield => $itemtagsubfield,
954 opisadd => ($nextop eq "saveitem") ? 0 : 1,
955 popup => $input->param('popup') ? 1: 0,
956 C4::Search::enabled_staff_search_views,
958 $template->{'VARS'}->{'searchid'} = $searchid;
960 if ($frameworkcode eq 'FA'){
961 # fast cataloguing datas
963 'circborrowernumber' => $fa_circborrowernumber,
964 'barcode' => $fa_barcode,
965 'branch' => $fa_branch,
966 'stickyduedate' => $fa_stickyduedate,
967 'duedatespec' => $fa_duedatespec,
971 foreach my $error (@errors) {
972 $template->param($error => 1);
974 output_html_with_http_headers $input, $cookie, $template->output;