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 $error = &DelItemCheck($dbh,$biblionumber,$itemnumber);
138 print $input->redirect("additem.pl?biblionumber=$biblionumber&frameworkcode=$frameworkcode");
144 #-------------------------------------------------------------------------------
145 } elsif ($op eq "delallitems") {
146 #-------------------------------------------------------------------------------
147 my @biblioitems = &GetBiblioItemByBiblioNumber($biblionumber);
148 foreach my $biblioitem (@biblioitems){
149 my $items = &GetItemsByBiblioitemnumber($biblioitem->{biblioitemnumber});
151 foreach my $item (@$items){
152 &DelItem($dbh,$biblionumber,$item->{itemnumber});
155 print $input->redirect("/cgi-bin/koha/catalogue/moredetail.pl?biblionumber=$biblionumber");
157 #-------------------------------------------------------------------------------
158 } elsif ($op eq "saveitem") {
159 #-------------------------------------------------------------------------------
161 my @tags = $input->param('tag');
162 my @subfields = $input->param('subfield');
163 my @values = $input->param('field_value');
164 # build indicator hash.
165 my @ind_tag = $input->param('ind_tag');
166 my @indicator = $input->param('indicator');
167 # my $itemnumber = $input->param('itemnumber');
168 my $xml = TransformHtmlToXml(\@tags,\@subfields,\@values,\@indicator,\@ind_tag,'ITEM');
169 my $itemtosave=MARC::Record::new_from_xml($xml, 'UTF-8');
170 # MARC::Record builded => now, record in DB
171 # warn "R: ".$record->as_formatted;
172 # check that the barcode don't exist already
173 my $addedolditem = TransformMarcToKoha($dbh,$itemtosave);
174 my $exist_itemnumber = get_item_from_barcode($addedolditem->{'barcode'});
175 if ($exist_itemnumber && $exist_itemnumber != $itemnumber) {
176 push @errors,"barcode_not_unique";
178 my ($oldbiblionumber,$oldbibnum,$oldbibitemnum) = ModItemFromMarc($itemtosave,$biblionumber,$itemnumber);
185 #-------------------------------------------------------------------------------
186 # build screen with existing items. and "new" one
187 #-------------------------------------------------------------------------------
189 # now, build existiing item list
190 my $temp = GetMarcBiblio( $biblionumber );
191 my @fields = $temp->fields();
192 #my @fields = $record->fields();
193 my %witness; #---- stores the list of subfields used at least once, with the "meaning" of the code
195 #---- finds where items.itemnumber is stored
196 my ( $itemtagfield, $itemtagsubfield) = &GetMarcFromKohaField("items.itemnumber", $frameworkcode);
197 my ($branchtagfield, $branchtagsubfield) = &GetMarcFromKohaField("items.homebranch", $frameworkcode);
199 foreach my $field (@fields) {
200 next if ($field->tag()<10);
201 my @subf = $field->subfields;
202 (defined @subf) or @subf = ();
204 # loop through each subfield
205 for my $i (0..$#subf) {
206 next if ($tagslib->{$field->tag()}->{$subf[$i][0]}->{tab} ne 10
207 && ($field->tag() ne $itemtagfield
208 && $subf[$i][0] ne $itemtagsubfield));
210 $witness{$subf[$i][0]} = $tagslib->{$field->tag()}->{$subf[$i][0]}->{lib} if ($tagslib->{$field->tag()}->{$subf[$i][0]}->{tab} eq 10);
211 if ($tagslib->{$field->tag()}->{$subf[$i][0]}->{tab} eq 10) {
212 $this_row{$subf[$i][0]}=GetAuthorisedValueDesc( $field->tag(),
213 $subf[$i][0], $subf[$i][1], '', $tagslib)
217 if (($field->tag eq $branchtagfield) && ($subf[$i][$0] eq $branchtagsubfield) && C4::Context->preference("IndependantBranches")) {
219 my $userenv = C4::Context->userenv();
220 unless (($userenv->{'flags'} == 1) or (($userenv->{'branch'} eq $subf[$i][1]))){
221 $this_row{'nomod'}=1;
224 $this_row{itemnumber} = $subf[$i][1] if ($field->tag() eq $itemtagfield && $subf[$i][0] eq $itemtagsubfield);
227 push(@big_array, \%this_row);
231 my ($holdingbrtagf,$holdingbrtagsubf) = &GetMarcFromKohaField("items.holdingbranch",$frameworkcode);
232 @big_array = sort {$a->{$holdingbrtagsubf} cmp $b->{$holdingbrtagsubf}} @big_array;
234 # now, construct template !
235 # First, the existing items for display
237 my @header_value_loop;
238 for my $row ( @big_array ) {
240 my @item_fields = map +{ field => $_ || '' }, @$row{ sort keys(%witness) };
241 $row_data{item_value} = [ @item_fields ];
242 $row_data{itemnumber} = $row->{itemnumber};
243 #reporting this_row values
244 $row_data{'nomod'} = $row->{'nomod'};
245 push(@item_value_loop,\%row_data);
247 foreach my $subfield_code (sort keys(%witness)) {
249 $header_value{header_value} = $witness{$subfield_code};
250 push(@header_value_loop, \%header_value);
253 # now, build the item form for entering a new item
256 my $authorised_values_sth = $dbh->prepare("SELECT authorised_value,lib FROM authorised_values WHERE category=? ORDER BY lib");
258 my $onlymine = C4::Context->preference('IndependantBranches') &&
259 C4::Context->userenv &&
260 C4::Context->userenv->{flags} % 2 == 0 &&
261 C4::Context->userenv->{branch};
262 my $branches = GetBranches($onlymine); # build once ahead of time, instead of multiple times later.
264 foreach my $tag (sort keys %{$tagslib}) {
265 # loop through each subfield
266 foreach my $subfield (sort keys %{$tagslib->{$tag}}) {
267 next if subfield_is_koha_internal_p($subfield);
268 next if ($tagslib->{$tag}->{$subfield}->{'tab'} ne "10");
271 my $index_subfield = int(rand(1000000));
272 if ($subfield eq '@'){
273 $subfield_data{id} = "tag_".$tag."_subfield_00_".$index_subfield;
275 $subfield_data{id} = "tag_".$tag."_subfield_".$subfield."_".$index_subfield;
277 $subfield_data{tag} = $tag;
278 $subfield_data{subfield} = $subfield;
279 $subfield_data{random} = int(rand(1000000)); # why do we need 2 different randoms?
280 # $subfield_data{marc_lib} = $tagslib->{$tag}->{$subfield}->{lib};
281 $subfield_data{marc_lib} ="<span id=\"error$i\" title=\"".$tagslib->{$tag}->{$subfield}->{lib}."\">".$tagslib->{$tag}->{$subfield}->{lib}."</span>";
282 $subfield_data{mandatory} = $tagslib->{$tag}->{$subfield}->{mandatory};
283 $subfield_data{repeatable} = $tagslib->{$tag}->{$subfield}->{repeatable};
285 ($x,$value) = find_value($tag,$subfield,$itemrecord) if ($itemrecord);
286 $value =~ s/"/"/g;
288 $value = $tagslib->{$tag}->{$subfield}->{defaultvalue};
289 # get today date & replace YYYY, MM, DD if provided in the default value
290 my ( $year, $month, $day ) = split ',', $today_iso;
291 $value =~ s/YYYY/$year/g;
292 $value =~ s/MM/$month/g;
293 $value =~ s/DD/$day/g;
295 $subfield_data{visibility} = "display:none;" if (($tagslib->{$tag}->{$subfield}->{hidden} > 4) || ($tagslib->{$tag}->{$subfield}->{hidden} < -4));
296 # testing branch value if IndependantBranches.
297 my $pref_itemcallnumber = C4::Context->preference('itemcallnumber');
298 if (!$value && $tagslib->{$tag}->{$subfield}->{kohafield} eq 'items.itemcallnumber' && $pref_itemcallnumber) {
299 my $CNtag = substr($pref_itemcallnumber, 0, 3);
300 my $CNsubfield = substr($pref_itemcallnumber, 3, 1);
301 my $CNsubfield2 = substr($pref_itemcallnumber, 4, 1);
302 my $temp2 = $temp->field($CNtag);
304 $value = ($temp2->subfield($CNsubfield)).' '.($temp2->subfield($CNsubfield2));
305 #remove any trailing space incase one subfield is used
306 $value =~ s/^\s+|\s+$//g;
310 my $attributes_no_value = qq(tabindex="1" id="$subfield_data{id}" name="field_value" class="input_marceditor" size="67" maxlength="255" );
311 my $attributes = qq($attributes_no_value value="$value" );
312 if ( $tagslib->{$tag}->{$subfield}->{authorised_value} ) {
313 my @authorised_values;
315 # builds list, depending on authorised value...
317 if ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "branches" ) {
318 foreach my $thisbranch ( sort keys %$branches ) {
319 push @authorised_values, $thisbranch;
320 $authorised_lib{$thisbranch} = $branches->{$thisbranch}->{'branchname'};
323 elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes" ) {
324 push @authorised_values, "" unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
325 my $sth = $dbh->prepare("select itemtype,description from itemtypes order by description");
327 my $itemtype; # FIXME: double declaration of $itemtype
328 while ( my ( $itemtype, $description ) = $sth->fetchrow_array ) {
329 push @authorised_values, $itemtype;
330 $authorised_lib{$itemtype} = $description;
334 my $default_itemtype;
335 my $itype_sth = $dbh->prepare("SELECT itemtype FROM biblioitems WHERE biblionumber = ?");
336 $itype_sth->execute( $biblionumber );
337 ( $default_itemtype ) = $itype_sth->fetchrow_array;
338 $value = $default_itemtype;
343 elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "cn_source" ) {
344 push @authorised_values, "" unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
346 my $class_sources = GetClassSources();
347 my $default_source = C4::Context->preference("DefaultClassificationSource");
349 foreach my $class_source (sort keys %$class_sources) {
350 next unless $class_sources->{$class_source}->{'used'} or
351 ($value and $class_source eq $value) or
352 ($class_source eq $default_source);
353 push @authorised_values, $class_source;
354 $authorised_lib{$class_source} = $class_sources->{$class_source}->{'description'};
356 $value = $default_source unless ($value);
358 #---- "true" authorised value
361 push @authorised_values, "" unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
362 $authorised_values_sth->execute( $tagslib->{$tag}->{$subfield}->{authorised_value} );
363 while ( my ( $value, $lib ) = $authorised_values_sth->fetchrow_array ) {
364 push @authorised_values, $value;
365 $authorised_lib{$value} = $lib;
368 $subfield_data{marc_value} =CGI::scrolling_list( # FIXME: factor out scrolling_list
369 -name => "field_value",
370 -values => \@authorised_values,
372 -labels => \%authorised_lib,
377 -id => "tag_".$tag."_subfield_".$subfield."_".$index_subfield,
378 -class => "input_marceditor",
380 # it's a thesaurus / authority field
382 elsif ( $tagslib->{$tag}->{$subfield}->{authtypecode} ) {
383 $subfield_data{marc_value} = "<input type=\"text\" $attributes />
384 <a href=\"#\" class=\"buttonDot\"
385 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>
387 # it's a plugin field
389 elsif ( $tagslib->{$tag}->{$subfield}->{value_builder} ) {
391 my $plugin = C4::Context->intranetdir . "/cataloguing/value_builder/" . $tagslib->{$tag}->{$subfield}->{'value_builder'};
393 my $extended_param = plugin_parameters( $dbh, $temp, $tagslib, $subfield_data{id}, \@loop_data );
394 my ( $function_name, $javascript ) = plugin_javascript( $dbh, $temp, $tagslib, $subfield_data{id}, \@loop_data );
395 $subfield_data{marc_value} = qq[<input $attributes
396 onfocus="Focus$function_name($subfield_data{random}, '$subfield_data{id}');"
397 onblur=" Blur$function_name($subfield_data{random}, '$subfield_data{id}');" />
398 <a href="#" class="buttonDot" onclick="Clic$function_name('$subfield_data{id}'); return false;" title="Tag Editor">...</a>
401 warn "Plugin Failed: $plugin";
402 $subfield_data{marc_value} = "<input $attributes />"; # supply default input form
405 elsif ( $tag eq '' ) { # it's an hidden field
406 $subfield_data{marc_value} = qq(<input type="hidden" $attributes />);
408 elsif ( $tagslib->{$tag}->{$subfield}->{'hidden'} ) { # FIXME: shouldn't input type be "hidden" ?
409 $subfield_data{marc_value} = qq(<input type="text" $attributes />);
411 elsif ( length($value) > 100
412 or (C4::Context->preference("marcflavour") eq "UNIMARC" and
413 300 <= $tag && $tag < 400 && $subfield eq 'a' )
414 or (C4::Context->preference("marcflavour") eq "MARC21" and
415 500 <= $tag && $tag < 600 )
417 # oversize field (textarea)
418 $subfield_data{marc_value} = "<textarea $attributes_no_value>$value</textarea>\n";
420 # it's a standard field
421 $subfield_data{marc_value} = "<input $attributes />";
423 # $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\">";
424 push (@loop_data, \%subfield_data);
429 # what's the next op ? it's what we are not in : an add if we're editing, otherwise, and edit.
430 $template->param( title => $record->title() ) if ($record ne "-1");
432 biblionumber => $biblionumber,
433 title => $oldrecord->{title},
434 author => $oldrecord->{author},
435 item_loop => \@item_value_loop,
436 item_header_loop => \@header_value_loop,
438 itemnumber => $itemnumber,
439 itemtagfield => $itemtagfield,
440 itemtagsubfield => $itemtagsubfield,
442 opisadd => ($nextop eq "saveitem") ? 0 : 1,
444 foreach my $error (@errors) {
445 $template->param($error => 1);
447 output_html_with_http_headers $input, $cookie, $template->output;