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 with
18 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
19 # Suite 330, Boston, MA 02111-1307 USA
28 use C4::Koha; # XXX subfield_is_koha_internal_p
29 use C4::Branch; # XXX subfield_is_koha_internal_p
38 my ($tagfield,$insubfield,$record) = @_;
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);
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');
61 my ($template, $loggedinuser, $cookie)
62 = get_template_and_user({template_name => "tools/batchMod.tmpl",
66 flagsrequired => {editcatalogue => 1},
70 my $today_iso = C4::Dates->today('iso');
71 $template->param(today_iso => $today_iso);
72 $template->param(del => $del);
76 my @errors; # store errors found while checking data BEFORE saving item.
77 my $items_display_hashref;
79 my $tagslib = &GetMarcStructure(1,$frameworkcode);
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' );
94 # my @params=$input->param();
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'})
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)};
108 $items_display_hashref=BuildItemsData(@itemnumbers);
113 #-------------------------------------------------------------------------------
114 # build screen with existing items. and "new" one
115 #-------------------------------------------------------------------------------
118 my $filefh = $input->upload('uploadfile');
119 my $filecontent = $input->param('filecontent');
123 while (my $content=<$filefh>){
125 push @contentlist, $content;
128 switch ($filecontent) {
129 case "barcode_file" {
130 push @itemnumbers,map{GetItemnumberFromBarcode($_)} @contentlist;
131 # Remove not found barcodes
132 @itemnumbers = grep(!/^$/, @itemnumbers);
136 @itemnumbers = @contentlist;
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);
148 $items_display_hashref=BuildItemsData(@itemnumbers);
150 # now, build the item form for entering a new item
153 my $authorised_values_sth = $dbh->prepare("SELECT authorised_value,lib FROM authorised_values WHERE category=? ORDER BY lib");
155 my $branches = GetBranchesLoop(); # build once ahead of time, instead of multiple times later.
156 my $pref_itemcallnumber = C4::Context->preference('itemcallnumber');
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");
165 my $index_subfield = int(rand(1000000));
166 if ($subfield eq '@'){
167 $subfield_data{id} = "tag_".$tag."_subfield_00_".$index_subfield;
169 $subfield_data{id} = "tag_".$tag."_subfield_".$subfield."_".$index_subfield;
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};
179 $value =~ s/"/"/g;
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;
188 $subfield_data{visibility} = "display:none;" if (($tagslib->{$tag}->{$subfield}->{hidden} > 4) || ($tagslib->{$tag}->{$subfield}->{hidden} < -4));
189 # testing branch value if IndependantBranches.
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;
196 # builds list, depending on authorised value...
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};
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");
209 while ( my ( $itemtype, $description ) = $sth->fetchrow_array ) {
210 push @authorised_values, $itemtype;
211 $authorised_lib{$itemtype} = $description;
216 elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "cn_source" ) {
217 push @authorised_values, "" unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
219 my $class_sources = GetClassSources();
220 my $default_source = C4::Context->preference("DefaultClassificationSource");
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'};
229 $value = $default_source unless ($value);
231 #---- "true" authorised value
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;
241 $subfield_data{marc_value} =CGI::scrolling_list( # FIXME: factor out scrolling_list
242 -name => "field_value",
243 -values => \@authorised_values,
245 -labels => \%authorised_lib,
250 -id => "tag_".$tag."_subfield_".$subfield."_".$index_subfield,
251 -class => "input_marceditor",
253 # it's a thesaurus / authority field
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>
260 # it's a plugin field
262 elsif ( $tagslib->{$tag}->{$subfield}->{value_builder} ) {
264 my $plugin = C4::Context->intranetdir . "/cataloguing/value_builder/" . $tagslib->{$tag}->{$subfield}->{'value_builder'};
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>
275 warn "Plugin Failed: $plugin";
276 $subfield_data{marc_value} = "<input $attributes />"; # supply default input form
279 elsif ( $tag eq '' ) { # it's an hidden field
280 $subfield_data{marc_value} = qq(<input type="hidden" $attributes />);
282 elsif ( $tagslib->{$tag}->{$subfield}->{'hidden'} ) { # FIXME: shouldn't input type be "hidden" ?
283 $subfield_data{marc_value} = qq(<input type="text" $attributes />);
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 )
291 # oversize field (textarea)
292 $subfield_data{marc_value} = "<textarea $attributes_no_value>$value</textarea>\n";
294 # it's a standard field
295 $subfield_data{marc_value} = "<input $attributes />";
297 # $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\">";
298 push (@loop_data, \%subfield_data);
303 # what's the next op ? it's what we are not in : an add if we're editing, otherwise, and edit.
309 $template->param(%$items_display_hashref) if $items_display_hashref;
313 opisadd => ($nextop eq "saveitem") ? 0 : 1,
315 foreach my $error (@errors) {
316 $template->param($error => 1);
318 output_html_with_http_headers $input, $cookie, $template->output;
323 # now, build existiing item list
324 my %witness; #---- stores the list of subfields used at least once, with the "meaning" of the code
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});
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")) {
338 my $userenv = C4::Context->userenv();
339 unless (($userenv->{'flags'} == 1) or (($userenv->{'branch'} eq $itembranchcode))){
340 $this_row{'nomod'}=1;
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);
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)
357 $this_row{itemnumber} = $subfvalue if ($tag eq $itemtagfield && $subfcode eq $itemtagsubfield);
360 $this_row{0}=join("\n",@$biblio{qw(title author ISBN)});
361 $witness{0}=" ";
363 push(@big_array, \%this_row);
366 @big_array = sort {$a->{0} cmp $b->{0}} @big_array;
368 # now, construct template !
369 # First, the existing items for display
371 my @witnesscodessorted=sort keys %witness;
372 for my $row ( @big_array ) {
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);
381 my @header_loop=map { { header_value=> $witness{$_}} } @witnesscodessorted;
382 return { item_loop => \@item_value_loop, item_header_loop => \@header_loop };
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
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]);
400 #warn "TO edited:",$marcto->as_formatted;