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
27 use C4::Koha; # XXX subfield_is_koha_internal_p
28 use C4::Branch; # XXX subfield_is_koha_internal_p
31 use Date::Calc qw(Today);
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},
78 my $frameworkcode = &GetFrameworkCode($biblionumber);
80 my $tagslib = &GetMarcStructure(1,$frameworkcode);
81 my $record = GetMarcBiblio($biblionumber);
82 my $oldrecord = TransformMarcToKoha($dbh,$record);
85 my @errors; # store errors found while checking data BEFORE saving item.
86 #-------------------------------------------------------------------------------
87 if ($op eq "additem") {
88 #-------------------------------------------------------------------------------
90 my @tags = $input->param('tag');
91 my @subfields = $input->param('subfield');
92 my @values = $input->param('field_value');
93 # build indicator hash.
94 my @ind_tag = $input->param('ind_tag');
95 my @indicator = $input->param('indicator');
96 my $xml = TransformHtmlToXml(\@tags,\@subfields,\@values,\@indicator,\@ind_tag, 'ITEM');
97 my $record=MARC::Record::new_from_xml($xml, 'UTF-8');
98 # if autoBarcode is ON, calculate barcode...
99 if (C4::Context->preference('autoBarcode')) {
100 my ($tagfield,$tagsubfield) = &GetMarcFromKohaField("items.barcode",$frameworkcode);
101 unless ($record->field($tagfield)->subfield($tagsubfield)) {
102 my $sth_barcode = $dbh->prepare("select max(abs(barcode)) from items");
103 $sth_barcode->execute;
104 my ($newbarcode) = $sth_barcode->fetchrow;
106 # OK, we have the new barcode, now create the entry in MARC record
107 my $fieldItem = $record->field($tagfield);
108 $record->delete_field($fieldItem);
109 $fieldItem->add_subfields($tagsubfield => $newbarcode);
110 $record->insert_fields_ordered($fieldItem);
113 # check for item barcode # being unique
114 my $addedolditem = TransformMarcToKoha($dbh,$record);
115 my $exist_itemnumber = get_item_from_barcode($addedolditem->{'barcode'});
116 push @errors,"barcode_not_unique" if($exist_itemnumber);
117 # if barcode exists, don't create, but report The problem.
118 my ($oldbiblionumber,$oldbibnum,$oldbibitemnum) = AddItem($record,$biblionumber) unless ($exist_itemnumber);
119 if ($exist_itemnumber) {
121 $itemrecord = $record;
125 #-------------------------------------------------------------------------------
126 } elsif ($op eq "edititem") {
127 #-------------------------------------------------------------------------------
128 # retrieve item if exist => then, it's a modif
129 $itemrecord = GetMarcItem($biblionumber,$itemnumber);
131 #-------------------------------------------------------------------------------
132 } elsif ($op eq "delitem") {
133 #-------------------------------------------------------------------------------
134 # check that there is no issue on this item before deletion.
135 my $sth=$dbh->prepare("select * from issues i where i.returndate is null and i.itemnumber=?");
136 $sth->execute($itemnumber);
137 my $onloan=$sth->fetchrow;
138 push @errors,"book_on_loan" if ($onloan); ##error book_on_loan added to template as well
142 &DelItem($dbh,$biblionumber,$itemnumber);
143 print $input->redirect("additem.pl?biblionumber=$biblionumber&frameworkcode=$frameworkcode");
146 #-------------------------------------------------------------------------------
147 } elsif ($op eq "saveitem") {
148 #-------------------------------------------------------------------------------
150 my @tags = $input->param('tag');
151 my @subfields = $input->param('subfield');
152 my @values = $input->param('field_value');
153 # build indicator hash.
154 my @ind_tag = $input->param('ind_tag');
155 my @indicator = $input->param('indicator');
156 # my $itemnumber = $input->param('itemnumber');
157 my $xml = TransformHtmlToXml(\@tags,\@subfields,\@values,\@indicator,\@ind_tag,'ITEM');
158 my $itemtosave=MARC::Record::new_from_xml($xml, 'UTF-8');
159 # MARC::Record builded => now, record in DB
160 # warn "R: ".$record->as_formatted;
161 # check that the barcode don't exist already
162 my $addedolditem = TransformMarcToKoha($dbh,$itemtosave);
163 my $exist_itemnumber = get_item_from_barcode($addedolditem->{'barcode'});
164 if ($exist_itemnumber && $exist_itemnumber != $itemnumber) {
165 push @errors,"barcode_not_unique";
167 my ($oldbiblionumber,$oldbibnum,$oldbibitemnum) = ModItem($itemtosave,$biblionumber,$itemnumber,0);
174 #-------------------------------------------------------------------------------
175 # build screen with existing items. and "new" one
176 #-------------------------------------------------------------------------------
178 # now, build existiing item list
179 my $temp = GetMarcBiblio( $biblionumber );
180 my @fields = $temp->fields();
181 #my @fields = $record->fields();
182 my %witness; #---- stores the list of subfields used at least once, with the "meaning" of the code
184 #---- finds where items.itemnumber is stored
185 my ($itemtagfield,$itemtagsubfield) = &GetMarcFromKohaField("items.itemnumber",$frameworkcode);
186 my ($branchtagfield,$branchtagsubfield) = &GetMarcFromKohaField("items.homebranch",$frameworkcode);
188 foreach my $field (@fields) {
189 next if ($field->tag()<10);
190 my @subf=$field->subfields;
192 # loop through each subfield
193 for my $i (0..$#subf) {
194 next if ($tagslib->{$field->tag()}->{$subf[$i][0]}->{tab} ne 10
195 && ($field->tag() ne $itemtagfield
196 && $subf[$i][0] ne $itemtagsubfield));
198 $witness{$subf[$i][0]} = $tagslib->{$field->tag()}->{$subf[$i][0]}->{lib} if ($tagslib->{$field->tag()}->{$subf[$i][0]}->{tab} eq 10);
200 $this_row{$subf[$i][0]} =$subf[$i][1] if ($tagslib->{$field->tag()}->{$subf[$i][0]}->{tab} eq 10);
202 if (($field->tag eq $branchtagfield) && ($subf[$i][$0] eq $branchtagsubfield) && C4::Context->preference("IndependantBranches")) {
204 my $userenv = C4::Context->userenv();
205 unless (($userenv->{'flags'} == 1) or (($userenv->{'branch'} eq $subf[$i][1]))){
206 $this_row{'nomod'}=1;
209 $this_row{itemnumber} = $subf[$i][1] if ($field->tag() eq $itemtagfield && $subf[$i][0] eq $itemtagsubfield);
212 push(@big_array, \%this_row);
215 #fill big_row with missing data
216 foreach my $subfield_code (keys(%witness)) {
217 for (my $i=0;$i<=$#big_array;$i++) {
218 $big_array[$i]{$subfield_code}=" " unless ($big_array[$i]{$subfield_code});
221 my ($holdingbrtagf,$holdingbrtagsubf) = &GetMarcFromKohaField("items.holdingbranch",$frameworkcode);
222 @big_array = sort {$a->{$holdingbrtagsubf} cmp $b->{$holdingbrtagsubf}} @big_array;
224 # now, construct template !
225 # First, the existing items for display
227 my @header_value_loop;
228 for (my $i=0;$i<=$#big_array; $i++) {
230 foreach my $subfield_code (sort keys(%witness)) {
231 $items_data .="<td>".$big_array[$i]{$subfield_code}."</td>";
234 $items_data =~ s/"/"/g;
235 $row_data{item_value} = $items_data;
236 $row_data{itemnumber} = $big_array[$i]->{itemnumber};
237 #reporting this_row values
238 $row_data{'nomod'} = $big_array[$i]{'nomod'};
239 push(@item_value_loop,\%row_data);
241 foreach my $subfield_code (sort keys(%witness)) {
243 $header_value{header_value} = $witness{$subfield_code};
244 push(@header_value_loop, \%header_value);
247 # now, build the item form for entering a new item
250 my $authorised_values_sth = $dbh->prepare("SELECT authorised_value,lib FROM authorised_values WHERE category=? ORDER BY lib");
252 foreach my $tag (sort keys %{$tagslib}) {
253 my $previous_tag = '';
254 # loop through each subfield
255 foreach my $subfield (sort keys %{$tagslib->{$tag}}) {
256 next if subfield_is_koha_internal_p($subfield);
257 next if ($tagslib->{$tag}->{$subfield}->{'tab'} ne "10");
260 my $index_subfield= int(rand(1000000));
261 if($subfield eq '@'){
262 $subfield_data{id} = "tag_".$tag."_subfield_00_".$index_subfield;
264 $subfield_data{id} = "tag_".$tag."_subfield_".$subfield."_".$index_subfield;
266 $subfield_data{tag}=$tag;
267 $subfield_data{subfield}=$subfield;
268 $subfield_data{random}=int(rand(1000000));
269 # $subfield_data{marc_lib}=$tagslib->{$tag}->{$subfield}->{lib};
270 $subfield_data{marc_lib}="<span id=\"error$i\" title=\"".$tagslib->{$tag}->{$subfield}->{lib}."\">".$tagslib->{$tag}->{$subfield}->{lib}."</span>";
271 $subfield_data{mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
272 $subfield_data{repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
274 ($x,$value) = find_value($tag,$subfield,$itemrecord) if ($itemrecord);
275 $value =~ s/"/"/g;
277 $value = $tagslib->{$tag}->{$subfield}->{defaultvalue};
279 # get today date & replace YYYY, MM, DD if provided in the default value
280 my ( $year, $month, $day ) = Today();
281 $month = sprintf( "%02d", $month );
282 $day = sprintf( "%02d", $day );
283 $value =~ s/YYYY/$year/g;
284 $value =~ s/MM/$month/g;
285 $value =~ s/DD/$day/g;
287 $subfield_data{visibility} = "display:none;" if (($tagslib->{$tag}->{$subfield}->{hidden} % 2 == 1));
288 #testing branch value if IndependantBranches.
289 my $test = (C4::Context->preference("IndependantBranches")) &&
290 ($tag eq $branchtagfield) && ($subfield eq $branchtagsubfield) &&
291 (C4::Context->userenv->{flags} != 1) && ($value) && ($value ne C4::Context->userenv->{branch}) ;
292 # print $input->redirect(".pl?biblionumber=$biblionumber") if ($test);
293 # search for itemcallnumber if applicable
294 if (!$value && $tagslib->{$tag}->{$subfield}->{kohafield} eq 'items.itemcallnumber' && C4::Context->preference('itemcallnumber')) {
295 my $CNtag = substr(C4::Context->preference('itemcallnumber'),0,3);
296 my $CNsubfield = substr(C4::Context->preference('itemcallnumber'),3,1);
297 my $CNsubfield2 = substr(C4::Context->preference('itemcallnumber'),4,1);
298 my $temp2 = $temp->field($CNtag);
300 $value = ($temp2->subfield($CNsubfield)).' '.($temp2->subfield($CNsubfield2));
301 #remove any trailing space incase one subfield is used
302 $value=~s/^\s+|\s+$//g;
305 if ( $tagslib->{$tag}->{$subfield}->{authorised_value} ) {
306 my @authorised_values;
308 my $dbh=C4::Context->dbh;
310 # builds list, depending on authorised value...
313 if ( $tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) {
314 #Use GetBranches($onlymine)
315 my $onlymine=C4::Context->preference('IndependantBranches') &&
316 C4::Context->userenv &&
317 C4::Context->userenv->{flags}!=1 &&
318 C4::Context->userenv->{branch};
319 my $branches = GetBranches($onlymine);
321 foreach my $thisbranch ( sort keys %$branches ) {
322 push @authorised_values, $thisbranch;
323 $authorised_lib{$thisbranch} = $branches->{$thisbranch}->{'branchname'};
328 elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes" ) {
331 "select itemtype,description from itemtypes order by description");
333 push @authorised_values, ""
334 unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
338 while ( my ( $itemtype, $description ) = $sth->fetchrow_array ) {
339 push @authorised_values, $itemtype;
340 $authorised_lib{$itemtype} = $description;
342 $value = $itemtype unless ($value);
346 elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "cn_source" ) {
347 push @authorised_values, ""
348 unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
350 my $class_sources = GetClassSources();
352 my $default_source = C4::Context->preference("DefaultClassificationSource");
354 foreach my $class_source (sort keys %$class_sources) {
355 next unless $class_sources->{$class_source}->{'used'} or
356 ($value and $class_source eq $value) or
357 ($class_source eq $default_source);
358 push @authorised_values, $class_source;
359 $authorised_lib{$class_source} = $class_sources->{$class_source}->{'description'};
360 $value = $class_source unless ($value);
361 $value = $default_source unless ($value);
364 #---- "true" authorised value
367 $authorised_values_sth->execute(
368 $tagslib->{$tag}->{$subfield}->{authorised_value} );
370 push @authorised_values, ""
371 unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
373 while ( my ( $value, $lib ) = $authorised_values_sth->fetchrow_array ) {
374 push @authorised_values, $value;
375 $authorised_lib{$value} = $lib;
378 $subfield_data{marc_value} =CGI::scrolling_list(
379 -name => "field_value",
380 -values => \@authorised_values,
382 -labels => \%authorised_lib,
387 -id => "tag_".$tag."_subfield_".$subfield."_".$index_subfield,
388 -class => "input_marceditor",
390 # it's a thesaurus / authority field
392 elsif ( $tagslib->{$tag}->{$subfield}->{authtypecode} ) {
393 $subfield_data{marc_value} =
394 "<input type=\"text\"
395 id=\"".$subfield_data{id}."\"
398 class=\"input_marceditor\"
403 <a href=\"#\" class=\"buttonDot\"
404 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>
406 # it's a plugin field
408 elsif ( $tagslib->{$tag}->{$subfield}->{'value_builder'} ) {
410 # opening plugin. Just check wether we are on a developper computer on a production one
411 # (the cgidir differs)
412 my $cgidir = C4::Context->intranetdir . "/cgi-bin/cataloguing/value_builder";
413 unless ( opendir( DIR, "$cgidir" ) ) {
414 $cgidir = C4::Context->intranetdir . "/cataloguing/value_builder";
417 my $plugin = $cgidir . "/" . $tagslib->{$tag}->{$subfield}->{'value_builder'};
419 my $extended_param = plugin_parameters( $dbh, $temp, $tagslib, $subfield_data{id}, \@loop_data );
420 my ( $function_name, $javascript ) = plugin_javascript( $dbh, $temp, $tagslib, $subfield_data{id}, \@loop_data );
422 $subfield_data{marc_value} =
423 "<input tabindex=\"1\"
425 id=\"".$subfield_data{id}."\"
428 class=\"input_marceditor\"
429 onfocus=\"Focus$function_name(".$subfield_data{random}.")\"
432 onblur=\"Blur$function_name(".$subfield_data{random}."); \" \/>
433 <a href=\"#\" class=\"buttonDot\" onclick=\"Clic$function_name('$subfield_data{id}'); return false;\" title=\"Tag Editor\">...</a>
436 warn "Plugin Failed: $plugin";
437 # supply default input form
438 $subfield_data{marc_value} =
439 "<input type=\"text\"
440 id=\"".$subfield_data{id}."\"
446 class=\"input_marceditor\"
450 # it's an hidden field
452 elsif ( $tag eq '' ) {
453 $subfield_data{marc_value} =
454 "<input tabindex=\"1\"
456 id=\"".$subfield_data{id}."\"
463 elsif ( $tagslib->{$tag}->{$subfield}->{'hidden'} ) {
464 $subfield_data{marc_value} =
465 "<input type=\"text\"
466 id=\"".$subfield_data{id}."\"
468 class=\"input_marceditor\"
475 # it's a standard field
481 ( C4::Context->preference("marcflavour") eq "UNIMARC" && $tag >= 300
482 and $tag < 400 && $subfield eq 'a' )
485 && C4::Context->preference("marcflavour") eq "MARC21" )
488 $subfield_data{marc_value} =
489 "<textarea cols=\"70\"
491 id=\"".$subfield_data{id}."\"
493 class=\"input_marceditor\"
501 $subfield_data{marc_value} =
502 "<input type=\"text\"
503 id=\"".$subfield_data{id}."\"
509 class=\"input_marceditor\"
514 # $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\">";
515 push(@loop_data, \%subfield_data);
520 # what's the next op ? it's what we are not in : an add if we're editing, otherwise, and edit.
521 $template->param( title => $record->title() ) if ($record ne "-1");
522 $template->param(item_loop => \@item_value_loop,
523 item_header_loop => \@header_value_loop,
524 biblionumber => $biblionumber,
525 title => $oldrecord->{title},
526 author => $oldrecord->{author},
528 itemnumber => $itemnumber,
529 itemtagfield => $itemtagfield,
530 itemtagsubfield =>$itemtagsubfield,
532 opisadd => ($nextop eq "saveitem")?0:1);
533 foreach my $error (@errors) {
534 $template->param($error => 1);
536 output_html_with_http_headers $input, $cookie, $template->output;