bug 6372: moved background-job.inc to background-job-progressbar.js
[koha.git] / tools / batchMod.pl
1 #!/usr/bin/perl
2
3
4 # Copyright 2000-2002 Katipo Communications
5 #
6 # This file is part of Koha.
7 #
8 # Koha is free software; you can redistribute it and/or modify it under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
11 # version.
12 #
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License along
18 # with Koha; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20
21 use CGI;
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; # XXX subfield_is_koha_internal_p
31 use C4::Branch; # XXX subfield_is_koha_internal_p
32 use C4::BackgroundJob;
33 use C4::ClassSource;
34 use C4::Dates;
35 use C4::Debug;
36 use MARC::File::XML;
37
38 my $input = new CGI;
39 my $dbh = C4::Context->dbh;
40 my $error        = $input->param('error');
41 my @itemnumbers  = $input->param('itemnumber');
42 my $op           = $input->param('op');
43 my $del          = $input->param('del');
44 my $del_records  = $input->param('del_records');
45 my $completedJobID = $input->param('completedJobID');
46 my $runinbackground = $input->param('runinbackground');
47
48
49 my $template_name;
50 my $template_flag;
51 if (!defined $op) {
52     $template_name = "tools/batchMod.tmpl";
53     $template_flag = { tools => '*' };
54 } else {
55     $template_name = ($del) ? "tools/batchMod-del.tmpl" : "tools/batchMod-edit.tmpl";
56     $template_flag = ($del) ? { tools => 'items_batchdel' }   : { tools => 'items_batchmod' };
57 }
58
59
60 my ($template, $loggedinuser, $cookie)
61     = get_template_and_user({template_name => $template_name,
62                  query => $input,
63                  type => "intranet",
64                  authnotrequired => 0,
65                  flagsrequired => $template_flag,
66                  });
67
68
69 my $today_iso = C4::Dates->today('iso');
70 $template->param(today_iso => $today_iso);
71 $template->param(del       => $del);
72
73 my $itemrecord;
74 my $nextop="";
75 my @errors; # store errors found while checking data BEFORE saving item.
76 my $items_display_hashref;
77 my $frameworkcode="";
78 my $tagslib = &GetMarcStructure(1,$frameworkcode);
79
80 my $deleted_items = 0;     # Number of deleted items
81 my $deleted_records = 0;   # Number of deleted records ( with no items attached )
82 my $not_deleted_items = 0; # Number of items that could not be deleted
83 my @not_deleted;           # List of the itemnumbers that could not be deleted
84
85 my %cookies = parse CGI::Cookie($cookie);
86 my $sessionID = $cookies{'CGISESSID'}->value;
87
88
89 #--- ----------------------------------------------------------------------------
90 if ($op eq "action") {
91 #-------------------------------------------------------------------------------
92     my @tags      = $input->param('tag');
93     my @subfields = $input->param('subfield');
94     my @values    = $input->param('field_value');
95     my @disabled  = $input->param('disable_input');
96     # build indicator hash.
97     my @ind_tag   = $input->param('ind_tag');
98     my @indicator = $input->param('indicator');
99
100     # Is there something to modify ?
101     # TODO : We shall use this var to warn the user in case no modification was done to the items
102     my $values_to_modify = scalar(grep {!/^$/} @values);
103     my $values_to_blank  = scalar(@disabled);
104     my $marcitem;
105
106     # Once the job is done
107     if ($completedJobID) {
108         # If we have a reasonable amount of items, we display them
109         if (scalar(@itemnumbers) <= 1000) {
110             $items_display_hashref=BuildItemsData(@itemnumbers);
111         } else {
112             # Else, we only display the barcode
113             my @simple_items_display = map {{ itemnumber => $_, barcode => (GetBarcodeFromItemnumber($_) or ""), biblionumber => (GetBiblionumberFromItemnumber($_) or "") }} @itemnumbers;
114             $template->param("simple_items_display" => \@simple_items_display);
115         }
116
117         # Setting the job as done
118         my $job = C4::BackgroundJob->fetch($sessionID, $completedJobID);
119
120         # Calling the template
121         add_saved_job_results_to_template($template, $completedJobID);
122
123     } else {
124     # While the job is getting done
125
126         # Job size is the number of items we have to process
127         my $job_size = scalar(@itemnumbers);
128         my $job = undef;
129
130         # If we asked for background processing
131         if ($runinbackground) {
132             $job = put_in_background($job_size);
133         }
134
135         #initializing values for updates
136         my (  $itemtagfield,   $itemtagsubfield) = &GetMarcFromKohaField("items.itemnumber", "");
137         if ($values_to_modify){
138             my $xml = TransformHtmlToXml(\@tags,\@subfields,\@values,\@indicator,\@ind_tag, 'ITEM');
139         utf8::encode($xml);
140             $marcitem = MARC::Record::new_from_xml($xml, 'UTF-8');
141         }
142         if ($values_to_blank){
143             foreach my $disabledsubf (@disabled){
144                 if ($marcitem && $marcitem->field($itemtagfield)){
145                     $marcitem->field($itemtagfield)->update( $disabledsubf => "" );
146                 }
147                 else {
148                     $marcitem = MARC::Record->new();
149                     $marcitem->append_fields( MARC::Field->new( $itemtagfield, '', '', $disabledsubf => "" ) );
150                 }
151             }
152         }
153
154         # For each item
155         my $i = 1; 
156         foreach my $itemnumber(@itemnumbers){
157
158                 $job->progress($i) if $runinbackground;
159                 my $itemdata = GetItem($itemnumber);
160                 if ($input->param("del")){
161                         my $return = DelItemCheck(C4::Context->dbh, $itemdata->{'biblionumber'}, $itemdata->{'itemnumber'});
162                         if ($return == 1) {
163                             $deleted_items++;
164                         } else {
165                             $not_deleted_items++;
166                             push @not_deleted,
167                                 { biblionumber => $itemdata->{'biblionumber'},
168                                   itemnumber => $itemdata->{'itemnumber'},
169                                   barcode => $itemdata->{'barcode'},
170                                   title => $itemdata->{'title'},
171                                   $return => 1
172                                 };
173                         }
174
175                         # If there are no items left, delete the biblio
176                         if ( $del_records ) {
177                             my $itemscount = GetItemsCount($itemdata->{'biblionumber'});
178                             if ( $itemscount == 0 ) {
179                                 my $error = DelBiblio($itemdata->{'biblionumber'});
180                                 $deleted_records++ unless ( $error );
181                             }
182                         }
183                 } else {
184                     if ($values_to_modify || $values_to_blank) {
185                         my $localmarcitem = Item2Marc($itemdata);
186                         UpdateMarcWith( $marcitem, $localmarcitem );
187                         eval{
188                             if ( my $item = ModItemFromMarc( $localmarcitem, $itemdata->{biblionumber}, $itemnumber ) ) {
189                                 LostItem($itemnumber, 'MARK RETURNED', 'CHARGE FEE') if $item->{itemlost};
190                             }
191                         };
192                     }
193                 }
194                 $i++;
195         }
196     }
197 }
198 #
199 #-------------------------------------------------------------------------------
200 # build screen with existing items. and "new" one
201 #-------------------------------------------------------------------------------
202
203 if ($op eq "show"){
204     my $filefh = $input->upload('uploadfile');
205     my $filecontent = $input->param('filecontent');
206     my @notfoundbarcodes;
207
208     my @contentlist;
209     if ($filefh){
210         while (my $content=<$filefh>){
211             $content =~ s/[\r\n]*$//;
212             push @contentlist, $content if $content;
213         }
214
215         if ($filecontent eq 'barcode_file') {
216             foreach my $barcode (@contentlist) {
217
218                 my $itemnumber = GetItemnumberFromBarcode($barcode);
219                 if ($itemnumber) {
220                     push @itemnumbers,$itemnumber;
221                 } else {
222                     push @notfoundbarcodes, $barcode;
223                 }
224             }
225         }
226         elsif ( $filecontent eq 'itemid_file') {
227             @itemnumbers = @contentlist;
228         }
229     } else {
230         if ( my $list=$input->param('barcodelist')){
231             push my @barcodelist, split(/\s\n/, $list);
232
233             foreach my $barcode (@barcodelist) {
234
235                 my $itemnumber = GetItemnumberFromBarcode($barcode);
236                 if ($itemnumber) {
237                     push @itemnumbers,$itemnumber;
238                 } else {
239                     push @notfoundbarcodes, $barcode;
240                 }
241             }
242
243         }
244     }
245
246     # Flag to tell the template there are valid results, hidden or not
247     if(scalar(@itemnumbers) > 0){ $template->param("itemresults" => 1); }
248     # Only display the items if there are no more than 1000
249     if (scalar(@itemnumbers) <= 1000) {
250         $items_display_hashref=BuildItemsData(@itemnumbers);
251     } else {
252         $template->param("too_many_items" => scalar(@itemnumbers));
253         # Even if we do not display the items, we need the itemnumbers
254         my @itemnumbers_hashref = map {{itemnumber => $_}} @itemnumbers;
255         $template->param("itemnumbers_hashref" => \@itemnumbers_hashref);
256     }
257 # now, build the item form for entering a new item
258 my @loop_data =();
259 my $i=0;
260 my $authorised_values_sth = $dbh->prepare("SELECT authorised_value,lib FROM authorised_values WHERE category=? ORDER BY lib");
261
262 my $branches = GetBranchesLoop();  # build once ahead of time, instead of multiple times later.
263
264 # Adding a default choice, in case the user does not want to modify the branch
265 my $nochange_branch = { branchname => '', value => '', selected => 1 };
266 unshift (@$branches, $nochange_branch);
267
268 my $pref_itemcallnumber = C4::Context->preference('itemcallnumber');
269
270
271 foreach my $tag (sort keys %{$tagslib}) {
272     # loop through each subfield
273     foreach my $subfield (sort keys %{$tagslib->{$tag}}) {
274         next if subfield_is_koha_internal_p($subfield);
275         next if ($tagslib->{$tag}->{$subfield}->{'tab'} ne "10");
276         # barcode and stocknumber are not meant to be batch-modified
277         next if $tagslib->{$tag}->{$subfield}->{'kohafield'} eq 'items.barcode';
278         next if $tagslib->{$tag}->{$subfield}->{'kohafield'} eq 'items.stocknumber';
279         my %subfield_data;
280  
281         my $index_subfield = int(rand(1000000)); 
282         if ($subfield eq '@'){
283             $subfield_data{id} = "tag_".$tag."_subfield_00_".$index_subfield;
284         } else {
285             $subfield_data{id} = "tag_".$tag."_subfield_".$subfield."_".$index_subfield;
286         }
287         $subfield_data{tag}        = $tag;
288         $subfield_data{subfield}   = $subfield;
289         $subfield_data{random}     = int(rand(1000000));    # why do we need 2 different randoms?
290     #   $subfield_data{marc_lib}   = $tagslib->{$tag}->{$subfield}->{lib};
291         $subfield_data{marc_lib}   ="<span id=\"error$i\" title=\"".$tagslib->{$tag}->{$subfield}->{lib}."\">".$tagslib->{$tag}->{$subfield}->{lib}."</span>";
292         $subfield_data{mandatory}  = $tagslib->{$tag}->{$subfield}->{mandatory};
293         $subfield_data{repeatable} = $tagslib->{$tag}->{$subfield}->{repeatable};
294         my ($x,$value);
295         $value =~ s/"/&quot;/g;
296         unless ($value) {
297             $value = $tagslib->{$tag}->{$subfield}->{defaultvalue};
298             # get today date & replace YYYY, MM, DD if provided in the default value
299             my ( $year, $month, $day ) = split ',', $today_iso;     # FIXME: iso dates don't have commas!
300             $value =~ s/YYYY/$year/g;
301             $value =~ s/MM/$month/g;
302             $value =~ s/DD/$day/g;
303         }
304         $subfield_data{visibility} = "display:none;" if (($tagslib->{$tag}->{$subfield}->{hidden} > 4) || ($tagslib->{$tag}->{$subfield}->{hidden} < -4));
305         # testing branch value if IndependantBranches.
306
307         my $attributes_no_value = qq(tabindex="1" id="$subfield_data{id}" name="field_value" class="input_marceditor" size="67" maxlength="255" );
308         my $attributes          = qq($attributes_no_value value="$value" );
309
310         if ( $tagslib->{$tag}->{$subfield}->{authorised_value} ) {
311         my @authorised_values;
312         my %authorised_lib;
313         # builds list, depending on authorised value...
314   
315         if ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "branches" ) {
316             foreach my $thisbranch (@$branches) {
317                 push @authorised_values, $thisbranch->{value};
318                 $authorised_lib{$thisbranch->{value}} = $thisbranch->{branchname};
319             }
320         $value = "";
321         }
322         elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes" ) {
323             push @authorised_values, "";
324             my $sth = $dbh->prepare("select itemtype,description from itemtypes order by description");
325             $sth->execute;
326             while ( my ( $itemtype, $description ) = $sth->fetchrow_array ) {
327                 push @authorised_values, $itemtype;
328                 $authorised_lib{$itemtype} = $description;
329             }
330         $value = "";
331
332           #---- class_sources
333       }
334       elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "cn_source" ) {
335           push @authorised_values, "" unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
336             
337           my $class_sources = GetClassSources();
338           my $default_source = C4::Context->preference("DefaultClassificationSource");
339           
340           foreach my $class_source (sort keys %$class_sources) {
341               next unless $class_sources->{$class_source}->{'used'} or
342                           ($value and $class_source eq $value)      or
343                           ($class_source eq $default_source);
344               push @authorised_values, $class_source;
345               $authorised_lib{$class_source} = $class_sources->{$class_source}->{'description'};
346           }
347                   $value = '';
348
349           #---- "true" authorised value
350       }
351       else {
352           push @authorised_values, ""; # unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
353           $authorised_values_sth->execute( $tagslib->{$tag}->{$subfield}->{authorised_value} );
354           while ( my ( $value, $lib ) = $authorised_values_sth->fetchrow_array ) {
355               push @authorised_values, $value;
356               $authorised_lib{$value} = $lib;
357           }
358           $value="";
359       }
360       $subfield_data{marc_value} =CGI::scrolling_list(      # FIXME: factor out scrolling_list
361           -name     => "field_value",
362           -values   => \@authorised_values,
363           -default  => $value,
364           -labels   => \%authorised_lib,
365           -override => 1,
366           -size     => 1,
367           -multiple => 0,
368           -tabindex => 1,
369           -id       => "tag_".$tag."_subfield_".$subfield."_".$index_subfield,
370           -class    => "input_marceditor",
371       );
372     # it's a thesaurus / authority field
373     }
374     elsif ( $tagslib->{$tag}->{$subfield}->{authtypecode} ) {
375         $subfield_data{marc_value} = "<input type=\"text\" $attributes />
376             <a href=\"#\" class=\"buttonDot\"
377                 onclick=\"Dopop('/cgi-bin/koha/authorities/auth_finder.pl?authtypecode=".$tagslib->{$tag}->{$subfield}->{authtypecode}."&index=$subfield_data{id}','$subfield_data{id}'); return false;\" title=\"Tag Editor\">...</a>
378     ";
379     # it's a plugin field
380     }
381     elsif ( $tagslib->{$tag}->{$subfield}->{value_builder} ) {
382         # opening plugin
383         my $plugin = C4::Context->intranetdir . "/cataloguing/value_builder/" . $tagslib->{$tag}->{$subfield}->{'value_builder'};
384         if (do $plugin) {
385                         my $temp;
386             my $extended_param = plugin_parameters( $dbh, $temp, $tagslib, $subfield_data{id}, \@loop_data );
387             my ( $function_name, $javascript ) = plugin_javascript( $dbh, $temp, $tagslib, $subfield_data{id}, \@loop_data );
388             $subfield_data{marc_value} = qq[<input $attributes
389                 onfocus="Focus$function_name($subfield_data{random}, '$subfield_data{id}');"
390                  onblur=" Blur$function_name($subfield_data{random}, '$subfield_data{id}');" />
391                 <a href="#" class="buttonDot" onclick="Clic$function_name('$subfield_data{id}'); return false;" title="Tag Editor">...</a>
392                 $javascript];
393         } else {
394             warn "Plugin Failed: $plugin";
395             $subfield_data{marc_value} = "<input $attributes />"; # supply default input form
396         }
397     }
398     elsif ( $tag eq '' ) {       # it's an hidden field
399         $subfield_data{marc_value} = qq(<input type="hidden" $attributes />);
400     }
401     elsif ( $tagslib->{$tag}->{$subfield}->{'hidden'} ) {   # FIXME: shouldn't input type be "hidden" ?
402         $subfield_data{marc_value} = qq(<input type="text" $attributes />);
403     }
404     elsif ( length($value) > 100
405             or (C4::Context->preference("marcflavour") eq "UNIMARC" and
406                   300 <= $tag && $tag < 400 && $subfield eq 'a' )
407             or (C4::Context->preference("marcflavour") eq "MARC21"  and
408                   500 <= $tag && $tag < 600                     )
409           ) {
410         # oversize field (textarea)
411         $subfield_data{marc_value} = "<textarea $attributes_no_value>$value</textarea>\n";
412     } else {
413         # it's a standard field
414          $subfield_data{marc_value} = "<input $attributes />";
415     }
416 #   $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\">";
417     push (@loop_data, \%subfield_data);
418     $i++
419   }
420 } # -- End foreach tag
421
422
423     # what's the next op ? it's what we are not in : an add if we're editing, otherwise, and edit.
424     $template->param(item => \@loop_data);
425     if (@notfoundbarcodes) { 
426         my @notfoundbarcodesloop = map{{barcode=>$_}}@notfoundbarcodes;
427         $template->param(notfoundbarcodes => \@notfoundbarcodesloop);
428     }
429     $nextop="action"
430 } # -- End action="show"
431
432 $template->param(%$items_display_hashref) if $items_display_hashref;
433 $template->param(
434     op      => $nextop,
435     $op => 1,
436 );
437
438 if ($op eq "action") {
439
440     #my @not_deleted_loop = map{{itemnumber=>$_}}@not_deleted;
441
442     $template->param(
443         not_deleted_items => $not_deleted_items,
444         deleted_items => $deleted_items,
445         delete_records => $del_records,
446         deleted_records => $deleted_records,
447         not_deleted_loop => \@not_deleted 
448     );
449 }
450
451 foreach my $error (@errors) {
452     $template->param($error => 1);
453 }
454 output_html_with_http_headers $input, $cookie, $template->output;
455 exit;
456
457
458 # ---------------- Functions
459
460 sub BuildItemsData{
461         my @itemnumbers=@_;
462                 # now, build existiing item list
463                 my %witness; #---- stores the list of subfields used at least once, with the "meaning" of the code
464                 my @big_array;
465                 #---- finds where items.itemnumber is stored
466                 my (  $itemtagfield,   $itemtagsubfield) = &GetMarcFromKohaField("items.itemnumber", "");
467                 my ($branchtagfield, $branchtagsubfield) = &GetMarcFromKohaField("items.homebranch", "");
468                 foreach my $itemnumber (@itemnumbers){
469                         my $itemdata=GetItem($itemnumber);
470                         my $itemmarc=Item2Marc($itemdata);
471                         my %this_row;
472                         foreach my $field (grep {$_->tag() eq $itemtagfield} $itemmarc->fields()) {
473                                 # loop through each subfield
474                                 my $itembranchcode=$field->subfield($branchtagsubfield);
475                                 if ($itembranchcode && C4::Context->preference("IndependantBranches")) {
476                                                 #verifying rights
477                                                 my $userenv = C4::Context->userenv();
478                                                 unless (($userenv->{'flags'} == 1) or (($userenv->{'branch'} eq $itembranchcode))){
479                                                                 $this_row{'nomod'}=1;
480                                                 }
481                                 }
482                                 my $tag=$field->tag();
483                                 foreach my $subfield ($field->subfields) {
484                                         my ($subfcode,$subfvalue)=@$subfield;
485                                         next if ($tagslib->{$tag}->{$subfcode}->{tab} ne 10 
486                                                         && $tag        ne $itemtagfield 
487                                                         && $subfcode   ne $itemtagsubfield);
488
489                                         $witness{$subfcode} = $tagslib->{$tag}->{$subfcode}->{lib} if ($tagslib->{$tag}->{$subfcode}->{tab}  eq 10);
490                                         if ($tagslib->{$tag}->{$subfcode}->{tab}  eq 10) {
491                                                 $this_row{$subfcode}=GetAuthorisedValueDesc( $tag,
492                                                                         $subfcode, $subfvalue, '', $tagslib) 
493                                                                         || $subfvalue;
494                                         }
495
496                                         $this_row{itemnumber} = $subfvalue if ($tag eq $itemtagfield && $subfcode eq $itemtagsubfield);
497                                 }
498                         }
499
500             # grab title, author, and ISBN to identify bib that the item
501             # belongs to in the display
502                          my $biblio=GetBiblioData($$itemdata{biblionumber});
503             $this_row{title} = $biblio->{title};
504             $this_row{author} = $biblio->{author};
505             $this_row{isbn} = $biblio->{isbn};
506             $this_row{biblionumber} = $biblio->{biblionumber};
507
508                         if (%this_row) {
509                                 push(@big_array, \%this_row);
510                         }
511                 }
512                 @big_array = sort {$a->{0} cmp $b->{0}} @big_array;
513
514                 # now, construct template !
515                 # First, the existing items for display
516                 my @item_value_loop;
517                 my @witnesscodessorted=sort keys %witness;
518                 for my $row ( @big_array ) {
519                         my %row_data;
520                         my @item_fields = map +{ field => $_ || '' }, @$row{ @witnesscodessorted };
521                         $row_data{item_value} = [ @item_fields ];
522                         $row_data{itemnumber} = $row->{itemnumber};
523                         #reporting this_row values
524                         $row_data{'nomod'} = $row->{'nomod'};
525       $row_data{bibinfo} = $row->{bibinfo};
526       $row_data{author} = $row->{author};
527       $row_data{title} = $row->{title};
528       $row_data{isbn} = $row->{isbn};
529       $row_data{biblionumber} = $row->{biblionumber};
530                         push(@item_value_loop,\%row_data);
531                 }
532                 my @header_loop=map { { header_value=> $witness{$_}} } @witnesscodessorted;
533
534         return { item_loop        => \@item_value_loop, item_header_loop => \@header_loop };
535 }
536
537 #BE WARN : it is not the general case 
538 # This function can be OK in the item marc record special case
539 # Where subfield is not repeated
540 # And where we are sure that field should correspond
541 # And $tag>10
542 sub UpdateMarcWith {
543   my ($marcfrom,$marcto)=@_;
544   #warn "FROM :",$marcfrom->as_formatted;
545         my (  $itemtag,   $itemtagsubfield) = &GetMarcFromKohaField("items.itemnumber", "");
546         my $fieldfrom=$marcfrom->field($itemtag);
547         my @fields_to=$marcto->field($itemtag);
548     foreach my $subfield ($fieldfrom->subfields()){
549                 foreach my $field_to_update (@fields_to){
550                     if ($subfield->[1]){
551                         $field_to_update->update($subfield->[0]=>$subfield->[1]);
552                     }
553                     else {
554                         $field_to_update->delete_subfield(code=> $subfield->[0]);
555                     }
556                 }
557     }
558   #warn "TO edited:",$marcto->as_formatted;
559 }
560
561 sub find_value {
562     my ($tagfield,$insubfield,$record) = @_;
563     my $result;
564     my $indicator;
565     foreach my $field ($record->field($tagfield)) {
566         my @subfields = $field->subfields();
567         foreach my $subfield (@subfields) {
568             if (@$subfield[0] eq $insubfield) {
569                 $result .= @$subfield[1];
570                 $indicator = $field->indicator(1).$field->indicator(2);
571             }
572         }
573     }
574     return($indicator,$result);
575 }
576
577 # ----------------------------
578 # Background functions
579
580
581 sub add_results_to_template {
582     my $template = shift;
583     my $results = shift;
584     $template->param(map { $_ => $results->{$_} } keys %{ $results });
585 }
586
587 sub add_saved_job_results_to_template {
588     my $template = shift;
589     my $completedJobID = shift;
590     my $job = C4::BackgroundJob->fetch($sessionID, $completedJobID);
591     my $results = $job->results();
592     add_results_to_template($template, $results);
593 }
594
595 sub put_in_background {
596     my $job_size = shift;
597
598     my $job = C4::BackgroundJob->new($sessionID, "test", $ENV{'SCRIPT_NAME'}, $job_size);
599     my $jobID = $job->id();
600
601     # fork off
602     if (my $pid = fork) {
603         # parent
604         # return job ID as JSON
605
606         # prevent parent exiting from
607         # destroying the kid's database handle
608         # FIXME: according to DBI doc, this may not work for Oracle
609         $dbh->{InactiveDestroy}  = 1;
610
611         my $reply = CGI->new("");
612         print $reply->header(-type => 'text/html');
613         print '{"jobID":"' . $jobID . '"}';
614         exit 0;
615     } elsif (defined $pid) {
616         # child
617         # close STDOUT to signal to Apache that
618         # we're now running in the background
619         close STDOUT;
620         close STDERR;
621     } else {
622         # fork failed, so exit immediately
623         warn "fork failed while attempting to run $ENV{'SCRIPT_NAME'} as a background job";
624         exit 0;
625     }
626     return $job;
627 }
628
629
630