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 &DelItemCheck($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 or (); # don't use ||, as that forces $field->subfelds to be interpreted in scalar context
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 = GetBranchesLoop(undef,$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;
286 if ( ! defined( $value ) || $value eq '') {
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; # FIXME: iso dates don't have commas!
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 (@$branches) {
323 push @authorised_values, $thisbranch->{value};
324 $authorised_lib{$thisbranch->{value}} = $thisbranch->{branchname};
325 $value = $thisbranch->{value} if $thisbranch->{selected};
328 elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes" ) {
329 push @authorised_values, "" unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
330 my $sth = $dbh->prepare("select itemtype,description from itemtypes order by description");
332 while ( my ( $itemtype, $description ) = $sth->fetchrow_array ) {
333 push @authorised_values, $itemtype;
334 $authorised_lib{$itemtype} = $description;
338 my $itype_sth = $dbh->prepare("SELECT itemtype FROM biblioitems WHERE biblionumber = ?");
339 $itype_sth->execute( $biblionumber );
340 ( $value ) = $itype_sth->fetchrow_array;
345 elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "cn_source" ) {
346 push @authorised_values, "" unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
348 my $class_sources = GetClassSources();
349 my $default_source = C4::Context->preference("DefaultClassificationSource");
351 foreach my $class_source (sort keys %$class_sources) {
352 next unless $class_sources->{$class_source}->{'used'} or
353 ($value and $class_source eq $value) or
354 ($class_source eq $default_source);
355 push @authorised_values, $class_source;
356 $authorised_lib{$class_source} = $class_sources->{$class_source}->{'description'};
358 $value = $default_source unless ($value);
360 #---- "true" authorised value
363 push @authorised_values, "" unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
364 $authorised_values_sth->execute( $tagslib->{$tag}->{$subfield}->{authorised_value} );
365 while ( my ( $value, $lib ) = $authorised_values_sth->fetchrow_array ) {
366 push @authorised_values, $value;
367 $authorised_lib{$value} = $lib;
370 $subfield_data{marc_value} =CGI::scrolling_list( # FIXME: factor out scrolling_list
371 -name => "field_value",
372 -values => \@authorised_values,
374 -labels => \%authorised_lib,
379 -id => "tag_".$tag."_subfield_".$subfield."_".$index_subfield,
380 -class => "input_marceditor",
382 # it's a thesaurus / authority field
384 elsif ( $tagslib->{$tag}->{$subfield}->{authtypecode} ) {
385 $subfield_data{marc_value} = "<input type=\"text\" $attributes />
386 <a href=\"#\" class=\"buttonDot\"
387 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>
389 # it's a plugin field
391 elsif ( $tagslib->{$tag}->{$subfield}->{value_builder} ) {
393 my $plugin = C4::Context->intranetdir . "/cataloguing/value_builder/" . $tagslib->{$tag}->{$subfield}->{'value_builder'};
395 my $extended_param = plugin_parameters( $dbh, $temp, $tagslib, $subfield_data{id}, \@loop_data );
396 my ( $function_name, $javascript ) = plugin_javascript( $dbh, $temp, $tagslib, $subfield_data{id}, \@loop_data );
397 $subfield_data{marc_value} = qq[<input $attributes
398 onfocus="Focus$function_name($subfield_data{random}, '$subfield_data{id}');"
399 onblur=" Blur$function_name($subfield_data{random}, '$subfield_data{id}');" />
400 <a href="#" class="buttonDot" onclick="Clic$function_name('$subfield_data{id}'); return false;" title="Tag Editor">...</a>
403 warn "Plugin Failed: $plugin";
404 $subfield_data{marc_value} = "<input $attributes />"; # supply default input form
407 elsif ( $tag eq '' ) { # it's an hidden field
408 $subfield_data{marc_value} = qq(<input type="hidden" $attributes />);
410 elsif ( $tagslib->{$tag}->{$subfield}->{'hidden'} ) { # FIXME: shouldn't input type be "hidden" ?
411 $subfield_data{marc_value} = qq(<input type="text" $attributes />);
413 elsif ( length($value) > 100
414 or (C4::Context->preference("marcflavour") eq "UNIMARC" and
415 300 <= $tag && $tag < 400 && $subfield eq 'a' )
416 or (C4::Context->preference("marcflavour") eq "MARC21" and
417 500 <= $tag && $tag < 600 )
419 # oversize field (textarea)
420 $subfield_data{marc_value} = "<textarea $attributes_no_value>$value</textarea>\n";
422 # it's a standard field
423 $subfield_data{marc_value} = "<input $attributes />";
425 # $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\">";
426 push (@loop_data, \%subfield_data);
431 # what's the next op ? it's what we are not in : an add if we're editing, otherwise, and edit.
432 $template->param( title => $record->title() ) if ($record ne "-1");
434 biblionumber => $biblionumber,
435 title => $oldrecord->{title},
436 author => $oldrecord->{author},
437 item_loop => \@item_value_loop,
438 item_header_loop => \@header_value_loop,
440 itemnumber => $itemnumber,
441 itemtagfield => $itemtagfield,
442 itemtagsubfield => $itemtagsubfield,
444 opisadd => ($nextop eq "saveitem") ? 0 : 1,
445 C4::Search::enabled_staff_search_views,
447 foreach my $error (@errors) {
448 $template->param($error => 1);
450 output_html_with_http_headers $input, $cookie, $template->output;