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 Date::Calc qw(Today);
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);
52 sub get_item_from_barcode {
54 my $dbh=C4::Context->dbh;
56 my $rq=$dbh->prepare("SELECT itemnumber from items where items.barcode=?");
57 $rq->execute($barcode);
58 ($result)=$rq->fetchrow;
63 my $dbh = C4::Context->dbh;
64 my $error = $input->param('error');
65 my $biblionumber = $input->param('biblionumber');
66 my $itemnumber = $input->param('itemnumber');
67 my $op = $input->param('op');
69 my ($template, $loggedinuser, $cookie)
70 = get_template_and_user({template_name => "cataloguing/additem.tmpl",
74 flagsrequired => {editcatalogue => 1},
79 my $frameworkcode = &GetFrameworkCode($biblionumber);
81 my $tagslib = &GetMarcStructure(1,$frameworkcode);
82 my $record = GetMarcBiblio($biblionumber);
83 my $oldrecord = TransformMarcToKoha($dbh,$record);
86 my @errors; # store errors found while checking data BEFORE saving item.
87 #-------------------------------------------------------------------------------
88 if ($op eq "additem") {
89 #-------------------------------------------------------------------------------
91 my @tags = $input->param('tag');
92 my @subfields = $input->param('subfield');
93 my @values = $input->param('field_value');
94 # build indicator hash.
95 my @ind_tag = $input->param('ind_tag');
96 my @indicator = $input->param('indicator');
97 my $xml = TransformHtmlToXml(\@tags,\@subfields,\@values,\@indicator,\@ind_tag, 'ITEM');
98 my $record=MARC::Record::new_from_xml($xml, 'UTF-8');
99 # if autoBarcode is set to 'incremental', calculate barcode...
100 # NOTE: This code is subject to change in 3.2 with the implemenation of ajax based autobarcode code
101 # NOTE: 'incremental' is the ONLY autoBarcode option available to those not using javascript
102 if (C4::Context->preference('autoBarcode') eq 'incremental') {
103 my ($tagfield,$tagsubfield) = &GetMarcFromKohaField("items.barcode",$frameworkcode);
104 unless ($record->field($tagfield)->subfield($tagsubfield)) {
105 my $sth_barcode = $dbh->prepare("select max(abs(barcode)) from items");
106 $sth_barcode->execute;
107 my ($newbarcode) = $sth_barcode->fetchrow;
109 # OK, we have the new barcode, now create the entry in MARC record
110 my $fieldItem = $record->field($tagfield);
111 $record->delete_field($fieldItem);
112 $fieldItem->add_subfields($tagsubfield => $newbarcode);
113 $record->insert_fields_ordered($fieldItem);
116 # check for item barcode # being unique
117 my $addedolditem = TransformMarcToKoha($dbh,$record);
118 my $exist_itemnumber = get_item_from_barcode($addedolditem->{'barcode'});
119 push @errors,"barcode_not_unique" if($exist_itemnumber);
120 # if barcode exists, don't create, but report The problem.
121 my ($oldbiblionumber,$oldbibnum,$oldbibitemnum) = AddItemFromMarc($record,$biblionumber) unless ($exist_itemnumber);
122 if ($exist_itemnumber) {
124 $itemrecord = $record;
128 #-------------------------------------------------------------------------------
129 } elsif ($op eq "edititem") {
130 #-------------------------------------------------------------------------------
131 # retrieve item if exist => then, it's a modif
132 $itemrecord = C4::Items::GetMarcItem($biblionumber,$itemnumber);
134 #-------------------------------------------------------------------------------
135 } elsif ($op eq "delitem") {
136 #-------------------------------------------------------------------------------
137 # check that there is no issue on this item before deletion.
138 my $sth=$dbh->prepare("select * from issues i where i.itemnumber=?");
139 $sth->execute($itemnumber);
140 my $onloan=$sth->fetchrow;
142 push @errors,"book_on_loan" if ($onloan); ##error book_on_loan added to template as well
146 # check it doesnt have a waiting reserve
147 $sth=$dbh->prepare("SELECT * FROM reserves WHERE found = 'W' AND itemnumber = ?");
148 $sth->execute($itemnumber);
149 my $reserve=$sth->fetchrow;
151 push @errors,"book_reserved";
155 &DelItem($dbh,$biblionumber,$itemnumber);
156 print $input->redirect("additem.pl?biblionumber=$biblionumber&frameworkcode=$frameworkcode");
159 #-------------------------------------------------------------------------------
160 } elsif ($op eq "saveitem") {
161 #-------------------------------------------------------------------------------
163 my @tags = $input->param('tag');
164 my @subfields = $input->param('subfield');
165 my @values = $input->param('field_value');
166 # build indicator hash.
167 my @ind_tag = $input->param('ind_tag');
168 my @indicator = $input->param('indicator');
169 # my $itemnumber = $input->param('itemnumber');
170 my $xml = TransformHtmlToXml(\@tags,\@subfields,\@values,\@indicator,\@ind_tag,'ITEM');
171 my $itemtosave=MARC::Record::new_from_xml($xml, 'UTF-8');
172 # MARC::Record builded => now, record in DB
173 # warn "R: ".$record->as_formatted;
174 # check that the barcode don't exist already
175 my $addedolditem = TransformMarcToKoha($dbh,$itemtosave);
176 my $exist_itemnumber = get_item_from_barcode($addedolditem->{'barcode'});
177 if ($exist_itemnumber && $exist_itemnumber != $itemnumber) {
178 push @errors,"barcode_not_unique";
180 my ($oldbiblionumber,$oldbibnum,$oldbibitemnum) = ModItemFromMarc($itemtosave,$biblionumber,$itemnumber);
187 #-------------------------------------------------------------------------------
188 # build screen with existing items. and "new" one
189 #-------------------------------------------------------------------------------
191 # now, build existiing item list
192 my $temp = GetMarcBiblio( $biblionumber );
193 my @fields = $temp->fields();
194 #my @fields = $record->fields();
195 my %witness; #---- stores the list of subfields used at least once, with the "meaning" of the code
197 #---- finds where items.itemnumber is stored
198 my ($itemtagfield,$itemtagsubfield) = &GetMarcFromKohaField("items.itemnumber",$frameworkcode);
199 my ($branchtagfield,$branchtagsubfield) = &GetMarcFromKohaField("items.homebranch",$frameworkcode);
201 foreach my $field (@fields) {
202 next if ($field->tag()<10);
203 my @subf=$field->subfields;
205 # loop through each subfield
206 for my $i (0..$#subf) {
207 next if ($tagslib->{$field->tag()}->{$subf[$i][0]}->{tab} ne 10
208 && ($field->tag() ne $itemtagfield
209 && $subf[$i][0] ne $itemtagsubfield));
211 $witness{$subf[$i][0]} = $tagslib->{$field->tag()}->{$subf[$i][0]}->{lib} if ($tagslib->{$field->tag()}->{$subf[$i][0]}->{tab} eq 10);
213 $this_row{$subf[$i][0]}=GetAuthorisedValueDesc( $field->tag(),
214 $subf[$i][0], $subf[$i][1], '', $tagslib) if ($tagslib->{$field->tag()}->{$subf[$i][0]}->{tab} eq 10);
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);
229 #fill big_row with missing data
230 foreach my $subfield_code (keys(%witness)) {
231 for (my $i=0;$i<=$#big_array;$i++) {
232 $big_array[$i]{$subfield_code}=" " unless ($big_array[$i]{$subfield_code});
235 my ($holdingbrtagf,$holdingbrtagsubf) = &GetMarcFromKohaField("items.holdingbranch",$frameworkcode);
236 @big_array = sort {$a->{$holdingbrtagsubf} cmp $b->{$holdingbrtagsubf}} @big_array;
238 # now, construct template !
239 # First, the existing items for display
241 my @header_value_loop;
242 for (my $i=0;$i<=$#big_array; $i++) {
244 foreach my $subfield_code (sort keys(%witness)) {
245 $items_data .="<td>".$big_array[$i]{$subfield_code}."</td>";
248 $items_data =~ s/"/"/g;
249 $row_data{item_value} = $items_data;
250 $row_data{itemnumber} = $big_array[$i]->{itemnumber};
251 #reporting this_row values
252 $row_data{'nomod'} = $big_array[$i]{'nomod'};
253 push(@item_value_loop,\%row_data);
255 foreach my $subfield_code (sort keys(%witness)) {
257 $header_value{header_value} = $witness{$subfield_code};
258 push(@header_value_loop, \%header_value);
261 # now, build the item form for entering a new item
264 my $authorised_values_sth = $dbh->prepare("SELECT authorised_value,lib FROM authorised_values WHERE category=? ORDER BY lib");
266 foreach my $tag (sort keys %{$tagslib}) {
267 my $previous_tag = '';
268 # loop through each subfield
269 foreach my $subfield (sort keys %{$tagslib->{$tag}}) {
270 next if subfield_is_koha_internal_p($subfield);
271 next if ($tagslib->{$tag}->{$subfield}->{'tab'} ne "10");
274 my $index_subfield= int(rand(1000000));
275 if($subfield eq '@'){
276 $subfield_data{id} = "tag_".$tag."_subfield_00_".$index_subfield;
278 $subfield_data{id} = "tag_".$tag."_subfield_".$subfield."_".$index_subfield;
280 $subfield_data{tag}=$tag;
281 $subfield_data{subfield}=$subfield;
282 $subfield_data{random}=int(rand(1000000));
283 # $subfield_data{marc_lib}=$tagslib->{$tag}->{$subfield}->{lib};
284 $subfield_data{marc_lib}="<span id=\"error$i\" title=\"".$tagslib->{$tag}->{$subfield}->{lib}."\">".$tagslib->{$tag}->{$subfield}->{lib}."</span>";
285 $subfield_data{mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
286 $subfield_data{repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
288 ($x,$value) = find_value($tag,$subfield,$itemrecord) if ($itemrecord);
289 $value =~ s/"/"/g;
291 $value = $tagslib->{$tag}->{$subfield}->{defaultvalue};
293 # get today date & replace YYYY, MM, DD if provided in the default value
294 my ( $year, $month, $day ) = Today();
295 $month = sprintf( "%02d", $month );
296 $day = sprintf( "%02d", $day );
297 $value =~ s/YYYY/$year/g;
298 $value =~ s/MM/$month/g;
299 $value =~ s/DD/$day/g;
301 $subfield_data{visibility} = "display:none;" if (($tagslib->{$tag}->{$subfield}->{hidden} > 4) || ($tagslib->{$tag}->{$subfield}->{hidden} < -4));
302 #testing branch value if IndependantBranches.
303 my $test = (C4::Context->preference("IndependantBranches")) &&
304 ($tag eq $branchtagfield) && ($subfield eq $branchtagsubfield) &&
305 (C4::Context->userenv->{flags} != 1) && ($value) && ($value ne C4::Context->userenv->{branch}) ;
306 # print $input->redirect(".pl?biblionumber=$biblionumber") if ($test);
307 # search for itemcallnumber if applicable
308 if (!$value && $tagslib->{$tag}->{$subfield}->{kohafield} eq 'items.itemcallnumber' && C4::Context->preference('itemcallnumber')) {
309 my $CNtag = substr(C4::Context->preference('itemcallnumber'),0,3);
310 my $CNsubfield = substr(C4::Context->preference('itemcallnumber'),3,1);
311 my $CNsubfield2 = substr(C4::Context->preference('itemcallnumber'),4,1);
312 my $temp2 = $temp->field($CNtag);
314 $value = ($temp2->subfield($CNsubfield)).' '.($temp2->subfield($CNsubfield2));
315 #remove any trailing space incase one subfield is used
316 $value=~s/^\s+|\s+$//g;
319 if ( $tagslib->{$tag}->{$subfield}->{authorised_value} ) {
320 my @authorised_values;
322 my $dbh=C4::Context->dbh;
324 # builds list, depending on authorised value...
327 if ( $tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) {
328 #Use GetBranches($onlymine)
329 my $onlymine=C4::Context->preference('IndependantBranches') &&
330 C4::Context->userenv &&
331 C4::Context->userenv->{flags}!=1 &&
332 C4::Context->userenv->{branch};
333 my $branches = GetBranches($onlymine);
335 foreach my $thisbranch ( sort keys %$branches ) {
336 push @authorised_values, $thisbranch;
337 $authorised_lib{$thisbranch} = $branches->{$thisbranch}->{'branchname'};
342 elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes" ) {
345 "select itemtype,description from itemtypes order by description");
347 push @authorised_values, ""
348 unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
352 while ( my ( $itemtype, $description ) = $sth->fetchrow_array ) {
353 push @authorised_values, $itemtype;
354 $authorised_lib{$itemtype} = $description;
356 $value = $itemtype unless ($value);
360 elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "cn_source" ) {
361 push @authorised_values, ""
362 unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
364 my $class_sources = GetClassSources();
366 my $default_source = C4::Context->preference("DefaultClassificationSource");
368 foreach my $class_source (sort keys %$class_sources) {
369 next unless $class_sources->{$class_source}->{'used'} or
370 ($value and $class_source eq $value) or
371 ($class_source eq $default_source);
372 push @authorised_values, $class_source;
373 $authorised_lib{$class_source} = $class_sources->{$class_source}->{'description'};
375 $value = $default_source unless ($value);
377 #---- "true" authorised value
380 $authorised_values_sth->execute(
381 $tagslib->{$tag}->{$subfield}->{authorised_value} );
383 push @authorised_values, ""
384 unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
386 while ( my ( $value, $lib ) = $authorised_values_sth->fetchrow_array ) {
387 push @authorised_values, $value;
388 $authorised_lib{$value} = $lib;
391 $subfield_data{marc_value} =CGI::scrolling_list(
392 -name => "field_value",
393 -values => \@authorised_values,
395 -labels => \%authorised_lib,
400 -id => "tag_".$tag."_subfield_".$subfield."_".$index_subfield,
401 -class => "input_marceditor",
403 # it's a thesaurus / authority field
405 elsif ( $tagslib->{$tag}->{$subfield}->{authtypecode} ) {
406 $subfield_data{marc_value} =
407 "<input type=\"text\"
408 id=\"".$subfield_data{id}."\"
411 class=\"input_marceditor\"
416 <a href=\"#\" class=\"buttonDot\"
417 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>
419 # it's a plugin field
421 elsif ( $tagslib->{$tag}->{$subfield}->{'value_builder'} ) {
423 # opening plugin. Just check wether we are on a developper computer on a production one
424 # (the cgidir differs)
425 my $cgidir = C4::Context->intranetdir . "/cgi-bin/cataloguing/value_builder";
426 unless ( opendir( DIR, "$cgidir" ) ) {
427 $cgidir = C4::Context->intranetdir . "/cataloguing/value_builder";
430 my $plugin = $cgidir . "/" . $tagslib->{$tag}->{$subfield}->{'value_builder'};
432 my $extended_param = plugin_parameters( $dbh, $temp, $tagslib, $subfield_data{id}, \@loop_data );
433 my ( $function_name, $javascript ) = plugin_javascript( $dbh, $temp, $tagslib, $subfield_data{id}, \@loop_data );
435 $subfield_data{marc_value} =
436 "<input tabindex=\"1\"
438 id=\"".$subfield_data{id}."\"
441 class=\"input_marceditor\"
442 onfocus=\"Focus$function_name(".$subfield_data{random}.")\"
445 onblur=\"Blur$function_name(".$subfield_data{random}."); \" \/>
446 <a href=\"#\" class=\"buttonDot\" onclick=\"Clic$function_name('$subfield_data{id}'); return false;\" title=\"Tag Editor\">...</a>
449 warn "Plugin Failed: $plugin";
450 # supply default input form
451 $subfield_data{marc_value} =
452 "<input type=\"text\"
453 id=\"".$subfield_data{id}."\"
459 class=\"input_marceditor\"
463 # it's an hidden field
465 elsif ( $tag eq '' ) {
466 $subfield_data{marc_value} =
467 "<input tabindex=\"1\"
469 id=\"".$subfield_data{id}."\"
476 elsif ( $tagslib->{$tag}->{$subfield}->{'hidden'} ) {
477 $subfield_data{marc_value} =
478 "<input type=\"text\"
479 id=\"".$subfield_data{id}."\"
481 class=\"input_marceditor\"
488 # it's a standard field
494 ( C4::Context->preference("marcflavour") eq "UNIMARC" && $tag >= 300
495 and $tag < 400 && $subfield eq 'a' )
498 && C4::Context->preference("marcflavour") eq "MARC21" )
501 $subfield_data{marc_value} =
502 "<textarea cols=\"70\"
504 id=\"".$subfield_data{id}."\"
506 class=\"input_marceditor\"
514 $subfield_data{marc_value} =
515 "<input type=\"text\"
516 id=\"".$subfield_data{id}."\"
522 class=\"input_marceditor\"
527 # $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\">";
528 push(@loop_data, \%subfield_data);
533 # what's the next op ? it's what we are not in : an add if we're editing, otherwise, and edit.
534 $template->param( title => $record->title() ) if ($record ne "-1");
535 $template->param(item_loop => \@item_value_loop,
536 item_header_loop => \@header_value_loop,
537 biblionumber => $biblionumber,
538 title => $oldrecord->{title},
539 author => $oldrecord->{author},
541 itemnumber => $itemnumber,
542 itemtagfield => $itemtagfield,
543 itemtagsubfield =>$itemtagsubfield,
545 opisadd => ($nextop eq "saveitem")?0:1);
546 foreach my $error (@errors) {
547 $template->param($error => 1);
549 output_html_with_http_headers $input, $cookie, $template->output;