Bug 4231 - Placing holds from cart fails silently
[koha.git] / tools / batchMod.pl
blobe88664255986744387b568c0be52ce7c7fd39da8
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 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.
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.
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.
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 use List::MoreUtils qw/uniq/;
39 my $input = new CGI;
40 my $dbh = C4::Context->dbh;
41 my $error = $input->param('error');
42 my @itemnumbers = $input->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 });
73 my $today_iso = C4::Dates->today('iso');
74 $template->param(today_iso => $today_iso);
75 $template->param(del => $del);
77 my $itemrecord;
78 my $nextop="";
79 my @errors; # store errors found while checking data BEFORE saving item.
80 my $items_display_hashref;
81 my $tagslib = &GetMarcStructure(1);
83 my $deleted_items = 0; # Number of deleted items
84 my $deleted_records = 0; # Number of deleted records ( with no items attached )
85 my $not_deleted_items = 0; # Number of items that could not be deleted
86 my @not_deleted; # List of the itemnumbers that could not be deleted
87 my $modified_items = 0; # Numbers of modified items
88 my $modified_fields = 0; # Numbers of modified fields
90 my %cookies = parse CGI::Cookie($cookie);
91 my $sessionID = $cookies{'CGISESSID'}->value;
94 #--- ----------------------------------------------------------------------------
95 if ($op eq "action") {
96 #-------------------------------------------------------------------------------
97 my @tags = $input->param('tag');
98 my @subfields = $input->param('subfield');
99 my @values = $input->param('field_value');
100 my @disabled = $input->param('disable_input');
101 # build indicator hash.
102 my @ind_tag = $input->param('ind_tag');
103 my @indicator = $input->param('indicator');
105 # Is there something to modify ?
106 # TODO : We shall use this var to warn the user in case no modification was done to the items
107 my $values_to_modify = scalar(grep {!/^$/} @values);
108 my $values_to_blank = scalar(@disabled);
109 my $marcitem;
111 # Once the job is done
112 if ($completedJobID) {
113 # If we have a reasonable amount of items, we display them
114 if (scalar(@itemnumbers) <= ( C4::Context->preference("MaxItemsForBatch") // 1000 ) ) {
115 $items_display_hashref=BuildItemsData(@itemnumbers);
116 } else {
117 # Else, we only display the barcode
118 my @simple_items_display = map {{ itemnumber => $_, barcode => (GetBarcodeFromItemnumber($_) or ""), biblionumber => (GetBiblionumberFromItemnumber($_) or "") }} @itemnumbers;
119 $template->param("simple_items_display" => \@simple_items_display);
122 # Setting the job as done
123 my $job = C4::BackgroundJob->fetch($sessionID, $completedJobID);
125 # Calling the template
126 add_saved_job_results_to_template($template, $completedJobID);
128 } else {
129 # While the job is getting done
131 # Job size is the number of items we have to process
132 my $job_size = scalar(@itemnumbers);
133 my $job = undef;
135 # If we asked for background processing
136 if ($runinbackground) {
137 $job = put_in_background($job_size);
140 #initializing values for updates
141 my ( $itemtagfield, $itemtagsubfield) = &GetMarcFromKohaField("items.itemnumber", "");
142 if ($values_to_modify){
143 my $xml = TransformHtmlToXml(\@tags,\@subfields,\@values,\@indicator,\@ind_tag, 'ITEM');
144 $marcitem = MARC::Record::new_from_xml($xml, 'UTF-8');
146 if ($values_to_blank){
147 foreach my $disabledsubf (@disabled){
148 if ($marcitem && $marcitem->field($itemtagfield)){
149 $marcitem->field($itemtagfield)->update( $disabledsubf => "" );
151 else {
152 $marcitem = MARC::Record->new();
153 $marcitem->append_fields( MARC::Field->new( $itemtagfield, '', '', $disabledsubf => "" ) );
158 # For each item
159 my $i = 1;
160 foreach my $itemnumber(@itemnumbers){
162 $job->progress($i) if $runinbackground;
163 my $itemdata = GetItem($itemnumber);
164 if ( $del ){
165 my $return = DelItemCheck(C4::Context->dbh, $itemdata->{'biblionumber'}, $itemdata->{'itemnumber'});
166 if ($return == 1) {
167 $deleted_items++;
168 } else {
169 $not_deleted_items++;
170 push @not_deleted,
171 { biblionumber => $itemdata->{'biblionumber'},
172 itemnumber => $itemdata->{'itemnumber'},
173 barcode => $itemdata->{'barcode'},
174 title => $itemdata->{'title'},
175 $return => 1
179 # If there are no items left, delete the biblio
180 if ( $del_records ) {
181 my $itemscount = GetItemsCount($itemdata->{'biblionumber'});
182 if ( $itemscount == 0 ) {
183 my $error = DelBiblio($itemdata->{'biblionumber'});
184 $deleted_records++ unless ( $error );
187 } else {
188 if ($values_to_modify || $values_to_blank) {
189 my $localmarcitem = Item2Marc($itemdata);
191 my $modified = UpdateMarcWith( $marcitem, $localmarcitem );
192 if ( $modified ) {
193 eval {
194 if ( my $item = ModItemFromMarc( $localmarcitem, $itemdata->{biblionumber}, $itemnumber ) ) {
195 LostItem($itemnumber, 'MARK RETURNED') if $item->{itemlost};
199 if ( $runinbackground ) {
200 $modified_items++ if $modified;
201 $modified_fields += $modified;
202 $job->set({
203 modified_items => $modified_items,
204 modified_fields => $modified_fields,
209 $i++;
214 #-------------------------------------------------------------------------------
215 # build screen with existing items. and "new" one
216 #-------------------------------------------------------------------------------
218 if ($op eq "show"){
219 my $filefh = $input->upload('uploadfile');
220 my $filecontent = $input->param('filecontent');
221 my @notfoundbarcodes;
223 my @contentlist;
224 if ($filefh){
225 while (my $content=<$filefh>){
226 $content =~ s/[\r\n]*$//;
227 push @contentlist, $content if $content;
230 if ($filecontent eq 'barcode_file') {
231 foreach my $barcode (@contentlist) {
233 my $itemnumber = GetItemnumberFromBarcode($barcode);
234 if ($itemnumber) {
235 push @itemnumbers,$itemnumber;
236 } else {
237 push @notfoundbarcodes, $barcode;
241 elsif ( $filecontent eq 'itemid_file') {
242 @itemnumbers = @contentlist;
244 } else {
245 if (defined $biblionumber){
246 my @all_items = GetItemsInfo( $biblionumber );
247 foreach my $itm (@all_items) {
248 push @itemnumbers, $itm->{itemnumber};
251 if ( my $list=$input->param('barcodelist')){
252 push my @barcodelist, uniq( split(/\s\n/, $list) );
254 foreach my $barcode (@barcodelist) {
256 my $itemnumber = GetItemnumberFromBarcode($barcode);
257 if ($itemnumber) {
258 push @itemnumbers,$itemnumber;
259 } else {
260 push @notfoundbarcodes, $barcode;
267 # Flag to tell the template there are valid results, hidden or not
268 if(scalar(@itemnumbers) > 0){ $template->param("itemresults" => 1); }
269 # Only display the items if there are no more than pref MaxItemsForBatch
270 if (scalar(@itemnumbers) <= ( C4::Context->preference("MaxItemsForBatch") // 1000 ) ) {
271 $items_display_hashref=BuildItemsData(@itemnumbers);
272 } else {
273 $template->param("too_many_items" => scalar(@itemnumbers));
274 # Even if we do not display the items, we need the itemnumbers
275 $template->param(itemnumbers_array => \@itemnumbers);
277 # now, build the item form for entering a new item
278 my @loop_data =();
279 my $i=0;
280 my $branch_limit = C4::Context->userenv ? C4::Context->userenv->{"branch"} : "";
281 my $query = qq{SELECT authorised_value, lib FROM authorised_values};
282 $query .= qq{ LEFT JOIN authorised_values_branches ON ( id = av_id ) } if $branch_limit;
283 $query .= qq{ WHERE category = ?};
284 $query .= qq{ AND ( branchcode = ? OR branchcode IS NULL ) } if $branch_limit;
285 $query .= qq{ GROUP BY lib ORDER BY lib, lib_opac};
286 my $authorised_values_sth = $dbh->prepare( $query );
288 my $branches = GetBranchesLoop(); # build once ahead of time, instead of multiple times later.
290 # Adding a default choice, in case the user does not want to modify the branch
291 my $nochange_branch = { branchname => '', value => '', selected => 1 };
292 unshift (@$branches, $nochange_branch);
294 my $pref_itemcallnumber = C4::Context->preference('itemcallnumber');
297 foreach my $tag (sort keys %{$tagslib}) {
298 # loop through each subfield
299 foreach my $subfield (sort keys %{$tagslib->{$tag}}) {
300 next if subfield_is_koha_internal_p($subfield);
301 next if ($tagslib->{$tag}->{$subfield}->{'tab'} ne "10");
302 # barcode and stocknumber are not meant to be batch-modified
303 next if $tagslib->{$tag}->{$subfield}->{'kohafield'} eq 'items.barcode';
304 next if $tagslib->{$tag}->{$subfield}->{'kohafield'} eq 'items.stocknumber';
305 my %subfield_data;
307 my $index_subfield = int(rand(1000000));
308 if ($subfield eq '@'){
309 $subfield_data{id} = "tag_".$tag."_subfield_00_".$index_subfield;
310 } else {
311 $subfield_data{id} = "tag_".$tag."_subfield_".$subfield."_".$index_subfield;
313 $subfield_data{tag} = $tag;
314 $subfield_data{subfield} = $subfield;
315 $subfield_data{random} = int(rand(1000000)); # why do we need 2 different randoms?
316 # $subfield_data{marc_lib} = $tagslib->{$tag}->{$subfield}->{lib};
317 $subfield_data{marc_lib} ="<span id=\"error$i\" title=\"".$tagslib->{$tag}->{$subfield}->{lib}."\">".$tagslib->{$tag}->{$subfield}->{lib}."</span>";
318 $subfield_data{mandatory} = $tagslib->{$tag}->{$subfield}->{mandatory};
319 $subfield_data{repeatable} = $tagslib->{$tag}->{$subfield}->{repeatable};
320 my ($x,$value);
321 $value =~ s/"/&quot;/g;
322 if ( !$value && $use_default_values) {
323 $value = $tagslib->{$tag}->{$subfield}->{defaultvalue};
324 # get today date & replace YYYY, MM, DD if provided in the default value
325 my ( $year, $month, $day ) = split ',', $today_iso; # FIXME: iso dates don't have commas!
326 $value =~ s/YYYY/$year/g;
327 $value =~ s/MM/$month/g;
328 $value =~ s/DD/$day/g;
330 $subfield_data{visibility} = "display:none;" if (($tagslib->{$tag}->{$subfield}->{hidden} > 4) || ($tagslib->{$tag}->{$subfield}->{hidden} < -4));
331 # testing branch value if IndependentBranches.
333 my $attributes_no_value = qq(tabindex="1" id="$subfield_data{id}" name="field_value" class="input_marceditor" size="50" maxlength="255" );
334 my $attributes = qq($attributes_no_value value="$value" );
336 if ( $tagslib->{$tag}->{$subfield}->{authorised_value} ) {
337 my @authorised_values;
338 my %authorised_lib;
339 # builds list, depending on authorised value...
341 if ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "branches" ) {
342 foreach my $thisbranch (@$branches) {
343 push @authorised_values, $thisbranch->{value};
344 $authorised_lib{$thisbranch->{value}} = $thisbranch->{branchname};
346 $value = "";
348 elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes" ) {
349 push @authorised_values, "";
350 my $sth = $dbh->prepare("select itemtype,description from itemtypes order by description");
351 $sth->execute;
352 while ( my ( $itemtype, $description ) = $sth->fetchrow_array ) {
353 push @authorised_values, $itemtype;
354 $authorised_lib{$itemtype} = $description;
356 $value = "";
358 #---- class_sources
360 elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "cn_source" ) {
361 push @authorised_values, "" unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
363 my $class_sources = GetClassSources();
364 my $default_source = C4::Context->preference("DefaultClassificationSource");
366 foreach my $class_source (sort keys %$class_sources) {
367 next unless $class_sources->{$class_source}->{'used'} or
368 ($value and $class_source eq $value) or
369 ($class_source eq $default_source);
370 push @authorised_values, $class_source;
371 $authorised_lib{$class_source} = $class_sources->{$class_source}->{'description'};
373 $value = '';
375 #---- "true" authorised value
377 else {
378 push @authorised_values, ""; # unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
379 $authorised_values_sth->execute( $tagslib->{$tag}->{$subfield}->{authorised_value}, $branch_limit ? $branch_limit : () );
380 while ( my ( $value, $lib ) = $authorised_values_sth->fetchrow_array ) {
381 push @authorised_values, $value;
382 $authorised_lib{$value} = $lib;
384 $value="";
386 $subfield_data{marc_value} =CGI::scrolling_list( # FIXME: factor out scrolling_list
387 -name => "field_value",
388 -values => \@authorised_values,
389 -default => $value,
390 -labels => \%authorised_lib,
391 -override => 1,
392 -size => 1,
393 -multiple => 0,
394 -tabindex => 1,
395 -id => "tag_".$tag."_subfield_".$subfield."_".$index_subfield,
396 -class => "input_marceditor",
398 # it's a thesaurus / authority field
400 elsif ( $tagslib->{$tag}->{$subfield}->{authtypecode} ) {
401 $subfield_data{marc_value} = "<input type=\"text\" $attributes />
402 <a href=\"#\" class=\"buttonDot\"
403 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>
405 # it's a plugin field
407 elsif ( $tagslib->{$tag}->{$subfield}->{value_builder} ) {
408 # opening plugin
409 my $plugin = C4::Context->intranetdir . "/cataloguing/value_builder/" . $tagslib->{$tag}->{$subfield}->{'value_builder'};
410 if (do $plugin) {
411 my $temp;
412 my $extended_param = plugin_parameters( $dbh, $temp, $tagslib, $subfield_data{id}, \@loop_data );
413 my ( $function_name, $javascript ) = plugin_javascript( $dbh, $temp, $tagslib, $subfield_data{id}, \@loop_data );
414 $subfield_data{marc_value} = qq[<input type="text" $attributes
415 onfocus="Focus$function_name($subfield_data{random}, '$subfield_data{id}');"
416 onblur=" Blur$function_name($subfield_data{random}, '$subfield_data{id}');" />
417 <a href="#" class="buttonDot" onclick="Clic$function_name('$subfield_data{id}'); return false;" title="Tag Editor">...</a>
418 $javascript];
419 } else {
420 warn "Plugin Failed: $plugin";
421 $subfield_data{marc_value} = "<input type=\"text\" $attributes />"; # supply default input form
424 elsif ( $tag eq '' ) { # it's an hidden field
425 $subfield_data{marc_value} = qq(<input type="hidden" $attributes />);
427 elsif ( $tagslib->{$tag}->{$subfield}->{'hidden'} ) { # FIXME: shouldn't input type be "hidden" ?
428 $subfield_data{marc_value} = qq(<input type="text" $attributes />);
430 elsif ( length($value) > 100
431 or (C4::Context->preference("marcflavour") eq "UNIMARC" and
432 300 <= $tag && $tag < 400 && $subfield eq 'a' )
433 or (C4::Context->preference("marcflavour") eq "MARC21" and
434 500 <= $tag && $tag < 600 )
436 # oversize field (textarea)
437 $subfield_data{marc_value} = "<textarea $attributes_no_value>$value</textarea>\n";
438 } else {
439 # it's a standard field
440 $subfield_data{marc_value} = "<input type=\"text\" $attributes />";
442 # $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\">";
443 push (@loop_data, \%subfield_data);
444 $i++
446 } # -- End foreach tag
447 $authorised_values_sth->finish;
451 # what's the next op ? it's what we are not in : an add if we're editing, otherwise, and edit.
452 $template->param(item => \@loop_data);
453 if (@notfoundbarcodes) {
454 my @notfoundbarcodesloop = map{{barcode=>$_}}@notfoundbarcodes;
455 $template->param(notfoundbarcodes => \@notfoundbarcodesloop);
457 $nextop="action"
458 } # -- End action="show"
460 $template->param(%$items_display_hashref) if $items_display_hashref;
461 $template->param(
462 op => $nextop,
464 $template->param( $op => 1 ) if $op;
466 if ($op eq "action") {
468 #my @not_deleted_loop = map{{itemnumber=>$_}}@not_deleted;
470 $template->param(
471 not_deleted_items => $not_deleted_items,
472 deleted_items => $deleted_items,
473 delete_records => $del_records,
474 deleted_records => $deleted_records,
475 not_deleted_loop => \@not_deleted
479 foreach my $error (@errors) {
480 $template->param($error => 1) if $error;
482 $template->param(src => $src);
483 $template->param(biblionumber => $biblionumber);
484 output_html_with_http_headers $input, $cookie, $template->output;
485 exit;
488 # ---------------- Functions
490 sub BuildItemsData{
491 my @itemnumbers=@_;
492 # now, build existiing item list
493 my %witness; #---- stores the list of subfields used at least once, with the "meaning" of the code
494 my @big_array;
495 #---- finds where items.itemnumber is stored
496 my ( $itemtagfield, $itemtagsubfield) = &GetMarcFromKohaField("items.itemnumber", "");
497 my ($branchtagfield, $branchtagsubfield) = &GetMarcFromKohaField("items.homebranch", "");
498 foreach my $itemnumber (@itemnumbers){
499 my $itemdata=GetItem($itemnumber);
500 my $itemmarc=Item2Marc($itemdata);
501 my %this_row;
502 foreach my $field (grep {$_->tag() eq $itemtagfield} $itemmarc->fields()) {
503 # loop through each subfield
504 my $itembranchcode=$field->subfield($branchtagsubfield);
505 if ($itembranchcode && C4::Context->preference("IndependentBranches")) {
506 #verifying rights
507 my $userenv = C4::Context->userenv();
508 unless (C4::Context->IsSuperLibrarian() or (($userenv->{'branch'} eq $itembranchcode))){
509 $this_row{'nomod'}=1;
512 my $tag=$field->tag();
513 foreach my $subfield ($field->subfields) {
514 my ($subfcode,$subfvalue)=@$subfield;
515 next if ($tagslib->{$tag}->{$subfcode}->{tab} ne 10
516 && $tag ne $itemtagfield
517 && $subfcode ne $itemtagsubfield);
519 $witness{$subfcode} = $tagslib->{$tag}->{$subfcode}->{lib} if ($tagslib->{$tag}->{$subfcode}->{tab} eq 10);
520 if ($tagslib->{$tag}->{$subfcode}->{tab} eq 10) {
521 $this_row{$subfcode}=GetAuthorisedValueDesc( $tag,
522 $subfcode, $subfvalue, '', $tagslib)
523 || $subfvalue;
526 $this_row{itemnumber} = $subfvalue if ($tag eq $itemtagfield && $subfcode eq $itemtagsubfield);
530 # grab title, author, and ISBN to identify bib that the item
531 # belongs to in the display
532 my $biblio=GetBiblioData($$itemdata{biblionumber});
533 $this_row{title} = $biblio->{title};
534 $this_row{author} = $biblio->{author};
535 $this_row{isbn} = $biblio->{isbn};
536 $this_row{biblionumber} = $biblio->{biblionumber};
538 if (%this_row) {
539 push(@big_array, \%this_row);
542 @big_array = sort {$a->{0} cmp $b->{0}} @big_array;
544 # now, construct template !
545 # First, the existing items for display
546 my @item_value_loop;
547 my @witnesscodessorted=sort keys %witness;
548 for my $row ( @big_array ) {
549 my %row_data;
550 my @item_fields = map +{ field => $_ || '' }, @$row{ @witnesscodessorted };
551 $row_data{item_value} = [ @item_fields ];
552 $row_data{itemnumber} = $row->{itemnumber};
553 #reporting this_row values
554 $row_data{'nomod'} = $row->{'nomod'};
555 $row_data{bibinfo} = $row->{bibinfo};
556 $row_data{author} = $row->{author};
557 $row_data{title} = $row->{title};
558 $row_data{isbn} = $row->{isbn};
559 $row_data{biblionumber} = $row->{biblionumber};
560 my $is_on_loan = C4::Circulation::IsItemIssued( $row->{itemnumber} );
561 $row_data{onloan} = $is_on_loan ? 1 : 0;
562 push(@item_value_loop,\%row_data);
564 my @header_loop=map { { header_value=> $witness{$_}} } @witnesscodessorted;
566 return { item_loop => \@item_value_loop, item_header_loop => \@header_loop };
569 #BE WARN : it is not the general case
570 # This function can be OK in the item marc record special case
571 # Where subfield is not repeated
572 # And where we are sure that field should correspond
573 # And $tag>10
574 sub UpdateMarcWith {
575 my ($marcfrom,$marcto)=@_;
576 my ( $itemtag, $itemtagsubfield) = &GetMarcFromKohaField("items.itemnumber", "");
577 my $fieldfrom=$marcfrom->field($itemtag);
578 my @fields_to=$marcto->field($itemtag);
579 my $modified = 0;
580 foreach my $subfield ( $fieldfrom->subfields() ) {
581 foreach my $field_to_update ( @fields_to ) {
582 if ( $subfield->[1] ) {
583 unless ( $field_to_update->subfield($subfield->[0]) eq $subfield->[1] ) {
584 $modified++;
585 $field_to_update->update( $subfield->[0] => $subfield->[1] );
588 else {
589 $modified++;
590 $field_to_update->delete_subfield( code => $subfield->[0] );
594 return $modified;
597 sub find_value {
598 my ($tagfield,$insubfield,$record) = @_;
599 my $result;
600 my $indicator;
601 foreach my $field ($record->field($tagfield)) {
602 my @subfields = $field->subfields();
603 foreach my $subfield (@subfields) {
604 if (@$subfield[0] eq $insubfield) {
605 $result .= @$subfield[1];
606 $indicator = $field->indicator(1).$field->indicator(2);
610 return($indicator,$result);
613 # ----------------------------
614 # Background functions
617 sub add_results_to_template {
618 my $template = shift;
619 my $results = shift;
620 $template->param(map { $_ => $results->{$_} } keys %{ $results });
623 sub add_saved_job_results_to_template {
624 my $template = shift;
625 my $completedJobID = shift;
626 my $job = C4::BackgroundJob->fetch($sessionID, $completedJobID);
627 my $results = $job->results();
628 add_results_to_template($template, $results);
630 my $fields = $job->get("modified_fields");
631 my $items = $job->get("modified_items");
632 $template->param(
633 modified_items => $items,
634 modified_fields => $fields,
638 sub put_in_background {
639 my $job_size = shift;
641 my $job = C4::BackgroundJob->new($sessionID, "test", $ENV{'SCRIPT_NAME'}, $job_size);
642 my $jobID = $job->id();
644 # fork off
645 if (my $pid = fork) {
646 # parent
647 # return job ID as JSON
649 # prevent parent exiting from
650 # destroying the kid's database handle
651 # FIXME: according to DBI doc, this may not work for Oracle
652 $dbh->{InactiveDestroy} = 1;
654 my $reply = CGI->new("");
655 print $reply->header(-type => 'text/html');
656 print '{"jobID":"' . $jobID . '"}';
657 exit 0;
658 } elsif (defined $pid) {
659 # child
660 # close STDOUT to signal to Apache that
661 # we're now running in the background
662 close STDOUT;
663 close STDERR;
664 } else {
665 # fork failed, so exit immediately
666 warn "fork failed while attempting to run $ENV{'SCRIPT_NAME'} as a background job";
667 exit 0;
669 return $job;