Bug 7673: New patron permissions
[koha.git] / tools / batchMod.pl
blobca10f6180c9b1de5ec6d505ff1d9a8165b8e10b1
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 C4::Members;
37 use MARC::File::XML;
38 use List::MoreUtils qw/uniq/;
40 my $input = new CGI;
41 my $dbh = C4::Context->dbh;
42 my $error = $input->param('error');
43 my @itemnumbers = $input->param('itemnumber');
44 my $biblionumber = $input->param('biblionumber');
45 my $op = $input->param('op');
46 my $del = $input->param('del');
47 my $del_records = $input->param('del_records');
48 my $completedJobID = $input->param('completedJobID');
49 my $runinbackground = $input->param('runinbackground');
50 my $src = $input->param('src');
51 my $use_default_values = $input->param('use_default_values');
53 my $template_name;
54 my $template_flag;
55 if (!defined $op) {
56 $template_name = "tools/batchMod.tt";
57 $template_flag = { tools => '*' };
58 $op = q{};
59 } else {
60 $template_name = ($del) ? "tools/batchMod-del.tt" : "tools/batchMod-edit.tt";
61 $template_flag = ($del) ? { tools => 'items_batchdel' } : { tools => 'items_batchmod' };
65 my ($template, $loggedinuser, $cookie)
66 = get_template_and_user({template_name => $template_name,
67 query => $input,
68 type => "intranet",
69 authnotrequired => 0,
70 flagsrequired => $template_flag,
71 });
73 # Does the user have a limited item edition permission?
74 my $uid = GetMember( borrowernumber => $loggedinuser )->{userid} if ($loggedinuser) ;
75 my $limitededition = haspermission($uid, {'tools' => 'items_limited_batchmod'}) if ($uid);
76 # In case user is a superlibrarian, edition is not limited
77 $limitededition = 0 if ($limitededition != 0 && $limitededition->{'superlibrarian'} eq 1);
79 my $today_iso = C4::Dates->today('iso');
80 $template->param(today_iso => $today_iso);
81 $template->param(del => $del);
83 my $itemrecord;
84 my $nextop="";
85 my @errors; # store errors found while checking data BEFORE saving item.
86 my $items_display_hashref;
87 my $tagslib = &GetMarcStructure(1);
89 my $deleted_items = 0; # Number of deleted items
90 my $deleted_records = 0; # Number of deleted records ( with no items attached )
91 my $not_deleted_items = 0; # Number of items that could not be deleted
92 my @not_deleted; # List of the itemnumbers that could not be deleted
93 my $modified_items = 0; # Numbers of modified items
94 my $modified_fields = 0; # Numbers of modified fields
96 my %cookies = parse CGI::Cookie($cookie);
97 my $sessionID = $cookies{'CGISESSID'}->value;
100 #--- ----------------------------------------------------------------------------
101 if ($op eq "action") {
102 #-------------------------------------------------------------------------------
103 my @tags = $input->param('tag');
104 my @subfields = $input->param('subfield');
105 my @values = $input->param('field_value');
106 my @disabled = $input->param('disable_input');
107 # build indicator hash.
108 my @ind_tag = $input->param('ind_tag');
109 my @indicator = $input->param('indicator');
111 # Is there something to modify ?
112 # TODO : We shall use this var to warn the user in case no modification was done to the items
113 my $values_to_modify = scalar(grep {!/^$/} @values);
114 my $values_to_blank = scalar(@disabled);
115 my $marcitem;
117 # Once the job is done
118 if ($completedJobID) {
119 # If we have a reasonable amount of items, we display them
120 if (scalar(@itemnumbers) <= ( C4::Context->preference("MaxItemsForBatch") // 1000 ) ) {
121 $items_display_hashref=BuildItemsData(@itemnumbers);
122 } else {
123 # Else, we only display the barcode
124 my @simple_items_display = map {{ itemnumber => $_, barcode => (GetBarcodeFromItemnumber($_) or ""), biblionumber => (GetBiblionumberFromItemnumber($_) or "") }} @itemnumbers;
125 $template->param("simple_items_display" => \@simple_items_display);
128 # Setting the job as done
129 my $job = C4::BackgroundJob->fetch($sessionID, $completedJobID);
131 # Calling the template
132 add_saved_job_results_to_template($template, $completedJobID);
134 } else {
135 # While the job is getting done
137 # Job size is the number of items we have to process
138 my $job_size = scalar(@itemnumbers);
139 my $job = undef;
141 # If we asked for background processing
142 if ($runinbackground) {
143 $job = put_in_background($job_size);
146 #initializing values for updates
147 my ( $itemtagfield, $itemtagsubfield) = &GetMarcFromKohaField("items.itemnumber", "");
148 if ($values_to_modify){
149 my $xml = TransformHtmlToXml(\@tags,\@subfields,\@values,\@indicator,\@ind_tag, 'ITEM');
150 $marcitem = MARC::Record::new_from_xml($xml, 'UTF-8');
152 if ($values_to_blank){
153 foreach my $disabledsubf (@disabled){
154 if ($marcitem && $marcitem->field($itemtagfield)){
155 $marcitem->field($itemtagfield)->update( $disabledsubf => "" );
157 else {
158 $marcitem = MARC::Record->new();
159 $marcitem->append_fields( MARC::Field->new( $itemtagfield, '', '', $disabledsubf => "" ) );
164 # For each item
165 my $i = 1;
166 foreach my $itemnumber(@itemnumbers){
168 $job->progress($i) if $runinbackground;
169 my $itemdata = GetItem($itemnumber);
170 if ( $del ){
171 my $return = DelItemCheck(C4::Context->dbh, $itemdata->{'biblionumber'}, $itemdata->{'itemnumber'});
172 if ($return == 1) {
173 $deleted_items++;
174 } else {
175 $not_deleted_items++;
176 push @not_deleted,
177 { biblionumber => $itemdata->{'biblionumber'},
178 itemnumber => $itemdata->{'itemnumber'},
179 barcode => $itemdata->{'barcode'},
180 title => $itemdata->{'title'},
181 $return => 1
185 # If there are no items left, delete the biblio
186 if ( $del_records ) {
187 my $itemscount = GetItemsCount($itemdata->{'biblionumber'});
188 if ( $itemscount == 0 ) {
189 my $error = DelBiblio($itemdata->{'biblionumber'});
190 $deleted_records++ unless ( $error );
193 } else {
194 if ($values_to_modify || $values_to_blank) {
195 my $localmarcitem = Item2Marc($itemdata);
197 my $modified = UpdateMarcWith( $marcitem, $localmarcitem );
198 if ( $modified ) {
199 eval {
200 if ( my $item = ModItemFromMarc( $localmarcitem, $itemdata->{biblionumber}, $itemnumber ) ) {
201 LostItem($itemnumber, 'MARK RETURNED') if $item->{itemlost};
205 if ( $runinbackground ) {
206 $modified_items++ if $modified;
207 $modified_fields += $modified;
208 $job->set({
209 modified_items => $modified_items,
210 modified_fields => $modified_fields,
215 $i++;
220 #-------------------------------------------------------------------------------
221 # build screen with existing items. and "new" one
222 #-------------------------------------------------------------------------------
224 if ($op eq "show"){
225 my $filefh = $input->upload('uploadfile');
226 my $filecontent = $input->param('filecontent');
227 my @notfoundbarcodes;
229 my @contentlist;
230 if ($filefh){
231 while (my $content=<$filefh>){
232 $content =~ s/[\r\n]*$//;
233 push @contentlist, $content if $content;
236 if ($filecontent eq 'barcode_file') {
237 foreach my $barcode (@contentlist) {
239 my $itemnumber = GetItemnumberFromBarcode($barcode);
240 if ($itemnumber) {
241 push @itemnumbers,$itemnumber;
242 } else {
243 push @notfoundbarcodes, $barcode;
247 elsif ( $filecontent eq 'itemid_file') {
248 @itemnumbers = @contentlist;
250 } else {
251 if (defined $biblionumber){
252 my @all_items = GetItemsInfo( $biblionumber );
253 foreach my $itm (@all_items) {
254 push @itemnumbers, $itm->{itemnumber};
257 if ( my $list=$input->param('barcodelist')){
258 push my @barcodelist, uniq( split(/\s\n/, $list) );
260 foreach my $barcode (@barcodelist) {
262 my $itemnumber = GetItemnumberFromBarcode($barcode);
263 if ($itemnumber) {
264 push @itemnumbers,$itemnumber;
265 } else {
266 push @notfoundbarcodes, $barcode;
273 # Flag to tell the template there are valid results, hidden or not
274 if(scalar(@itemnumbers) > 0){ $template->param("itemresults" => 1); }
275 # Only display the items if there are no more than pref MaxItemsForBatch
276 if (scalar(@itemnumbers) <= ( C4::Context->preference("MaxItemsForBatch") // 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 $branches = GetBranchesLoop(); # 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 (@$branches, $nochange_branch);
300 my $pref_itemcallnumber = C4::Context->preference('itemcallnumber');
302 # Getting list of subfields to keep when limited batchmod edit is enabled
303 my $subfieldsToAllowForBatchmod = C4::Context->preference('SubfieldsToAllowForLimitedBatchmod');
304 my @subfieldsToAllow = split(/ /, $subfieldsToAllowForBatchmod);
306 foreach my $tag (sort keys %{$tagslib}) {
307 # loop through each subfield
308 foreach my $subfield (sort keys %{$tagslib->{$tag}}) {
309 next if subfield_is_koha_internal_p($subfield);
310 next if ($limitededition && !grep { $tag . '$' . $subfield eq $_ } @subfieldsToAllow );
311 next if ($tagslib->{$tag}->{$subfield}->{'tab'} ne "10");
312 # barcode and stocknumber are not meant to be batch-modified
313 next if $tagslib->{$tag}->{$subfield}->{'kohafield'} eq 'items.barcode';
314 next if $tagslib->{$tag}->{$subfield}->{'kohafield'} eq 'items.stocknumber';
315 my %subfield_data;
317 my $index_subfield = int(rand(1000000));
318 if ($subfield eq '@'){
319 $subfield_data{id} = "tag_".$tag."_subfield_00_".$index_subfield;
320 } else {
321 $subfield_data{id} = "tag_".$tag."_subfield_".$subfield."_".$index_subfield;
323 $subfield_data{tag} = $tag;
324 $subfield_data{subfield} = $subfield;
325 $subfield_data{random} = int(rand(1000000)); # why do we need 2 different randoms?
326 # $subfield_data{marc_lib} = $tagslib->{$tag}->{$subfield}->{lib};
327 $subfield_data{marc_lib} ="<span id=\"error$i\" title=\"".$tagslib->{$tag}->{$subfield}->{lib}."\">".$tagslib->{$tag}->{$subfield}->{lib}."</span>";
328 $subfield_data{mandatory} = $tagslib->{$tag}->{$subfield}->{mandatory};
329 $subfield_data{repeatable} = $tagslib->{$tag}->{$subfield}->{repeatable};
330 my ($x,$value);
331 $value =~ s/"/&quot;/g;
332 if ( !$value && $use_default_values) {
333 $value = $tagslib->{$tag}->{$subfield}->{defaultvalue};
334 # get today date & replace YYYY, MM, DD if provided in the default value
335 my ( $year, $month, $day ) = split ',', $today_iso; # FIXME: iso dates don't have commas!
336 $value =~ s/YYYY/$year/g;
337 $value =~ s/MM/$month/g;
338 $value =~ s/DD/$day/g;
340 $subfield_data{visibility} = "display:none;" if (($tagslib->{$tag}->{$subfield}->{hidden} > 4) || ($tagslib->{$tag}->{$subfield}->{hidden} < -4));
341 # testing branch value if IndependentBranches.
343 if ( $tagslib->{$tag}->{$subfield}->{authorised_value} ) {
344 my @authorised_values;
345 my %authorised_lib;
346 # builds list, depending on authorised value...
348 if ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "branches" ) {
349 foreach my $thisbranch (@$branches) {
350 push @authorised_values, $thisbranch->{value};
351 $authorised_lib{$thisbranch->{value}} = $thisbranch->{branchname};
353 $value = "";
355 elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes" ) {
356 push @authorised_values, "";
357 my $sth = $dbh->prepare("select itemtype,description from itemtypes order by description");
358 $sth->execute;
359 while ( my ( $itemtype, $description ) = $sth->fetchrow_array ) {
360 push @authorised_values, $itemtype;
361 $authorised_lib{$itemtype} = $description;
363 $value = "";
365 #---- class_sources
367 elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "cn_source" ) {
368 push @authorised_values, "" unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
370 my $class_sources = GetClassSources();
371 my $default_source = C4::Context->preference("DefaultClassificationSource");
373 foreach my $class_source (sort keys %$class_sources) {
374 next unless $class_sources->{$class_source}->{'used'} or
375 ($value and $class_source eq $value) or
376 ($class_source eq $default_source);
377 push @authorised_values, $class_source;
378 $authorised_lib{$class_source} = $class_sources->{$class_source}->{'description'};
380 $value = '';
382 #---- "true" authorised value
384 else {
385 push @authorised_values, ""; # unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
386 $authorised_values_sth->execute( $tagslib->{$tag}->{$subfield}->{authorised_value}, $branch_limit ? $branch_limit : () );
387 while ( my ( $value, $lib ) = $authorised_values_sth->fetchrow_array ) {
388 push @authorised_values, $value;
389 $authorised_lib{$value} = $lib;
391 $value="";
393 $subfield_data{marc_value} = {
394 type => 'select',
395 id => "tag_".$tag."_subfield_".$subfield."_".$index_subfield,
396 name => "field_value",
397 values => \@authorised_values,
398 labels => \%authorised_lib,
399 default => $value,
401 # it's a thesaurus / authority field
403 elsif ( $tagslib->{$tag}->{$subfield}->{authtypecode} ) {
404 $subfield_data{marc_value} = {
405 type => 'text1',
406 id => $subfield_data{id},
407 value => $value,
408 authtypecode => $tagslib->{$tag}->{$subfield}->{authtypecode},
410 # it's a plugin field
412 elsif ( $tagslib->{$tag}->{$subfield}->{value_builder} ) {
413 # opening plugin
414 my $plugin = C4::Context->intranetdir . "/cataloguing/value_builder/" . $tagslib->{$tag}->{$subfield}->{'value_builder'};
415 if (do $plugin) {
416 my $temp;
417 my $extended_param = plugin_parameters( $dbh, $temp, $tagslib, $subfield_data{id}, \@loop_data );
418 my ( $function_name, $javascript ) = plugin_javascript( $dbh, $temp, $tagslib, $subfield_data{id}, \@loop_data );
419 $subfield_data{marc_value} = {
420 type => 'text2',
421 id => $subfield_data{id},
422 value => $value,
423 function => $function_name,
424 random => $subfield_data{random},
425 javascript => $javascript,
427 } else {
428 warn "Plugin Failed: $plugin";
429 $subfield_data{marc_value} = { # supply default input form
430 type => 'text',
431 id => $subfield_data{id},
432 value => $value,
436 elsif ( $tag eq '' ) { # it's an hidden field
437 $subfield_data{marc_value} = {
438 type => 'hidden',
439 id => $subfield_data{id},
440 value => $value,
443 elsif ( $tagslib->{$tag}->{$subfield}->{'hidden'} ) { # FIXME: shouldn't input type be "hidden" ?
444 $subfield_data{marc_value} = {
445 type => 'text',
446 id => $subfield_data{id},
447 value => $value,
450 elsif ( length($value) > 100
451 or (C4::Context->preference("marcflavour") eq "UNIMARC" and
452 300 <= $tag && $tag < 400 && $subfield eq 'a' )
453 or (C4::Context->preference("marcflavour") eq "MARC21" and
454 500 <= $tag && $tag < 600 )
456 # oversize field (textarea)
457 $subfield_data{marc_value} = {
458 type => 'textarea',
459 id => $subfield_data{id},
460 value => $value,
462 } else {
463 # it's a standard field
464 $subfield_data{marc_value} = {
465 type => 'text',
466 id => $subfield_data{id},
467 value => $value,
470 # $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\">";
471 push (@loop_data, \%subfield_data);
472 $i++
474 } # -- End foreach tag
475 $authorised_values_sth->finish;
479 # what's the next op ? it's what we are not in : an add if we're editing, otherwise, and edit.
480 $template->param(item => \@loop_data);
481 if (@notfoundbarcodes) {
482 my @notfoundbarcodesloop = map{{barcode=>$_}}@notfoundbarcodes;
483 $template->param(notfoundbarcodes => \@notfoundbarcodesloop);
485 $nextop="action"
486 } # -- End action="show"
488 $template->param(%$items_display_hashref) if $items_display_hashref;
489 $template->param(
490 op => $nextop,
492 $template->param( $op => 1 ) if $op;
494 if ($op eq "action") {
496 #my @not_deleted_loop = map{{itemnumber=>$_}}@not_deleted;
498 $template->param(
499 not_deleted_items => $not_deleted_items,
500 deleted_items => $deleted_items,
501 delete_records => $del_records,
502 deleted_records => $deleted_records,
503 not_deleted_loop => \@not_deleted
507 foreach my $error (@errors) {
508 $template->param($error => 1) if $error;
510 $template->param(src => $src);
511 $template->param(biblionumber => $biblionumber);
512 output_html_with_http_headers $input, $cookie, $template->output;
513 exit;
516 # ---------------- Functions
518 sub BuildItemsData{
519 my @itemnumbers=@_;
520 # now, build existiing item list
521 my %witness; #---- stores the list of subfields used at least once, with the "meaning" of the code
522 my @big_array;
523 #---- finds where items.itemnumber is stored
524 my ( $itemtagfield, $itemtagsubfield) = &GetMarcFromKohaField("items.itemnumber", "");
525 my ($branchtagfield, $branchtagsubfield) = &GetMarcFromKohaField("items.homebranch", "");
526 foreach my $itemnumber (@itemnumbers){
527 my $itemdata=GetItem($itemnumber);
528 my $itemmarc=Item2Marc($itemdata);
529 my %this_row;
530 foreach my $field (grep {$_->tag() eq $itemtagfield} $itemmarc->fields()) {
531 # loop through each subfield
532 my $itembranchcode=$field->subfield($branchtagsubfield);
533 if ($itembranchcode && C4::Context->preference("IndependentBranches")) {
534 #verifying rights
535 my $userenv = C4::Context->userenv();
536 unless (C4::Context->IsSuperLibrarian() or (($userenv->{'branch'} eq $itembranchcode))){
537 $this_row{'nomod'}=1;
540 my $tag=$field->tag();
541 foreach my $subfield ($field->subfields) {
542 my ($subfcode,$subfvalue)=@$subfield;
543 next if ($tagslib->{$tag}->{$subfcode}->{tab} ne 10
544 && $tag ne $itemtagfield
545 && $subfcode ne $itemtagsubfield);
547 $witness{$subfcode} = $tagslib->{$tag}->{$subfcode}->{lib} if ($tagslib->{$tag}->{$subfcode}->{tab} eq 10);
548 if ($tagslib->{$tag}->{$subfcode}->{tab} eq 10) {
549 $this_row{$subfcode}=GetAuthorisedValueDesc( $tag,
550 $subfcode, $subfvalue, '', $tagslib)
551 || $subfvalue;
554 $this_row{itemnumber} = $subfvalue if ($tag eq $itemtagfield && $subfcode eq $itemtagsubfield);
558 # grab title, author, and ISBN to identify bib that the item
559 # belongs to in the display
560 my $biblio=GetBiblioData($$itemdata{biblionumber});
561 $this_row{title} = $biblio->{title};
562 $this_row{author} = $biblio->{author};
563 $this_row{isbn} = $biblio->{isbn};
564 $this_row{biblionumber} = $biblio->{biblionumber};
566 if (%this_row) {
567 push(@big_array, \%this_row);
570 @big_array = sort {$a->{0} cmp $b->{0}} @big_array;
572 # now, construct template !
573 # First, the existing items for display
574 my @item_value_loop;
575 my @witnesscodessorted=sort keys %witness;
576 for my $row ( @big_array ) {
577 my %row_data;
578 my @item_fields = map +{ field => $_ || '' }, @$row{ @witnesscodessorted };
579 $row_data{item_value} = [ @item_fields ];
580 $row_data{itemnumber} = $row->{itemnumber};
581 #reporting this_row values
582 $row_data{'nomod'} = $row->{'nomod'};
583 $row_data{bibinfo} = $row->{bibinfo};
584 $row_data{author} = $row->{author};
585 $row_data{title} = $row->{title};
586 $row_data{isbn} = $row->{isbn};
587 $row_data{biblionumber} = $row->{biblionumber};
588 my $is_on_loan = C4::Circulation::IsItemIssued( $row->{itemnumber} );
589 $row_data{onloan} = $is_on_loan ? 1 : 0;
590 push(@item_value_loop,\%row_data);
592 my @header_loop=map { { header_value=> $witness{$_}} } @witnesscodessorted;
594 return { item_loop => \@item_value_loop, item_header_loop => \@header_loop };
597 #BE WARN : it is not the general case
598 # This function can be OK in the item marc record special case
599 # Where subfield is not repeated
600 # And where we are sure that field should correspond
601 # And $tag>10
602 sub UpdateMarcWith {
603 my ($marcfrom,$marcto)=@_;
604 my ( $itemtag, $itemtagsubfield) = &GetMarcFromKohaField("items.itemnumber", "");
605 my $fieldfrom=$marcfrom->field($itemtag);
606 my @fields_to=$marcto->field($itemtag);
607 my $modified = 0;
608 foreach my $subfield ( $fieldfrom->subfields() ) {
609 foreach my $field_to_update ( @fields_to ) {
610 if ( $subfield->[1] ) {
611 unless ( $field_to_update->subfield($subfield->[0]) eq $subfield->[1] ) {
612 $modified++;
613 $field_to_update->update( $subfield->[0] => $subfield->[1] );
616 else {
617 $modified++;
618 $field_to_update->delete_subfield( code => $subfield->[0] );
622 return $modified;
625 sub find_value {
626 my ($tagfield,$insubfield,$record) = @_;
627 my $result;
628 my $indicator;
629 foreach my $field ($record->field($tagfield)) {
630 my @subfields = $field->subfields();
631 foreach my $subfield (@subfields) {
632 if (@$subfield[0] eq $insubfield) {
633 $result .= @$subfield[1];
634 $indicator = $field->indicator(1).$field->indicator(2);
638 return($indicator,$result);
641 # ----------------------------
642 # Background functions
645 sub add_results_to_template {
646 my $template = shift;
647 my $results = shift;
648 $template->param(map { $_ => $results->{$_} } keys %{ $results });
651 sub add_saved_job_results_to_template {
652 my $template = shift;
653 my $completedJobID = shift;
654 my $job = C4::BackgroundJob->fetch($sessionID, $completedJobID);
655 my $results = $job->results();
656 add_results_to_template($template, $results);
658 my $fields = $job->get("modified_fields");
659 my $items = $job->get("modified_items");
660 $template->param(
661 modified_items => $items,
662 modified_fields => $fields,
666 sub put_in_background {
667 my $job_size = shift;
669 my $job = C4::BackgroundJob->new($sessionID, "test", $ENV{'SCRIPT_NAME'}, $job_size);
670 my $jobID = $job->id();
672 # fork off
673 if (my $pid = fork) {
674 # parent
675 # return job ID as JSON
677 # prevent parent exiting from
678 # destroying the kid's database handle
679 # FIXME: according to DBI doc, this may not work for Oracle
680 $dbh->{InactiveDestroy} = 1;
682 my $reply = CGI->new("");
683 print $reply->header(-type => 'text/html');
684 print '{"jobID":"' . $jobID . '"}';
685 exit 0;
686 } elsif (defined $pid) {
687 # child
688 # close STDOUT to signal to Apache that
689 # we're now running in the background
690 close STDOUT;
691 close STDERR;
692 } else {
693 # fork failed, so exit immediately
694 warn "fork failed while attempting to run $ENV{'SCRIPT_NAME'} as a background job";
695 exit 0;
697 return $job;