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
37 my ($tagfield,$insubfield,$record) = @_;
40 foreach my $field ($record->field($tagfield)) {
41 my @subfields = $field->subfields();
42 foreach my $subfield (@subfields) {
43 if (@$subfield[0] eq $insubfield) {
44 $result .= @$subfield[1];
45 $indicator = $field->indicator(1).$field->indicator(2);
49 return($indicator,$result);
54 my $dbh = C4::Context->dbh;
55 my $error = $input->param('error');
56 my @itemnumbers = $input->param('itemnumber');
57 my $op = $input->param('op');
59 my ($template, $loggedinuser, $cookie)
60 = get_template_and_user({template_name => "tools/batchMod.tmpl",
64 flagsrequired => {editcatalogue => 1},
68 my $today_iso = C4::Dates->today('iso');
69 $template->param(today_iso => $today_iso);
73 my @errors; # store errors found while checking data BEFORE saving item.
74 my $items_display_hashref;
76 my $tagslib = &GetMarcStructure(1,$frameworkcode);
78 #--- ----------------------------------------------------------------------------
79 if ($op eq "action") {
80 #-------------------------------------------------------------------------------
81 my @tags = $input->param('tag');
82 my @subfields = $input->param('subfield');
83 my @values = $input->param('field_value');
84 # build indicator hash.
85 my @ind_tag = $input->param('ind_tag');
86 my @indicator = $input->param('indicator');
87 my $xml = TransformHtmlToXml(\@tags,\@subfields,\@values,\@indicator,\@ind_tag, 'ITEM');
88 my $marcitem = MARC::Record::new_from_xml($xml, 'UTF-8');
89 my $localitem = TransformMarcToKoha( $dbh, $marcitem, "", 'items' );
91 # my @params=$input->param();
93 # my $marcitem = TransformHtmlToMarc(\@params,$input);
94 foreach my $itemnumber(@itemnumbers){
95 my $itemdata=GetItem($itemnumber);
96 if ($input->param("del")){
97 DelItemCheck(C4::Context->dbh, $itemdata->{'biblionumber'}, $itemdata->{'itemnumber'})
99 my $localmarcitem=Item2Marc($itemdata);
100 UpdateMarcWith($marcitem,$localmarcitem);
101 eval{my ($oldbiblionumber,$oldbibnum,$oldbibitemnum) = ModItemFromMarc($localmarcitem,$itemdata->{biblionumber},$itemnumber)};
102 # eval{ModItem($localitem,$itemdata->{biblionumber},$itemnumber)};
105 $items_display_hashref=BuildItemsData(@itemnumbers);
110 #-------------------------------------------------------------------------------
111 # build screen with existing items. and "new" one
112 #-------------------------------------------------------------------------------
115 my $barcodefh = $input->upload('uploadbarcodes');
118 while (my $barcode=<$barcodefh>){
120 push @barcodelist, $barcode;
123 if ( my $list=$input->param('barcodelist')){
124 push @barcodelist, split(/\s\n/, $list);
126 push @itemnumbers,map{GetItemnumberFromBarcode($_)} @barcodelist;
128 warn Dump(@itemnumbers);
129 $items_display_hashref=BuildItemsData(@itemnumbers);
130 warn Dump($items_display_hashref);
132 # now, build the item form for entering a new item
135 my $authorised_values_sth = $dbh->prepare("SELECT authorised_value,lib FROM authorised_values WHERE category=? ORDER BY lib");
137 my $branches = GetBranchesLoop(); # build once ahead of time, instead of multiple times later.
138 my $pref_itemcallnumber = C4::Context->preference('itemcallnumber');
140 foreach my $tag (sort keys %{$tagslib}) {
141 # loop through each subfield
142 foreach my $subfield (sort keys %{$tagslib->{$tag}}) {
143 next if subfield_is_koha_internal_p($subfield);
144 next if ($tagslib->{$tag}->{$subfield}->{'tab'} ne "10");
147 my $index_subfield = int(rand(1000000));
148 if ($subfield eq '@'){
149 $subfield_data{id} = "tag_".$tag."_subfield_00_".$index_subfield;
151 $subfield_data{id} = "tag_".$tag."_subfield_".$subfield."_".$index_subfield;
153 $subfield_data{tag} = $tag;
154 $subfield_data{subfield} = $subfield;
155 $subfield_data{random} = int(rand(1000000)); # why do we need 2 different randoms?
156 # $subfield_data{marc_lib} = $tagslib->{$tag}->{$subfield}->{lib};
157 $subfield_data{marc_lib} ="<span id=\"error$i\" title=\"".$tagslib->{$tag}->{$subfield}->{lib}."\">".$tagslib->{$tag}->{$subfield}->{lib}."</span>";
158 $subfield_data{mandatory} = $tagslib->{$tag}->{$subfield}->{mandatory};
159 $subfield_data{repeatable} = $tagslib->{$tag}->{$subfield}->{repeatable};
161 $value =~ s/"/"/g;
163 $value = $tagslib->{$tag}->{$subfield}->{defaultvalue};
164 # get today date & replace YYYY, MM, DD if provided in the default value
165 my ( $year, $month, $day ) = split ',', $today_iso; # FIXME: iso dates don't have commas!
166 $value =~ s/YYYY/$year/g;
167 $value =~ s/MM/$month/g;
168 $value =~ s/DD/$day/g;
170 $subfield_data{visibility} = "display:none;" if (($tagslib->{$tag}->{$subfield}->{hidden} > 4) || ($tagslib->{$tag}->{$subfield}->{hidden} < -4));
171 # testing branch value if IndependantBranches.
173 my $attributes_no_value = qq(tabindex="1" id="$subfield_data{id}" name="field_value" class="input_marceditor" size="67" maxlength="255" );
174 my $attributes = qq($attributes_no_value value="$value" );
175 if ( $tagslib->{$tag}->{$subfield}->{authorised_value} ) {
176 my @authorised_values;
178 # builds list, depending on authorised value...
180 if ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "branches" ) {
181 foreach my $thisbranch (@$branches) {
182 push @authorised_values, $thisbranch->{value};
183 $authorised_lib{$thisbranch->{value}} = $thisbranch->{branchname};
184 $value = $thisbranch->{value} if $thisbranch->{selected};
187 elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes" ) {
188 push @authorised_values, "" unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
189 my $sth = $dbh->prepare("select itemtype,description from itemtypes order by description");
191 while ( my ( $itemtype, $description ) = $sth->fetchrow_array ) {
192 push @authorised_values, $itemtype;
193 $authorised_lib{$itemtype} = $description;
198 elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "cn_source" ) {
199 push @authorised_values, "" unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
201 my $class_sources = GetClassSources();
202 my $default_source = C4::Context->preference("DefaultClassificationSource");
204 foreach my $class_source (sort keys %$class_sources) {
205 next unless $class_sources->{$class_source}->{'used'} or
206 ($value and $class_source eq $value) or
207 ($class_source eq $default_source);
208 push @authorised_values, $class_source;
209 $authorised_lib{$class_source} = $class_sources->{$class_source}->{'description'};
211 $value = $default_source unless ($value);
213 #---- "true" authorised value
216 push @authorised_values, "" unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
217 $authorised_values_sth->execute( $tagslib->{$tag}->{$subfield}->{authorised_value} );
218 while ( my ( $value, $lib ) = $authorised_values_sth->fetchrow_array ) {
219 push @authorised_values, $value;
220 $authorised_lib{$value} = $lib;
223 $subfield_data{marc_value} =CGI::scrolling_list( # FIXME: factor out scrolling_list
224 -name => "field_value",
225 -values => \@authorised_values,
227 -labels => \%authorised_lib,
232 -id => "tag_".$tag."_subfield_".$subfield."_".$index_subfield,
233 -class => "input_marceditor",
235 # it's a thesaurus / authority field
237 elsif ( $tagslib->{$tag}->{$subfield}->{authtypecode} ) {
238 $subfield_data{marc_value} = "<input type=\"text\" $attributes />
239 <a href=\"#\" class=\"buttonDot\"
240 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>
242 # it's a plugin field
244 elsif ( $tagslib->{$tag}->{$subfield}->{value_builder} ) {
246 my $plugin = C4::Context->intranetdir . "/cataloguing/value_builder/" . $tagslib->{$tag}->{$subfield}->{'value_builder'};
249 my $extended_param = plugin_parameters( $dbh, $temp, $tagslib, $subfield_data{id}, \@loop_data );
250 my ( $function_name, $javascript ) = plugin_javascript( $dbh, $temp, $tagslib, $subfield_data{id}, \@loop_data );
251 $subfield_data{marc_value} = qq[<input $attributes
252 onfocus="Focus$function_name($subfield_data{random}, '$subfield_data{id}');"
253 onblur=" Blur$function_name($subfield_data{random}, '$subfield_data{id}');" />
254 <a href="#" class="buttonDot" onclick="Clic$function_name('$subfield_data{id}'); return false;" title="Tag Editor">...</a>
257 warn "Plugin Failed: $plugin";
258 $subfield_data{marc_value} = "<input $attributes />"; # supply default input form
261 elsif ( $tag eq '' ) { # it's an hidden field
262 $subfield_data{marc_value} = qq(<input type="hidden" $attributes />);
264 elsif ( $tagslib->{$tag}->{$subfield}->{'hidden'} ) { # FIXME: shouldn't input type be "hidden" ?
265 $subfield_data{marc_value} = qq(<input type="text" $attributes />);
267 elsif ( length($value) > 100
268 or (C4::Context->preference("marcflavour") eq "UNIMARC" and
269 300 <= $tag && $tag < 400 && $subfield eq 'a' )
270 or (C4::Context->preference("marcflavour") eq "MARC21" and
271 500 <= $tag && $tag < 600 )
273 # oversize field (textarea)
274 $subfield_data{marc_value} = "<textarea $attributes_no_value>$value</textarea>\n";
276 # it's a standard field
277 $subfield_data{marc_value} = "<input $attributes />";
279 # $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\">";
280 push (@loop_data, \%subfield_data);
285 # what's the next op ? it's what we are not in : an add if we're editing, otherwise, and edit.
291 $template->param(%$items_display_hashref) if $items_display_hashref;
295 opisadd => ($nextop eq "saveitem") ? 0 : 1,
297 foreach my $error (@errors) {
298 $template->param($error => 1);
300 output_html_with_http_headers $input, $cookie, $template->output;
305 # now, build existiing item list
306 my %witness; #---- stores the list of subfields used at least once, with the "meaning" of the code
308 #---- finds where items.itemnumber is stored
309 my ( $itemtagfield, $itemtagsubfield) = &GetMarcFromKohaField("items.itemnumber", "");
310 my ($branchtagfield, $branchtagsubfield) = &GetMarcFromKohaField("items.homebranch", "");
311 foreach my $itemnumber (@itemnumbers){
312 my $itemdata=GetItem($itemnumber);
313 my $itemmarc=Item2Marc($itemdata);
314 my $biblio=GetBiblioData($$itemdata{biblionumber});
316 foreach my $field (grep {$_->tag() eq $itemtagfield} $itemmarc->fields()) {
317 # loop through each subfield
318 if (my $itembranchcode=$field->subfield($branchtagsubfield) && C4::Context->preference("IndependantBranches")) {
320 my $userenv = C4::Context->userenv();
321 unless (($userenv->{'flags'} == 1) or (($userenv->{'branch'} eq $itembranchcode))){
322 $this_row{'nomod'}=1;
325 my $tag=$field->tag();
326 foreach my $subfield ($field->subfields) {
327 my ($subfcode,$subfvalue)=@$subfield;
328 next if ($tagslib->{$tag}->{$subfcode}->{tab} ne 10
329 && $tag ne $itemtagfield
330 && $subfcode ne $itemtagsubfield);
332 $witness{$subfcode} = $tagslib->{$tag}->{$subfcode}->{lib} if ($tagslib->{$tag}->{$subfcode}->{tab} eq 10);
333 if ($tagslib->{$tag}->{$subfcode}->{tab} eq 10) {
334 $this_row{$subfcode}=GetAuthorisedValueDesc( $tag,
335 $subfcode, $subfvalue, '', $tagslib)
339 $this_row{itemnumber} = $subfvalue if ($tag eq $itemtagfield && $subfcode eq $itemtagsubfield);
342 $this_row{0}=join("\n",@$biblio{qw(title author ISBN)});
343 $witness{0}=" ";
345 push(@big_array, \%this_row);
348 @big_array = sort {$a->{0} cmp $b->{0}} @big_array;
350 # now, construct template !
351 # First, the existing items for display
353 my @witnesscodessorted=sort keys %witness;
354 for my $row ( @big_array ) {
356 my @item_fields = map +{ field => $_ || '' }, @$row{ @witnesscodessorted };
357 $row_data{item_value} = [ @item_fields ];
358 $row_data{itemnumber} = $row->{itemnumber};
359 #reporting this_row values
360 $row_data{'nomod'} = $row->{'nomod'};
361 push(@item_value_loop,\%row_data);
363 my @header_loop=map { { header_value=> $witness{$_}} } @witnesscodessorted;
364 return { item_loop => \@item_value_loop, item_header_loop => \@header_loop };
366 #BE WARN : it is not the general case
367 # This function can be OK in the item marc record special case
368 # Where subfield is not repeated
369 # And where we are sure that field should correspond
371 sub UpdateMarcWith($$){
372 my ($marcfrom,$marcto)=@_;
373 warn "FROM :",$marcfrom->as_formatted;
374 my ( $itemtag, $itemtagsubfield) = &GetMarcFromKohaField("items.itemnumber", "");
375 my $fieldfrom=$marcfrom->field($itemtag);
376 my @fields_to=$marcto->field($itemtag);
377 foreach my $subfield ($fieldfrom->subfields()){
378 foreach my $field_to_update (@fields_to){
379 $field_to_update->update($$subfield[0]=>$$subfield[1]) if ($$subfield[1]);
382 warn "TO edited:",$marcto->as_formatted;