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 Date::Calc qw(Today);
33 my ($tagfield,$insubfield,$record) = @_;
36 foreach my $field ($record->field($tagfield)) {
37 my @subfields = $field->subfields();
38 foreach my $subfield (@subfields) {
39 if (@$subfield[0] eq $insubfield) {
40 $result .= @$subfield[1];
41 $indicator = $field->indicator(1).$field->indicator(2);
45 return($indicator,$result);
48 sub get_item_from_barcode {
50 my $dbh=C4::Context->dbh;
52 my $rq=$dbh->prepare("SELECT itemnumber from items where items.barcode=?");
53 $rq->execute($barcode);
54 ($result)=$rq->fetchrow;
59 my $dbh = C4::Context->dbh;
60 my $error = $input->param('error');
61 my $biblionumber = $input->param('biblionumber');
62 my $itemnumber = $input->param('itemnumber');
63 my $op = $input->param('op');
65 my ($template, $loggedinuser, $cookie)
66 = get_template_and_user({template_name => "cataloguing/additem.tmpl",
70 flagsrequired => {editcatalogue => 1},
75 my $frameworkcode = &GetFrameworkCode($biblionumber);
77 my $tagslib = &GetMarcStructure(1,$frameworkcode);
78 my $record = GetMarcBiblio($biblionumber);
79 my $oldrecord = TransformMarcToKoha($dbh,$record);
82 my @errors; # store errors found while checking data BEFORE saving item.
83 #-------------------------------------------------------------------------------
84 if ($op eq "additem") {
85 #-------------------------------------------------------------------------------
87 my @tags = $input->param('tag');
88 my @subfields = $input->param('subfield');
89 my @values = $input->param('field_value');
90 # build indicator hash.
91 my @ind_tag = $input->param('ind_tag');
92 my @indicator = $input->param('indicator');
93 my $xml = TransformHtmlToXml(\@tags,\@subfields,\@values,\@indicator,\@ind_tag, 'ITEM');
94 my $record=MARC::Record::new_from_xml($xml, 'UTF-8');
95 # if autoBarcode is ON, calculate barcode...
96 if (C4::Context->preference('autoBarcode')) {
97 my ($tagfield,$tagsubfield) = &GetMarcFromKohaField("items.barcode",$frameworkcode);
98 unless ($record->field($tagfield)->subfield($tagsubfield)) {
99 my $sth_barcode = $dbh->prepare("select max(abs(barcode)) from items");
100 $sth_barcode->execute;
101 my ($newbarcode) = $sth_barcode->fetchrow;
103 # OK, we have the new barcode, now create the entry in MARC record
104 my $fieldItem = $record->field($tagfield);
105 $record->delete_field($fieldItem);
106 $fieldItem->add_subfields($tagsubfield => $newbarcode);
107 $record->insert_fields_ordered($fieldItem);
110 # check for item barcode # being unique
111 my $addedolditem = TransformMarcToKoha($dbh,$record);
112 my $exist_itemnumber = get_item_from_barcode($addedolditem->{'barcode'});
113 push @errors,"barcode_not_unique" if($exist_itemnumber);
114 # if barcode exists, don't create, but report The problem.
115 my ($oldbiblionumber,$oldbibnum,$oldbibitemnum) = AddItem($record,$biblionumber) unless ($exist_itemnumber);
116 if ($exist_itemnumber) {
118 $itemrecord = $record;
122 #-------------------------------------------------------------------------------
123 } elsif ($op eq "edititem") {
124 #-------------------------------------------------------------------------------
125 # retrieve item if exist => then, it's a modif
126 $itemrecord = GetMarcItem($biblionumber,$itemnumber);
128 #-------------------------------------------------------------------------------
129 } elsif ($op eq "delitem") {
130 #-------------------------------------------------------------------------------
131 # check that there is no issue on this item before deletion.
132 my $sth=$dbh->prepare("select * from issues i where i.returndate is null and i.itemnumber=?");
133 $sth->execute($itemnumber);
134 my $onloan=$sth->fetchrow;
135 push @errors,"book_on_loan" if ($onloan); ##error book_on_loan added to template as well
139 &DelItem($dbh,$biblionumber,$itemnumber);
140 print $input->redirect("additem.pl?biblionumber=$biblionumber&frameworkcode=$frameworkcode");
143 #-------------------------------------------------------------------------------
144 } elsif ($op eq "saveitem") {
145 #-------------------------------------------------------------------------------
147 my @tags = $input->param('tag');
148 my @subfields = $input->param('subfield');
149 my @values = $input->param('field_value');
150 # build indicator hash.
151 my @ind_tag = $input->param('ind_tag');
152 my @indicator = $input->param('indicator');
153 # my $itemnumber = $input->param('itemnumber');
154 my $xml = TransformHtmlToXml(\@tags,\@subfields,\@values,\@indicator,\@ind_tag,'ITEM');
155 $itemrecord=MARC::Record::new_from_xml($xml, 'UTF-8');
156 # MARC::Record builded => now, record in DB
157 # warn "R: ".$record->as_formatted;
158 # check that the barcode don't exist already
159 my $addedolditem = TransformMarcToKoha($dbh,$itemrecord);
160 my $exist_itemnumber = get_item_from_barcode($addedolditem->{'barcode'});
161 if ($exist_itemnumber && $exist_itemnumber != $itemnumber) {
162 push @errors,"barcode_not_unique";
164 my ($oldbiblionumber,$oldbibnum,$oldbibitemnum) = ModItem($itemrecord,$biblionumber,$itemnumber,0);
171 #-------------------------------------------------------------------------------
172 # build screen with existing items. and "new" one
173 #-------------------------------------------------------------------------------
175 # now, build existiing item list
176 my $temp = GetMarcBiblio( $biblionumber );
177 my @fields = $temp->fields();
178 #my @fields = $record->fields();
179 my %witness; #---- stores the list of subfields used at least once, with the "meaning" of the code
181 #---- finds where items.itemnumber is stored
182 my ($itemtagfield,$itemtagsubfield) = &GetMarcFromKohaField("items.itemnumber",$frameworkcode);
183 my ($branchtagfield,$branchtagsubfield) = &GetMarcFromKohaField("items.homebranch",$frameworkcode);
185 foreach my $field (@fields) {
186 next if ($field->tag()<10);
187 my @subf=$field->subfields;
189 # loop through each subfield
190 for my $i (0..$#subf) {
191 next if ($tagslib->{$field->tag()}->{$subf[$i][0]}->{tab} ne 10
192 && ($field->tag() ne $itemtagfield
193 && $subf[$i][0] ne $itemtagsubfield));
195 $witness{$subf[$i][0]} = $tagslib->{$field->tag()}->{$subf[$i][0]}->{lib} if ($tagslib->{$field->tag()}->{$subf[$i][0]}->{tab} eq 10);
197 $this_row{$subf[$i][0]} =$subf[$i][1] if ($tagslib->{$field->tag()}->{$subf[$i][0]}->{tab} eq 10);
199 if (($field->tag eq $branchtagfield) && ($subf[$i][$0] eq $branchtagsubfield) && C4::Context->preference("IndependantBranches")) {
201 my $userenv = C4::Context->userenv();
202 unless (($userenv->{'flags'} == 1) or (($userenv->{'branch'} eq $subf[$i][1]))){
203 $this_row{'nomod'}=1;
206 $this_row{itemnumber} = $subf[$i][1] if ($field->tag() eq $itemtagfield && $subf[$i][0] eq $itemtagsubfield);
209 push(@big_array, \%this_row);
212 #fill big_row with missing data
213 foreach my $subfield_code (keys(%witness)) {
214 for (my $i=0;$i<=$#big_array;$i++) {
215 $big_array[$i]{$subfield_code}=" " unless ($big_array[$i]{$subfield_code});
218 my ($holdingbrtagf,$holdingbrtagsubf) = &GetMarcFromKohaField("items.holdingbranch",$frameworkcode);
219 @big_array = sort {$a->{$holdingbrtagsubf} cmp $b->{$holdingbrtagsubf}} @big_array;
221 # now, construct template !
222 # First, the existing items for display
224 my @header_value_loop;
225 for (my $i=0;$i<=$#big_array; $i++) {
227 foreach my $subfield_code (sort keys(%witness)) {
228 $items_data .="<td>".$big_array[$i]{$subfield_code}."</td>";
231 $items_data =~ s/"/"/g;
232 $row_data{item_value} = $items_data;
233 $row_data{itemnumber} = $big_array[$i]->{itemnumber};
234 #reporting this_row values
235 $row_data{'nomod'} = $big_array[$i]{'nomod'};
236 push(@item_value_loop,\%row_data);
238 foreach my $subfield_code (sort keys(%witness)) {
240 $header_value{header_value} = $witness{$subfield_code};
241 push(@header_value_loop, \%header_value);
244 # now, build the item form for entering a new item
247 my $authorised_values_sth = $dbh->prepare("SELECT authorised_value,lib FROM authorised_values WHERE category=? ORDER BY lib");
249 foreach my $tag (sort keys %{$tagslib}) {
250 my $previous_tag = '';
251 # loop through each subfield
252 foreach my $subfield (sort keys %{$tagslib->{$tag}}) {
253 next if subfield_is_koha_internal_p($subfield);
254 next if ($tagslib->{$tag}->{$subfield}->{'tab'} ne "10");
257 my $index_subfield= int(rand(1000000));
258 if($subfield eq '@'){
259 $subfield_data{id} = "tag_".$tag."_subfield_0_".$index_subfield;
261 $subfield_data{id} = "tag_".$tag."_subfield_".$subfield."_".$index_subfield;
263 $subfield_data{tag}=$tag;
264 $subfield_data{subfield}=$subfield;
265 $subfield_data{random}=int(rand(1000000));
266 # $subfield_data{marc_lib}=$tagslib->{$tag}->{$subfield}->{lib};
267 $subfield_data{marc_lib}="<span id=\"error$i\" title=\"".$tagslib->{$tag}->{$subfield}->{lib}."\">".$tagslib->{$tag}->{$subfield}->{lib}."</span>";
268 $subfield_data{mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
269 $subfield_data{repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
271 ($x,$value) = find_value($tag,$subfield,$itemrecord) if ($itemrecord);
272 $value =~ s/"/"/g;
274 $value = $tagslib->{$tag}->{$subfield}->{defaultvalue};
276 # get today date & replace YYYY, MM, DD if provided in the default value
277 my ( $year, $month, $day ) = Today();
278 $month = sprintf( "%02d", $month );
279 $day = sprintf( "%02d", $day );
280 $value =~ s/YYYY/$year/g;
281 $value =~ s/MM/$month/g;
282 $value =~ s/DD/$day/g;
284 $subfield_data{visibility} = "display:none;" if (($tagslib->{$tag}->{$subfield}->{hidden} % 2 == 1));
285 #testing branch value if IndependantBranches.
286 my $test = (C4::Context->preference("IndependantBranches")) &&
287 ($tag eq $branchtagfield) && ($subfield eq $branchtagsubfield) &&
288 (C4::Context->userenv->{flags} != 1) && ($value) && ($value ne C4::Context->userenv->{branch}) ;
289 # print $input->redirect(".pl?biblionumber=$biblionumber") if ($test);
290 # search for itemcallnumber if applicable
291 if (!$value && $tagslib->{$tag}->{$subfield}->{kohafield} eq 'items.itemcallnumber' && C4::Context->preference('itemcallnumber')) {
292 my $CNtag = substr(C4::Context->preference('itemcallnumber'),0,3);
293 my $CNsubfield = substr(C4::Context->preference('itemcallnumber'),3,1);
294 my $CNsubfield2 = substr(C4::Context->preference('itemcallnumber'),4,1);
295 my $temp2 = $temp->field($CNtag);
297 $value = ($temp2->subfield($CNsubfield)).' '.($temp2->subfield($CNsubfield2));
298 #remove any trailing space incase one subfield is used
299 $value=~s/^\s+|\s+$//g;
302 if ( $tagslib->{$tag}->{$subfield}->{authorised_value} ) {
303 my @authorised_values;
305 my $dbh=C4::Context->dbh;
307 # builds list, depending on authorised value...
310 if ( $tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) {
313 "select branchcode,branchname from branches order by branchname");
315 push @authorised_values, ""
316 unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
318 while ( my ( $branchcode, $branchname ) = $sth->fetchrow_array ) {
319 push @authorised_values, $branchcode;
320 $authorised_lib{$branchcode} = $branchname;
325 elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes" ) {
328 "select itemtype,description from itemtypes order by description");
330 push @authorised_values, ""
331 unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
335 while ( my ( $itemtype, $description ) = $sth->fetchrow_array ) {
336 push @authorised_values, $itemtype;
337 $authorised_lib{$itemtype} = $description;
339 $value = $itemtype unless ($value);
341 #---- "true" authorised value
344 $authorised_values_sth->execute(
345 $tagslib->{$tag}->{$subfield}->{authorised_value} );
347 push @authorised_values, ""
348 unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
350 while ( my ( $value, $lib ) = $authorised_values_sth->fetchrow_array ) {
351 push @authorised_values, $value;
352 $authorised_lib{$value} = $lib;
355 $subfield_data{marc_value} =CGI::scrolling_list(
356 -name => "field_value",
357 -values => \@authorised_values,
359 -labels => \%authorised_lib,
364 -id => "tag_".$tag."_subfield_".$subfield."_".$index_subfield,
365 -class => "input_marceditor",
367 # it's a thesaurus / authority field
369 elsif ( $tagslib->{$tag}->{$subfield}->{authtypecode} ) {
370 $subfield_data{marc_value} =
371 "<input type=\"text\"
372 id=\"".$subfield_data{id}."\"
375 class=\"input_marceditor\"
380 <a href=\"#\" class=\"buttonDot\"
381 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>
383 # it's a plugin field
385 elsif ( $tagslib->{$tag}->{$subfield}->{'value_builder'} ) {
387 # opening plugin. Just check wether we are on a developper computer on a production one
388 # (the cgidir differs)
389 my $cgidir = C4::Context->intranetdir . "/cgi-bin/cataloguing/value_builder";
390 unless ( opendir( DIR, "$cgidir" ) ) {
391 $cgidir = C4::Context->intranetdir . "/cataloguing/value_builder";
393 my $plugin = $cgidir . "/" . $tagslib->{$tag}->{$subfield}->{'value_builder'};
394 do $plugin || die "Plugin Failed: ".$plugin;
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 # my ( $function_name, $javascript,$extended_param );
399 $subfield_data{marc_value} =
400 "<input tabindex=\"1\"
402 id=\"".$subfield_data{id}."\"
405 class=\"input_marceditor\"
406 onfocus=\"Focus$function_name(".$subfield_data{random}.")\"
409 onblur=\"Blur$function_name(".$subfield_data{random}."); \" \/>
410 <a href=\"#\" class=\"buttonDot\" onclick=\"Clic$function_name('$subfield_data{id}'; return false;)\" title=\"Tag Editor\">...</a>
412 # it's an hidden field
414 elsif ( $tag eq '' ) {
415 $subfield_data{marc_value} =
416 "<input tabindex=\"1\"
418 id=\"".$subfield_data{id}."\"
425 elsif ( $tagslib->{$tag}->{$subfield}->{'hidden'} ) {
426 $subfield_data{marc_value} =
427 "<input type=\"text\"
428 id=\"".$subfield_data{id}."\"
430 class=\"input_marceditor\"
437 # it's a standard field
443 ( C4::Context->preference("marcflavour") eq "UNIMARC" && $tag >= 300
444 and $tag < 400 && $subfield eq 'a' )
447 && C4::Context->preference("marcflavour") eq "MARC21" )
450 $subfield_data{marc_value} =
451 "<textarea cols=\"70\"
453 id=\"".$subfield_data{id}."\"
455 class=\"input_marceditor\"
463 $subfield_data{marc_value} =
464 "<input type=\"text\"
465 id=\"".$subfield_data{id}."\"
471 class=\"input_marceditor\"
476 # $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\">";
477 push(@loop_data, \%subfield_data);
482 # what's the next op ? it's what we are not in : an add if we're editing, otherwise, and edit.
483 $template->param( title => $record->title() ) if ($record ne "-1");
484 $template->param(item_loop => \@item_value_loop,
485 item_header_loop => \@header_value_loop,
486 biblionumber => $biblionumber,
487 title => $oldrecord->{title},
488 author => $oldrecord->{author},
490 itemnumber => $itemnumber,
491 itemtagfield => $itemtagfield,
492 itemtagsubfield =>$itemtagsubfield,
494 opisadd => ($nextop eq "saveitem")?0:1);
495 foreach my $error (@errors) {
496 $template->param($error => 1);
498 output_html_with_http_headers $input, $cookie, $template->output;