Bug 17894 - Remove and replace WriteOffFee
[koha.git] / tools / batchMod.pl
blob2920da9eb37cda9fe2ca158f052b8a0f83fe9501
1 #!/usr/bin/perl
4 # Copyright 2000-2002 Katipo Communications
6 # This file is part of Koha.
8 # Koha is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
21 use CGI qw ( -utf8 );
22 use strict;
23 #use warnings; FIXME - Bug 2505
24 use C4::Auth;
25 use C4::Output;
26 use C4::Biblio;
27 use C4::Items;
28 use C4::Circulation;
29 use C4::Context;
30 use C4::Koha;
31 use C4::BackgroundJob;
32 use C4::ClassSource;
33 use C4::Debug;
34 use C4::Members;
35 use MARC::File::XML;
36 use List::MoreUtils qw/uniq/;
37 use Koha::DateUtils;
39 my $input = new CGI;
40 my $dbh = C4::Context->dbh;
41 my $error = $input->param('error');
42 my @itemnumbers = $input->multi_param('itemnumber');
43 my $biblionumber = $input->param('biblionumber');
44 my $op = $input->param('op');
45 my $del = $input->param('del');
46 my $del_records = $input->param('del_records');
47 my $completedJobID = $input->param('completedJobID');
48 my $runinbackground = $input->param('runinbackground');
49 my $src = $input->param('src');
50 my $use_default_values = $input->param('use_default_values');
52 my $template_name;
53 my $template_flag;
54 if (!defined $op) {
55 $template_name = "tools/batchMod.tt";
56 $template_flag = { tools => '*' };
57 $op = q{};
58 } else {
59 $template_name = ($del) ? "tools/batchMod-del.tt" : "tools/batchMod-edit.tt";
60 $template_flag = ($del) ? { tools => 'items_batchdel' } : { tools => 'items_batchmod' };
64 my ($template, $loggedinuser, $cookie)
65 = get_template_and_user({template_name => $template_name,
66 query => $input,
67 type => "intranet",
68 authnotrequired => 0,
69 flagsrequired => $template_flag,
70 });
72 # Does the user have a restricted item edition permission?
73 my $uid = $loggedinuser ? GetMember( borrowernumber => $loggedinuser )->{userid} : undef;
74 my $restrictededition = $uid ? haspermission($uid, {'tools' => 'items_batchmod_restricted'}) : undef;
75 # In case user is a superlibrarian, edition is not restricted
76 $restrictededition = 0 if ($restrictededition != 0 && C4::Context->IsSuperLibrarian());
78 $template->param(del => $del);
80 my $itemrecord;
81 my $nextop="";
82 my @errors; # store errors found while checking data BEFORE saving item.
83 my $items_display_hashref;
84 our $tagslib = &GetMarcStructure(1);
86 my $deleted_items = 0; # Number of deleted items
87 my $deleted_records = 0; # Number of deleted records ( with no items attached )
88 my $not_deleted_items = 0; # Number of items that could not be deleted
89 my @not_deleted; # List of the itemnumbers that could not be deleted
90 my $modified_items = 0; # Numbers of modified items
91 my $modified_fields = 0; # Numbers of modified fields
93 my %cookies = parse CGI::Cookie($cookie);
94 my $sessionID = $cookies{'CGISESSID'}->value;
97 #--- ----------------------------------------------------------------------------
98 if ($op eq "action") {
99 #-------------------------------------------------------------------------------
100 my @tags = $input->multi_param('tag');
101 my @subfields = $input->multi_param('subfield');
102 my @values = $input->multi_param('field_value');
103 my @disabled = $input->multi_param('disable_input');
104 # build indicator hash.
105 my @ind_tag = $input->multi_param('ind_tag');
106 my @indicator = $input->multi_param('indicator');
108 # Is there something to modify ?
109 # TODO : We shall use this var to warn the user in case no modification was done to the items
110 my $values_to_modify = scalar(grep {!/^$/} @values);
111 my $values_to_blank = scalar(@disabled);
112 my $marcitem;
114 # Once the job is done
115 if ($completedJobID) {
116 # If we have a reasonable amount of items, we display them
117 if (scalar(@itemnumbers) <= ( C4::Context->preference("MaxItemsToDisplayForBatchDel") // 1000 ) ) {
118 $items_display_hashref=BuildItemsData(@itemnumbers);
119 } else {
120 # Else, we only display the barcode
121 my @simple_items_display = map {{ itemnumber => $_, barcode => (GetBarcodeFromItemnumber($_) or ""), biblionumber => (GetBiblionumberFromItemnumber($_) or "") }} @itemnumbers;
122 $template->param("simple_items_display" => \@simple_items_display);
125 # Setting the job as done
126 my $job = C4::BackgroundJob->fetch($sessionID, $completedJobID);
128 # Calling the template
129 add_saved_job_results_to_template($template, $completedJobID);
131 } else {
132 # While the job is getting done
134 # Job size is the number of items we have to process
135 my $job_size = scalar(@itemnumbers);
136 my $job = undef;
138 # If we asked for background processing
139 if ($runinbackground) {
140 $job = put_in_background($job_size);
143 #initializing values for updates
144 my ( $itemtagfield, $itemtagsubfield) = &GetMarcFromKohaField("items.itemnumber", "");
145 if ($values_to_modify){
146 my $xml = TransformHtmlToXml(\@tags,\@subfields,\@values,\@indicator,\@ind_tag, 'ITEM');
147 $marcitem = MARC::Record::new_from_xml($xml, 'UTF-8');
149 if ($values_to_blank){
150 foreach my $disabledsubf (@disabled){
151 if ($marcitem && $marcitem->field($itemtagfield)){
152 $marcitem->field($itemtagfield)->update( $disabledsubf => "" );
154 else {
155 $marcitem = MARC::Record->new();
156 $marcitem->append_fields( MARC::Field->new( $itemtagfield, '', '', $disabledsubf => "" ) );
161 # For each item
162 my $i = 1;
163 foreach my $itemnumber(@itemnumbers){
165 $job->progress($i) if $runinbackground;
166 my $itemdata = GetItem($itemnumber);
167 if ( $del ){
168 my $return = DelItemCheck( $itemdata->{'biblionumber'}, $itemdata->{'itemnumber'});
169 if ($return == 1) {
170 $deleted_items++;
171 } else {
172 $not_deleted_items++;
173 push @not_deleted,
174 { biblionumber => $itemdata->{'biblionumber'},
175 itemnumber => $itemdata->{'itemnumber'},
176 barcode => $itemdata->{'barcode'},
177 title => $itemdata->{'title'},
178 $return => 1
182 # If there are no items left, delete the biblio
183 if ( $del_records ) {
184 my $itemscount = GetItemsCount($itemdata->{'biblionumber'});
185 if ( $itemscount == 0 ) {
186 my $error = DelBiblio($itemdata->{'biblionumber'});
187 $deleted_records++ unless ( $error );
190 } else {
191 if ($values_to_modify || $values_to_blank) {
192 my $localmarcitem = Item2Marc($itemdata);
194 my $modified = UpdateMarcWith( $marcitem, $localmarcitem );
195 if ( $modified ) {
196 eval {
197 if ( my $item = ModItemFromMarc( $localmarcitem, $itemdata->{biblionumber}, $itemnumber ) ) {
198 LostItem($itemnumber, 'MARK RETURNED') if $item->{itemlost} and not $itemdata->{itemlost};
202 if ( $runinbackground ) {
203 $modified_items++ if $modified;
204 $modified_fields += $modified;
205 $job->set({
206 modified_items => $modified_items,
207 modified_fields => $modified_fields,
212 $i++;
217 #-------------------------------------------------------------------------------
218 # build screen with existing items. and "new" one
219 #-------------------------------------------------------------------------------
221 if ($op eq "show"){
222 my $filefh = $input->upload('uploadfile');
223 my $filecontent = $input->param('filecontent');
224 my @notfoundbarcodes;
226 my @contentlist;
227 if ($filefh){
228 while (my $content=<$filefh>){
229 $content =~ s/[\r\n]*$//;
230 push @contentlist, $content if $content;
233 if ($filecontent eq 'barcode_file') {
234 foreach my $barcode (@contentlist) {
236 my $itemnumber = GetItemnumberFromBarcode($barcode);
237 if ($itemnumber) {
238 push @itemnumbers,$itemnumber;
239 } else {
240 push @notfoundbarcodes, $barcode;
244 elsif ( $filecontent eq 'itemid_file') {
245 @itemnumbers = @contentlist;
247 } else {
248 if (defined $biblionumber){
249 my @all_items = GetItemsInfo( $biblionumber );
250 foreach my $itm (@all_items) {
251 push @itemnumbers, $itm->{itemnumber};
254 if ( my $list=$input->param('barcodelist')){
255 push my @barcodelist, uniq( split(/\s\n/, $list) );
257 foreach my $barcode (@barcodelist) {
259 my $itemnumber = GetItemnumberFromBarcode($barcode);
260 if ($itemnumber) {
261 push @itemnumbers,$itemnumber;
262 } else {
263 push @notfoundbarcodes, $barcode;
270 # Flag to tell the template there are valid results, hidden or not
271 if(scalar(@itemnumbers) > 0){ $template->param("itemresults" => 1); }
272 # Only display the items if there are no more than pref MaxItemsToProcessForBatchMod or MaxItemsToDisplayForBatchDel
273 my $max_items = $del
274 ? C4::Context->preference("MaxItemsToDisplayForBatchDel")
275 : C4::Context->preference("MaxItemsToProcessForBatchMod");
276 if (scalar(@itemnumbers) <= ( $max_items // 1000 ) ) {
277 $items_display_hashref=BuildItemsData(@itemnumbers);
278 } else {
279 $template->param("too_many_items" => scalar(@itemnumbers));
280 # Even if we do not display the items, we need the itemnumbers
281 $template->param(itemnumbers_array => \@itemnumbers);
283 # now, build the item form for entering a new item
284 my @loop_data =();
285 my $i=0;
286 my $branch_limit = C4::Context->userenv ? C4::Context->userenv->{"branch"} : "";
287 my $query = qq{SELECT authorised_value, lib FROM authorised_values};
288 $query .= qq{ LEFT JOIN authorised_values_branches ON ( id = av_id ) } if $branch_limit;
289 $query .= qq{ WHERE category = ?};
290 $query .= qq{ AND ( branchcode = ? OR branchcode IS NULL ) } if $branch_limit;
291 $query .= qq{ GROUP BY lib ORDER BY lib, lib_opac};
292 my $authorised_values_sth = $dbh->prepare( $query );
294 my $libraries = Koha::Libraries->search({}, { order_by => ['branchname'] })->unblessed;# build once ahead of time, instead of multiple times later.
296 # Adding a default choice, in case the user does not want to modify the branch
297 my $nochange_branch = { branchname => '', value => '', selected => 1 };
298 unshift (@$libraries, $nochange_branch);
300 my $pref_itemcallnumber = C4::Context->preference('itemcallnumber');
302 # Getting list of subfields to keep when restricted batchmod edit is enabled
303 my $subfieldsToAllowForBatchmod = C4::Context->preference('SubfieldsToAllowForRestrictedBatchmod');
304 my $allowAllSubfields = (
305 not defined $subfieldsToAllowForBatchmod
306 or $subfieldsToAllowForBatchmod == q||
307 ) ? 1 : 0;
308 my @subfieldsToAllow = split(/ /, $subfieldsToAllowForBatchmod);
310 foreach my $tag (sort keys %{$tagslib}) {
311 # loop through each subfield
312 foreach my $subfield (sort keys %{$tagslib->{$tag}}) {
313 next if IsMarcStructureInternal( $tagslib->{$tag}{$subfield} );
314 next if (not $allowAllSubfields and $restrictededition && !grep { $tag . '$' . $subfield eq $_ } @subfieldsToAllow );
315 next if ($tagslib->{$tag}->{$subfield}->{'tab'} ne "10");
316 # barcode and stocknumber are not meant to be batch-modified
317 next if $tagslib->{$tag}->{$subfield}->{'kohafield'} eq 'items.barcode';
318 next if $tagslib->{$tag}->{$subfield}->{'kohafield'} eq 'items.stocknumber';
319 my %subfield_data;
321 my $index_subfield = int(rand(1000000));
322 if ($subfield eq '@'){
323 $subfield_data{id} = "tag_".$tag."_subfield_00_".$index_subfield;
324 } else {
325 $subfield_data{id} = "tag_".$tag."_subfield_".$subfield."_".$index_subfield;
327 $subfield_data{tag} = $tag;
328 $subfield_data{subfield} = $subfield;
329 $subfield_data{marc_lib} ="<span id=\"error$i\" title=\"".$tagslib->{$tag}->{$subfield}->{lib}."\">".$tagslib->{$tag}->{$subfield}->{lib}."</span>";
330 $subfield_data{mandatory} = $tagslib->{$tag}->{$subfield}->{mandatory};
331 $subfield_data{repeatable} = $tagslib->{$tag}->{$subfield}->{repeatable};
332 my ($x,$value);
333 $value =~ s/"/&quot;/g;
334 if ( !$value && $use_default_values) {
335 $value = $tagslib->{$tag}->{$subfield}->{defaultvalue};
336 # get today date & replace YYYY, MM, DD if provided in the default value
337 my $today = dt_from_string;
338 my $year = $today->year;
339 my $month = $today->month;
340 my $day = $today->day;
341 $value =~ s/YYYY/$year/g;
342 $value =~ s/MM/$month/g;
343 $value =~ s/DD/$day/g;
345 $subfield_data{visibility} = "display:none;" if (($tagslib->{$tag}->{$subfield}->{hidden} > 4) || ($tagslib->{$tag}->{$subfield}->{hidden} < -4));
346 # testing branch value if IndependentBranches.
348 if ( $tagslib->{$tag}->{$subfield}->{authorised_value} ) {
349 my @authorised_values;
350 my %authorised_lib;
351 # builds list, depending on authorised value...
353 if ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "branches" ) {
354 foreach my $library (@$libraries) {
355 push @authorised_values, $library->{branchcode};
356 $authorised_lib{$library->{branchcode}} = $library->{branchname};
358 $value = "";
360 elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes" ) {
361 push @authorised_values, "";
362 my $itemtypes = GetItemTypes( style => 'array' );
363 for my $itemtype ( @$itemtypes ) {
364 push @authorised_values, $itemtype->{itemtype};
365 $authorised_lib{$itemtype->{itemtype}} = $itemtype->{translated_description};
367 $value = "";
369 #---- class_sources
371 elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "cn_source" ) {
372 push @authorised_values, "" unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
374 my $class_sources = GetClassSources();
375 my $default_source = C4::Context->preference("DefaultClassificationSource");
377 foreach my $class_source (sort keys %$class_sources) {
378 next unless $class_sources->{$class_source}->{'used'} or
379 ($value and $class_source eq $value) or
380 ($class_source eq $default_source);
381 push @authorised_values, $class_source;
382 $authorised_lib{$class_source} = $class_sources->{$class_source}->{'description'};
384 $value = '';
386 #---- "true" authorised value
388 else {
389 push @authorised_values, ""; # unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
390 $authorised_values_sth->execute( $tagslib->{$tag}->{$subfield}->{authorised_value}, $branch_limit ? $branch_limit : () );
391 while ( my ( $value, $lib ) = $authorised_values_sth->fetchrow_array ) {
392 push @authorised_values, $value;
393 $authorised_lib{$value} = $lib;
395 $value="";
397 $subfield_data{marc_value} = {
398 type => 'select',
399 id => "tag_".$tag."_subfield_".$subfield."_".$index_subfield,
400 name => "field_value",
401 values => \@authorised_values,
402 labels => \%authorised_lib,
403 default => $value,
405 # it's a thesaurus / authority field
407 elsif ( $tagslib->{$tag}->{$subfield}->{authtypecode} ) {
408 $subfield_data{marc_value} = {
409 type => 'text1',
410 id => $subfield_data{id},
411 value => $value,
412 authtypecode => $tagslib->{$tag}->{$subfield}->{authtypecode},
415 elsif ( $tagslib->{$tag}->{$subfield}->{value_builder} ) { # plugin
416 require Koha::FrameworkPlugin;
417 my $plugin = Koha::FrameworkPlugin->new( {
418 name => $tagslib->{$tag}->{$subfield}->{'value_builder'},
419 item_style => 1,
421 my $temp;
422 my $pars= { dbh => $dbh, record => $temp, tagslib => $tagslib,
423 id => $subfield_data{id}, tabloop => \@loop_data };
424 $plugin->build( $pars );
425 if( !$plugin->errstr ) {
426 $subfield_data{marc_value} = {
427 type => 'text2',
428 id => $subfield_data{id},
429 value => $value,
430 javascript => $plugin->javascript,
431 noclick => $plugin->noclick,
433 } else {
434 warn $plugin->errstr;
435 $subfield_data{marc_value} = { # supply default input form
436 type => 'text',
437 id => $subfield_data{id},
438 value => $value,
442 elsif ( $tag eq '' ) { # it's an hidden field
443 $subfield_data{marc_value} = {
444 type => 'hidden',
445 id => $subfield_data{id},
446 value => $value,
449 elsif ( $tagslib->{$tag}->{$subfield}->{'hidden'} ) { # FIXME: shouldn't input type be "hidden" ?
450 $subfield_data{marc_value} = {
451 type => 'text',
452 id => $subfield_data{id},
453 value => $value,
456 elsif ( length($value) > 100
457 or (C4::Context->preference("marcflavour") eq "UNIMARC" and
458 300 <= $tag && $tag < 400 && $subfield eq 'a' )
459 or (C4::Context->preference("marcflavour") eq "MARC21" and
460 500 <= $tag && $tag < 600 )
462 # oversize field (textarea)
463 $subfield_data{marc_value} = {
464 type => 'textarea',
465 id => $subfield_data{id},
466 value => $value,
468 } else {
469 # it's a standard field
470 $subfield_data{marc_value} = {
471 type => 'text',
472 id => $subfield_data{id},
473 value => $value,
476 # $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\">";
477 push (@loop_data, \%subfield_data);
478 $i++
480 } # -- End foreach tag
481 $authorised_values_sth->finish;
485 # what's the next op ? it's what we are not in : an add if we're editing, otherwise, and edit.
486 $template->param(item => \@loop_data);
487 if (@notfoundbarcodes) {
488 my @notfoundbarcodesloop = map{{barcode=>$_}}@notfoundbarcodes;
489 $template->param(notfoundbarcodes => \@notfoundbarcodesloop);
491 $nextop="action"
492 } # -- End action="show"
494 $template->param(%$items_display_hashref) if $items_display_hashref;
495 $template->param(
496 op => $nextop,
498 $template->param( $op => 1 ) if $op;
500 if ($op eq "action") {
502 #my @not_deleted_loop = map{{itemnumber=>$_}}@not_deleted;
504 $template->param(
505 not_deleted_items => $not_deleted_items,
506 deleted_items => $deleted_items,
507 delete_records => $del_records,
508 deleted_records => $deleted_records,
509 not_deleted_loop => \@not_deleted
513 foreach my $error (@errors) {
514 $template->param($error => 1) if $error;
516 $template->param(src => $src);
517 $template->param(biblionumber => $biblionumber);
518 output_html_with_http_headers $input, $cookie, $template->output;
519 exit;
522 # ---------------- Functions
524 sub BuildItemsData{
525 my @itemnumbers=@_;
526 # now, build existiing item list
527 my %witness; #---- stores the list of subfields used at least once, with the "meaning" of the code
528 my @big_array;
529 #---- finds where items.itemnumber is stored
530 my ( $itemtagfield, $itemtagsubfield) = &GetMarcFromKohaField("items.itemnumber", "");
531 my ($branchtagfield, $branchtagsubfield) = &GetMarcFromKohaField("items.homebranch", "");
532 foreach my $itemnumber (@itemnumbers){
533 my $itemdata=GetItem($itemnumber);
534 my $itemmarc=Item2Marc($itemdata);
535 my %this_row;
536 foreach my $field (grep {$_->tag() eq $itemtagfield} $itemmarc->fields()) {
537 # loop through each subfield
538 my $itembranchcode=$field->subfield($branchtagsubfield);
539 if ($itembranchcode && C4::Context->preference("IndependentBranches")) {
540 #verifying rights
541 my $userenv = C4::Context->userenv();
542 unless (C4::Context->IsSuperLibrarian() or (($userenv->{'branch'} eq $itembranchcode))){
543 $this_row{'nomod'}=1;
546 my $tag=$field->tag();
547 foreach my $subfield ($field->subfields) {
548 my ($subfcode,$subfvalue)=@$subfield;
549 next if ($tagslib->{$tag}->{$subfcode}->{tab} ne 10
550 && $tag ne $itemtagfield
551 && $subfcode ne $itemtagsubfield);
553 $witness{$subfcode} = $tagslib->{$tag}->{$subfcode}->{lib} if ($tagslib->{$tag}->{$subfcode}->{tab} eq 10);
554 if ($tagslib->{$tag}->{$subfcode}->{tab} eq 10) {
555 $this_row{$subfcode}=GetAuthorisedValueDesc( $tag,
556 $subfcode, $subfvalue, '', $tagslib)
557 || $subfvalue;
560 $this_row{itemnumber} = $subfvalue if ($tag eq $itemtagfield && $subfcode eq $itemtagsubfield);
564 # grab title, author, and ISBN to identify bib that the item
565 # belongs to in the display
566 my $biblio=GetBiblioData($$itemdata{biblionumber});
567 $this_row{title} = $biblio->{title};
568 $this_row{author} = $biblio->{author};
569 $this_row{isbn} = $biblio->{isbn};
570 $this_row{biblionumber} = $biblio->{biblionumber};
572 if (%this_row) {
573 push(@big_array, \%this_row);
576 @big_array = sort {$a->{0} cmp $b->{0}} @big_array;
578 # now, construct template !
579 # First, the existing items for display
580 my @item_value_loop;
581 my @witnesscodessorted=sort keys %witness;
582 for my $row ( @big_array ) {
583 my %row_data;
584 my @item_fields = map +{ field => $_ || '' }, @$row{ @witnesscodessorted };
585 $row_data{item_value} = [ @item_fields ];
586 $row_data{itemnumber} = $row->{itemnumber};
587 #reporting this_row values
588 $row_data{'nomod'} = $row->{'nomod'};
589 $row_data{bibinfo} = $row->{bibinfo};
590 $row_data{author} = $row->{author};
591 $row_data{title} = $row->{title};
592 $row_data{isbn} = $row->{isbn};
593 $row_data{biblionumber} = $row->{biblionumber};
594 my $is_on_loan = C4::Circulation::IsItemIssued( $row->{itemnumber} );
595 $row_data{onloan} = $is_on_loan ? 1 : 0;
596 push(@item_value_loop,\%row_data);
598 my @header_loop=map { { header_value=> $witness{$_}} } @witnesscodessorted;
600 return { item_loop => \@item_value_loop, item_header_loop => \@header_loop };
603 #BE WARN : it is not the general case
604 # This function can be OK in the item marc record special case
605 # Where subfield is not repeated
606 # And where we are sure that field should correspond
607 # And $tag>10
608 sub UpdateMarcWith {
609 my ($marcfrom,$marcto)=@_;
610 my ( $itemtag, $itemtagsubfield) = &GetMarcFromKohaField("items.itemnumber", "");
611 my $fieldfrom=$marcfrom->field($itemtag);
612 my @fields_to=$marcto->field($itemtag);
613 my $modified = 0;
614 foreach my $subfield ( $fieldfrom->subfields() ) {
615 foreach my $field_to_update ( @fields_to ) {
616 if ( $subfield->[1] ) {
617 unless ( $field_to_update->subfield($subfield->[0]) eq $subfield->[1] ) {
618 $modified++;
619 $field_to_update->update( $subfield->[0] => $subfield->[1] );
622 else {
623 $modified++;
624 $field_to_update->delete_subfield( code => $subfield->[0] );
628 return $modified;
631 sub find_value {
632 my ($tagfield,$insubfield,$record) = @_;
633 my $result;
634 my $indicator;
635 foreach my $field ($record->field($tagfield)) {
636 my @subfields = $field->subfields();
637 foreach my $subfield (@subfields) {
638 if (@$subfield[0] eq $insubfield) {
639 $result .= @$subfield[1];
640 $indicator = $field->indicator(1).$field->indicator(2);
644 return($indicator,$result);
647 # ----------------------------
648 # Background functions
651 sub add_results_to_template {
652 my $template = shift;
653 my $results = shift;
654 $template->param(map { $_ => $results->{$_} } keys %{ $results });
657 sub add_saved_job_results_to_template {
658 my $template = shift;
659 my $completedJobID = shift;
660 my $job = C4::BackgroundJob->fetch($sessionID, $completedJobID);
661 my $results = $job->results();
662 add_results_to_template($template, $results);
664 my $fields = $job->get("modified_fields");
665 my $items = $job->get("modified_items");
666 $template->param(
667 modified_items => $items,
668 modified_fields => $fields,
672 sub put_in_background {
673 my $job_size = shift;
675 my $job = C4::BackgroundJob->new($sessionID, "test", '/cgi-bin/koha/tools/batchMod.pl', $job_size);
676 my $jobID = $job->id();
678 # fork off
679 if (my $pid = fork) {
680 # parent
681 # return job ID as JSON
683 # prevent parent exiting from
684 # destroying the kid's database handle
685 # FIXME: according to DBI doc, this may not work for Oracle
686 $dbh->{InactiveDestroy} = 1;
688 my $reply = CGI->new("");
689 print $reply->header(-type => 'text/html');
690 print '{"jobID":"' . $jobID . '"}';
691 exit 0;
692 } elsif (defined $pid) {
693 # child
694 # close STDOUT to signal to Apache that
695 # we're now running in the background
696 close STDOUT;
697 close STDERR;
698 } else {
699 # fork failed, so exit immediately
700 warn "fork failed while attempting to run tools/batchMod.pl as a background job";
701 exit 0;
703 return $job;