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
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.
23 #use warnings; FIXME - Bug 2505
30 use C4
::Koha
; # XXX subfield_is_koha_internal_p
31 use C4
::Branch
; # XXX subfield_is_koha_internal_p
32 use C4
::BackgroundJob
;
39 my $dbh = C4
::Context
->dbh;
40 my $error = $input->param('error');
41 my @itemnumbers = $input->param('itemnumber');
42 my $biblionumber = $input->param('biblionumber');
43 my $op = $input->param('op');
44 my $del = $input->param('del');
45 my $del_records = $input->param('del_records');
46 my $completedJobID = $input->param('completedJobID');
47 my $runinbackground = $input->param('runinbackground');
48 my $src = $input->param('src');
54 $template_name = "tools/batchMod.tmpl";
55 $template_flag = { tools
=> '*' };
57 $template_name = ($del) ?
"tools/batchMod-del.tmpl" : "tools/batchMod-edit.tmpl";
58 $template_flag = ($del) ?
{ tools
=> 'items_batchdel' } : { tools
=> 'items_batchmod' };
62 my ($template, $loggedinuser, $cookie)
63 = get_template_and_user
({template_name
=> $template_name,
67 flagsrequired
=> $template_flag,
71 my $today_iso = C4
::Dates
->today('iso');
72 $template->param(today_iso
=> $today_iso);
73 $template->param(del
=> $del);
77 my @errors; # store errors found while checking data BEFORE saving item.
78 my $items_display_hashref;
80 my $tagslib = &GetMarcStructure
(1,$frameworkcode);
82 my $deleted_items = 0; # Number of deleted items
83 my $deleted_records = 0; # Number of deleted records ( with no items attached )
84 my $not_deleted_items = 0; # Number of items that could not be deleted
85 my @not_deleted; # List of the itemnumbers that could not be deleted
87 my %cookies = parse CGI
::Cookie
($cookie);
88 my $sessionID = $cookies{'CGISESSID'}->value;
91 #--- ----------------------------------------------------------------------------
92 if ($op eq "action") {
93 #-------------------------------------------------------------------------------
94 my @tags = $input->param('tag');
95 my @subfields = $input->param('subfield');
96 my @values = $input->param('field_value');
97 my @disabled = $input->param('disable_input');
98 # build indicator hash.
99 my @ind_tag = $input->param('ind_tag');
100 my @indicator = $input->param('indicator');
102 # Is there something to modify ?
103 # TODO : We shall use this var to warn the user in case no modification was done to the items
104 my $values_to_modify = scalar(grep {!/^$/} @values);
105 my $values_to_blank = scalar(@disabled);
108 # Once the job is done
109 if ($completedJobID) {
110 # If we have a reasonable amount of items, we display them
111 if (scalar(@itemnumbers) <= 1000) {
112 $items_display_hashref=BuildItemsData
(@itemnumbers);
114 # Else, we only display the barcode
115 my @simple_items_display = map {{ itemnumber
=> $_, barcode
=> (GetBarcodeFromItemnumber
($_) or ""), biblionumber
=> (GetBiblionumberFromItemnumber
($_) or "") }} @itemnumbers;
116 $template->param("simple_items_display" => \
@simple_items_display);
119 # Setting the job as done
120 my $job = C4
::BackgroundJob
->fetch($sessionID, $completedJobID);
122 # Calling the template
123 add_saved_job_results_to_template
($template, $completedJobID);
126 # While the job is getting done
128 # Job size is the number of items we have to process
129 my $job_size = scalar(@itemnumbers);
132 # If we asked for background processing
133 if ($runinbackground) {
134 $job = put_in_background
($job_size);
137 #initializing values for updates
138 my ( $itemtagfield, $itemtagsubfield) = &GetMarcFromKohaField
("items.itemnumber", "");
139 if ($values_to_modify){
140 my $xml = TransformHtmlToXml
(\
@tags,\
@subfields,\
@values,\
@indicator,\
@ind_tag, 'ITEM');
142 $marcitem = MARC
::Record
::new_from_xml
($xml, 'UTF-8');
144 if ($values_to_blank){
145 foreach my $disabledsubf (@disabled){
146 if ($marcitem && $marcitem->field($itemtagfield)){
147 $marcitem->field($itemtagfield)->update( $disabledsubf => "" );
150 $marcitem = MARC
::Record
->new();
151 $marcitem->append_fields( MARC
::Field
->new( $itemtagfield, '', '', $disabledsubf => "" ) );
158 foreach my $itemnumber(@itemnumbers){
160 $job->progress($i) if $runinbackground;
161 my $itemdata = GetItem
($itemnumber);
162 if ($input->param("del")){
163 my $return = DelItemCheck
(C4
::Context
->dbh, $itemdata->{'biblionumber'}, $itemdata->{'itemnumber'});
167 $not_deleted_items++;
169 { biblionumber
=> $itemdata->{'biblionumber'},
170 itemnumber
=> $itemdata->{'itemnumber'},
171 barcode
=> $itemdata->{'barcode'},
172 title
=> $itemdata->{'title'},
177 # If there are no items left, delete the biblio
178 if ( $del_records ) {
179 my $itemscount = GetItemsCount
($itemdata->{'biblionumber'});
180 if ( $itemscount == 0 ) {
181 my $error = DelBiblio
($itemdata->{'biblionumber'});
182 $deleted_records++ unless ( $error );
186 if ($values_to_modify || $values_to_blank) {
187 my $localmarcitem = Item2Marc
($itemdata);
188 UpdateMarcWith
( $marcitem, $localmarcitem );
190 if ( my $item = ModItemFromMarc
( $localmarcitem, $itemdata->{biblionumber
}, $itemnumber ) ) {
191 LostItem
($itemnumber, 'MARK RETURNED', 'CHARGE FEE') if $item->{itemlost
};
201 #-------------------------------------------------------------------------------
202 # build screen with existing items. and "new" one
203 #-------------------------------------------------------------------------------
206 my $filefh = $input->upload('uploadfile');
207 my $filecontent = $input->param('filecontent');
208 my @notfoundbarcodes;
212 while (my $content=<$filefh>){
213 $content =~ s/[\r\n]*$//;
214 push @contentlist, $content if $content;
217 if ($filecontent eq 'barcode_file') {
218 foreach my $barcode (@contentlist) {
220 my $itemnumber = GetItemnumberFromBarcode
($barcode);
222 push @itemnumbers,$itemnumber;
224 push @notfoundbarcodes, $barcode;
228 elsif ( $filecontent eq 'itemid_file') {
229 @itemnumbers = @contentlist;
232 if (defined $biblionumber){
233 my @all_items = GetItemsInfo
( $biblionumber );
234 foreach my $itm (@all_items) {
235 push @itemnumbers, $itm->{itemnumber
};
238 if ( my $list=$input->param('barcodelist')){
239 push my @barcodelist, split(/\s\n/, $list);
241 foreach my $barcode (@barcodelist) {
243 my $itemnumber = GetItemnumberFromBarcode
($barcode);
245 push @itemnumbers,$itemnumber;
247 push @notfoundbarcodes, $barcode;
254 # Flag to tell the template there are valid results, hidden or not
255 if(scalar(@itemnumbers) > 0){ $template->param("itemresults" => 1); }
256 # Only display the items if there are no more than 1000
257 if (scalar(@itemnumbers) <= 1000) {
258 $items_display_hashref=BuildItemsData
(@itemnumbers);
260 $template->param("too_many_items" => scalar(@itemnumbers));
261 # Even if we do not display the items, we need the itemnumbers
262 my @itemnumbers_hashref = map {{itemnumber
=> $_}} @itemnumbers;
263 $template->param("itemnumbers_hashref" => \
@itemnumbers_hashref);
265 # now, build the item form for entering a new item
268 my $authorised_values_sth = $dbh->prepare("SELECT authorised_value,lib FROM authorised_values WHERE category=? ORDER BY lib");
270 my $branches = GetBranchesLoop
(); # build once ahead of time, instead of multiple times later.
272 # Adding a default choice, in case the user does not want to modify the branch
273 my $nochange_branch = { branchname
=> '', value
=> '', selected
=> 1 };
274 unshift (@
$branches, $nochange_branch);
276 my $pref_itemcallnumber = C4
::Context
->preference('itemcallnumber');
279 foreach my $tag (sort keys %{$tagslib}) {
280 # loop through each subfield
281 foreach my $subfield (sort keys %{$tagslib->{$tag}}) {
282 next if subfield_is_koha_internal_p
($subfield);
283 next if ($tagslib->{$tag}->{$subfield}->{'tab'} ne "10");
284 # barcode and stocknumber are not meant to be batch-modified
285 next if $tagslib->{$tag}->{$subfield}->{'kohafield'} eq 'items.barcode';
286 next if $tagslib->{$tag}->{$subfield}->{'kohafield'} eq 'items.stocknumber';
289 my $index_subfield = int(rand(1000000));
290 if ($subfield eq '@'){
291 $subfield_data{id
} = "tag_".$tag."_subfield_00_".$index_subfield;
293 $subfield_data{id
} = "tag_".$tag."_subfield_".$subfield."_".$index_subfield;
295 $subfield_data{tag
} = $tag;
296 $subfield_data{subfield
} = $subfield;
297 $subfield_data{random
} = int(rand(1000000)); # why do we need 2 different randoms?
298 # $subfield_data{marc_lib} = $tagslib->{$tag}->{$subfield}->{lib};
299 $subfield_data{marc_lib
} ="<span id=\"error$i\" title=\"".$tagslib->{$tag}->{$subfield}->{lib
}."\">".$tagslib->{$tag}->{$subfield}->{lib
}."</span>";
300 $subfield_data{mandatory
} = $tagslib->{$tag}->{$subfield}->{mandatory
};
301 $subfield_data{repeatable
} = $tagslib->{$tag}->{$subfield}->{repeatable
};
303 $value =~ s/"/"/g;
305 $value = $tagslib->{$tag}->{$subfield}->{defaultvalue
};
306 # get today date & replace YYYY, MM, DD if provided in the default value
307 my ( $year, $month, $day ) = split ',', $today_iso; # FIXME: iso dates don't have commas!
308 $value =~ s/YYYY/$year/g;
309 $value =~ s/MM/$month/g;
310 $value =~ s/DD/$day/g;
312 $subfield_data{visibility
} = "display:none;" if (($tagslib->{$tag}->{$subfield}->{hidden
} > 4) || ($tagslib->{$tag}->{$subfield}->{hidden
} < -4));
313 # testing branch value if IndependantBranches.
315 my $attributes_no_value = qq(tabindex
="1" id
="$subfield_data{id}" name
="field_value" class="input_marceditor" size
="67" maxlength
="255" );
316 my $attributes = qq($attributes_no_value value
="$value" );
318 if ( $tagslib->{$tag}->{$subfield}->{authorised_value
} ) {
319 my @authorised_values;
321 # builds list, depending on authorised value...
323 if ( $tagslib->{$tag}->{$subfield}->{authorised_value
} eq "branches" ) {
324 foreach my $thisbranch (@
$branches) {
325 push @authorised_values, $thisbranch->{value
};
326 $authorised_lib{$thisbranch->{value
}} = $thisbranch->{branchname
};
330 elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value
} eq "itemtypes" ) {
331 push @authorised_values, "";
332 my $sth = $dbh->prepare("select itemtype,description from itemtypes order by description");
334 while ( my ( $itemtype, $description ) = $sth->fetchrow_array ) {
335 push @authorised_values, $itemtype;
336 $authorised_lib{$itemtype} = $description;
342 elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value
} eq "cn_source" ) {
343 push @authorised_values, "" unless ( $tagslib->{$tag}->{$subfield}->{mandatory
} );
345 my $class_sources = GetClassSources
();
346 my $default_source = C4
::Context
->preference("DefaultClassificationSource");
348 foreach my $class_source (sort keys %$class_sources) {
349 next unless $class_sources->{$class_source}->{'used'} or
350 ($value and $class_source eq $value) or
351 ($class_source eq $default_source);
352 push @authorised_values, $class_source;
353 $authorised_lib{$class_source} = $class_sources->{$class_source}->{'description'};
357 #---- "true" authorised value
360 push @authorised_values, ""; # unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
361 $authorised_values_sth->execute( $tagslib->{$tag}->{$subfield}->{authorised_value
} );
362 while ( my ( $value, $lib ) = $authorised_values_sth->fetchrow_array ) {
363 push @authorised_values, $value;
364 $authorised_lib{$value} = $lib;
368 $subfield_data{marc_value
} =CGI
::scrolling_list
( # FIXME: factor out scrolling_list
369 -name
=> "field_value",
370 -values => \
@authorised_values,
372 -labels
=> \
%authorised_lib,
377 -id
=> "tag_".$tag."_subfield_".$subfield."_".$index_subfield,
378 -class => "input_marceditor",
380 # it's a thesaurus / authority field
382 elsif ( $tagslib->{$tag}->{$subfield}->{authtypecode
} ) {
383 $subfield_data{marc_value
} = "<input type=\"text\" $attributes />
384 <a href=\"#\" class=\"buttonDot\"
385 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>
387 # it's a plugin field
389 elsif ( $tagslib->{$tag}->{$subfield}->{value_builder
} ) {
391 my $plugin = C4
::Context
->intranetdir . "/cataloguing/value_builder/" . $tagslib->{$tag}->{$subfield}->{'value_builder'};
394 my $extended_param = plugin_parameters
( $dbh, $temp, $tagslib, $subfield_data{id
}, \
@loop_data );
395 my ( $function_name, $javascript ) = plugin_javascript
( $dbh, $temp, $tagslib, $subfield_data{id
}, \
@loop_data );
396 $subfield_data{marc_value
} = qq[<input
$attributes
397 onfocus
="Focus$function_name($subfield_data{random}, '$subfield_data{id}');"
398 onblur
=" Blur$function_name($subfield_data{random}, '$subfield_data{id}');" />
399 <a href
="#" class="buttonDot" onclick
="Clic$function_name('$subfield_data{id}'); return false;" title
="Tag Editor">...</a
>
402 warn "Plugin Failed: $plugin";
403 $subfield_data{marc_value
} = "<input $attributes />"; # supply default input form
406 elsif ( $tag eq '' ) { # it's an hidden field
407 $subfield_data{marc_value
} = qq(<input type
="hidden" $attributes />);
409 elsif ( $tagslib->{$tag}->{$subfield}->{'hidden'} ) { # FIXME: shouldn't input type be "hidden" ?
410 $subfield_data{marc_value
} = qq(<input type
="text" $attributes />);
412 elsif ( length($value) > 100
413 or (C4
::Context
->preference("marcflavour") eq "UNIMARC" and
414 300 <= $tag && $tag < 400 && $subfield eq 'a' )
415 or (C4
::Context
->preference("marcflavour") eq "MARC21" and
416 500 <= $tag && $tag < 600 )
418 # oversize field (textarea)
419 $subfield_data{marc_value
} = "<textarea $attributes_no_value>$value</textarea>\n";
421 # it's a standard field
422 $subfield_data{marc_value
} = "<input $attributes />";
424 # $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\">";
425 push (@loop_data, \
%subfield_data);
428 } # -- End foreach tag
431 # what's the next op ? it's what we are not in : an add if we're editing, otherwise, and edit.
432 $template->param(item
=> \
@loop_data);
433 if (@notfoundbarcodes) {
434 my @notfoundbarcodesloop = map{{barcode
=>$_}}@notfoundbarcodes;
435 $template->param(notfoundbarcodes
=> \
@notfoundbarcodesloop);
438 } # -- End action="show"
440 $template->param(%$items_display_hashref) if $items_display_hashref;
446 if ($op eq "action") {
448 #my @not_deleted_loop = map{{itemnumber=>$_}}@not_deleted;
451 not_deleted_items
=> $not_deleted_items,
452 deleted_items
=> $deleted_items,
453 delete_records
=> $del_records,
454 deleted_records
=> $deleted_records,
455 not_deleted_loop
=> \
@not_deleted
459 foreach my $error (@errors) {
460 $template->param($error => 1);
462 $template->param(src
=> $src);
463 output_html_with_http_headers
$input, $cookie, $template->output;
467 # ---------------- Functions
471 # now, build existiing item list
472 my %witness; #---- stores the list of subfields used at least once, with the "meaning" of the code
474 #---- finds where items.itemnumber is stored
475 my ( $itemtagfield, $itemtagsubfield) = &GetMarcFromKohaField
("items.itemnumber", "");
476 my ($branchtagfield, $branchtagsubfield) = &GetMarcFromKohaField
("items.homebranch", "");
477 foreach my $itemnumber (@itemnumbers){
478 my $itemdata=GetItem
($itemnumber);
479 my $itemmarc=Item2Marc
($itemdata);
481 foreach my $field (grep {$_->tag() eq $itemtagfield} $itemmarc->fields()) {
482 # loop through each subfield
483 my $itembranchcode=$field->subfield($branchtagsubfield);
484 if ($itembranchcode && C4
::Context
->preference("IndependantBranches")) {
486 my $userenv = C4
::Context
->userenv();
487 unless (($userenv->{'flags'} == 1) or (($userenv->{'branch'} eq $itembranchcode))){
488 $this_row{'nomod'}=1;
491 my $tag=$field->tag();
492 foreach my $subfield ($field->subfields) {
493 my ($subfcode,$subfvalue)=@
$subfield;
494 next if ($tagslib->{$tag}->{$subfcode}->{tab
} ne 10
495 && $tag ne $itemtagfield
496 && $subfcode ne $itemtagsubfield);
498 $witness{$subfcode} = $tagslib->{$tag}->{$subfcode}->{lib
} if ($tagslib->{$tag}->{$subfcode}->{tab
} eq 10);
499 if ($tagslib->{$tag}->{$subfcode}->{tab
} eq 10) {
500 $this_row{$subfcode}=GetAuthorisedValueDesc
( $tag,
501 $subfcode, $subfvalue, '', $tagslib)
505 $this_row{itemnumber
} = $subfvalue if ($tag eq $itemtagfield && $subfcode eq $itemtagsubfield);
509 # grab title, author, and ISBN to identify bib that the item
510 # belongs to in the display
511 my $biblio=GetBiblioData
($$itemdata{biblionumber
});
512 $this_row{title
} = $biblio->{title
};
513 $this_row{author
} = $biblio->{author
};
514 $this_row{isbn
} = $biblio->{isbn
};
515 $this_row{biblionumber
} = $biblio->{biblionumber
};
518 push(@big_array, \
%this_row);
521 @big_array = sort {$a->{0} cmp $b->{0}} @big_array;
523 # now, construct template !
524 # First, the existing items for display
526 my @witnesscodessorted=sort keys %witness;
527 for my $row ( @big_array ) {
529 my @item_fields = map +{ field
=> $_ || '' }, @
$row{ @witnesscodessorted };
530 $row_data{item_value
} = [ @item_fields ];
531 $row_data{itemnumber
} = $row->{itemnumber
};
532 #reporting this_row values
533 $row_data{'nomod'} = $row->{'nomod'};
534 $row_data{bibinfo
} = $row->{bibinfo
};
535 $row_data{author
} = $row->{author
};
536 $row_data{title
} = $row->{title
};
537 $row_data{isbn
} = $row->{isbn
};
538 $row_data{biblionumber
} = $row->{biblionumber
};
539 push(@item_value_loop,\
%row_data);
541 my @header_loop=map { { header_value
=> $witness{$_}} } @witnesscodessorted;
543 return { item_loop
=> \
@item_value_loop, item_header_loop
=> \
@header_loop };
546 #BE WARN : it is not the general case
547 # This function can be OK in the item marc record special case
548 # Where subfield is not repeated
549 # And where we are sure that field should correspond
552 my ($marcfrom,$marcto)=@_;
553 #warn "FROM :",$marcfrom->as_formatted;
554 my ( $itemtag, $itemtagsubfield) = &GetMarcFromKohaField
("items.itemnumber", "");
555 my $fieldfrom=$marcfrom->field($itemtag);
556 my @fields_to=$marcto->field($itemtag);
557 foreach my $subfield ($fieldfrom->subfields()){
558 foreach my $field_to_update (@fields_to){
560 $field_to_update->update($subfield->[0]=>$subfield->[1]);
563 $field_to_update->delete_subfield(code
=> $subfield->[0]);
567 #warn "TO edited:",$marcto->as_formatted;
571 my ($tagfield,$insubfield,$record) = @_;
574 foreach my $field ($record->field($tagfield)) {
575 my @subfields = $field->subfields();
576 foreach my $subfield (@subfields) {
577 if (@
$subfield[0] eq $insubfield) {
578 $result .= @
$subfield[1];
579 $indicator = $field->indicator(1).$field->indicator(2);
583 return($indicator,$result);
586 # ----------------------------
587 # Background functions
590 sub add_results_to_template
{
591 my $template = shift;
593 $template->param(map { $_ => $results->{$_} } keys %{ $results });
596 sub add_saved_job_results_to_template
{
597 my $template = shift;
598 my $completedJobID = shift;
599 my $job = C4
::BackgroundJob
->fetch($sessionID, $completedJobID);
600 my $results = $job->results();
601 add_results_to_template
($template, $results);
604 sub put_in_background
{
605 my $job_size = shift;
607 my $job = C4
::BackgroundJob
->new($sessionID, "test", $ENV{'SCRIPT_NAME'}, $job_size);
608 my $jobID = $job->id();
611 if (my $pid = fork) {
613 # return job ID as JSON
615 # prevent parent exiting from
616 # destroying the kid's database handle
617 # FIXME: according to DBI doc, this may not work for Oracle
618 $dbh->{InactiveDestroy
} = 1;
620 my $reply = CGI
->new("");
621 print $reply->header(-type
=> 'text/html');
622 print '{"jobID":"' . $jobID . '"}';
624 } elsif (defined $pid) {
626 # close STDOUT to signal to Apache that
627 # we're now running in the background
631 # fork failed, so exit immediately
632 warn "fork failed while attempting to run $ENV{'SCRIPT_NAME'} as a background job";