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;
91 #------------------------------------------------------------------------------------------------------------------------------
92 } elsif ($op eq "edititem") {
93 #------------------------------------------------------------------------------------------------------------------------------
94 # retrieve item if exist => then, it's a modif
95 $itemrecord = MARCgetitem($dbh,$bibid,$itemnum);
97 #------------------------------------------------------------------------------------------------------------------------------
98 } elsif ($op eq "delitem") {
99 #------------------------------------------------------------------------------------------------------------------------------
100 # retrieve item if exist => then, it's a modif
101 &NEWdelitem($dbh,$bibid,$itemnum);
103 #------------------------------------------------------------------------------------------------------------------------------
104 } elsif ($op eq "saveitem") {
105 #------------------------------------------------------------------------------------------------------------------------------
107 my @tags = $input->param('tag');
108 my @subfields = $input->param('subfield');
109 my @values = $input->param('field_value');
110 # build indicator hash.
111 my @ind_tag = $input->param('ind_tag');
112 my @indicator = $input->param('indicator');
113 # my $itemnum = $input->param('itemnum');
115 for (my $i=0;$i<=$#ind_tag;$i++) {
116 $indicators{$ind_tag[$i]} = $indicator[$i];
118 my $record = MARChtml2marc($dbh,\@tags,\@subfields,\@values,%indicators);
119 # MARC::Record builded => now, record in DB
120 my ($oldbiblionumber,$oldbibnum,$oldbibitemnum) = NEWmoditem($dbh,$record,$bibid,$itemnum,0);
126 #------------------------------------------------------------------------------------------------------------------------------
127 # build screen with existing items. and "new" one
128 #------------------------------------------------------------------------------------------------------------------------------
131 $indicators{995}=' ';
132 # now, build existiing item list
133 my $temp = MARCgetbiblio($dbh,$bibid);
134 my @fields = $temp->fields();
135 my %witness; #---- stores the list of subfields used at least once, with the "meaning" of the code
137 #---- finds where items.itemnumber is stored
138 my ($itemtagfield,$itemtagsubfield) = &MARCfind_marc_from_kohafield($dbh,"items.itemnumber");
139 my @itemnums; # array to store itemnums
140 foreach my $field (@fields) {
141 next if ($field->tag()<10);
142 my @subf=$field->subfields;
144 # loop through each subfield
145 for my $i (0..$#subf) {
146 next if ($tagslib->{$field->tag()}->{$subf[$i][0]}->{tab} ne 10 && ($field->tag() ne $itemtagfield && $subf[$i][0] ne $itemtagsubfield));
147 $witness{$subf[$i][0]} = $tagslib->{$field->tag()}->{$subf[$i][0]}->{lib} if ($tagslib->{$field->tag()}->{$subf[$i][0]}->{tab} eq 10);
148 $this_row{$subf[$i][0]} =$subf[$i][1] if ($tagslib->{$field->tag()}->{$subf[$i][0]}->{tab} eq 10);
149 push @itemnums,$this_row{$subf[$i][0]} =$subf[$i][1] if ($field->tag() eq $itemtagfield && $subf[$i][0] eq $itemtagsubfield);
152 push(@big_array, \%this_row);
155 #fill big_row with missing datas
156 foreach my $subfield_code (keys(%witness)) {
157 for (my $i=0;$i<=$#big_array;$i++) {
158 $big_array[$i]{$subfield_code}=" " unless ($big_array[$i]{$subfield_code});
161 # now, construct template !
163 my @header_value_loop;
164 for (my $i=0;$i<=$#big_array; $i++) {
166 foreach my $subfield_code (sort keys(%witness)) {
167 $items_data .="<td>".$big_array[$i]{$subfield_code}."</td>";
170 $row_data{item_value} = $items_data;
171 $row_data{itemnum} = $itemnums[$i];
172 push(@item_value_loop,\%row_data);
174 foreach my $subfield_code (sort keys(%witness)) {
176 $header_value{header_value} = $witness{$subfield_code};
177 push(@header_value_loop, \%header_value);
183 my $authorised_values_sth = $dbh->prepare("select authorised_value,lib from authorised_values where category=? order by lib");
185 foreach my $tag (sort keys %{$tagslib}) {
186 my $previous_tag = '';
187 # loop through each subfield
188 foreach my $subfield (sort keys %{$tagslib->{$tag}}) {
189 next if subfield_is_koha_internal_p($subfield);
190 next if ($tagslib->{$tag}->{$subfield}->{'tab'} ne "10");
192 $subfield_data{tag}=$tag;
193 $subfield_data{subfield}=$subfield;
194 # $subfield_data{marc_lib}=$tagslib->{$tag}->{$subfield}->{lib};
195 $subfield_data{marc_lib}="<DIV id=\"error$i\">".$tagslib->{$tag}->{$subfield}->{lib}."</div>";
196 $subfield_data{mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
197 $subfield_data{repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
199 ($x,$value) = find_value($tag,$subfield,$itemrecord) if ($itemrecord);
200 if ($tagslib->{$tag}->{$subfield}->{authorised_value}) {
201 my @authorised_values;
203 # builds list, depending on authorised value...
205 if ($tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) {
206 my $sth=$dbh->prepare("select branchcode,branchname from branches order by branchname");
208 push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
209 while (my ($branchcode,$branchname) = $sth->fetchrow_array) {
210 push @authorised_values, $branchcode;
211 $authorised_lib{$branchcode}=$branchname;
214 } elsif ($tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes") {
215 my $sth=$dbh->prepare("select itemtype,description from itemtypes order by description");
217 push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
218 while (my ($itemtype,$description) = $sth->fetchrow_array) {
219 push @authorised_values, $itemtype;
220 $authorised_lib{$itemtype}=$description;
222 #---- "true" authorised value
224 $authorised_values_sth->execute($tagslib->{$tag}->{$subfield}->{authorised_value});
225 push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
226 while (my ($value,$lib) = $authorised_values_sth->fetchrow_array) {
227 push @authorised_values, $value;
228 $authorised_lib{$value}=$lib;
231 $subfield_data{marc_value}= CGI::scrolling_list(-name=>'field_value',
232 -values=> \@authorised_values,
234 -labels => \%authorised_lib,
238 } elsif ($tagslib->{$tag}->{$subfield}->{thesaurus_category}) {
239 $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>";
240 } elsif ($tagslib->{$tag}->{$subfield}->{'value_builder'}) {
241 my $plugin="../value_builder/".$tagslib->{$tag}->{$subfield}->{'value_builder'};
243 my $extended_param = plugin_parameters($dbh,$record,$tagslib,$i,0);
244 my ($function_name,$javascript) = plugin_javascript($dbh,$record,$tagslib,$i,0);
245 $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";
247 $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\" value=\"$value\" size=50 maxlength=255>";
249 # $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\">";
250 push(@loop_data, \%subfield_data);
254 my ($template, $loggedinuser, $cookie)
255 = get_template_and_user({template_name => "acqui.simple/additem.tmpl",
258 authnotrequired => 0,
259 flagsrequired => {parameters => 1},
263 # what's the next op ? it's what we are not in : an add if we're editing, otherwise, and edit.
264 $template->param(item_loop => \@item_value_loop,
265 item_header_loop => \@header_value_loop,
267 biblionumber =>$oldbiblionumber,
270 itemtagfield => $itemtagfield,
271 itemtagsubfield =>$itemtagsubfield,
273 opisadd => ($nextop eq "saveitem")?0:1);
274 foreach my $error (@errors) {
275 $template->param($error => 1);
277 output_html_with_http_headers $input, $cookie, $template->output;