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