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
32 use List::MoreUtils qw/any/;
36 sub get_item_from_barcode {
38 my $dbh=C4::Context->dbh;
40 my $rq=$dbh->prepare("SELECT itemnumber from items where items.barcode=?");
41 $rq->execute($barcode);
42 ($result)=$rq->fetchrow;
46 sub generate_subfield_form {
47 my ($tag, $subfieldtag, $value, $tagslib,$subfieldlib, $branches, $today_iso, $biblionumber, $temp, $loop_data, $i) = @_;
50 my $dbh = C4::Context->dbh;
51 my $authorised_values_sth = $dbh->prepare("SELECT authorised_value,lib FROM authorised_values WHERE category=? ORDER BY lib");
53 my $index_subfield = int(rand(1000000));
54 if ($subfieldtag eq '@'){
55 $subfield_data{id} = "tag_".$tag."_subfield_00_".$index_subfield;
57 $subfield_data{id} = "tag_".$tag."_subfield_".$subfieldtag."_".$index_subfield;
60 $subfield_data{tag} = $tag;
61 $subfield_data{subfield} = $subfieldtag;
62 $subfield_data{random} = int(rand(1000000)); # why do we need 2 different randoms?
63 $subfield_data{marc_lib} ="<span id=\"error$i\" title=\"".$subfieldlib->{lib}."\">".$subfieldlib->{lib}."</span>";
64 $subfield_data{mandatory} = $subfieldlib->{mandatory};
65 $subfield_data{repeatable} = $subfieldlib->{repeatable};
67 $value =~ s/"/"/g;
68 if ( ! defined( $value ) || $value eq '') {
69 $value = $subfieldlib->{defaultvalue};
70 # get today date & replace YYYY, MM, DD if provided in the default value
71 my ( $year, $month, $day ) = split ',', $today_iso; # FIXME: iso dates don't have commas!
72 $value =~ s/YYYY/$year/g;
73 $value =~ s/MM/$month/g;
74 $value =~ s/DD/$day/g;
77 $subfield_data{visibility} = "display:none;" if (($subfieldlib->{hidden} > 4) || ($subfieldlib->{hidden} < -4));
79 my $pref_itemcallnumber = C4::Context->preference('itemcallnumber');
80 if (!$value && $subfieldlib->{kohafield} eq 'items.itemcallnumber' && $pref_itemcallnumber) {
81 my $CNtag = substr($pref_itemcallnumber, 0, 3);
82 my $CNsubfield = substr($pref_itemcallnumber, 3, 1);
83 my $CNsubfield2 = substr($pref_itemcallnumber, 4, 1);
84 my $temp2 = $temp->field($CNtag);
86 $value = ($temp2->subfield($CNsubfield)).' '.($temp2->subfield($CNsubfield2));
87 #remove any trailing space incase one subfield is used
88 $value =~ s/^\s+|\s+$//g;
92 my $attributes_no_value = qq(tabindex="1" id="$subfield_data{id}" name="field_value" class="input_marceditor" size="67" maxlength="255" );
93 my $attributes = qq($attributes_no_value value="$value" );
95 if ( $subfieldlib->{authorised_value} ) {
96 my @authorised_values;
98 # builds list, depending on authorised value...
99 if ( $subfieldlib->{authorised_value} eq "branches" ) {
100 foreach my $thisbranch (@$branches) {
101 push @authorised_values, $thisbranch->{value};
102 $authorised_lib{$thisbranch->{value}} = $thisbranch->{branchname};
103 $value = $thisbranch->{value} if $thisbranch->{selected};
106 elsif ( $subfieldlib->{authorised_value} eq "itemtypes" ) {
107 push @authorised_values, "" unless ( $subfieldlib->{mandatory} );
108 my $sth = $dbh->prepare("SELECT itemtype,description FROM itemtypes ORDER BY description");
110 while ( my ( $itemtype, $description ) = $sth->fetchrow_array ) {
111 push @authorised_values, $itemtype;
112 $authorised_lib{$itemtype} = $description;
116 my $itype_sth = $dbh->prepare("SELECT itemtype FROM biblioitems WHERE biblionumber = ?");
117 $itype_sth->execute( $biblionumber );
118 ( $value ) = $itype_sth->fetchrow_array;
123 elsif ( $subfieldlib->{authorised_value} eq "cn_source" ) {
124 push @authorised_values, "" unless ( $subfieldlib->{mandatory} );
126 my $class_sources = GetClassSources();
127 my $default_source = C4::Context->preference("DefaultClassificationSource");
129 foreach my $class_source (sort keys %$class_sources) {
130 next unless $class_sources->{$class_source}->{'used'} or
131 ($value and $class_source eq $value) or
132 ($class_source eq $default_source);
133 push @authorised_values, $class_source;
134 $authorised_lib{$class_source} = $class_sources->{$class_source}->{'description'};
136 $value = $default_source unless ($value);
138 #---- "true" authorised value
141 push @authorised_values, "" unless ( $subfieldlib->{mandatory} );
142 $authorised_values_sth->execute( $subfieldlib->{authorised_value} );
143 while ( my ( $value, $lib ) = $authorised_values_sth->fetchrow_array ) {
144 push @authorised_values, $value;
145 $authorised_lib{$value} = $lib;
149 $subfield_data{marc_value} =CGI::scrolling_list( # FIXME: factor out scrolling_list
150 -name => "field_value",
151 -values => \@authorised_values,
153 -labels => \%authorised_lib,
158 -id => "tag_".$tag."_subfield_".$subfieldtag."_".$index_subfield,
159 -class => "input_marceditor",
162 # it's a thesaurus / authority field
164 elsif ( $subfieldlib->{authtypecode} ) {
165 $subfield_data{marc_value} = "<input type=\"text\" $attributes />
166 <a href=\"#\" class=\"buttonDot\"
167 onclick=\"Dopop('/cgi-bin/koha/authorities/auth_finder.pl?authtypecode=".$subfieldlib->{authtypecode}."&index=$subfield_data{id}','$subfield_data{id}'); return false;\" title=\"Tag Editor\">...</a>
169 # it's a plugin field
171 elsif ( $subfieldlib->{value_builder} ) {
173 my $plugin = C4::Context->intranetdir . "/cataloguing/value_builder/" . $subfieldlib->{'value_builder'};
175 my $extended_param = plugin_parameters( $dbh, $temp, $tagslib, $subfield_data{id}, $loop_data );
176 my ( $function_name, $javascript ) = plugin_javascript( $dbh, $temp, $tagslib, $subfield_data{id}, $loop_data );
177 $subfield_data{marc_value} = qq[<input $attributes
178 onfocus="Focus$function_name($subfield_data{random}, '$subfield_data{id}');"
179 onblur=" Blur$function_name($subfield_data{random}, '$subfield_data{id}');" />
180 <a href="#" class="buttonDot" onclick="Clic$function_name('$subfield_data{id}'); return false;" title="Tag Editor">...</a>
183 warn "Plugin Failed: $plugin";
184 $subfield_data{marc_value} = "<input $attributes />"; # supply default input form
187 elsif ( $tag eq '' ) { # it's an hidden field
188 $subfield_data{marc_value} = qq(<input type="hidden" $attributes />);
190 elsif ( $subfieldlib->{'hidden'} ) { # FIXME: shouldn't input type be "hidden" ?
191 $subfield_data{marc_value} = qq(<input type="text" $attributes />);
193 elsif ( length($value) > 100
194 or (C4::Context->preference("marcflavour") eq "UNIMARC" and
195 300 <= $tag && $tag < 400 && $subfieldtag eq 'a' )
196 or (C4::Context->preference("marcflavour") eq "MARC21" and
197 500 <= $tag && $tag < 600 )
199 # oversize field (textarea)
200 $subfield_data{marc_value} = "<textarea $attributes_no_value>$value</textarea>\n";
202 # it's a standard field
203 $subfield_data{marc_value} = "<input $attributes />";
206 return \%subfield_data;
211 my $dbh = C4::Context->dbh;
212 my $error = $input->param('error');
213 my $biblionumber = $input->param('biblionumber');
214 my $itemnumber = $input->param('itemnumber');
215 my $op = $input->param('op');
217 my ($template, $loggedinuser, $cookie)
218 = get_template_and_user({template_name => "cataloguing/additem.tmpl",
221 authnotrequired => 0,
222 flagsrequired => {editcatalogue => 'edit_items'},
226 my $frameworkcode = &GetFrameworkCode($biblionumber);
228 my $today_iso = C4::Dates->today('iso');
229 $template->param(today_iso => $today_iso);
231 my $tagslib = &GetMarcStructure(1,$frameworkcode);
232 my $record = GetMarcBiblio($biblionumber);
233 my $oldrecord = TransformMarcToKoha($dbh,$record);
235 my $nextop="additem";
236 my @errors; # store errors found while checking data BEFORE saving item.
237 #-------------------------------------------------------------------------------
238 if ($op eq "additem") {
239 #-------------------------------------------------------------------------------
241 my @tags = $input->param('tag');
242 my @subfields = $input->param('subfield');
243 my @values = $input->param('field_value');
244 # build indicator hash.
245 my @ind_tag = $input->param('ind_tag');
246 my @indicator = $input->param('indicator');
247 my $xml = TransformHtmlToXml(\@tags,\@subfields,\@values,\@indicator,\@ind_tag, 'ITEM');
248 my $record = MARC::Record::new_from_xml($xml, 'UTF-8');
249 # if autoBarcode is set to 'incremental', calculate barcode...
250 # NOTE: This code is subject to change in 3.2 with the implemenation of ajax based autobarcode code
251 # NOTE: 'incremental' is the ONLY autoBarcode option available to those not using javascript
252 if (C4::Context->preference('autoBarcode') eq 'incremental') {
253 my ($tagfield,$tagsubfield) = &GetMarcFromKohaField("items.barcode",$frameworkcode);
254 unless ($record->field($tagfield)->subfield($tagsubfield)) {
255 my $sth_barcode = $dbh->prepare("select max(abs(barcode)) from items");
256 $sth_barcode->execute;
257 my ($newbarcode) = $sth_barcode->fetchrow;
259 # OK, we have the new barcode, now create the entry in MARC record
260 my $fieldItem = $record->field($tagfield);
261 $record->delete_field($fieldItem);
262 $fieldItem->add_subfields($tagsubfield => $newbarcode);
263 $record->insert_fields_ordered($fieldItem);
266 # check for item barcode # being unique
267 my $addedolditem = TransformMarcToKoha($dbh,$record);
268 my $exist_itemnumber = get_item_from_barcode($addedolditem->{'barcode'});
269 push @errors,"barcode_not_unique" if($exist_itemnumber);
270 # if barcode exists, don't create, but report The problem.
271 my ($oldbiblionumber,$oldbibnum,$oldbibitemnum) = AddItemFromMarc($record,$biblionumber) unless ($exist_itemnumber);
273 if ($exist_itemnumber) {
274 $itemrecord = $record;
276 #-------------------------------------------------------------------------------
277 } elsif ($op eq "edititem") {
278 #-------------------------------------------------------------------------------
279 # retrieve item if exist => then, it's a modif
280 $itemrecord = C4::Items::GetMarcItem($biblionumber,$itemnumber);
281 $nextop = "saveitem";
282 #-------------------------------------------------------------------------------
283 } elsif ($op eq "delitem") {
284 #-------------------------------------------------------------------------------
285 $error = &DelItemCheck($dbh,$biblionumber,$itemnumber);
287 print $input->redirect("additem.pl?biblionumber=$biblionumber&frameworkcode=$frameworkcode");
293 #-------------------------------------------------------------------------------
294 } elsif ($op eq "delallitems") {
295 #-------------------------------------------------------------------------------
296 my @biblioitems = &GetBiblioItemByBiblioNumber($biblionumber);
297 foreach my $biblioitem (@biblioitems){
298 my $items = &GetItemsByBiblioitemnumber($biblioitem->{biblioitemnumber});
300 foreach my $item (@$items){
301 &DelItemCheck($dbh,$biblionumber,$item->{itemnumber});
304 print $input->redirect("/cgi-bin/koha/catalogue/moredetail.pl?biblionumber=$biblionumber");
306 #-------------------------------------------------------------------------------
307 } elsif ($op eq "saveitem") {
308 #-------------------------------------------------------------------------------
310 my @tags = $input->param('tag');
311 my @subfields = $input->param('subfield');
312 my @values = $input->param('field_value');
313 # build indicator hash.
314 my @ind_tag = $input->param('ind_tag');
315 my @indicator = $input->param('indicator');
316 # my $itemnumber = $input->param('itemnumber');
317 my $xml = TransformHtmlToXml(\@tags,\@subfields,\@values,\@indicator,\@ind_tag,'ITEM');
319 my $itemtosave=MARC::Record::new_from_xml($xml, 'UTF-8');
320 # MARC::Record builded => now, record in DB
321 # warn "R: ".$record->as_formatted;
322 # check that the barcode don't exist already
323 my $addedolditem = TransformMarcToKoha($dbh,$itemtosave);
324 my $exist_itemnumber = get_item_from_barcode($addedolditem->{'barcode'});
325 if ($exist_itemnumber && $exist_itemnumber != $itemnumber) {
326 push @errors,"barcode_not_unique";
328 my ($oldbiblionumber,$oldbibnum,$oldbibitemnum) = ModItemFromMarc($itemtosave,$biblionumber,$itemnumber);
335 #-------------------------------------------------------------------------------
336 # build screen with existing items. and "new" one
337 #-------------------------------------------------------------------------------
339 # now, build existiing item list
340 my $temp = GetMarcBiblio( $biblionumber );
341 my @fields = $temp->fields();
343 #my @fields = $record->fields();
344 my %witness; #---- stores the list of subfields used at least once, with the "meaning" of the code
346 #---- finds where items.itemnumber is stored
347 my ( $itemtagfield, $itemtagsubfield) = &GetMarcFromKohaField("items.itemnumber", $frameworkcode);
348 my ($branchtagfield, $branchtagsubfield) = &GetMarcFromKohaField("items.homebranch", $frameworkcode);
350 foreach my $field (@fields) {
351 next if ($field->tag()<10);
353 my @subf = $field->subfields or (); # don't use ||, as that forces $field->subfelds to be interpreted in scalar context
355 # loop through each subfield
357 foreach my $subfield (@subf){
358 my $subfieldcode = $subfield->[0];
359 my $subfieldvalue= $subfield->[1];
361 next if ($tagslib->{$field->tag()}->{$subfieldcode}->{tab} ne 10
362 && ($field->tag() ne $itemtagfield
363 && $subfieldcode ne $itemtagsubfield));
364 $witness{$subfieldcode} = $tagslib->{$field->tag()}->{$subfieldcode}->{lib} if ($tagslib->{$field->tag()}->{$subfieldcode}->{tab} eq 10);
365 if ($tagslib->{$field->tag()}->{$subfieldcode}->{tab} eq 10) {
366 $this_row{$subfieldcode} .= " | " if($this_row{$subfieldcode});
367 $this_row{$subfieldcode} .= GetAuthorisedValueDesc( $field->tag(),
368 $subfieldcode, $subfieldvalue, '', $tagslib)
372 if (($field->tag eq $branchtagfield) && ($subfieldcode eq $branchtagsubfield) && C4::Context->preference("IndependantBranches")) {
374 my $userenv = C4::Context->userenv();
375 unless (($userenv->{'flags'} == 1) or (($userenv->{'branch'} eq $subfieldvalue))){
376 $this_row{'nomod'}=1;
379 $this_row{itemnumber} = $subfieldvalue if ($field->tag() eq $itemtagfield && $subfieldcode eq $itemtagsubfield);
382 push(@big_array, \%this_row);
386 my ($holdingbrtagf,$holdingbrtagsubf) = &GetMarcFromKohaField("items.holdingbranch",$frameworkcode);
387 @big_array = sort {$a->{$holdingbrtagsubf} cmp $b->{$holdingbrtagsubf}} @big_array;
389 # now, construct template !
390 # First, the existing items for display
392 my @header_value_loop;
393 for my $row ( @big_array ) {
395 my @item_fields = map +{ field => $_ || '' }, @$row{ sort keys(%witness) };
396 $row_data{item_value} = [ @item_fields ];
397 $row_data{itemnumber} = $row->{itemnumber};
398 #reporting this_row values
399 $row_data{'nomod'} = $row->{'nomod'};
400 push(@item_value_loop,\%row_data);
402 foreach my $subfield_code (sort keys(%witness)) {
404 $header_value{header_value} = $witness{$subfield_code};
405 push(@header_value_loop, \%header_value);
408 # now, build the item form for entering a new item
412 my $onlymine = C4::Context->preference('IndependantBranches') &&
413 C4::Context->userenv &&
414 C4::Context->userenv->{flags}!=1 &&
415 C4::Context->userenv->{branch};
416 my $branches = GetBranchesLoop(undef,$onlymine); # build once ahead of time, instead of multiple times later.
418 # We generate form, from actuel record
421 foreach my $field ($itemrecord->fields()){
422 my $tag = $field->{_tag};
423 foreach my $subfield ( $field->subfields() ){
425 my $subfieldtag = $subfield->[0];
426 my $value = $subfield->[1];
427 my $subfieldlib = $tagslib->{$tag}->{$subfieldtag};
429 next if subfield_is_koha_internal_p($subfieldtag);
430 next if ($tagslib->{$tag}->{$subfieldtag}->{'tab'} ne "10");
432 my $subfield_data = generate_subfield_form($tag, $subfieldtag, $value, $tagslib, $subfieldlib, $branches, $today_iso, $biblionumber, $temp, \@loop_data, $i);
434 push @fields, "$tag$subfieldtag";
435 push (@loop_data, $subfield_data);
440 # and now we add fields that are empty
442 foreach my $tag ( keys %{$tagslib}){
443 foreach my $subtag (keys %{$tagslib->{$tag}}){
444 next if subfield_is_koha_internal_p($subtag);
445 next if ($tagslib->{$tag}->{$subtag}->{'tab'} ne "10");
446 next if any { /^$tag$subtag$/ } @fields;
449 my $subfield_data = generate_subfield_form($tag, $subtag, $value, $tagslib, $tagslib->{$tag}->{$subtag}, $branches, $today_iso, $biblionumber, $temp, \@loop_data, $i);
451 push (@loop_data, $subfield_data);
455 # what's the next op ? it's what we are not in : an add if we're editing, otherwise, and edit.
456 $template->param( title => $record->title() ) if ($record ne "-1");
458 biblionumber => $biblionumber,
459 title => $oldrecord->{title},
460 author => $oldrecord->{author},
461 item_loop => \@item_value_loop,
462 item_header_loop => \@header_value_loop,
464 itemnumber => $itemnumber,
465 itemtagfield => $itemtagfield,
466 itemtagsubfield => $itemtagsubfield,
468 opisadd => ($nextop eq "saveitem") ? 0 : 1,
469 C4::Search::enabled_staff_search_views,
471 foreach my $error (@errors) {
472 $template->param($error => 1);
474 output_html_with_http_headers $input, $cookie, $template->output;