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 warn Data::Dumper::Dumper($input->param());
64 my $op = $input->param('op');
67 my $frameworkcode = &GetFrameworkCode($biblionumber);
69 my $tagslib = &GetMarcStructure(1,$frameworkcode);
70 my $record = GetMarcBiblio($biblionumber);
71 my $oldrecord = TransformMarcToKoha($dbh,$record);
74 my @errors; # store errors found while checking data BEFORE saving item.
75 #-------------------------------------------------------------------------------
76 if ($op eq "additem") {
77 #-------------------------------------------------------------------------------
79 my @tags = $input->param('tag');
80 my @subfields = $input->param('subfield');
81 my @values = $input->param('field_value');
82 # build indicator hash.
83 my @ind_tag = $input->param('ind_tag');
84 my @indicator = $input->param('indicator');
85 my $xml = TransformHtmlToXml(\@tags,\@subfields,\@values,\@indicator,\@ind_tag, 'ITEM');
86 my $record=MARC::Record::new_from_xml($xml, 'UTF-8');
87 # if autoBarcode is ON, calculate barcode...
88 if (C4::Context->preference('autoBarcode')) {
89 my ($tagfield,$tagsubfield) = &GetMarcFromKohaField("items.barcode",$frameworkcode);
90 unless ($record->field($tagfield)->subfield($tagsubfield)) {
91 my $sth_barcode = $dbh->prepare("select max(abs(barcode)) from items");
92 $sth_barcode->execute;
93 my ($newbarcode) = $sth_barcode->fetchrow;
95 # OK, we have the new barcode, now create the entry in MARC record
96 my $fieldItem = $record->field($tagfield);
97 $record->delete_field($fieldItem);
98 $fieldItem->add_subfields($tagsubfield => $newbarcode);
99 $record->insert_fields_ordered($fieldItem);
102 # check for item barcode # being unique
103 my $addedolditem = TransformMarcToKoha($dbh,$record);
104 my $exist_itemnumber = get_item_from_barcode($addedolditem->{'barcode'});
105 push @errors,"barcode_not_unique" if($exist_itemnumber);
106 # if barcode exists, don't create, but report The problem.
107 my ($oldbiblionumber,$oldbibnum,$oldbibitemnum) = AddItem($record,$biblionumber) unless ($exist_itemnumber);
108 if ($exist_itemnumber) {
110 $itemrecord = $record;
114 #-------------------------------------------------------------------------------
115 } elsif ($op eq "edititem") {
116 #-------------------------------------------------------------------------------
117 # retrieve item if exist => then, it's a modif
118 $itemrecord = GetMarcItem($biblionumber,$itemnumber);
120 #-------------------------------------------------------------------------------
121 } elsif ($op eq "delitem") {
122 #-------------------------------------------------------------------------------
123 # check that there is no issue on this item before deletion.
124 my $sth=$dbh->prepare("select * from issues i where i.returndate is null and i.itemnumber=?");
125 $sth->execute($itemnumber);
126 my $onloan=$sth->fetchrow;
127 push @errors,"book_on_loan" if ($onloan); ##error book_on_loan added to template as well
131 &DelItem($dbh,$biblionumber,$itemnumber);
132 print $input->redirect("additem.pl?biblionumber=$biblionumber&frameworkcode=$frameworkcode");
135 #-------------------------------------------------------------------------------
136 } elsif ($op eq "saveitem") {
137 #-------------------------------------------------------------------------------
139 my @tags = $input->param('tag');
140 my @subfields = $input->param('subfield');
141 my @values = $input->param('field_value');
142 # build indicator hash.
143 my @ind_tag = $input->param('ind_tag');
144 my @indicator = $input->param('indicator');
146 warn "subfields :@subfields" ;
147 warn "values :@values" ;
148 # my $itemnumber = $input->param('itemnumber');
149 my $xml = TransformHtmlToXml(\@tags,\@subfields,\@values,\@indicator,\@ind_tag,'ITEM');
150 $itemrecord=MARC::Record::new_from_xml($xml, 'UTF-8');
151 # MARC::Record builded => now, record in DB
152 # warn "R: ".$record->as_formatted;
153 # check that the barcode don't exist already
154 my $addedolditem = TransformMarcToKoha($dbh,$itemrecord);
155 my $exist_itemnumber = get_item_from_barcode($addedolditem->{'barcode'});
156 if ($exist_itemnumber && $exist_itemnumber != $itemnumber) {
157 push @errors,"barcode_not_unique";
158 # $nextop= "additem";
160 my ($oldbiblionumber,$oldbibnum,$oldbibitemnum) = ModItem($itemrecord,$biblionumber,$itemnumber,0);
167 #-------------------------------------------------------------------------------
168 # build screen with existing items. and "new" one
169 #-------------------------------------------------------------------------------
170 my ($template, $loggedinuser, $cookie)
171 = get_template_and_user({template_name => "cataloguing/additem.tmpl",
174 authnotrequired => 0,
175 flagsrequired => {editcatalogue => 1},
179 # now, build existiing item list
180 my $temp = GetMarcBiblio( $biblionumber );
181 my @fields = $temp->fields();
182 #my @fields = $record->fields();
183 my %witness; #---- stores the list of subfields used at least once, with the "meaning" of the code
185 #---- finds where items.itemnumber is stored
186 my ($itemtagfield,$itemtagsubfield) = &GetMarcFromKohaField("items.itemnumber",$frameworkcode);
187 my ($branchtagfield,$branchtagsubfield) = &GetMarcFromKohaField("items.homebranch",$frameworkcode);
189 foreach my $field (@fields) {
190 next if ($field->tag()<10);
191 my @subf=$field->subfields;
193 # loop through each subfield
194 for my $i (0..$#subf) {
195 next if ($tagslib->{$field->tag()}->{$subf[$i][0]}->{tab} ne 10
196 && ($field->tag() ne $itemtagfield
197 && $subf[$i][0] ne $itemtagsubfield));
199 $witness{$subf[$i][0]} = $tagslib->{$field->tag()}->{$subf[$i][0]}->{lib} if ($tagslib->{$field->tag()}->{$subf[$i][0]}->{tab} eq 10);
201 $this_row{$subf[$i][0]} =$subf[$i][1] if ($tagslib->{$field->tag()}->{$subf[$i][0]}->{tab} eq 10);
203 if (($field->tag eq $branchtagfield) && ($subf[$i][$0] eq $branchtagsubfield) && C4::Context->preference("IndependantBranches")) {
205 my $userenv = C4::Context->userenv();
206 unless (($userenv->{'flags'} == 1) or (($userenv->{'branch'} eq $subf[$i][1]))){
207 $this_row{'nomod'}=1;
210 $this_row{itemnumber} = $subf[$i][1] if ($field->tag() eq $itemtagfield && $subf[$i][0] eq $itemtagsubfield);
213 push(@big_array, \%this_row);
216 #fill big_row with missing data
217 foreach my $subfield_code (keys(%witness)) {
218 for (my $i=0;$i<=$#big_array;$i++) {
219 $big_array[$i]{$subfield_code}=" " unless ($big_array[$i]{$subfield_code});
222 my ($holdingbrtagf,$holdingbrtagsubf) = &GetMarcFromKohaField("items.holdingbranch",$frameworkcode);
223 @big_array = sort {$a->{$holdingbrtagsubf} cmp $b->{$holdingbrtagsubf}} @big_array;
225 # now, construct template !
226 # First, the existing items for display
228 my @header_value_loop;
229 for (my $i=0;$i<=$#big_array; $i++) {
231 foreach my $subfield_code (sort keys(%witness)) {
232 $items_data .="<td>".$big_array[$i]{$subfield_code}."</td>";
235 $items_data =~ s/"/"/g;
236 $row_data{item_value} = $items_data;
237 $row_data{itemnumber} = $big_array[$i]->{itemnumber};
238 #reporting this_row values
239 $row_data{'nomod'} = $big_array[$i]{'nomod'};
240 push(@item_value_loop,\%row_data);
242 foreach my $subfield_code (sort keys(%witness)) {
244 $header_value{header_value} = $witness{$subfield_code};
245 push(@header_value_loop, \%header_value);
248 # now, build the item form for entering a new item
251 my $authorised_values_sth = $dbh->prepare("SELECT authorised_value,lib FROM authorised_values WHERE category=? ORDER BY lib");
253 foreach my $tag (sort keys %{$tagslib}) {
254 my $previous_tag = '';
255 # loop through each subfield
256 foreach my $subfield (sort keys %{$tagslib->{$tag}}) {
257 next if subfield_is_koha_internal_p($subfield);
258 next if ($tagslib->{$tag}->{$subfield}->{'tab'} ne "10");
261 my $index_subfield= int(rand(1000000));
262 if($subfield eq '@'){
263 $subfield_data{id} = "tag_".$tag."_subfield_0_".$index_subfield;
265 $subfield_data{id} = "tag_".$tag."_subfield_".$subfield."_".$index_subfield;
267 $subfield_data{tag}=$tag;
268 $subfield_data{subfield}=$subfield;
269 $subfield_data{random}=int(rand(1000000));
270 # $subfield_data{marc_lib}=$tagslib->{$tag}->{$subfield}->{lib};
271 $subfield_data{marc_lib}="<span id=\"error$i\" title=\"".$tagslib->{$tag}->{$subfield}->{lib}."\">".$tagslib->{$tag}->{$subfield}->{lib}."</span>";
272 $subfield_data{mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
273 $subfield_data{repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
275 ($x,$value) = find_value($tag,$subfield,$itemrecord) if ($itemrecord);
276 $value =~ s/"/"/g;
278 $value = $tagslib->{$tag}->{$subfield}->{defaultvalue};
280 # get today date & replace YYYY, MM, DD if provided in the default value
281 my ( $year, $month, $day ) = Today();
282 $month = sprintf( "%02d", $month );
283 $day = sprintf( "%02d", $day );
284 $value =~ s/YYYY/$year/g;
285 $value =~ s/MM/$month/g;
286 $value =~ s/DD/$day/g;
288 $subfield_data{visibility} = "display:none;" if (($tagslib->{$tag}->{$subfield}->{hidden} % 2 == 1) and $value ne '');
289 #testing branch value if IndependantBranches.
290 my $test = (C4::Context->preference("IndependantBranches")) &&
291 ($tag eq $branchtagfield) && ($subfield eq $branchtagsubfield) &&
292 (C4::Context->userenv->{flags} != 1) && ($value) && ($value ne C4::Context->userenv->{branch}) ;
293 # print $input->redirect(".pl?biblionumber=$biblionumber") if ($test);
294 # search for itemcallnumber if applicable
295 if (!$value && $tagslib->{$tag}->{$subfield}->{kohafield} eq 'items.itemcallnumber' && C4::Context->preference('itemcallnumber')) {
296 my $CNtag = substr(C4::Context->preference('itemcallnumber'),0,3);
297 my $CNsubfield = substr(C4::Context->preference('itemcallnumber'),3,1);
298 my $CNsubfield2 = substr(C4::Context->preference('itemcallnumber'),4,1);
299 my $temp2 = $temp->field($CNtag);
301 $value = ($temp2->subfield($CNsubfield)).' '.($temp2->subfield($CNsubfield2));
302 #remove any trailing space incase one subfield is used
303 $value=~s/^\s+|\s+$//g;
306 if ( $tagslib->{$tag}->{$subfield}->{authorised_value} ) {
307 my @authorised_values;
309 my $dbh=C4::Context->dbh;
311 # builds list, depending on authorised value...
314 if ( $tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) {
317 "select branchcode,branchname from branches order by branchname");
319 push @authorised_values, ""
320 unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
322 while ( my ( $branchcode, $branchname ) = $sth->fetchrow_array ) {
323 push @authorised_values, $branchcode;
324 $authorised_lib{$branchcode} = $branchname;
329 elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes" ) {
332 "select itemtype,description from itemtypes order by description");
334 push @authorised_values, ""
335 unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
339 while ( my ( $itemtype, $description ) = $sth->fetchrow_array ) {
340 push @authorised_values, $itemtype;
341 $authorised_lib{$itemtype} = $description;
343 $value = $itemtype unless ($value);
345 #---- "true" authorised value
348 $authorised_values_sth->execute(
349 $tagslib->{$tag}->{$subfield}->{authorised_value} );
351 push @authorised_values, ""
352 unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
354 while ( my ( $value, $lib ) = $authorised_values_sth->fetchrow_array ) {
355 push @authorised_values, $value;
356 $authorised_lib{$value} = $lib;
359 $subfield_data{marc_value} =CGI::scrolling_list(
360 -name => "field_value",
361 -values => \@authorised_values,
363 -labels => \%authorised_lib,
368 -id => "tag_".$tag."_subfield_".$subfield."_".$index_subfield,
369 -class => "input_marceditor",
371 # it's a thesaurus / authority field
373 elsif ( $tagslib->{$tag}->{$subfield}->{authtypecode} ) {
374 $subfield_data{marc_value} =
375 "<input type=\"text\"
376 id=\"".$subfield_data{id}."\"
379 class=\"input_marceditor\"
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 # opening plugin. Just check wether we are on a developper computer on a production one
392 # (the cgidir differs)
393 my $cgidir = C4::Context->intranetdir . "/cgi-bin/cataloguing/value_builder";
394 unless ( opendir( DIR, "$cgidir" ) ) {
395 $cgidir = C4::Context->intranetdir . "/cataloguing/value_builder";
397 my $plugin = $cgidir . "/" . $tagslib->{$tag}->{$subfield}->{'value_builder'};
398 do $plugin || die "Plugin Failed: ".$plugin;
399 my $extended_param = plugin_parameters( $dbh, $temp, $tagslib, $subfield_data{id}, \@loop_data );
400 my ( $function_name, $javascript ) = plugin_javascript( $dbh, $temp, $tagslib, $subfield_data{id}, \@loop_data );
401 # my ( $function_name, $javascript,$extended_param );
403 $subfield_data{marc_value} =
404 "<input tabindex=\"1\"
406 id=\"".$subfield_data{id}."\"
409 class=\"input_marceditor\"
410 onfocus=\"Focus$function_name(".$subfield_data{random}.")\"
413 onblur=\"Blur$function_name(".$subfield_data{random}."); \" \/>
414 <a href=\"#\" class=\"buttonDot\" onclick=\"Clic$function_name('$subfield_data{id}'; return false;)\" title=\"Tag Editor\">...</a>
416 # it's an hidden field
418 elsif ( $tag eq '' ) {
419 $subfield_data{marc_value} =
420 "<input tabindex=\"1\"
422 id=\"".$subfield_data{id}."\"
429 elsif ( $tagslib->{$tag}->{$subfield}->{'hidden'} ) {
430 $subfield_data{marc_value} =
431 "<input type=\"text\"
432 id=\"".$subfield_data{id}."\"
434 class=\"input_marceditor\"
441 # it's a standard field
447 ( C4::Context->preference("marcflavour") eq "UNIMARC" && $tag >= 300
448 and $tag < 400 && $subfield eq 'a' )
451 && C4::Context->preference("marcflavour") eq "MARC21" )
454 $subfield_data{marc_value} =
455 "<textarea cols=\"70\"
457 id=\"".$subfield_data{id}."\"
459 class=\"input_marceditor\"
467 $subfield_data{marc_value} =
468 "<input type=\"text\"
469 id=\"".$subfield_data{id}."\"
475 class=\"input_marceditor\"
480 # $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\">";
481 push(@loop_data, \%subfield_data);
486 # what's the next op ? it's what we are not in : an add if we're editing, otherwise, and edit.
487 $template->param( title => $record->title() ) if ($record ne "-1");
488 $template->param(item_loop => \@item_value_loop,
489 item_header_loop => \@header_value_loop,
490 biblionumber => $biblionumber,
491 title => $oldrecord->{title},
492 author => $oldrecord->{author},
494 itemnumber => $itemnumber,
495 itemtagfield => $itemtagfield,
496 itemtagsubfield =>$itemtagsubfield,
498 opisadd => ($nextop eq "saveitem")?0:1);
499 foreach my $error (@errors) {
500 $template->param($error => 1);
502 output_html_with_http_headers $input, $cookie, $template->output;