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;
148 $subfield_data{visibility
} = "display:none;" if (($subfieldlib->{hidden
} > 4) || ($subfieldlib->{hidden
} <= -4));
150 my $pref_itemcallnumber = C4
::Context
->preference('itemcallnumber');
151 if (!$value && $subfieldlib->{kohafield
} eq 'items.itemcallnumber' && $pref_itemcallnumber) {
152 my $CNtag = substr($pref_itemcallnumber, 0, 3);
153 my $CNsubfield = substr($pref_itemcallnumber, 3, 1);
154 my $CNsubfield2 = substr($pref_itemcallnumber, 4, 1);
155 my $temp2 = $temp->field($CNtag);
157 $value = join ' ', $temp2->subfield($CNsubfield) || q{}, $temp2->subfield($CNsubfield2) || q{};
158 #remove any trailing space incase one subfield is used
159 $value =~ s/^\s+|\s+$//g;
163 if ($frameworkcode eq 'FA' && $subfieldlib->{kohafield
} eq 'items.barcode' && !$value){
165 $value = $input->param('barcode');
168 if ( $subfieldlib->{authorised_value
} ) {
169 my @authorised_values;
171 # builds list, depending on authorised value...
172 if ( $subfieldlib->{authorised_value
} eq "branches" ) {
173 foreach my $thisbranch (@
$branches) {
174 push @authorised_values, $thisbranch->{branchcode
};
175 $authorised_lib{$thisbranch->{branchcode
}} = $thisbranch->{branchname
};
176 $value = $thisbranch->{branchcode
} if $thisbranch->{selected
} && !$value;
179 elsif ( $subfieldlib->{authorised_value
} eq "itemtypes" ) {
180 push @authorised_values, "";
181 my $itemtypes = Koha
::ItemTypes
->search_with_localization;
182 while ( my $itemtype = $itemtypes->next ) {
183 push @authorised_values, $itemtype->itemtype;
184 $authorised_lib{$itemtype->itemtype} = $itemtype->translated_description;
188 my $itype_sth = $dbh->prepare("SELECT itemtype FROM biblioitems WHERE biblionumber = ?");
189 $itype_sth->execute( $biblionumber );
190 ( $value ) = $itype_sth->fetchrow_array;
195 elsif ( $subfieldlib->{authorised_value
} eq "cn_source" ) {
196 push @authorised_values, "";
198 my $class_sources = GetClassSources
();
199 my $default_source = C4
::Context
->preference("DefaultClassificationSource");
201 foreach my $class_source (sort keys %$class_sources) {
202 next unless $class_sources->{$class_source}->{'used'} or
203 ($value and $class_source eq $value) or
204 ($class_source eq $default_source);
205 push @authorised_values, $class_source;
206 $authorised_lib{$class_source} = $class_sources->{$class_source}->{'description'};
208 $value = $default_source unless ($value);
210 #---- "true" authorised value
213 push @authorised_values, qq{};
214 my $av = GetAuthorisedValues
( $subfieldlib->{authorised_value
} );
216 push @authorised_values, $r->{authorised_value
};
217 $authorised_lib{$r->{authorised_value
}} = $r->{lib
};
221 if ( $subfieldlib->{hidden
} > 4 or $subfieldlib->{hidden
} <= -4 ) {
222 $subfield_data{marc_value
} = {
224 id
=> $subfield_data{id
},
225 maxlength
=> $subfield_data{maxlength
},
230 $subfield_data{marc_value
} = {
232 id
=> "tag_".$tag."_subfield_".$subfieldtag."_".$index_subfield,
233 values => \
@authorised_values,
234 labels
=> \
%authorised_lib,
239 # it's a thesaurus / authority field
240 elsif ( $subfieldlib->{authtypecode
} ) {
241 $subfield_data{marc_value
} = {
243 id
=> $subfield_data{id
},
244 maxlength
=> $subfield_data{maxlength
},
246 authtypecode
=> $subfieldlib->{authtypecode
},
249 # it's a plugin field
250 elsif ( $subfieldlib->{value_builder
} ) { # plugin
251 require Koha
::FrameworkPlugin
;
252 my $plugin = Koha
::FrameworkPlugin
->new({
253 name
=> $subfieldlib->{'value_builder'},
256 my $pars= { dbh
=> $dbh, record
=> $temp, tagslib
=>$tagslib,
257 id
=> $subfield_data{id
}, tabloop
=> $loop_data };
258 $plugin->build( $pars );
259 if( !$plugin->errstr ) {
260 my $class= 'buttonDot'. ( $plugin->noclick?
' disabled': '' );
261 $subfield_data{marc_value
} = {
262 type
=> 'text_plugin',
263 id
=> $subfield_data{id
},
264 maxlength
=> $subfield_data{maxlength
},
267 nopopup
=> $plugin->noclick,
268 javascript
=> $plugin->javascript,
271 warn $plugin->errstr;
272 $subfield_data{marc_value
} = {
274 id
=> $subfield_data{id
},
275 maxlength
=> $subfield_data{maxlength
},
277 }; # supply default input form
280 elsif ( $tag eq '' ) { # it's an hidden field
281 $subfield_data{marc_value
} = {
283 id
=> $subfield_data{id
},
284 maxlength
=> $subfield_data{maxlength
},
288 elsif ( $subfieldlib->{'hidden'} ) { # FIXME: shouldn't input type be "hidden" ?
289 $subfield_data{marc_value
} = {
291 id
=> $subfield_data{id
},
292 maxlength
=> $subfield_data{maxlength
},
298 $value and length($value) > 100
301 C4
::Context
->preference("marcflavour") eq "UNIMARC"
302 and 300 <= $tag && $tag < 400 && $subfieldtag eq 'a'
305 C4
::Context
->preference("marcflavour") eq "MARC21"
306 and 500 <= $tag && $tag < 600
309 # oversize field (textarea)
310 $subfield_data{marc_value
} = {
312 id
=> $subfield_data{id
},
316 # it's a standard field
317 $subfield_data{marc_value
} = {
319 id
=> $subfield_data{id
},
320 maxlength
=> $subfield_data{maxlength
},
325 # Getting list of subfields to keep when restricted editing is enabled
326 my $subfieldsToAllowForRestrictedEditing = C4
::Context
->preference('SubfieldsToAllowForRestrictedEditing');
327 my $allowAllSubfields = (
328 not defined $subfieldsToAllowForRestrictedEditing
329 or $subfieldsToAllowForRestrictedEditing eq q
||
331 my @subfieldsToAllow = split(/ /, $subfieldsToAllowForRestrictedEditing);
333 # If we're on restricted editing, and our field is not in the list of subfields to allow,
334 # then it is read-only
335 $subfield_data{marc_value
}->{readonly
} = (
336 not $allowAllSubfields
337 and $restrictededition
338 and !grep { $tag . '$' . $subfieldtag eq $_ } @subfieldsToAllow
341 return \
%subfield_data;
344 # Removes some subfields when prefilling items
345 # This function will remove any subfield that is not in the SubfieldsToUseWhenPrefill syspref
346 sub removeFieldsForPrefill
{
351 my ($tag, $subtag) = GetMarcFromKohaField
("items.barcode", '');
353 # Getting list of subfields to keep
354 my $subfieldsToUseWhenPrefill = C4
::Context
->preference('SubfieldsToUseWhenPrefill');
356 # Removing subfields that are not in the syspref
357 if ($tag && $subfieldsToUseWhenPrefill) {
358 my $field = $item->field($tag);
359 my @subfieldsToUse= split(/ /,$subfieldsToUseWhenPrefill);
360 foreach my $subfield ($field->subfields()) {
361 if (!grep { $subfield->[0] eq $_ } @subfieldsToUse) {
362 $field->delete_subfield(code
=> $subfield->[0]);
373 my $error = $input->param('error');
374 my $biblionumber = $input->param('biblionumber');
375 my $itemnumber = $input->param('itemnumber');
376 my $op = $input->param('op') || q{};
377 my $hostitemnumber = $input->param('hostitemnumber');
378 my $marcflavour = C4
::Context
->preference("marcflavour");
379 my $searchid = $input->param('searchid');
380 # fast cataloguing datas
381 my $fa_circborrowernumber = $input->param('circborrowernumber');
382 my $fa_barcode = $input->param('barcode');
383 my $fa_branch = $input->param('branch');
384 my $fa_stickyduedate = $input->param('stickyduedate');
385 my $fa_duedatespec = $input->param('duedatespec');
387 my $frameworkcode = &GetFrameworkCode
($biblionumber);
389 # Defining which userflag is needing according to the framework currently used
391 if (defined $input->param('frameworkcode')) {
392 $userflags = ($input->param('frameworkcode') eq 'FA') ?
"fast_cataloging" : "edit_items";
395 if (not defined $userflags) {
396 $userflags = ($frameworkcode eq 'FA') ?
"fast_cataloging" : "edit_items";
399 my ($template, $loggedinuser, $cookie)
400 = get_template_and_user
({template_name
=> "cataloguing/additem.tt",
403 authnotrequired
=> 0,
404 flagsrequired
=> {editcatalogue
=> $userflags},
409 # Does the user have a restricted item editing permission?
410 my $uid = Koha
::Patrons
->find( $loggedinuser )->userid;
411 my $restrictededition = $uid ? haspermission
($uid, {'editcatalogue' => 'edit_items_restricted'}) : undef;
412 # In case user is a superlibrarian, editing is not restricted
413 $restrictededition = 0 if ($restrictededition != 0 && C4
::Context
->IsSuperLibrarian());
414 # In case user has fast cataloging permission (and we're in fast cataloging), editing is not restricted
415 $restrictededition = 0 if ($restrictededition != 0 && $frameworkcode eq 'FA' && haspermission
($uid, {'editcatalogue' => 'fast_cataloging'}));
417 my $tagslib = &GetMarcStructure
(1,$frameworkcode);
418 my $record = GetMarcBiblio
({ biblionumber
=> $biblionumber });
419 my $oldrecord = TransformMarcToKoha
($record);
421 my $nextop="additem";
422 my @errors; # store errors found while checking data BEFORE saving item.
424 # Getting last created item cookie
425 my $prefillitem = C4
::Context
->preference('PrefillItem');
427 my $cookieitemrecord;
429 my $lastitemcookie = $input->cookie('LastCreatedItem');
430 if ($lastitemcookie) {
431 $lastitemcookie = uri_unescape
($lastitemcookie);
433 if ( thaw
($lastitemcookie) ) {
434 $cookieitemrecord = thaw
($lastitemcookie);
435 $cookieitemrecord = removeFieldsForPrefill
($cookieitemrecord);
439 $lastitemcookie = 'undef' unless $lastitemcookie;
440 warn "Storable::thaw failed to thaw LastCreatedItem-cookie. Cookie value '$lastitemcookie'. Caught error follows: '$@'";
445 #-------------------------------------------------------------------------------
446 if ($op eq "additem") {
448 #-------------------------------------------------------------------------------
450 my @tags = $input->multi_param('tag');
451 my @subfields = $input->multi_param('subfield');
452 my @values = $input->multi_param('field_value');
453 # build indicator hash.
454 my @ind_tag = $input->multi_param('ind_tag');
455 my @indicator = $input->multi_param('indicator');
456 my $xml = TransformHtmlToXml
(\
@tags,\
@subfields,\
@values,\
@indicator,\
@ind_tag, 'ITEM');
457 my $record = MARC
::Record
::new_from_xml
($xml, 'UTF-8');
460 my $add_submit = $input->param('add_submit');
461 my $add_duplicate_submit = $input->param('add_duplicate_submit');
462 my $add_multiple_copies_submit = $input->param('add_multiple_copies_submit');
463 my $number_of_copies = $input->param('number_of_copies');
465 # This is a bit tricky : if there is a cookie for the last created item and
466 # we just added an item, the cookie value is not correct yet (it will be updated
467 # next page). To prevent the form from being filled with outdated values, we
468 # force the use of "add and duplicate" feature, so the form will be filled with
470 $add_duplicate_submit = 1 if ($prefillitem);
473 # if autoBarcode is set to 'incremental', calculate barcode...
474 if ( C4
::Context
->preference('autoBarcode') eq 'incremental' ) {
475 $record = _increment_barcode
($record, $frameworkcode);
478 my $addedolditem = TransformMarcToKoha
( $record );
480 # If we have to add or add & duplicate, we add the item
481 if ( $add_submit || $add_duplicate_submit ) {
483 # check for item barcode # being unique
484 my $exist_itemnumber = get_item_from_barcode
( $addedolditem->{'barcode'} );
485 push @errors, "barcode_not_unique" if ($exist_itemnumber);
487 # if barcode exists, don't create, but report The problem.
488 unless ($exist_itemnumber) {
489 my ( $oldbiblionumber, $oldbibnum, $oldbibitemnum ) = AddItemFromMarc
( $record, $biblionumber );
490 set_item_default_location
($oldbibitemnum);
492 # Pushing the last created item cookie back
493 if ($prefillitem && defined $record) {
494 my $itemcookie = $input->cookie(
495 -name
=> 'LastCreatedItem',
496 # We uri_escape the whole freezed structure so we're sure we won't have any encoding problems
497 -value
=> uri_escape_utf8
( freeze
( $record ) ),
502 $cookie = [ $cookie, $itemcookie ];
507 if ($exist_itemnumber) {
508 $itemrecord = $record;
512 # If we have to add & duplicate
513 if ($add_duplicate_submit) {
514 $itemrecord = $record;
515 if (C4
::Context
->preference('autoBarcode') eq 'incremental') {
516 $itemrecord = _increment_barcode
($itemrecord, $frameworkcode);
519 # we have to clear the barcode field in the duplicate item record to make way for the new one generated by the javascript plugin
520 my ($tagfield,$tagsubfield) = &GetMarcFromKohaField
("items.barcode",$frameworkcode);
521 my $fieldItem = $itemrecord->field($tagfield);
522 $itemrecord->delete_field($fieldItem);
523 $fieldItem->delete_subfields($tagsubfield);
524 $itemrecord->insert_fields_ordered($fieldItem);
526 $itemrecord = removeFieldsForPrefill
($itemrecord) if ($prefillitem);
529 # If we have to add multiple copies
530 if ($add_multiple_copies_submit) {
533 my $barcodeobj = C4
::Barcodes
->new;
534 my $copynumber = $addedolditem->{'copynumber'};
535 my $oldbarcode = $addedolditem->{'barcode'};
536 my ($tagfield,$tagsubfield) = &GetMarcFromKohaField
("items.barcode",$frameworkcode);
537 my ($copytagfield,$copytagsubfield) = &GetMarcFromKohaField
("items.copynumber",$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);
571 # Updating record with the new copynumber
573 $record->field($copytagfield)->update($copytagsubfield => $copynumber);
577 if (!$exist_itemnumber) {
578 my ($oldbiblionumber,$oldbibnum,$oldbibitemnum) = AddItemFromMarc
($record,$biblionumber);
579 set_item_default_location
($oldbibitemnum);
581 # We count the item only if it was really added
582 # That way, all items are added, even if there was some already existing barcodes
583 # FIXME : Please note that there is a risk of infinite loop here if we never find a suitable barcode
585 # Only increment copynumber if item was really added
586 $copynumber++ if ( $copynumber && $copynumber =~ m/^\d+$/ );
589 # Preparing the next iteration
590 $oldbarcode = $barcodevalue;
595 if ($frameworkcode eq 'FA' && $fa_circborrowernumber){
596 print $input->redirect(
597 '/cgi-bin/koha/circ/circulation.pl?'
598 .'borrowernumber='.$fa_circborrowernumber
599 .'&barcode='.uri_escape_utf8
($fa_barcode)
600 .'&duedatespec='.$fa_duedatespec
607 #-------------------------------------------------------------------------------
608 } elsif ($op eq "edititem") {
609 #-------------------------------------------------------------------------------
610 # retrieve item if exist => then, it's a modif
611 $itemrecord = C4
::Items
::GetMarcItem
($biblionumber,$itemnumber);
612 $nextop = "saveitem";
613 #-------------------------------------------------------------------------------
614 } elsif ($op eq "dupeitem") {
615 #-------------------------------------------------------------------------------
616 # retrieve item if exist => then, it's a modif
617 $itemrecord = C4
::Items
::GetMarcItem
($biblionumber,$itemnumber);
618 if (C4
::Context
->preference('autoBarcode') eq 'incremental') {
619 $itemrecord = _increment_barcode
($itemrecord, $frameworkcode);
622 # we have to clear the barcode field in the duplicate item record to make way for the new one generated by the javascript plugin
623 my ($tagfield,$tagsubfield) = &GetMarcFromKohaField
("items.barcode",$frameworkcode);
624 my $fieldItem = $itemrecord->field($tagfield);
625 $itemrecord->delete_field($fieldItem);
626 $fieldItem->delete_subfields($tagsubfield);
627 $itemrecord->insert_fields_ordered($fieldItem);
630 #check for hidden subfield and remove them for the duplicated item
631 foreach my $field ($itemrecord->fields()){
632 my $tag = $field->{_tag
};
633 foreach my $subfield ($field->subfields()){
634 my $subfieldtag = $subfield->[0];
635 if ($tagslib->{$tag}->{$subfieldtag}->{'tab'} ne "10"
636 || abs($tagslib->{$tag}->{$subfieldtag}->{hidden
})>4 ){
637 my $fieldItem = $itemrecord->field($tag);
638 $itemrecord->delete_field($fieldItem);
639 $fieldItem->delete_subfields($subfieldtag);
640 $itemrecord->insert_fields_ordered($fieldItem);
645 $itemrecord = removeFieldsForPrefill
($itemrecord) if ($prefillitem);
647 #-------------------------------------------------------------------------------
648 } elsif ($op eq "delitem") {
649 #-------------------------------------------------------------------------------
650 # check that there is no issue on this item before deletion.
651 $error = &DelItemCheck
( $biblionumber,$itemnumber);
653 print $input->redirect("additem.pl?biblionumber=$biblionumber&frameworkcode=$frameworkcode&searchid=$searchid");
658 #-------------------------------------------------------------------------------
659 } elsif ($op eq "delallitems") {
660 #-------------------------------------------------------------------------------
661 my @itemnumbers = Koha
::Items
->search({ biblionumber
=> $biblionumber })->get_column('itemnumber');
662 foreach my $itemnumber ( @itemnumbers ) {
663 $error = C4
::Items
::DelItemCheck
( $biblionumber, $itemnumber );
664 next if $error == 1; # Means ok
670 my $defaultview = C4
::Context
->preference('IntranetBiblioDefaultView');
671 my $views = { C4
::Search
::enabled_staff_search_views
};
672 if ($defaultview eq 'isbd' && $views->{can_view_ISBD
}) {
673 print $input->redirect("/cgi-bin/koha/catalogue/ISBDdetail.pl?biblionumber=$biblionumber&searchid=$searchid");
674 } elsif ($defaultview eq 'marc' && $views->{can_view_MARC
}) {
675 print $input->redirect("/cgi-bin/koha/catalogue/MARCdetail.pl?biblionumber=$biblionumber&searchid=$searchid");
676 } elsif ($defaultview eq 'labeled_marc' && $views->{can_view_labeledMARC
}) {
677 print $input->redirect("/cgi-bin/koha/catalogue/labeledMARCdetail.pl?biblionumber=$biblionumber&searchid=$searchid");
679 print $input->redirect("/cgi-bin/koha/catalogue/detail.pl?biblionumber=$biblionumber&searchid=$searchid");
683 #-------------------------------------------------------------------------------
684 } elsif ($op eq "saveitem") {
685 #-------------------------------------------------------------------------------
687 my @tags = $input->multi_param('tag');
688 my @subfields = $input->multi_param('subfield');
689 my @values = $input->multi_param('field_value');
690 # build indicator hash.
691 my @ind_tag = $input->multi_param('ind_tag');
692 my @indicator = $input->multi_param('indicator');
693 # my $itemnumber = $input->param('itemnumber');
694 my $xml = TransformHtmlToXml
(\
@tags,\
@subfields,\
@values,\
@indicator,\
@ind_tag,'ITEM');
695 my $itemtosave=MARC
::Record
::new_from_xml
($xml, 'UTF-8');
696 # MARC::Record builded => now, record in DB
697 # warn "R: ".$record->as_formatted;
698 # check that the barcode don't exist already
699 my $addedolditem = TransformMarcToKoha
($itemtosave);
700 my $exist_itemnumber = get_item_from_barcode
($addedolditem->{'barcode'});
701 if ($exist_itemnumber && $exist_itemnumber != $itemnumber) {
702 push @errors,"barcode_not_unique";
704 my $item = GetItem
( $itemnumber );
705 my $newitem = ModItemFromMarc
($itemtosave, $biblionumber, $itemnumber);
707 my $olditemlost = $item->{itemlost
};
708 my $newitemlost = $newitem->{itemlost
};
709 LostItem
( $item->{itemnumber
}, 'additem' )
710 if $newitemlost && $newitemlost ge '1' && !$olditemlost;
713 } elsif ($op eq "delinkitem"){
714 my $analyticfield = '773';
715 if ($marcflavour eq 'MARC21' || $marcflavour eq 'NORMARC'){
716 $analyticfield = '773';
717 } elsif ($marcflavour eq 'UNIMARC') {
718 $analyticfield = '461';
720 foreach my $field ($record->field($analyticfield)){
721 if ($field->subfield('9') eq $hostitemnumber){
722 $record->delete_field($field);
726 my $modbibresult = ModBiblio
($record, $biblionumber,'');
730 #-------------------------------------------------------------------------------
731 # build screen with existing items. and "new" one
732 #-------------------------------------------------------------------------------
734 # now, build existiing item list
735 my $temp = GetMarcBiblio
({ biblionumber
=> $biblionumber });
736 #my @fields = $record->fields();
739 my %witness; #---- stores the list of subfields used at least once, with the "meaning" of the code
741 #---- finds where items.itemnumber is stored
742 my ( $itemtagfield, $itemtagsubfield) = &GetMarcFromKohaField
("items.itemnumber", $frameworkcode);
743 my ($branchtagfield, $branchtagsubfield) = &GetMarcFromKohaField
("items.homebranch", $frameworkcode);
744 C4
::Biblio
::EmbedItemsInMarcBiblio
({
745 marc_record
=> $temp,
746 biblionumber
=> $biblionumber });
747 my @fields = $temp->fields();
751 if ( C4
::Context
->preference('EasyAnalyticalRecords') ) {
752 my $analyticfield = '773';
753 if ($marcflavour eq 'MARC21' || $marcflavour eq 'NORMARC') {
754 $analyticfield = '773';
755 } elsif ($marcflavour eq 'UNIMARC') {
756 $analyticfield = '461';
758 foreach my $hostfield ($temp->field($analyticfield)){
759 my $hostbiblionumber = $hostfield->subfield('0');
760 if ($hostbiblionumber){
761 my $hostrecord = GetMarcBiblio
({
762 biblionumber
=> $hostbiblionumber,
765 my ($itemfield, undef) = GetMarcFromKohaField
( 'items.itemnumber', GetFrameworkCode
($hostbiblionumber) );
766 foreach my $hostitem ($hostrecord->field($itemfield)){
767 if ($hostitem->subfield('9') eq $hostfield->subfield('9')){
768 push (@fields, $hostitem);
769 push (@hostitemnumbers, $hostfield->subfield('9'));
778 foreach my $field (@fields) {
779 next if ( $field->tag() < 10 );
781 my @subf = $field->subfields or (); # don't use ||, as that forces $field->subfelds to be interpreted in scalar context
783 # loop through each subfield
785 foreach my $subfield (@subf){
786 my $subfieldcode = $subfield->[0];
787 my $subfieldvalue= $subfield->[1];
789 next if ($tagslib->{$field->tag()}->{$subfieldcode}->{tab
} ne 10
790 && ($field->tag() ne $itemtagfield
791 && $subfieldcode ne $itemtagsubfield));
792 $witness{$subfieldcode} = $tagslib->{$field->tag()}->{$subfieldcode}->{lib
} if ($tagslib->{$field->tag()}->{$subfieldcode}->{tab
} eq 10);
793 if ($tagslib->{$field->tag()}->{$subfieldcode}->{tab
} eq 10) {
794 $this_row{$subfieldcode} .= " | " if($this_row{$subfieldcode});
795 $this_row{$subfieldcode} .= GetAuthorisedValueDesc
( $field->tag(),
796 $subfieldcode, $subfieldvalue, '', $tagslib)
800 if (($field->tag eq $branchtagfield) && ($subfieldcode eq $branchtagsubfield) && C4
::Context
->preference("IndependentBranches")) {
802 my $userenv = C4
::Context
->userenv();
803 unless (C4
::Context
->IsSuperLibrarian() or (($userenv->{'branch'} eq $subfieldvalue))){
804 $this_row{'nomod'} = 1;
807 $this_row{itemnumber
} = $subfieldvalue if ($field->tag() eq $itemtagfield && $subfieldcode eq $itemtagsubfield);
809 if ( C4
::Context
->preference('EasyAnalyticalRecords') ) {
810 foreach my $hostitemnumber (@hostitemnumbers) {
811 my $item = Koha
::Items
->find( $hostitemnumber );
812 if ($this_row{itemnumber
} eq $hostitemnumber) {
813 $this_row{hostitemflag
} = 1;
814 $this_row{hostbiblionumber
}= $item->biblio->biblionumber;
821 push(@big_array, \
%this_row);
825 my ($holdingbrtagf,$holdingbrtagsubf) = &GetMarcFromKohaField
("items.holdingbranch",$frameworkcode);
826 @big_array = sort {$a->{$holdingbrtagsubf} cmp $b->{$holdingbrtagsubf}} @big_array;
828 # now, construct template !
829 # First, the existing items for display
831 my @header_value_loop;
832 for my $row ( @big_array ) {
834 my @item_fields = map +{ field
=> $_ || '' }, @
$row{ sort keys(%witness) };
835 $row_data{item_value
} = [ @item_fields ];
836 $row_data{itemnumber
} = $row->{itemnumber
};
837 #reporting this_row values
838 $row_data{'nomod'} = $row->{'nomod'};
839 $row_data{'hostitemflag'} = $row->{'hostitemflag'};
840 $row_data{'hostbiblionumber'} = $row->{'hostbiblionumber'};
841 # $row_data{'countanalytics'} = $row->{'countanalytics'};
842 push(@item_value_loop,\
%row_data);
844 foreach my $subfield_code (sort keys(%witness)) {
846 $header_value{header_value
} = $witness{$subfield_code};
848 my $subfieldlib = $tagslib->{$itemtagfield}->{$subfield_code};
849 my $kohafield = $subfieldlib->{kohafield
};
850 if ( $kohafield && $kohafield =~ /items.(.+)/ ) {
851 $header_value{column_name
} = $1;
854 push(@header_value_loop, \
%header_value);
857 # now, build the item form for entering a new item
861 my $pref_itemcallnumber = C4
::Context
->preference('itemcallnumber');
863 my $branch = $input->param('branch') || C4
::Context
->userenv->{branch
};
864 my $libraries = Koha
::Libraries
->search({}, { order_by
=> ['branchname'] })->unblessed;# build once ahead of time, instead of multiple times later.
865 for my $library ( @
$libraries ) {
866 $library->{selected
} = 1 if $library->{branchcode
} eq $branch
869 # We generate form, from actuel record
872 foreach my $field ($itemrecord->fields()){
873 my $tag = $field->{_tag
};
874 foreach my $subfield ( $field->subfields() ){
876 my $subfieldtag = $subfield->[0];
877 my $value = $subfield->[1];
878 my $subfieldlib = $tagslib->{$tag}->{$subfieldtag};
880 next if ($tagslib->{$tag}->{$subfieldtag}->{'tab'} ne "10");
882 my $subfield_data = generate_subfield_form
($tag, $subfieldtag, $value, $tagslib, $subfieldlib, $libraries, $biblionumber, $temp, \
@loop_data, $i, $restrictededition);
883 push @fields, "$tag$subfieldtag";
884 push (@loop_data, $subfield_data);
890 # and now we add fields that are empty
892 # Using last created item if it exists
894 $itemrecord = $cookieitemrecord if ($prefillitem and not $justaddeditem and $op ne "edititem");
896 # We generate form, and fill with values if defined
897 foreach my $tag ( keys %{$tagslib}){
898 foreach my $subtag (keys %{$tagslib->{$tag}}){
899 next if IsMarcStructureInternal
($tagslib->{$tag}{$subtag});
900 next if ($tagslib->{$tag}->{$subtag}->{'tab'} ne "10");
901 next if any
{ /^$tag$subtag$/ } @fields;
903 my @values = (undef);
904 @values = $itemrecord->field($tag)->subfield($subtag) if ($itemrecord && defined($itemrecord->field($tag)) && defined($itemrecord->field($tag)->subfield($subtag)));
905 for my $value (@values){
906 my $subfield_data = generate_subfield_form
($tag, $subtag, $value, $tagslib, $tagslib->{$tag}->{$subtag}, $libraries, $biblionumber, $temp, \
@loop_data, $i, $restrictededition);
907 push (@loop_data, $subfield_data);
912 @loop_data = sort {$a->{subfield
} cmp $b->{subfield
} } @loop_data;
914 my $item = Koha
::Items
->find($itemnumber); # We certainly want to fetch it earlier
916 # what's the next op ? it's what we are not in : an add if we're editing, otherwise, and edit.
918 biblionumber
=> $biblionumber,
919 title
=> $oldrecord->{title
},
920 author
=> $oldrecord->{author
},
921 item_loop
=> \
@item_value_loop,
922 item_header_loop
=> \
@header_value_loop,
924 itemnumber
=> $itemnumber,
925 barcode
=> $item ?
$item->barcode : undef,
926 itemtagfield
=> $itemtagfield,
927 itemtagsubfield
=> $itemtagsubfield,
929 opisadd
=> ($nextop eq "saveitem") ?
0 : 1,
930 popup
=> scalar $input->param('popup') ?
1: 0,
931 C4
::Search
::enabled_staff_search_views
,
933 $template->{'VARS'}->{'searchid'} = $searchid;
935 if ($frameworkcode eq 'FA'){
936 # fast cataloguing datas
938 'circborrowernumber' => $fa_circborrowernumber,
939 'barcode' => $fa_barcode,
940 'branch' => $fa_branch,
941 'stickyduedate' => $fa_stickyduedate,
942 'duedatespec' => $fa_duedatespec,
946 foreach my $error (@errors) {
947 $template->param($error => 1);
949 output_html_with_http_headers
$input, $cookie, $template->output;