Bug 15758: Koha::Libraries - Move onlymine to C4::Context::only_my_library
[koha.git] / tools / batchMod.pl
blob439e4243f76afa9c216df89d6a8e5df14ad2c2db
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::Branch;
32 use C4::BackgroundJob;
33 use C4::ClassSource;
34 use C4::Debug;
35 use C4::Members;
36 use MARC::File::XML;
37 use List::MoreUtils qw/uniq/;
38 use Koha::DateUtils;
40 my $input = new CGI;
41 my $dbh = C4::Context->dbh;
42 my $error = $input->param('error');
43 my @itemnumbers = $input->multi_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 restricted item edition permission?
74 my $uid = $loggedinuser ? GetMember( borrowernumber => $loggedinuser )->{userid} : undef;
75 my $restrictededition = $uid ? haspermission($uid, {'tools' => 'items_batchmod_restricted'}) : undef;
76 # In case user is a superlibrarian, edition is not restricted
77 $restrictededition = 0 if ($restrictededition != 0 && C4::Context->IsSuperLibrarian());
79 $template->param(del => $del);
81 my $itemrecord;
82 my $nextop="";
83 my @errors; # store errors found while checking data BEFORE saving item.
84 my $items_display_hashref;
85 our $tagslib = &GetMarcStructure(1);
87 my $deleted_items = 0; # Number of deleted items
88 my $deleted_records = 0; # Number of deleted records ( with no items attached )
89 my $not_deleted_items = 0; # Number of items that could not be deleted
90 my @not_deleted; # List of the itemnumbers that could not be deleted
91 my $modified_items = 0; # Numbers of modified items
92 my $modified_fields = 0; # Numbers of modified fields
94 my %cookies = parse CGI::Cookie($cookie);
95 my $sessionID = $cookies{'CGISESSID'}->value;
98 #--- ----------------------------------------------------------------------------
99 if ($op eq "action") {
100 #-------------------------------------------------------------------------------
101 my @tags = $input->multi_param('tag');
102 my @subfields = $input->multi_param('subfield');
103 my @values = $input->multi_param('field_value');
104 my @disabled = $input->multi_param('disable_input');
105 # build indicator hash.
106 my @ind_tag = $input->multi_param('ind_tag');
107 my @indicator = $input->multi_param('indicator');
109 # Is there something to modify ?
110 # TODO : We shall use this var to warn the user in case no modification was done to the items
111 my $values_to_modify = scalar(grep {!/^$/} @values);
112 my $values_to_blank = scalar(@disabled);
113 my $marcitem;
115 # Once the job is done
116 if ($completedJobID) {
117 # If we have a reasonable amount of items, we display them
118 if (scalar(@itemnumbers) <= ( C4::Context->preference("MaxItemsToDisplayForBatchDel") // 1000 ) ) {
119 $items_display_hashref=BuildItemsData(@itemnumbers);
120 } else {
121 # Else, we only display the barcode
122 my @simple_items_display = map {{ itemnumber => $_, barcode => (GetBarcodeFromItemnumber($_) or ""), biblionumber => (GetBiblionumberFromItemnumber($_) or "") }} @itemnumbers;
123 $template->param("simple_items_display" => \@simple_items_display);
126 # Setting the job as done
127 my $job = C4::BackgroundJob->fetch($sessionID, $completedJobID);
129 # Calling the template
130 add_saved_job_results_to_template($template, $completedJobID);
132 } else {
133 # While the job is getting done
135 # Job size is the number of items we have to process
136 my $job_size = scalar(@itemnumbers);
137 my $job = undef;
139 # If we asked for background processing
140 if ($runinbackground) {
141 $job = put_in_background($job_size);
144 #initializing values for updates
145 my ( $itemtagfield, $itemtagsubfield) = &GetMarcFromKohaField("items.itemnumber", "");
146 if ($values_to_modify){
147 my $xml = TransformHtmlToXml(\@tags,\@subfields,\@values,\@indicator,\@ind_tag, 'ITEM');
148 $marcitem = MARC::Record::new_from_xml($xml, 'UTF-8');
150 if ($values_to_blank){
151 foreach my $disabledsubf (@disabled){
152 if ($marcitem && $marcitem->field($itemtagfield)){
153 $marcitem->field($itemtagfield)->update( $disabledsubf => "" );
155 else {
156 $marcitem = MARC::Record->new();
157 $marcitem->append_fields( MARC::Field->new( $itemtagfield, '', '', $disabledsubf => "" ) );
162 # For each item
163 my $i = 1;
164 foreach my $itemnumber(@itemnumbers){
166 $job->progress($i) if $runinbackground;
167 my $itemdata = GetItem($itemnumber);
168 if ( $del ){
169 my $return = DelItemCheck( $itemdata->{'biblionumber'}, $itemdata->{'itemnumber'});
170 if ($return == 1) {
171 $deleted_items++;
172 } else {
173 $not_deleted_items++;
174 push @not_deleted,
175 { biblionumber => $itemdata->{'biblionumber'},
176 itemnumber => $itemdata->{'itemnumber'},
177 barcode => $itemdata->{'barcode'},
178 title => $itemdata->{'title'},
179 $return => 1
183 # If there are no items left, delete the biblio
184 if ( $del_records ) {
185 my $itemscount = GetItemsCount($itemdata->{'biblionumber'});
186 if ( $itemscount == 0 ) {
187 my $error = DelBiblio($itemdata->{'biblionumber'});
188 $deleted_records++ unless ( $error );
191 } else {
192 if ($values_to_modify || $values_to_blank) {
193 my $localmarcitem = Item2Marc($itemdata);
195 my $modified = UpdateMarcWith( $marcitem, $localmarcitem );
196 if ( $modified ) {
197 eval {
198 if ( my $item = ModItemFromMarc( $localmarcitem, $itemdata->{biblionumber}, $itemnumber ) ) {
199 LostItem($itemnumber, 'MARK RETURNED') if $item->{itemlost} and not $itemdata->{itemlost};
203 if ( $runinbackground ) {
204 $modified_items++ if $modified;
205 $modified_fields += $modified;
206 $job->set({
207 modified_items => $modified_items,
208 modified_fields => $modified_fields,
213 $i++;
218 #-------------------------------------------------------------------------------
219 # build screen with existing items. and "new" one
220 #-------------------------------------------------------------------------------
222 if ($op eq "show"){
223 my $filefh = $input->upload('uploadfile');
224 my $filecontent = $input->param('filecontent');
225 my @notfoundbarcodes;
227 my @contentlist;
228 if ($filefh){
229 while (my $content=<$filefh>){
230 $content =~ s/[\r\n]*$//;
231 push @contentlist, $content if $content;
234 if ($filecontent eq 'barcode_file') {
235 foreach my $barcode (@contentlist) {
237 my $itemnumber = GetItemnumberFromBarcode($barcode);
238 if ($itemnumber) {
239 push @itemnumbers,$itemnumber;
240 } else {
241 push @notfoundbarcodes, $barcode;
245 elsif ( $filecontent eq 'itemid_file') {
246 @itemnumbers = @contentlist;
248 } else {
249 if (defined $biblionumber){
250 my @all_items = GetItemsInfo( $biblionumber );
251 foreach my $itm (@all_items) {
252 push @itemnumbers, $itm->{itemnumber};
255 if ( my $list=$input->param('barcodelist')){
256 push my @barcodelist, uniq( split(/\s\n/, $list) );
258 foreach my $barcode (@barcodelist) {
260 my $itemnumber = GetItemnumberFromBarcode($barcode);
261 if ($itemnumber) {
262 push @itemnumbers,$itemnumber;
263 } else {
264 push @notfoundbarcodes, $barcode;
271 # Flag to tell the template there are valid results, hidden or not
272 if(scalar(@itemnumbers) > 0){ $template->param("itemresults" => 1); }
273 # Only display the items if there are no more than pref MaxItemsToProcessForBatchMod or MaxItemsToDisplayForBatchDel
274 my $max_items = $del
275 ? C4::Context->preference("MaxItemsToDisplayForBatchDel")
276 : C4::Context->preference("MaxItemsToProcessForBatchMod");
277 if (scalar(@itemnumbers) <= ( $max_items // 1000 ) ) {
278 $items_display_hashref=BuildItemsData(@itemnumbers);
279 } else {
280 $template->param("too_many_items" => scalar(@itemnumbers));
281 # Even if we do not display the items, we need the itemnumbers
282 $template->param(itemnumbers_array => \@itemnumbers);
284 # now, build the item form for entering a new item
285 my @loop_data =();
286 my $i=0;
287 my $branch_limit = C4::Context->userenv ? C4::Context->userenv->{"branch"} : "";
288 my $query = qq{SELECT authorised_value, lib FROM authorised_values};
289 $query .= qq{ LEFT JOIN authorised_values_branches ON ( id = av_id ) } if $branch_limit;
290 $query .= qq{ WHERE category = ?};
291 $query .= qq{ AND ( branchcode = ? OR branchcode IS NULL ) } if $branch_limit;
292 $query .= qq{ GROUP BY lib ORDER BY lib, lib_opac};
293 my $authorised_values_sth = $dbh->prepare( $query );
295 my $libraries = Koha::Libraries->search({}, { order_by => ['branchname'] })->unblessed;# build once ahead of time, instead of multiple times later.
297 # Adding a default choice, in case the user does not want to modify the branch
298 my $nochange_branch = { branchname => '', value => '', selected => 1 };
299 unshift (@$libraries, $nochange_branch);
301 my $pref_itemcallnumber = C4::Context->preference('itemcallnumber');
303 # Getting list of subfields to keep when restricted batchmod edit is enabled
304 my $subfieldsToAllowForBatchmod = C4::Context->preference('SubfieldsToAllowForRestrictedBatchmod');
305 my $allowAllSubfields = (
306 not defined $subfieldsToAllowForBatchmod
307 or $subfieldsToAllowForBatchmod == q||
308 ) ? 1 : 0;
309 my @subfieldsToAllow = split(/ /, $subfieldsToAllowForBatchmod);
311 foreach my $tag (sort keys %{$tagslib}) {
312 # loop through each subfield
313 foreach my $subfield (sort keys %{$tagslib->{$tag}}) {
314 next if IsMarcStructureInternal( $tagslib->{$tag}{$subfield} );
315 next if (not $allowAllSubfields and $restrictededition && !grep { $tag . '$' . $subfield eq $_ } @subfieldsToAllow );
316 next if ($tagslib->{$tag}->{$subfield}->{'tab'} ne "10");
317 # barcode and stocknumber are not meant to be batch-modified
318 next if $tagslib->{$tag}->{$subfield}->{'kohafield'} eq 'items.barcode';
319 next if $tagslib->{$tag}->{$subfield}->{'kohafield'} eq 'items.stocknumber';
320 my %subfield_data;
322 my $index_subfield = int(rand(1000000));
323 if ($subfield eq '@'){
324 $subfield_data{id} = "tag_".$tag."_subfield_00_".$index_subfield;
325 } else {
326 $subfield_data{id} = "tag_".$tag."_subfield_".$subfield."_".$index_subfield;
328 $subfield_data{tag} = $tag;
329 $subfield_data{subfield} = $subfield;
330 $subfield_data{marc_lib} ="<span id=\"error$i\" title=\"".$tagslib->{$tag}->{$subfield}->{lib}."\">".$tagslib->{$tag}->{$subfield}->{lib}."</span>";
331 $subfield_data{mandatory} = $tagslib->{$tag}->{$subfield}->{mandatory};
332 $subfield_data{repeatable} = $tagslib->{$tag}->{$subfield}->{repeatable};
333 my ($x,$value);
334 $value =~ s/"/&quot;/g;
335 if ( !$value && $use_default_values) {
336 $value = $tagslib->{$tag}->{$subfield}->{defaultvalue};
337 # get today date & replace YYYY, MM, DD if provided in the default value
338 my $today = dt_from_string;
339 my $year = $today->year;
340 my $month = $today->month;
341 my $day = $today->day;
342 $value =~ s/YYYY/$year/g;
343 $value =~ s/MM/$month/g;
344 $value =~ s/DD/$day/g;
346 $subfield_data{visibility} = "display:none;" if (($tagslib->{$tag}->{$subfield}->{hidden} > 4) || ($tagslib->{$tag}->{$subfield}->{hidden} < -4));
347 # testing branch value if IndependentBranches.
349 if ( $tagslib->{$tag}->{$subfield}->{authorised_value} ) {
350 my @authorised_values;
351 my %authorised_lib;
352 # builds list, depending on authorised value...
354 if ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "branches" ) {
355 foreach my $library (@$libraries) {
356 push @authorised_values, $library->{branchcode};
357 $authorised_lib{$library->{branchcode}} = $library->{branchname};
359 $value = "";
361 elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes" ) {
362 push @authorised_values, "";
363 my $itemtypes = GetItemTypes( style => 'array' );
364 for my $itemtype ( @$itemtypes ) {
365 push @authorised_values, $itemtype->{itemtype};
366 $authorised_lib{$itemtype->{itemtype}} = $itemtype->{translated_description};
368 $value = "";
370 #---- class_sources
372 elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "cn_source" ) {
373 push @authorised_values, "" unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
375 my $class_sources = GetClassSources();
376 my $default_source = C4::Context->preference("DefaultClassificationSource");
378 foreach my $class_source (sort keys %$class_sources) {
379 next unless $class_sources->{$class_source}->{'used'} or
380 ($value and $class_source eq $value) or
381 ($class_source eq $default_source);
382 push @authorised_values, $class_source;
383 $authorised_lib{$class_source} = $class_sources->{$class_source}->{'description'};
385 $value = '';
387 #---- "true" authorised value
389 else {
390 push @authorised_values, ""; # unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
391 $authorised_values_sth->execute( $tagslib->{$tag}->{$subfield}->{authorised_value}, $branch_limit ? $branch_limit : () );
392 while ( my ( $value, $lib ) = $authorised_values_sth->fetchrow_array ) {
393 push @authorised_values, $value;
394 $authorised_lib{$value} = $lib;
396 $value="";
398 $subfield_data{marc_value} = {
399 type => 'select',
400 id => "tag_".$tag."_subfield_".$subfield."_".$index_subfield,
401 name => "field_value",
402 values => \@authorised_values,
403 labels => \%authorised_lib,
404 default => $value,
406 # it's a thesaurus / authority field
408 elsif ( $tagslib->{$tag}->{$subfield}->{authtypecode} ) {
409 $subfield_data{marc_value} = {
410 type => 'text1',
411 id => $subfield_data{id},
412 value => $value,
413 authtypecode => $tagslib->{$tag}->{$subfield}->{authtypecode},
416 elsif ( $tagslib->{$tag}->{$subfield}->{value_builder} ) { # plugin
417 require Koha::FrameworkPlugin;
418 my $plugin = Koha::FrameworkPlugin->new( {
419 name => $tagslib->{$tag}->{$subfield}->{'value_builder'},
420 item_style => 1,
422 my $temp;
423 my $pars= { dbh => $dbh, record => $temp, tagslib => $tagslib,
424 id => $subfield_data{id}, tabloop => \@loop_data };
425 $plugin->build( $pars );
426 if( !$plugin->errstr ) {
427 $subfield_data{marc_value} = {
428 type => 'text2',
429 id => $subfield_data{id},
430 value => $value,
431 javascript => $plugin->javascript,
432 noclick => $plugin->noclick,
434 } else {
435 warn $plugin->errstr;
436 $subfield_data{marc_value} = { # supply default input form
437 type => 'text',
438 id => $subfield_data{id},
439 value => $value,
443 elsif ( $tag eq '' ) { # it's an hidden field
444 $subfield_data{marc_value} = {
445 type => 'hidden',
446 id => $subfield_data{id},
447 value => $value,
450 elsif ( $tagslib->{$tag}->{$subfield}->{'hidden'} ) { # FIXME: shouldn't input type be "hidden" ?
451 $subfield_data{marc_value} = {
452 type => 'text',
453 id => $subfield_data{id},
454 value => $value,
457 elsif ( length($value) > 100
458 or (C4::Context->preference("marcflavour") eq "UNIMARC" and
459 300 <= $tag && $tag < 400 && $subfield eq 'a' )
460 or (C4::Context->preference("marcflavour") eq "MARC21" and
461 500 <= $tag && $tag < 600 )
463 # oversize field (textarea)
464 $subfield_data{marc_value} = {
465 type => 'textarea',
466 id => $subfield_data{id},
467 value => $value,
469 } else {
470 # it's a standard field
471 $subfield_data{marc_value} = {
472 type => 'text',
473 id => $subfield_data{id},
474 value => $value,
477 # $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\">";
478 push (@loop_data, \%subfield_data);
479 $i++
481 } # -- End foreach tag
482 $authorised_values_sth->finish;
486 # what's the next op ? it's what we are not in : an add if we're editing, otherwise, and edit.
487 $template->param(item => \@loop_data);
488 if (@notfoundbarcodes) {
489 my @notfoundbarcodesloop = map{{barcode=>$_}}@notfoundbarcodes;
490 $template->param(notfoundbarcodes => \@notfoundbarcodesloop);
492 $nextop="action"
493 } # -- End action="show"
495 $template->param(%$items_display_hashref) if $items_display_hashref;
496 $template->param(
497 op => $nextop,
499 $template->param( $op => 1 ) if $op;
501 if ($op eq "action") {
503 #my @not_deleted_loop = map{{itemnumber=>$_}}@not_deleted;
505 $template->param(
506 not_deleted_items => $not_deleted_items,
507 deleted_items => $deleted_items,
508 delete_records => $del_records,
509 deleted_records => $deleted_records,
510 not_deleted_loop => \@not_deleted
514 foreach my $error (@errors) {
515 $template->param($error => 1) if $error;
517 $template->param(src => $src);
518 $template->param(biblionumber => $biblionumber);
519 output_html_with_http_headers $input, $cookie, $template->output;
520 exit;
523 # ---------------- Functions
525 sub BuildItemsData{
526 my @itemnumbers=@_;
527 # now, build existiing item list
528 my %witness; #---- stores the list of subfields used at least once, with the "meaning" of the code
529 my @big_array;
530 #---- finds where items.itemnumber is stored
531 my ( $itemtagfield, $itemtagsubfield) = &GetMarcFromKohaField("items.itemnumber", "");
532 my ($branchtagfield, $branchtagsubfield) = &GetMarcFromKohaField("items.homebranch", "");
533 foreach my $itemnumber (@itemnumbers){
534 my $itemdata=GetItem($itemnumber);
535 my $itemmarc=Item2Marc($itemdata);
536 my %this_row;
537 foreach my $field (grep {$_->tag() eq $itemtagfield} $itemmarc->fields()) {
538 # loop through each subfield
539 my $itembranchcode=$field->subfield($branchtagsubfield);
540 if ($itembranchcode && C4::Context->preference("IndependentBranches")) {
541 #verifying rights
542 my $userenv = C4::Context->userenv();
543 unless (C4::Context->IsSuperLibrarian() or (($userenv->{'branch'} eq $itembranchcode))){
544 $this_row{'nomod'}=1;
547 my $tag=$field->tag();
548 foreach my $subfield ($field->subfields) {
549 my ($subfcode,$subfvalue)=@$subfield;
550 next if ($tagslib->{$tag}->{$subfcode}->{tab} ne 10
551 && $tag ne $itemtagfield
552 && $subfcode ne $itemtagsubfield);
554 $witness{$subfcode} = $tagslib->{$tag}->{$subfcode}->{lib} if ($tagslib->{$tag}->{$subfcode}->{tab} eq 10);
555 if ($tagslib->{$tag}->{$subfcode}->{tab} eq 10) {
556 $this_row{$subfcode}=GetAuthorisedValueDesc( $tag,
557 $subfcode, $subfvalue, '', $tagslib)
558 || $subfvalue;
561 $this_row{itemnumber} = $subfvalue if ($tag eq $itemtagfield && $subfcode eq $itemtagsubfield);
565 # grab title, author, and ISBN to identify bib that the item
566 # belongs to in the display
567 my $biblio=GetBiblioData($$itemdata{biblionumber});
568 $this_row{title} = $biblio->{title};
569 $this_row{author} = $biblio->{author};
570 $this_row{isbn} = $biblio->{isbn};
571 $this_row{biblionumber} = $biblio->{biblionumber};
573 if (%this_row) {
574 push(@big_array, \%this_row);
577 @big_array = sort {$a->{0} cmp $b->{0}} @big_array;
579 # now, construct template !
580 # First, the existing items for display
581 my @item_value_loop;
582 my @witnesscodessorted=sort keys %witness;
583 for my $row ( @big_array ) {
584 my %row_data;
585 my @item_fields = map +{ field => $_ || '' }, @$row{ @witnesscodessorted };
586 $row_data{item_value} = [ @item_fields ];
587 $row_data{itemnumber} = $row->{itemnumber};
588 #reporting this_row values
589 $row_data{'nomod'} = $row->{'nomod'};
590 $row_data{bibinfo} = $row->{bibinfo};
591 $row_data{author} = $row->{author};
592 $row_data{title} = $row->{title};
593 $row_data{isbn} = $row->{isbn};
594 $row_data{biblionumber} = $row->{biblionumber};
595 my $is_on_loan = C4::Circulation::IsItemIssued( $row->{itemnumber} );
596 $row_data{onloan} = $is_on_loan ? 1 : 0;
597 push(@item_value_loop,\%row_data);
599 my @header_loop=map { { header_value=> $witness{$_}} } @witnesscodessorted;
601 return { item_loop => \@item_value_loop, item_header_loop => \@header_loop };
604 #BE WARN : it is not the general case
605 # This function can be OK in the item marc record special case
606 # Where subfield is not repeated
607 # And where we are sure that field should correspond
608 # And $tag>10
609 sub UpdateMarcWith {
610 my ($marcfrom,$marcto)=@_;
611 my ( $itemtag, $itemtagsubfield) = &GetMarcFromKohaField("items.itemnumber", "");
612 my $fieldfrom=$marcfrom->field($itemtag);
613 my @fields_to=$marcto->field($itemtag);
614 my $modified = 0;
615 foreach my $subfield ( $fieldfrom->subfields() ) {
616 foreach my $field_to_update ( @fields_to ) {
617 if ( $subfield->[1] ) {
618 unless ( $field_to_update->subfield($subfield->[0]) eq $subfield->[1] ) {
619 $modified++;
620 $field_to_update->update( $subfield->[0] => $subfield->[1] );
623 else {
624 $modified++;
625 $field_to_update->delete_subfield( code => $subfield->[0] );
629 return $modified;
632 sub find_value {
633 my ($tagfield,$insubfield,$record) = @_;
634 my $result;
635 my $indicator;
636 foreach my $field ($record->field($tagfield)) {
637 my @subfields = $field->subfields();
638 foreach my $subfield (@subfields) {
639 if (@$subfield[0] eq $insubfield) {
640 $result .= @$subfield[1];
641 $indicator = $field->indicator(1).$field->indicator(2);
645 return($indicator,$result);
648 # ----------------------------
649 # Background functions
652 sub add_results_to_template {
653 my $template = shift;
654 my $results = shift;
655 $template->param(map { $_ => $results->{$_} } keys %{ $results });
658 sub add_saved_job_results_to_template {
659 my $template = shift;
660 my $completedJobID = shift;
661 my $job = C4::BackgroundJob->fetch($sessionID, $completedJobID);
662 my $results = $job->results();
663 add_results_to_template($template, $results);
665 my $fields = $job->get("modified_fields");
666 my $items = $job->get("modified_items");
667 $template->param(
668 modified_items => $items,
669 modified_fields => $fields,
673 sub put_in_background {
674 my $job_size = shift;
676 my $job = C4::BackgroundJob->new($sessionID, "test", '/cgi-bin/koha/tools/batchMod.pl', $job_size);
677 my $jobID = $job->id();
679 # fork off
680 if (my $pid = fork) {
681 # parent
682 # return job ID as JSON
684 # prevent parent exiting from
685 # destroying the kid's database handle
686 # FIXME: according to DBI doc, this may not work for Oracle
687 $dbh->{InactiveDestroy} = 1;
689 my $reply = CGI->new("");
690 print $reply->header(-type => 'text/html');
691 print '{"jobID":"' . $jobID . '"}';
692 exit 0;
693 } elsif (defined $pid) {
694 # child
695 # close STDOUT to signal to Apache that
696 # we're now running in the background
697 close STDOUT;
698 close STDERR;
699 } else {
700 # fork failed, so exit immediately
701 warn "fork failed while attempting to run tools/batchMod.pl as a background job";
702 exit 0;
704 return $job;