MT 0002070: Batch item deletion
[koha.git] / tools / batchMod.pl
1 #!/usr/bin/perl
2
3
4 # Copyright 2000-2002 Katipo Communications
5 #
6 # This file is part of Koha.
7 #
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.
12 #
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.
16 #
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
20
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;
36
37 sub find_value {
38     my ($tagfield,$insubfield,$record) = @_;
39     my $result;
40     my $indicator;
41     foreach my $field ($record->field($tagfield)) {
42         my @subfields = $field->subfields();
43         foreach my $subfield (@subfields) {
44             if (@$subfield[0] eq $insubfield) {
45                 $result .= @$subfield[1];
46                 $indicator = $field->indicator(1).$field->indicator(2);
47             }
48         }
49     }
50     return($indicator,$result);
51 }
52
53
54 my $input = new CGI;
55 my $dbh = C4::Context->dbh;
56 my $error        = $input->param('error');
57 my @itemnumbers  = $input->param('itemnumber');
58 my $op           = $input->param('op');
59 my $del          = $input->param('del');
60
61 my ($template, $loggedinuser, $cookie)
62     = get_template_and_user({template_name => "tools/batchMod.tmpl",
63                  query => $input,
64                  type => "intranet",
65                  authnotrequired => 0,
66                  flagsrequired => {editcatalogue => 1},
67                  });
68
69
70 my $today_iso = C4::Dates->today('iso');
71 $template->param(today_iso => $today_iso);
72 $template->param(del       => $del);
73
74 my $itemrecord;
75 my $nextop="";
76 my @errors; # store errors found while checking data BEFORE saving item.
77 my $items_display_hashref;
78 my $frameworkcode="";
79 my $tagslib = &GetMarcStructure(1,$frameworkcode);
80
81 #--- ----------------------------------------------------------------------------
82 if ($op eq "action") {
83 #-------------------------------------------------------------------------------
84     my @tags      = $input->param('tag');
85     my @subfields = $input->param('subfield');
86     my @values    = $input->param('field_value');
87     # build indicator hash.
88     my @ind_tag   = $input->param('ind_tag');
89     my @indicator = $input->param('indicator');
90     my $xml = TransformHtmlToXml(\@tags,\@subfields,\@values,\@indicator,\@ind_tag, 'ITEM');
91     my $marcitem = MARC::Record::new_from_xml($xml, 'UTF-8');
92     my $localitem = TransformMarcToKoha( $dbh, $marcitem, "", 'items' );
93
94 #       my @params=$input->param();
95 #       warn @params;
96 #    my $marcitem = TransformHtmlToMarc(\@params,$input);
97         foreach my $itemnumber(@itemnumbers){
98                 my $itemdata=GetItem($itemnumber);
99                 if ($input->param("del")){
100                         DelItemCheck(C4::Context->dbh, $itemdata->{'biblionumber'}, $itemdata->{'itemnumber'})
101                 } else {
102                         my $localmarcitem=Item2Marc($itemdata);
103                         UpdateMarcWith($marcitem,$localmarcitem);
104             eval{my ($oldbiblionumber,$oldbibnum,$oldbibitemnum) = ModItemFromMarc($localmarcitem,$itemdata->{biblionumber},$itemnumber)};
105                 #       eval{ModItem($localitem,$itemdata->{biblionumber},$itemnumber)};
106                 }
107         }
108         $items_display_hashref=BuildItemsData(@itemnumbers);
109     $nextop="action";
110 }
111
112 #
113 #-------------------------------------------------------------------------------
114 # build screen with existing items. and "new" one
115 #-------------------------------------------------------------------------------
116
117 if ($op eq "show"){
118         my $filefh = $input->upload('uploadfile');
119         my $filecontent = $input->param('filecontent');
120
121     my @contentlist;
122     if ($filefh){
123         while (my $content=<$filefh>){
124             chomp $content;
125             push @contentlist, $content;
126         }
127
128         switch ($filecontent) {
129             case "barcode_file" {
130                 push @itemnumbers,map{GetItemnumberFromBarcode($_)} @contentlist;
131                 # Remove not found barcodes
132                 @itemnumbers = grep(!/^$/, @itemnumbers);
133             }
134
135             case "itemid_file" {
136                 @itemnumbers = @contentlist;
137             }
138         }
139     } else {
140        if ( my $list=$input->param('barcodelist')){
141         push my @barcodelist, split(/\s\n/, $list);
142         push @itemnumbers,map{GetItemnumberFromBarcode($_)} @barcodelist;
143         # Remove not found barcodes
144         @itemnumbers = grep(!/^$/, @itemnumbers);
145
146     }
147 }
148         $items_display_hashref=BuildItemsData(@itemnumbers);
149
150 # now, build the item form for entering a new item
151 my @loop_data =();
152 my $i=0;
153 my $authorised_values_sth = $dbh->prepare("SELECT authorised_value,lib FROM authorised_values WHERE category=? ORDER BY lib");
154
155 my $branches = GetBranchesLoop();  # build once ahead of time, instead of multiple times later.
156 my $pref_itemcallnumber = C4::Context->preference('itemcallnumber');
157
158 foreach my $tag (sort keys %{$tagslib}) {
159 # loop through each subfield
160   foreach my $subfield (sort keys %{$tagslib->{$tag}}) {
161     next if subfield_is_koha_internal_p($subfield);
162     next if ($tagslib->{$tag}->{$subfield}->{'tab'} ne "10");
163     my %subfield_data;
164  
165     my $index_subfield = int(rand(1000000)); 
166     if ($subfield eq '@'){
167         $subfield_data{id} = "tag_".$tag."_subfield_00_".$index_subfield;
168     } else {
169         $subfield_data{id} = "tag_".$tag."_subfield_".$subfield."_".$index_subfield;
170     }
171     $subfield_data{tag}        = $tag;
172     $subfield_data{subfield}   = $subfield;
173     $subfield_data{random}     = int(rand(1000000));    # why do we need 2 different randoms?
174 #   $subfield_data{marc_lib}   = $tagslib->{$tag}->{$subfield}->{lib};
175     $subfield_data{marc_lib}   ="<span id=\"error$i\" title=\"".$tagslib->{$tag}->{$subfield}->{lib}."\">".$tagslib->{$tag}->{$subfield}->{lib}."</span>";
176     $subfield_data{mandatory}  = $tagslib->{$tag}->{$subfield}->{mandatory};
177     $subfield_data{repeatable} = $tagslib->{$tag}->{$subfield}->{repeatable};
178     my ($x,$value);
179     $value =~ s/"/&quot;/g;
180     unless ($value) {
181         $value = $tagslib->{$tag}->{$subfield}->{defaultvalue};
182         # get today date & replace YYYY, MM, DD if provided in the default value
183         my ( $year, $month, $day ) = split ',', $today_iso;     # FIXME: iso dates don't have commas!
184         $value =~ s/YYYY/$year/g;
185         $value =~ s/MM/$month/g;
186         $value =~ s/DD/$day/g;
187     }
188     $subfield_data{visibility} = "display:none;" if (($tagslib->{$tag}->{$subfield}->{hidden} > 4) || ($tagslib->{$tag}->{$subfield}->{hidden} < -4));
189     # testing branch value if IndependantBranches.
190
191     my $attributes_no_value = qq(tabindex="1" id="$subfield_data{id}" name="field_value" class="input_marceditor" size="67" maxlength="255" );
192     my $attributes          = qq($attributes_no_value value="$value" );
193     if ( $tagslib->{$tag}->{$subfield}->{authorised_value} ) {
194       my @authorised_values;
195       my %authorised_lib;
196       # builds list, depending on authorised value...
197   
198       if ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "branches" ) {
199           foreach my $thisbranch (@$branches) {
200               push @authorised_values, $thisbranch->{value};
201               $authorised_lib{$thisbranch->{value}} = $thisbranch->{branchname};
202               $value = $thisbranch->{value} if $thisbranch->{selected};
203           }
204       }
205       elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes" ) {
206           push @authorised_values, "" unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
207           my $sth = $dbh->prepare("select itemtype,description from itemtypes order by description");
208           $sth->execute;
209           while ( my ( $itemtype, $description ) = $sth->fetchrow_array ) {
210               push @authorised_values, $itemtype;
211               $authorised_lib{$itemtype} = $description;
212           }
213
214           #---- class_sources
215       }
216       elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "cn_source" ) {
217           push @authorised_values, "" unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
218             
219           my $class_sources = GetClassSources();
220           my $default_source = C4::Context->preference("DefaultClassificationSource");
221           
222           foreach my $class_source (sort keys %$class_sources) {
223               next unless $class_sources->{$class_source}->{'used'} or
224                           ($value and $class_source eq $value)      or
225                           ($class_source eq $default_source);
226               push @authorised_values, $class_source;
227               $authorised_lib{$class_source} = $class_sources->{$class_source}->{'description'};
228           }
229                   $value = $default_source unless ($value);
230
231           #---- "true" authorised value
232       }
233       else {
234           push @authorised_values, "" unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
235           $authorised_values_sth->execute( $tagslib->{$tag}->{$subfield}->{authorised_value} );
236           while ( my ( $value, $lib ) = $authorised_values_sth->fetchrow_array ) {
237               push @authorised_values, $value;
238               $authorised_lib{$value} = $lib;
239           }
240       }
241       $subfield_data{marc_value} =CGI::scrolling_list(      # FIXME: factor out scrolling_list
242           -name     => "field_value",
243           -values   => \@authorised_values,
244           -default  => $value,
245           -labels   => \%authorised_lib,
246           -override => 1,
247           -size     => 1,
248           -multiple => 0,
249           -tabindex => 1,
250           -id       => "tag_".$tag."_subfield_".$subfield."_".$index_subfield,
251           -class    => "input_marceditor",
252       );
253     # it's a thesaurus / authority field
254     }
255     elsif ( $tagslib->{$tag}->{$subfield}->{authtypecode} ) {
256         $subfield_data{marc_value} = "<input type=\"text\" $attributes />
257             <a href=\"#\" class=\"buttonDot\"
258                 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>
259     ";
260     # it's a plugin field
261     }
262     elsif ( $tagslib->{$tag}->{$subfield}->{value_builder} ) {
263         # opening plugin
264         my $plugin = C4::Context->intranetdir . "/cataloguing/value_builder/" . $tagslib->{$tag}->{$subfield}->{'value_builder'};
265         if (do $plugin) {
266                         my $temp;
267             my $extended_param = plugin_parameters( $dbh, $temp, $tagslib, $subfield_data{id}, \@loop_data );
268             my ( $function_name, $javascript ) = plugin_javascript( $dbh, $temp, $tagslib, $subfield_data{id}, \@loop_data );
269             $subfield_data{marc_value} = qq[<input $attributes
270                 onfocus="Focus$function_name($subfield_data{random}, '$subfield_data{id}');"
271                  onblur=" Blur$function_name($subfield_data{random}, '$subfield_data{id}');" />
272                 <a href="#" class="buttonDot" onclick="Clic$function_name('$subfield_data{id}'); return false;" title="Tag Editor">...</a>
273                 $javascript];
274         } else {
275             warn "Plugin Failed: $plugin";
276             $subfield_data{marc_value} = "<input $attributes />"; # supply default input form
277         }
278     }
279     elsif ( $tag eq '' ) {       # it's an hidden field
280         $subfield_data{marc_value} = qq(<input type="hidden" $attributes />);
281     }
282     elsif ( $tagslib->{$tag}->{$subfield}->{'hidden'} ) {   # FIXME: shouldn't input type be "hidden" ?
283         $subfield_data{marc_value} = qq(<input type="text" $attributes />);
284     }
285     elsif ( length($value) > 100
286             or (C4::Context->preference("marcflavour") eq "UNIMARC" and
287                   300 <= $tag && $tag < 400 && $subfield eq 'a' )
288             or (C4::Context->preference("marcflavour") eq "MARC21"  and
289                   500 <= $tag && $tag < 600                     )
290           ) {
291         # oversize field (textarea)
292         $subfield_data{marc_value} = "<textarea $attributes_no_value>$value</textarea>\n";
293     } else {
294         # it's a standard field
295          $subfield_data{marc_value} = "<input $attributes />";
296     }
297 #   $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\">";
298     push (@loop_data, \%subfield_data);
299     $i++
300   }
301 }
302
303 # what's the next op ? it's what we are not in : an add if we're editing, otherwise, and edit.
304 $template->param(
305     item             => \@loop_data,
306 );
307 $nextop="action"
308 }
309 $template->param(%$items_display_hashref) if $items_display_hashref;
310 $template->param(
311     op      => $nextop,
312     $op => 1,
313     opisadd => ($nextop eq "saveitem") ? 0 : 1,
314 );
315 foreach my $error (@errors) {
316     $template->param($error => 1);
317 }
318 output_html_with_http_headers $input, $cookie, $template->output;
319 exit;
320
321 sub BuildItemsData{
322         my @itemnumbers=@_;
323                 # now, build existiing item list
324                 my %witness; #---- stores the list of subfields used at least once, with the "meaning" of the code
325                 my @big_array;
326                 #---- finds where items.itemnumber is stored
327                 my (  $itemtagfield,   $itemtagsubfield) = &GetMarcFromKohaField("items.itemnumber", "");
328                 my ($branchtagfield, $branchtagsubfield) = &GetMarcFromKohaField("items.homebranch", "");
329                 foreach my $itemnumber (@itemnumbers){
330                         my $itemdata=GetItem($itemnumber);
331                         my $itemmarc=Item2Marc($itemdata);
332                         my $biblio=GetBiblioData($$itemdata{biblionumber});
333                         my %this_row;
334                         foreach my $field (grep {$_->tag() eq $itemtagfield} $itemmarc->fields()) {
335                                 # loop through each subfield
336                                 if (my $itembranchcode=$field->subfield($branchtagsubfield) && C4::Context->preference("IndependantBranches")) {
337                                                 #verifying rights
338                                                 my $userenv = C4::Context->userenv();
339                                                 unless (($userenv->{'flags'} == 1) or (($userenv->{'branch'} eq $itembranchcode))){
340                                                                 $this_row{'nomod'}=1;
341                                                 }
342                                 }
343                                 my $tag=$field->tag();
344                                 foreach my $subfield ($field->subfields) {
345                                         my ($subfcode,$subfvalue)=@$subfield;
346                                         next if ($tagslib->{$tag}->{$subfcode}->{tab} ne 10 
347                                                         && $tag        ne $itemtagfield 
348                                                         && $subfcode   ne $itemtagsubfield);
349
350                                         $witness{$subfcode} = $tagslib->{$tag}->{$subfcode}->{lib} if ($tagslib->{$tag}->{$subfcode}->{tab}  eq 10);
351                                         if ($tagslib->{$tag}->{$subfcode}->{tab}  eq 10) {
352                                                 $this_row{$subfcode}=GetAuthorisedValueDesc( $tag,
353                                                                         $subfcode, $subfvalue, '', $tagslib) 
354                                                                         || $subfvalue;
355                                         }
356
357                                         $this_row{itemnumber} = $subfvalue if ($tag eq $itemtagfield && $subfcode eq $itemtagsubfield);
358                                 }
359                         }
360                         $this_row{0}=join("\n",@$biblio{qw(title author ISBN)});
361                         $witness{0}="&nbsp;";
362                         if (%this_row) {
363                                 push(@big_array, \%this_row);
364                         }
365                 }
366                 @big_array = sort {$a->{0} cmp $b->{0}} @big_array;
367
368                 # now, construct template !
369                 # First, the existing items for display
370                 my @item_value_loop;
371                 my @witnesscodessorted=sort keys %witness;
372                 for my $row ( @big_array ) {
373                         my %row_data;
374                         my @item_fields = map +{ field => $_ || '' }, @$row{ @witnesscodessorted };
375                         $row_data{item_value} = [ @item_fields ];
376                         $row_data{itemnumber} = $row->{itemnumber};
377                         #reporting this_row values
378                         $row_data{'nomod'} = $row->{'nomod'};
379                         push(@item_value_loop,\%row_data);
380                 }
381                 my @header_loop=map { { header_value=> $witness{$_}} } @witnesscodessorted;
382         return { item_loop        => \@item_value_loop, item_header_loop => \@header_loop };
383 }
384 #BE WARN : it is not the general case 
385 # This function can be OK in the item marc record special case
386 # Where subfield is not repeated
387 # And where we are sure that field should correspond
388 # And $tag>10
389 sub UpdateMarcWith($$){
390   my ($marcfrom,$marcto)=@_;
391   #warn "FROM :",$marcfrom->as_formatted;
392         my (  $itemtag,   $itemtagsubfield) = &GetMarcFromKohaField("items.itemnumber", "");
393         my $fieldfrom=$marcfrom->field($itemtag);
394         my @fields_to=$marcto->field($itemtag);
395     foreach my $subfield ($fieldfrom->subfields()){
396                 foreach my $field_to_update (@fields_to){
397                                 $field_to_update->update($$subfield[0]=>$$subfield[1]) if ($$subfield[1]);
398                 }
399     }
400   #warn "TO edited:",$marcto->as_formatted;
401 }