MT 2050, Follow-up, Fast Cataloging
[koha.git] / tools / batchMod.pl
blob84236eaf683e93cf8b2aa4a9eefc9750fb46ea5d
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 with
18 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
19 # Suite 330, Boston, MA 02111-1307 USA
21 use CGI;
22 use strict;
23 use C4::Auth;
24 use C4::Output;
25 use C4::Biblio;
26 use C4::Items;
27 use C4::Context;
28 use C4::Koha; # XXX subfield_is_koha_internal_p
29 use C4::Branch; # XXX subfield_is_koha_internal_p
30 use C4::ClassSource;
31 use C4::Dates;
32 use C4::Debug;
33 use YAML;
34 use Switch;
35 use MARC::File::XML;
37 my $input = new CGI;
38 my $dbh = C4::Context->dbh;
39 my $error = $input->param('error');
40 my @itemnumbers = $input->param('itemnumber');
41 my $op = $input->param('op');
42 my $del = $input->param('del');
44 my $template_name;
45 my $template_flag;
46 if (!defined $op) {
47 $template_name = "tools/batchMod.tmpl";
48 $template_flag = { tools => '*' };
49 } else {
50 $template_name = ($del) ? "tools/batchMod-del.tmpl" : "tools/batchMod-edit.tmpl";
51 $template_flag = ($del) ? { tools => 'batchdel' } : { tools => 'batchmod' };
55 my ($template, $loggedinuser, $cookie)
56 = get_template_and_user({template_name => $template_name,
57 query => $input,
58 type => "intranet",
59 authnotrequired => 0,
60 flagsrequired => $template_flag,
61 });
64 my $today_iso = C4::Dates->today('iso');
65 $template->param(today_iso => $today_iso);
66 $template->param(del => $del);
68 my $itemrecord;
69 my $nextop="";
70 my @errors; # store errors found while checking data BEFORE saving item.
71 my $items_display_hashref;
72 my $frameworkcode="";
73 my $tagslib = &GetMarcStructure(1,$frameworkcode);
75 my $deleted_items = 0; # Numbers of deleted items
76 my $not_deleted_items = 0; # Numbers of items that could not be deleted
77 my @not_deleted; # List of the itemnumbers that could not be deleted
79 #--- ----------------------------------------------------------------------------
80 if ($op eq "action") {
81 #-------------------------------------------------------------------------------
82 my @tags = $input->param('tag');
83 my @subfields = $input->param('subfield');
84 my @values = $input->param('field_value');
85 # build indicator hash.
86 my @ind_tag = $input->param('ind_tag');
87 my @indicator = $input->param('indicator');
89 my $xml = TransformHtmlToXml(\@tags,\@subfields,\@values,\@indicator,\@ind_tag, 'ITEM');
90 my $marcitem = MARC::Record::new_from_xml($xml, 'UTF-8');
91 my $localitem = TransformMarcToKoha( $dbh, $marcitem, "", 'items' );
93 foreach my $itemnumber(@itemnumbers){
94 my $itemdata=GetItem($itemnumber);
95 if ($input->param("del")){
96 my $return = DelItemCheck(C4::Context->dbh, $itemdata->{'biblionumber'}, $itemdata->{'itemnumber'});
97 if ($return == 1) {
98 $deleted_items++;
99 } else {
100 $not_deleted_items++;
101 push @not_deleted, { itemnumber => $itemdata->{'itemnumber'}, barcode => $itemdata->{'barcode'}, title => $itemdata->{'title'}, $return => 1 };
103 } else {
104 my $localmarcitem=Item2Marc($itemdata);
105 UpdateMarcWith($marcitem,$localmarcitem);
106 eval{my ($oldbiblionumber,$oldbibnum,$oldbibitemnum) = ModItemFromMarc($localmarcitem,$itemdata->{biblionumber},$itemnumber)};
109 $items_display_hashref=BuildItemsData(@itemnumbers);
113 #-------------------------------------------------------------------------------
114 # build screen with existing items. and "new" one
115 #-------------------------------------------------------------------------------
117 if ($op eq "show"){
118 my $filefh = $input->upload('uploadfile');
119 my $filecontent = $input->param('filecontent');
120 my @notfoundbarcodes;
122 my @contentlist;
123 if ($filefh){
124 while (my $content=<$filefh>){
125 chomp $content;
126 push @contentlist, $content;
129 switch ($filecontent) {
130 case "barcode_file" {
131 foreach my $barcode (@contentlist) {
133 my $itemnumber = GetItemnumberFromBarcode($barcode);
134 if ($itemnumber) {
135 push @itemnumbers,$itemnumber;
136 } else {
137 push @notfoundbarcodes, $barcode;
143 case "itemid_file" {
144 @itemnumbers = @contentlist;
147 } else {
148 if ( my $list=$input->param('barcodelist')){
149 push my @barcodelist, split(/\s\n/, $list);
151 foreach my $barcode (@barcodelist) {
153 my $itemnumber = GetItemnumberFromBarcode($barcode);
154 if ($itemnumber) {
155 push @itemnumbers,$itemnumber;
156 } else {
157 push @notfoundbarcodes, $barcode;
163 $items_display_hashref=BuildItemsData(@itemnumbers);
165 # now, build the item form for entering a new item
166 my @loop_data =();
167 my $i=0;
168 my $authorised_values_sth = $dbh->prepare("SELECT authorised_value,lib FROM authorised_values WHERE category=? ORDER BY lib");
170 my $branches = GetBranchesLoop(); # build once ahead of time, instead of multiple times later.
171 my $pref_itemcallnumber = C4::Context->preference('itemcallnumber');
174 foreach my $tag (sort keys %{$tagslib}) {
175 # loop through each subfield
176 foreach my $subfield (sort keys %{$tagslib->{$tag}}) {
177 next if subfield_is_koha_internal_p($subfield);
178 next if ($tagslib->{$tag}->{$subfield}->{'tab'} ne "10");
179 my %subfield_data;
181 my $index_subfield = int(rand(1000000));
182 if ($subfield eq '@'){
183 $subfield_data{id} = "tag_".$tag."_subfield_00_".$index_subfield;
184 } else {
185 $subfield_data{id} = "tag_".$tag."_subfield_".$subfield."_".$index_subfield;
187 $subfield_data{tag} = $tag;
188 $subfield_data{subfield} = $subfield;
189 $subfield_data{random} = int(rand(1000000)); # why do we need 2 different randoms?
190 # $subfield_data{marc_lib} = $tagslib->{$tag}->{$subfield}->{lib};
191 $subfield_data{marc_lib} ="<span id=\"error$i\" title=\"".$tagslib->{$tag}->{$subfield}->{lib}."\">".$tagslib->{$tag}->{$subfield}->{lib}."</span>";
192 $subfield_data{mandatory} = $tagslib->{$tag}->{$subfield}->{mandatory};
193 $subfield_data{repeatable} = $tagslib->{$tag}->{$subfield}->{repeatable};
194 my ($x,$value);
195 $value =~ s/"/&quot;/g;
196 unless ($value) {
197 $value = $tagslib->{$tag}->{$subfield}->{defaultvalue};
198 # get today date & replace YYYY, MM, DD if provided in the default value
199 my ( $year, $month, $day ) = split ',', $today_iso; # FIXME: iso dates don't have commas!
200 $value =~ s/YYYY/$year/g;
201 $value =~ s/MM/$month/g;
202 $value =~ s/DD/$day/g;
204 $subfield_data{visibility} = "display:none;" if (($tagslib->{$tag}->{$subfield}->{hidden} > 4) || ($tagslib->{$tag}->{$subfield}->{hidden} < -4));
205 # testing branch value if IndependantBranches.
207 my $attributes_no_value;
208 my $not_editable = 0;
209 # Disable barcode and stock numbers batch editing
210 my @not_editable_koha_fields = ( 'items.barcode', 'items.stocknumber' );
211 foreach (@not_editable_koha_fields) {
212 my ($bctag, $bcsubfield) = GetMarcFromKohaField($_, $frameworkcode);
213 if (($bctag eq $subfield_data{tag}) && ($bcsubfield eq $subfield_data{subfield})) {
214 $not_editable = 1;
219 my $attributes;
220 # If a field is found to be non-editable,
221 if ($not_editable) {
222 # We mark it as disabled, so the user won't be able to edit it
223 $attributes_no_value = qq(disabled="disabled");
224 $attributes = $attributes_no_value;
225 # We also remove it's data, so it won't be modified
226 undef($subfield_data{tag});
227 undef($subfield_data{subfield});
228 } else {
229 $attributes_no_value = qq(tabindex="1" id="$subfield_data{id}" name="field_value" class="input_marceditor" size="67" maxlength="255" );
230 $attributes = qq($attributes_no_value value="$value" );
233 if ( $tagslib->{$tag}->{$subfield}->{authorised_value} ) {
234 my @authorised_values;
235 my %authorised_lib;
236 # builds list, depending on authorised value...
238 if ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "branches" ) {
239 foreach my $thisbranch (@$branches) {
240 push @authorised_values, $thisbranch->{value};
241 $authorised_lib{$thisbranch->{value}} = $thisbranch->{branchname};
242 $value = $thisbranch->{value} if $thisbranch->{selected};
245 elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes" ) {
246 push @authorised_values, "" unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
247 my $sth = $dbh->prepare("select itemtype,description from itemtypes order by description");
248 $sth->execute;
249 while ( my ( $itemtype, $description ) = $sth->fetchrow_array ) {
250 push @authorised_values, $itemtype;
251 $authorised_lib{$itemtype} = $description;
254 #---- class_sources
256 elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "cn_source" ) {
257 push @authorised_values, "" unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
259 my $class_sources = GetClassSources();
260 my $default_source = C4::Context->preference("DefaultClassificationSource");
262 foreach my $class_source (sort keys %$class_sources) {
263 next unless $class_sources->{$class_source}->{'used'} or
264 ($value and $class_source eq $value) or
265 ($class_source eq $default_source);
266 push @authorised_values, $class_source;
267 $authorised_lib{$class_source} = $class_sources->{$class_source}->{'description'};
269 $value = $default_source unless ($value);
271 #---- "true" authorised value
273 else {
274 push @authorised_values, "" unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
275 $authorised_values_sth->execute( $tagslib->{$tag}->{$subfield}->{authorised_value} );
276 while ( my ( $value, $lib ) = $authorised_values_sth->fetchrow_array ) {
277 push @authorised_values, $value;
278 $authorised_lib{$value} = $lib;
281 $subfield_data{marc_value} =CGI::scrolling_list( # FIXME: factor out scrolling_list
282 -name => "field_value",
283 -values => \@authorised_values,
284 -default => $value,
285 -labels => \%authorised_lib,
286 -override => 1,
287 -size => 1,
288 -multiple => 0,
289 -tabindex => 1,
290 -id => "tag_".$tag."_subfield_".$subfield."_".$index_subfield,
291 -class => "input_marceditor",
293 # it's a thesaurus / authority field
295 elsif ( $tagslib->{$tag}->{$subfield}->{authtypecode} ) {
296 $subfield_data{marc_value} = "<input type=\"text\" $attributes />
297 <a href=\"#\" class=\"buttonDot\"
298 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>
300 # it's a plugin field
302 elsif ( $tagslib->{$tag}->{$subfield}->{value_builder} ) {
303 # opening plugin
304 my $plugin = C4::Context->intranetdir . "/cataloguing/value_builder/" . $tagslib->{$tag}->{$subfield}->{'value_builder'};
305 if (do $plugin) {
306 my $temp;
307 my $extended_param = plugin_parameters( $dbh, $temp, $tagslib, $subfield_data{id}, \@loop_data );
308 my ( $function_name, $javascript ) = plugin_javascript( $dbh, $temp, $tagslib, $subfield_data{id}, \@loop_data );
309 $subfield_data{marc_value} = qq[<input $attributes
310 onfocus="Focus$function_name($subfield_data{random}, '$subfield_data{id}');"
311 onblur=" Blur$function_name($subfield_data{random}, '$subfield_data{id}');" />
312 <a href="#" class="buttonDot" onclick="Clic$function_name('$subfield_data{id}'); return false;" title="Tag Editor">...</a>
313 $javascript];
314 } else {
315 warn "Plugin Failed: $plugin";
316 $subfield_data{marc_value} = "<input $attributes />"; # supply default input form
319 elsif ( $tag eq '' ) { # it's an hidden field
320 $subfield_data{marc_value} = qq(<input type="hidden" $attributes />);
322 elsif ( $tagslib->{$tag}->{$subfield}->{'hidden'} ) { # FIXME: shouldn't input type be "hidden" ?
323 $subfield_data{marc_value} = qq(<input type="text" $attributes />);
325 elsif ( length($value) > 100
326 or (C4::Context->preference("marcflavour") eq "UNIMARC" and
327 300 <= $tag && $tag < 400 && $subfield eq 'a' )
328 or (C4::Context->preference("marcflavour") eq "MARC21" and
329 500 <= $tag && $tag < 600 )
331 # oversize field (textarea)
332 $subfield_data{marc_value} = "<textarea $attributes_no_value>$value</textarea>\n";
333 } else {
334 # it's a standard field
335 $subfield_data{marc_value} = "<input $attributes />";
337 # $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\">";
338 push (@loop_data, \%subfield_data);
339 $i++
341 } # -- End foreach tag
344 # what's the next op ? it's what we are not in : an add if we're editing, otherwise, and edit.
345 $template->param(item => \@loop_data);
346 if (@notfoundbarcodes) {
347 my @notfoundbarcodesloop = map{{barcode=>$_}}@notfoundbarcodes;
348 $template->param(notfoundbarcodes => \@notfoundbarcodesloop);
350 $nextop="action"
351 } # -- End action="show"
353 $template->param(%$items_display_hashref) if $items_display_hashref;
354 $template->param(
355 op => $nextop,
356 $op => 1,
359 if ($op eq "action") {
361 #my @not_deleted_loop = map{{itemnumber=>$_}}@not_deleted;
363 $template->param(
364 not_deleted_items => $not_deleted_items,
365 deleted_items => $deleted_items,
366 not_deleted_loop => \@not_deleted
370 foreach my $error (@errors) {
371 $template->param($error => 1);
373 output_html_with_http_headers $input, $cookie, $template->output;
374 exit;
377 # ---------------- Functions
379 sub BuildItemsData{
380 my @itemnumbers=@_;
381 # now, build existiing item list
382 my %witness; #---- stores the list of subfields used at least once, with the "meaning" of the code
383 my @big_array;
384 #---- finds where items.itemnumber is stored
385 my ( $itemtagfield, $itemtagsubfield) = &GetMarcFromKohaField("items.itemnumber", "");
386 my ($branchtagfield, $branchtagsubfield) = &GetMarcFromKohaField("items.homebranch", "");
387 foreach my $itemnumber (@itemnumbers){
388 my $itemdata=GetItem($itemnumber);
389 my $itemmarc=Item2Marc($itemdata);
390 my $biblio=GetBiblioData($$itemdata{biblionumber});
391 my %this_row;
392 foreach my $field (grep {$_->tag() eq $itemtagfield} $itemmarc->fields()) {
393 # loop through each subfield
394 if (my $itembranchcode=$field->subfield($branchtagsubfield) && C4::Context->preference("IndependantBranches")) {
395 #verifying rights
396 my $userenv = C4::Context->userenv();
397 unless (($userenv->{'flags'} == 1) or (($userenv->{'branch'} eq $itembranchcode))){
398 $this_row{'nomod'}=1;
401 my $tag=$field->tag();
402 foreach my $subfield ($field->subfields) {
403 my ($subfcode,$subfvalue)=@$subfield;
404 next if ($tagslib->{$tag}->{$subfcode}->{tab} ne 10
405 && $tag ne $itemtagfield
406 && $subfcode ne $itemtagsubfield);
408 $witness{$subfcode} = $tagslib->{$tag}->{$subfcode}->{lib} if ($tagslib->{$tag}->{$subfcode}->{tab} eq 10);
409 if ($tagslib->{$tag}->{$subfcode}->{tab} eq 10) {
410 $this_row{$subfcode}=GetAuthorisedValueDesc( $tag,
411 $subfcode, $subfvalue, '', $tagslib)
412 || $subfvalue;
415 $this_row{itemnumber} = $subfvalue if ($tag eq $itemtagfield && $subfcode eq $itemtagsubfield);
418 $this_row{0}=join("\n",@$biblio{qw(title author ISBN)});
419 $witness{0}="&nbsp;";
420 if (%this_row) {
421 push(@big_array, \%this_row);
424 @big_array = sort {$a->{0} cmp $b->{0}} @big_array;
426 # now, construct template !
427 # First, the existing items for display
428 my @item_value_loop;
429 my @witnesscodessorted=sort keys %witness;
430 for my $row ( @big_array ) {
431 my %row_data;
432 my @item_fields = map +{ field => $_ || '' }, @$row{ @witnesscodessorted };
433 $row_data{item_value} = [ @item_fields ];
434 $row_data{itemnumber} = $row->{itemnumber};
435 #reporting this_row values
436 $row_data{'nomod'} = $row->{'nomod'};
437 push(@item_value_loop,\%row_data);
439 my @header_loop=map { { header_value=> $witness{$_}} } @witnesscodessorted;
440 return { item_loop => \@item_value_loop, item_header_loop => \@header_loop };
443 #BE WARN : it is not the general case
444 # This function can be OK in the item marc record special case
445 # Where subfield is not repeated
446 # And where we are sure that field should correspond
447 # And $tag>10
448 sub UpdateMarcWith($$){
449 my ($marcfrom,$marcto)=@_;
450 #warn "FROM :",$marcfrom->as_formatted;
451 my ( $itemtag, $itemtagsubfield) = &GetMarcFromKohaField("items.itemnumber", "");
452 my $fieldfrom=$marcfrom->field($itemtag);
453 my @fields_to=$marcto->field($itemtag);
454 foreach my $subfield ($fieldfrom->subfields()){
455 foreach my $field_to_update (@fields_to){
456 $field_to_update->update($$subfield[0]=>$$subfield[1]) if ($$subfield[1]);
459 #warn "TO edited:",$marcto->as_formatted;
462 sub find_value {
463 my ($tagfield,$insubfield,$record) = @_;
464 my $result;
465 my $indicator;
466 foreach my $field ($record->field($tagfield)) {
467 my @subfields = $field->subfields();
468 foreach my $subfield (@subfields) {
469 if (@$subfield[0] eq $insubfield) {
470 $result .= @$subfield[1];
471 $indicator = $field->indicator(1).$field->indicator(2);
475 return($indicator,$result);