5 # Copyright 2000-2002 Katipo Communications
7 # This file is part of Koha.
9 # Koha is free software; you can redistribute it and/or modify it under the
10 # terms of the GNU General Public License as published by the Free Software
11 # Foundation; either version 2 of the License, or (at your option) any later
14 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License along with
19 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
20 # Suite 330, Boston, MA 02111-1307 USA
26 use C4::Interface::CGI::Output;
29 use C4::Koha; # XXX subfield_is_koha_internal_p
32 use MARC::File::USMARC;
35 my ($tagfield,$insubfield,$record) = @_;
38 foreach my $field ($record->field($tagfield)) {
39 my @subfields = $field->subfields();
40 foreach my $subfield (@subfields) {
41 if (@$subfield[0] eq $insubfield) {
42 $result .= @$subfield[1];
43 $indicator = $field->indicator(1).$field->indicator(2);
47 return($indicator,$result);
50 my $dbh = C4::Context->dbh;
51 my $error = $input->param('error');
52 my $bibid = $input->param('bibid');
53 my $oldbiblionumber = &MARCfind_oldbiblionumber_from_MARCbibid($dbh,$bibid);
55 my $op = $input->param('op');
56 my $itemnum = $input->param('itemnum');
58 my $tagslib = &MARCgettagslib($dbh,1);
59 my $record = MARCgetbiblio($dbh,$bibid);
62 my @errors; # store errors found while checking data BEFORE saving item.
63 #------------------------------------------------------------------------------------------------------------------------------
64 if ($op eq "additem") {
65 #------------------------------------------------------------------------------------------------------------------------------
67 my @tags = $input->param('tag');
68 my @subfields = $input->param('subfield');
69 my @values = $input->param('field_value');
70 # build indicator hash.
71 my @ind_tag = $input->param('ind_tag');
72 my @indicator = $input->param('indicator');
74 for (my $i=0;$i<=$#ind_tag;$i++) {
75 $indicators{$ind_tag[$i]} = $indicator[$i];
77 my $record = MARChtml2marc($dbh,\@tags,\@subfields,\@values,%indicators);
78 # check for item barcode # being unique
79 my $oldbibid = MARCmarc2koha($dbh,$record);
80 my $exists = itemdata($oldbibid->{'barcode'});
81 push @errors,"barcode_not_unique" if($exists);
82 # MARC::Record builded => now, record in DB
83 # if barcode exists, don't create, but report The problem.
84 my ($oldbiblionumber,$oldbibnum,$oldbibitemnum) = NEWnewitem($dbh,$record,$bibid) unless ($exists);
87 $itemrecord = $record;
88 warn "==>".$record->as_formatted;
92 #------------------------------------------------------------------------------------------------------------------------------
93 } elsif ($op eq "edititem") {
94 #------------------------------------------------------------------------------------------------------------------------------
95 # retrieve item if exist => then, it's a modif
96 $itemrecord = MARCgetitem($dbh,$bibid,$itemnum);
98 #------------------------------------------------------------------------------------------------------------------------------
99 } elsif ($op eq "saveitem") {
100 #------------------------------------------------------------------------------------------------------------------------------
102 my @tags = $input->param('tag');
103 my @subfields = $input->param('subfield');
104 my @values = $input->param('field_value');
105 # build indicator hash.
106 my @ind_tag = $input->param('ind_tag');
107 my @indicator = $input->param('indicator');
108 # my $itemnum = $input->param('itemnum');
110 for (my $i=0;$i<=$#ind_tag;$i++) {
111 $indicators{$ind_tag[$i]} = $indicator[$i];
113 my $record = MARChtml2marc($dbh,\@tags,\@subfields,\@values,%indicators);
114 # MARC::Record builded => now, record in DB
115 my ($oldbiblionumber,$oldbibnum,$oldbibitemnum) = NEWmoditem($dbh,$record,$bibid,$itemnum,0);
121 #------------------------------------------------------------------------------------------------------------------------------
122 # build screen with existing items. and "new" one
123 #------------------------------------------------------------------------------------------------------------------------------
126 $indicators{995}=' ';
127 # now, build existiing item list
128 my $temp = MARCgetbiblio($dbh,$bibid);
129 my @fields = $temp->fields();
130 my %witness; #---- stores the list of subfields used at least once, with the "meaning" of the code
132 #---- finds where items.itemnumber is stored
133 my ($itemtagfield,$itemtagsubfield) = &MARCfind_marc_from_kohafield($dbh,"items.itemnumber");
134 my @itemnums; # array to store itemnums
135 foreach my $field (@fields) {
136 next if ($field->tag()<10);
137 my @subf=$field->subfields;
139 # loop through each subfield
140 for my $i (0..$#subf) {
141 next if ($tagslib->{$field->tag()}->{$subf[$i][0]}->{tab} ne 10 && ($field->tag() ne $itemtagfield && $subf[$i][0] ne $itemtagsubfield));
142 $witness{$subf[$i][0]} = $tagslib->{$field->tag()}->{$subf[$i][0]}->{lib} if ($tagslib->{$field->tag()}->{$subf[$i][0]}->{tab} eq 10);
143 $this_row{$subf[$i][0]} =$subf[$i][1] if ($tagslib->{$field->tag()}->{$subf[$i][0]}->{tab} eq 10);
144 push @itemnums,$this_row{$subf[$i][0]} =$subf[$i][1] if ($field->tag() eq $itemtagfield && $subf[$i][0] eq $itemtagsubfield);
147 push(@big_array, \%this_row);
150 #fill big_row with missing datas
151 foreach my $subfield_code (keys(%witness)) {
152 for (my $i=0;$i<=$#big_array;$i++) {
153 $big_array[$i]{$subfield_code}=" " unless ($big_array[$i]{$subfield_code});
156 # now, construct template !
158 my @header_value_loop;
159 for (my $i=0;$i<=$#big_array; $i++) {
161 foreach my $subfield_code (sort keys(%witness)) {
162 $items_data .="<td>".$big_array[$i]{$subfield_code}."</td>";
165 $row_data{item_value} = $items_data;
166 $row_data{itemnum} = $itemnums[$i];
167 push(@item_value_loop,\%row_data);
169 foreach my $subfield_code (sort keys(%witness)) {
171 $header_value{header_value} = $witness{$subfield_code};
172 push(@header_value_loop, \%header_value);
178 my $authorised_values_sth = $dbh->prepare("select authorised_value,lib from authorised_values where category=? order by authorised_value");
180 foreach my $tag (sort keys %{$tagslib}) {
181 my $previous_tag = '';
182 # loop through each subfield
183 foreach my $subfield (sort keys %{$tagslib->{$tag}}) {
184 next if subfield_is_koha_internal_p($subfield);
185 next if ($tagslib->{$tag}->{$subfield}->{'tab'} ne "10");
187 $subfield_data{tag}=$tag;
188 $subfield_data{subfield}=$subfield;
189 # $subfield_data{marc_lib}=$tagslib->{$tag}->{$subfield}->{lib};
190 $subfield_data{marc_lib}="<DIV id=\"error$i\">".$tagslib->{$tag}->{$subfield}->{lib}."</div>";
191 $subfield_data{mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
192 $subfield_data{repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
194 ($x,$value) = find_value($tag,$subfield,$itemrecord) if ($itemrecord);
195 if ($tagslib->{$tag}->{$subfield}->{authorised_value}) {
196 my @authorised_values;
198 # builds list, depending on authorised value...
200 if ($tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) {
201 my $sth=$dbh->prepare("select branchcode,branchname from branches");
203 push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
204 while (my ($branchcode,$branchname) = $sth->fetchrow_array) {
205 push @authorised_values, $branchcode;
206 $authorised_lib{$branchcode}=$branchname;
209 } elsif ($tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes") {
210 my $sth=$dbh->prepare("select itemtype,description from itemtypes");
212 push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
213 while (my ($itemtype,$description) = $sth->fetchrow_array) {
214 push @authorised_values, $itemtype;
215 $authorised_lib{$itemtype}=$description;
217 #---- "true" authorised value
219 $authorised_values_sth->execute($tagslib->{$tag}->{$subfield}->{authorised_value});
220 push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
221 while (my ($value,$lib) = $authorised_values_sth->fetchrow_array) {
222 push @authorised_values, $value;
223 $authorised_lib{$value}=$lib;
226 $subfield_data{marc_value}= CGI::scrolling_list(-name=>'field_value',
227 -values=> \@authorised_values,
229 -labels => \%authorised_lib,
233 } elsif ($tagslib->{$tag}->{$subfield}->{thesaurus_category}) {
234 $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\" size=47 maxlength=255> <a href=\"javascript:Dopop('../thesaurus_popup.pl?category=$tagslib->{$tag}->{$subfield}->{thesaurus_category}&index=$i',$i)\">...</a>";
235 } elsif ($tagslib->{$tag}->{$subfield}->{'value_builder'}) {
236 my $plugin="../value_builder/".$tagslib->{$tag}->{$subfield}->{'value_builder'};
238 my $extended_param = plugin_parameters($dbh,$record,$tagslib,$i,0);
239 my ($function_name,$javascript) = plugin_javascript($dbh,$record,$tagslib,$i,0);
240 $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\" size=47 maxlength=255 DISABLE READONLY OnFocus=\"javascript:Focus$function_name($i)\" OnBlur=\"javascript:Blur$function_name($i)\"> <a href=\"javascript:Clic$function_name($i)\">...</a> $javascript";
242 $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\" value=\"$value\" size=50 maxlength=255>";
244 # $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\">";
245 push(@loop_data, \%subfield_data);
249 my ($template, $loggedinuser, $cookie)
250 = get_template_and_user({template_name => "acqui.simple/additem.tmpl",
253 authnotrequired => 0,
254 flagsrequired => {parameters => 1},
258 # what's the next op ? it's what we are not in : an add if we're editing, otherwise, and edit.
259 $template->param(item_loop => \@item_value_loop,
260 item_header_loop => \@header_value_loop,
262 biblionumber =>$oldbiblionumber,
265 itemtagfield => $itemtagfield,
266 itemtagsubfield =>$itemtagsubfield,
268 opisadd => ($nextop eq "saveitem")?0:1);
269 foreach my $error (@errors) {
270 $template->param($error => 1);
272 output_html_with_http_headers $input, $cookie, $template->output;