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
36 my ($tagfield,$insubfield,$record) = @_;
39 foreach my $field ($record->field($tagfield)) {
40 my @subfields = $field->subfields();
41 foreach my $subfield (@subfields) {
42 if (@
$subfield[0] eq $insubfield) {
43 $result .= @
$subfield[1];
44 $indicator = $field->indicator(1).$field->indicator(2);
48 return($indicator,$result);
51 sub get_item_from_barcode
{
53 my $dbh=C4
::Context
->dbh;
55 my $rq=$dbh->prepare("SELECT itemnumber from items where items.barcode=?");
56 $rq->execute($barcode);
57 ($result)=$rq->fetchrow;
62 my $dbh = C4
::Context
->dbh;
63 my $error = $input->param('error');
64 my $biblionumber = $input->param('biblionumber');
65 my $itemnumber = $input->param('itemnumber');
66 my $op = $input->param('op');
68 my ($template, $loggedinuser, $cookie)
69 = get_template_and_user
({template_name
=> "cataloguing/additem.tmpl",
73 flagsrequired
=> {editcatalogue
=> 1},
77 my $frameworkcode = &GetFrameworkCode
($biblionumber);
79 my $today_iso = C4
::Dates
->today('iso');
80 $template->param(today_iso
=> $today_iso);
82 my $tagslib = &GetMarcStructure
(1,$frameworkcode);
83 my $record = GetMarcBiblio
($biblionumber);
84 my $oldrecord = TransformMarcToKoha
($dbh,$record);
87 my @errors; # store errors found while checking data BEFORE saving item.
88 #-------------------------------------------------------------------------------
89 if ($op eq "additem") {
90 #-------------------------------------------------------------------------------
92 my @tags = $input->param('tag');
93 my @subfields = $input->param('subfield');
94 my @values = $input->param('field_value');
95 # build indicator hash.
96 my @ind_tag = $input->param('ind_tag');
97 my @indicator = $input->param('indicator');
98 my $xml = TransformHtmlToXml
(\
@tags,\
@subfields,\
@values,\
@indicator,\
@ind_tag, 'ITEM');
99 my $record = MARC
::Record
::new_from_xml
($xml, 'UTF-8');
100 # if autoBarcode is set to 'incremental', calculate barcode...
101 # NOTE: This code is subject to change in 3.2 with the implemenation of ajax based autobarcode code
102 # NOTE: 'incremental' is the ONLY autoBarcode option available to those not using javascript
103 if (C4
::Context
->preference('autoBarcode') eq 'incremental') {
104 my ($tagfield,$tagsubfield) = &GetMarcFromKohaField
("items.barcode",$frameworkcode);
105 unless ($record->field($tagfield)->subfield($tagsubfield)) {
106 my $sth_barcode = $dbh->prepare("select max(abs(barcode)) from items");
107 $sth_barcode->execute;
108 my ($newbarcode) = $sth_barcode->fetchrow;
110 # OK, we have the new barcode, now create the entry in MARC record
111 my $fieldItem = $record->field($tagfield);
112 $record->delete_field($fieldItem);
113 $fieldItem->add_subfields($tagsubfield => $newbarcode);
114 $record->insert_fields_ordered($fieldItem);
117 # check for item barcode # being unique
118 my $addedolditem = TransformMarcToKoha
($dbh,$record);
119 my $exist_itemnumber = get_item_from_barcode
($addedolditem->{'barcode'});
120 push @errors,"barcode_not_unique" if($exist_itemnumber);
121 # if barcode exists, don't create, but report The problem.
122 my ($oldbiblionumber,$oldbibnum,$oldbibitemnum) = AddItemFromMarc
($record,$biblionumber) unless ($exist_itemnumber);
124 if ($exist_itemnumber) {
125 $itemrecord = $record;
127 #-------------------------------------------------------------------------------
128 } elsif ($op eq "edititem") {
129 #-------------------------------------------------------------------------------
130 # retrieve item if exist => then, it's a modif
131 $itemrecord = C4
::Items
::GetMarcItem
($biblionumber,$itemnumber);
132 $nextop = "saveitem";
133 #-------------------------------------------------------------------------------
134 } elsif ($op eq "delitem") {
135 #-------------------------------------------------------------------------------
136 # check that there is no issue on this item before deletion.
137 my $sth=$dbh->prepare("select * from issues i where i.itemnumber=?");
138 $sth->execute($itemnumber);
139 my $onloan=$sth->fetchrow;
143 push @errors,"book_on_loan";
145 # check it doesnt have a waiting reserve
146 $sth=$dbh->prepare("SELECT * FROM reserves WHERE found = 'W' AND itemnumber = ?");
147 $sth->execute($itemnumber);
148 my $reserve=$sth->fetchrow;
150 &DelItem
($dbh,$biblionumber,$itemnumber);
151 print $input->redirect("additem.pl?biblionumber=$biblionumber&frameworkcode=$frameworkcode");
154 push @errors,"book_reserved";
156 #-------------------------------------------------------------------------------
157 } elsif ($op eq "saveitem") {
158 #-------------------------------------------------------------------------------
160 my @tags = $input->param('tag');
161 my @subfields = $input->param('subfield');
162 my @values = $input->param('field_value');
163 # build indicator hash.
164 my @ind_tag = $input->param('ind_tag');
165 my @indicator = $input->param('indicator');
166 # my $itemnumber = $input->param('itemnumber');
167 my $xml = TransformHtmlToXml
(\
@tags,\
@subfields,\
@values,\
@indicator,\
@ind_tag,'ITEM');
168 my $itemtosave=MARC
::Record
::new_from_xml
($xml, 'UTF-8');
169 # MARC::Record builded => now, record in DB
170 # warn "R: ".$record->as_formatted;
171 # check that the barcode don't exist already
172 my $addedolditem = TransformMarcToKoha
($dbh,$itemtosave);
173 my $exist_itemnumber = get_item_from_barcode
($addedolditem->{'barcode'});
174 if ($exist_itemnumber && $exist_itemnumber != $itemnumber) {
175 push @errors,"barcode_not_unique";
177 my ($oldbiblionumber,$oldbibnum,$oldbibitemnum) = ModItemFromMarc
($itemtosave,$biblionumber,$itemnumber);
184 #-------------------------------------------------------------------------------
185 # build screen with existing items. and "new" one
186 #-------------------------------------------------------------------------------
188 # now, build existiing item list
189 my $temp = GetMarcBiblio
( $biblionumber );
190 my @fields = $temp->fields();
191 #my @fields = $record->fields();
192 my %witness; #---- stores the list of subfields used at least once, with the "meaning" of the code
194 #---- finds where items.itemnumber is stored
195 my ( $itemtagfield, $itemtagsubfield) = &GetMarcFromKohaField
("items.itemnumber", $frameworkcode);
196 my ($branchtagfield, $branchtagsubfield) = &GetMarcFromKohaField
("items.homebranch", $frameworkcode);
198 foreach my $field (@fields) {
199 next if ($field->tag()<10);
200 my @subf = $field->subfields;
201 (defined @subf) or @subf = ();
203 # loop through each subfield
204 for my $i (0..$#subf) {
205 next if ($tagslib->{$field->tag()}->{$subf[$i][0]}->{tab
} ne 10
206 && ($field->tag() ne $itemtagfield
207 && $subf[$i][0] ne $itemtagsubfield));
209 $witness{$subf[$i][0]} = $tagslib->{$field->tag()}->{$subf[$i][0]}->{lib
} if ($tagslib->{$field->tag()}->{$subf[$i][0]}->{tab
} eq 10);
210 if ($tagslib->{$field->tag()}->{$subf[$i][0]}->{tab
} eq 10) {
211 $this_row{$subf[$i][0]}=GetAuthorisedValueDesc
( $field->tag(),
212 $subf[$i][0], $subf[$i][1], '', $tagslib)
216 if (($field->tag eq $branchtagfield) && ($subf[$i][$0] eq $branchtagsubfield) && C4
::Context
->preference("IndependantBranches")) {
218 my $userenv = C4
::Context
->userenv();
219 unless (($userenv->{'flags'} == 1) or (($userenv->{'branch'} eq $subf[$i][1]))){
220 $this_row{'nomod'}=1;
223 $this_row{itemnumber
} = $subf[$i][1] if ($field->tag() eq $itemtagfield && $subf[$i][0] eq $itemtagsubfield);
226 push(@big_array, \
%this_row);
230 my ($holdingbrtagf,$holdingbrtagsubf) = &GetMarcFromKohaField
("items.holdingbranch",$frameworkcode);
231 @big_array = sort {$a->{$holdingbrtagsubf} cmp $b->{$holdingbrtagsubf}} @big_array;
233 # now, construct template !
234 # First, the existing items for display
236 my @header_value_loop;
237 for my $row ( @big_array ) {
239 my @item_fields = map +{ field
=> $_ || '' }, @
$row{ sort keys(%witness) };
240 $row_data{item_value
} = [ @item_fields ];
241 $row_data{itemnumber
} = $row->{itemnumber
};
242 #reporting this_row values
243 $row_data{'nomod'} = $row->{'nomod'};
244 push(@item_value_loop,\
%row_data);
246 foreach my $subfield_code (sort keys(%witness)) {
248 $header_value{header_value
} = $witness{$subfield_code};
249 push(@header_value_loop, \
%header_value);
252 # now, build the item form for entering a new item
255 my $authorised_values_sth = $dbh->prepare("SELECT authorised_value,lib FROM authorised_values WHERE category=? ORDER BY lib");
257 my $onlymine = C4
::Context
->preference('IndependantBranches') &&
258 C4
::Context
->userenv &&
259 C4
::Context
->userenv->{flags
}!=1 &&
260 C4
::Context
->userenv->{branch
};
261 my $branches = GetBranches
($onlymine); # build once ahead of time, instead of multiple times later.
263 foreach my $tag (sort keys %{$tagslib}) {
264 # loop through each subfield
265 foreach my $subfield (sort keys %{$tagslib->{$tag}}) {
266 next if subfield_is_koha_internal_p
($subfield);
267 next if ($tagslib->{$tag}->{$subfield}->{'tab'} ne "10");
270 my $index_subfield = int(rand(1000000));
271 if ($subfield eq '@'){
272 $subfield_data{id
} = "tag_".$tag."_subfield_00_".$index_subfield;
274 $subfield_data{id
} = "tag_".$tag."_subfield_".$subfield."_".$index_subfield;
276 $subfield_data{tag
} = $tag;
277 $subfield_data{subfield
} = $subfield;
278 $subfield_data{random
} = int(rand(1000000)); # why do we need 2 different randoms?
279 # $subfield_data{marc_lib} = $tagslib->{$tag}->{$subfield}->{lib};
280 $subfield_data{marc_lib
} ="<span id=\"error$i\" title=\"".$tagslib->{$tag}->{$subfield}->{lib
}."\">".$tagslib->{$tag}->{$subfield}->{lib
}."</span>";
281 $subfield_data{mandatory
} = $tagslib->{$tag}->{$subfield}->{mandatory
};
282 $subfield_data{repeatable
} = $tagslib->{$tag}->{$subfield}->{repeatable
};
284 ($x,$value) = find_value
($tag,$subfield,$itemrecord) if ($itemrecord);
285 $value =~ s/"/"/g;
287 $value = $tagslib->{$tag}->{$subfield}->{defaultvalue
};
288 # get today date & replace YYYY, MM, DD if provided in the default value
289 my ( $year, $month, $day ) = split ',', $today_iso;
290 $value =~ s/YYYY/$year/g;
291 $value =~ s/MM/$month/g;
292 $value =~ s/DD/$day/g;
294 $subfield_data{visibility
} = "display:none;" if (($tagslib->{$tag}->{$subfield}->{hidden
} > 4) || ($tagslib->{$tag}->{$subfield}->{hidden
} < -4));
295 # testing branch value if IndependantBranches.
296 # my $test = (C4::Context->preference("IndependantBranches")) &&
297 # ($tag eq $branchtagfield) && ($subfield eq $branchtagsubfield) &&
298 # (C4::Context->userenv->{flags} != 1) && ($value) && ($value ne C4::Context->userenv->{branch}) ;
299 # $test and print $input->redirect(".pl?biblionumber=$biblionumber") and exit;
300 # search for itemcallnumber if applicable
301 my $pref_itemcallnumber = C4
::Context
->preference('itemcallnumber');
302 if (!$value && $tagslib->{$tag}->{$subfield}->{kohafield
} eq 'items.itemcallnumber' && $pref_itemcallnumber) {
303 my $CNtag = substr($pref_itemcallnumber, 0, 3);
304 my $CNsubfield = substr($pref_itemcallnumber, 3, 1);
305 my $CNsubfield2 = substr($pref_itemcallnumber, 4, 1);
306 my $temp2 = $temp->field($CNtag);
308 $value = ($temp2->subfield($CNsubfield)).' '.($temp2->subfield($CNsubfield2));
309 #remove any trailing space incase one subfield is used
310 $value =~ s/^\s+|\s+$//g;
314 my $attributes_no_value = qq(tabindex
="1" id
="$subfield_data{id}" name
="field_value" class="input_marceditor" size
="67" maxlength
="255" );
315 my $attributes = qq($attributes_no_value value
="$value" );
316 if ( $tagslib->{$tag}->{$subfield}->{authorised_value
} ) {
317 my @authorised_values;
319 # builds list, depending on authorised value...
321 if ( $tagslib->{$tag}->{$subfield}->{authorised_value
} eq "branches" ) {
322 foreach my $thisbranch ( sort keys %$branches ) {
323 push @authorised_values, $thisbranch;
324 $authorised_lib{$thisbranch} = $branches->{$thisbranch}->{'branchname'};
327 elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value
} eq "itemtypes" ) {
328 push @authorised_values, "" unless ( $tagslib->{$tag}->{$subfield}->{mandatory
} );
329 my $sth = $dbh->prepare("select itemtype,description from itemtypes order by description");
331 my $itemtype; # FIXME: double declaration of $itemtype
332 while ( my ( $itemtype, $description ) = $sth->fetchrow_array ) {
333 push @authorised_values, $itemtype;
334 $authorised_lib{$itemtype} = $description;
338 my $default_itemtype;
339 my $itype_sth = $dbh->prepare("SELECT itemtype FROM biblioitems WHERE biblionumber = ?");
340 $itype_sth->execute( $biblionumber );
341 ( $default_itemtype ) = $itype_sth->fetchrow_array;
342 $value = $default_itemtype;
347 elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value
} eq "cn_source" ) {
348 push @authorised_values, "" unless ( $tagslib->{$tag}->{$subfield}->{mandatory
} );
350 my $class_sources = GetClassSources
();
351 my $default_source = C4
::Context
->preference("DefaultClassificationSource");
353 foreach my $class_source (sort keys %$class_sources) {
354 next unless $class_sources->{$class_source}->{'used'} or
355 ($value and $class_source eq $value) or
356 ($class_source eq $default_source);
357 push @authorised_values, $class_source;
358 $authorised_lib{$class_source} = $class_sources->{$class_source}->{'description'};
360 $value = $default_source unless ($value);
362 #---- "true" authorised value
365 push @authorised_values, "" unless ( $tagslib->{$tag}->{$subfield}->{mandatory
} );
366 $authorised_values_sth->execute( $tagslib->{$tag}->{$subfield}->{authorised_value
} );
367 while ( my ( $value, $lib ) = $authorised_values_sth->fetchrow_array ) {
368 push @authorised_values, $value;
369 $authorised_lib{$value} = $lib;
372 $subfield_data{marc_value
} =CGI
::scrolling_list
( # FIXME: factor out scrolling_list
373 -name
=> "field_value",
374 -values => \
@authorised_values,
376 -labels
=> \
%authorised_lib,
381 -id
=> "tag_".$tag."_subfield_".$subfield."_".$index_subfield,
382 -class => "input_marceditor",
384 # it's a thesaurus / authority field
386 elsif ( $tagslib->{$tag}->{$subfield}->{authtypecode
} ) {
387 $subfield_data{marc_value
} = "<input type=\"text\" $attributes />
388 <a href=\"#\" class=\"buttonDot\"
389 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>
391 # it's a plugin field
393 elsif ( $tagslib->{$tag}->{$subfield}->{value_builder
} ) {
395 my $plugin = C4
::Context
->intranetdir . "/cataloguing/value_builder/" . $tagslib->{$tag}->{$subfield}->{'value_builder'};
397 my $extended_param = plugin_parameters
( $dbh, $temp, $tagslib, $subfield_data{id
}, \
@loop_data );
398 my ( $function_name, $javascript ) = plugin_javascript
( $dbh, $temp, $tagslib, $subfield_data{id
}, \
@loop_data );
399 $subfield_data{marc_value
} = qq[<input
$attributes
400 onfocus
="Focus$function_name($subfield_data{random}, '$subfield_data{id}');"
401 onblur
=" Blur$function_name($subfield_data{random}, '$subfield_data{id}');" />
402 <a href
="#" class="buttonDot" onclick
="Clic$function_name('$subfield_data{id}'); return false;" title
="Tag Editor">...</a
>
405 warn "Plugin Failed: $plugin";
406 $subfield_data{marc_value
} = "<input $attributes />"; # supply default input form
409 elsif ( $tag eq '' ) { # it's an hidden field
410 $subfield_data{marc_value
} = qq(<input type
="hidden" $attributes />);
412 elsif ( $tagslib->{$tag}->{$subfield}->{'hidden'} ) { # FIXME: shouldn't input type be "hidden" ?
413 $subfield_data{marc_value
} = qq(<input type
="text" $attributes />);
415 elsif ( length($value) > 100
416 or (C4
::Context
->preference("marcflavour") eq "UNIMARC" and
417 300 <= $tag && $tag < 400 && $subfield eq 'a' )
418 or (C4
::Context
->preference("marcflavour") eq "MARC21" and
419 500 <= $tag && $tag < 600 )
421 # oversize field (textarea)
422 $subfield_data{marc_value
} = "<textarea $attributes_no_value>$value</textarea>\n";
424 # it's a standard field
425 $subfield_data{marc_value
} = "<input $attributes />";
427 # $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\">";
428 push (@loop_data, \
%subfield_data);
433 # what's the next op ? it's what we are not in : an add if we're editing, otherwise, and edit.
434 $template->param( title
=> $record->title() ) if ($record ne "-1");
436 biblionumber
=> $biblionumber,
437 title
=> $oldrecord->{title
},
438 author
=> $oldrecord->{author
},
439 item_loop
=> \
@item_value_loop,
440 item_header_loop
=> \
@header_value_loop,
442 itemnumber
=> $itemnumber,
443 itemtagfield
=> $itemtagfield,
444 itemtagsubfield
=> $itemtagsubfield,
446 opisadd
=> ($nextop eq "saveitem") ?
0 : 1,
448 foreach my $error (@errors) {
449 $template->param($error => 1);
451 output_html_with_http_headers
$input, $cookie, $template->output;