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');
59 my $itemtype = &MARCfind_frameworkcode($dbh,$bibid);
61 my $tagslib = &MARCgettagslib($dbh,1,$itemtype);
62 my $record = MARCgetbiblio($dbh,$bibid);
65 my @errors; # store errors found while checking data BEFORE saving item.
66 #------------------------------------------------------------------------------------------------------------------------------
67 if ($op eq "additem") {
68 #------------------------------------------------------------------------------------------------------------------------------
70 my @tags = $input->param('tag');
71 my @subfields = $input->param('subfield');
72 my @values = $input->param('field_value');
73 # build indicator hash.
74 my @ind_tag = $input->param('ind_tag');
75 my @indicator = $input->param('indicator');
77 for (my $i=0;$i<=$#ind_tag;$i++) {
78 $indicators{$ind_tag[$i]} = $indicator[$i];
80 my $record = MARChtml2marc($dbh,\@tags,\@subfields,\@values,%indicators);
81 # check for item barcode # being unique
82 my $oldbibid = MARCmarc2koha($dbh,$record);
83 my $exists = itemdata($oldbibid->{'barcode'});
84 push @errors,"barcode_not_unique" if($exists);
85 # MARC::Record builded => now, record in DB
86 # if barcode exists, don't create, but report The problem.
87 my ($oldbiblionumber,$oldbibnum,$oldbibitemnum) = NEWnewitem($dbh,$record,$bibid) unless ($exists);
90 $itemrecord = $record;
94 #------------------------------------------------------------------------------------------------------------------------------
95 } elsif ($op eq "edititem") {
96 #------------------------------------------------------------------------------------------------------------------------------
97 # retrieve item if exist => then, it's a modif
98 $itemrecord = MARCgetitem($dbh,$bibid,$itemnum);
100 #------------------------------------------------------------------------------------------------------------------------------
101 } elsif ($op eq "delitem") {
102 #------------------------------------------------------------------------------------------------------------------------------
103 # retrieve item if exist => then, it's a modif
104 &NEWdelitem($dbh,$bibid,$itemnum);
106 #------------------------------------------------------------------------------------------------------------------------------
107 } elsif ($op eq "saveitem") {
108 #------------------------------------------------------------------------------------------------------------------------------
110 my @tags = $input->param('tag');
111 my @subfields = $input->param('subfield');
112 my @values = $input->param('field_value');
113 # build indicator hash.
114 my @ind_tag = $input->param('ind_tag');
115 my @indicator = $input->param('indicator');
116 # my $itemnum = $input->param('itemnum');
118 for (my $i=0;$i<=$#ind_tag;$i++) {
119 $indicators{$ind_tag[$i]} = $indicator[$i];
121 my $record = MARChtml2marc($dbh,\@tags,\@subfields,\@values,%indicators);
122 # MARC::Record builded => now, record in DB
123 # warn "R: ".$record->as_formatted;
124 my ($oldbiblionumber,$oldbibnum,$oldbibitemnum) = NEWmoditem($dbh,$record,$bibid,$itemnum,0);
130 #------------------------------------------------------------------------------------------------------------------------------
131 # build screen with existing items. and "new" one
132 #------------------------------------------------------------------------------------------------------------------------------
135 $indicators{995}=' ';
136 # now, build existiing item list
137 my $temp = MARCgetbiblio($dbh,$bibid);
138 my @fields = $temp->fields();
139 my %witness; #---- stores the list of subfields used at least once, with the "meaning" of the code
141 #---- finds where items.itemnumber is stored
142 my ($itemtagfield,$itemtagsubfield) = &MARCfind_marc_from_kohafield($dbh,"items.itemnumber",$itemtype);
143 my @itemnums; # array to store itemnums
144 foreach my $field (@fields) {
145 next if ($field->tag()<10);
146 my @subf=$field->subfields;
148 # loop through each subfield
149 for my $i (0..$#subf) {
150 next if ($tagslib->{$field->tag()}->{$subf[$i][0]}->{tab} ne 10 && ($field->tag() ne $itemtagfield && $subf[$i][0] ne $itemtagsubfield));
151 $witness{$subf[$i][0]} = $tagslib->{$field->tag()}->{$subf[$i][0]}->{lib} if ($tagslib->{$field->tag()}->{$subf[$i][0]}->{tab} eq 10);
152 $this_row{$subf[$i][0]} =$subf[$i][1] if ($tagslib->{$field->tag()}->{$subf[$i][0]}->{tab} eq 10);
153 push @itemnums,$this_row{$subf[$i][0]} =$subf[$i][1] if ($field->tag() eq $itemtagfield && $subf[$i][0] eq $itemtagsubfield);
156 push(@big_array, \%this_row);
159 #fill big_row with missing datas
160 foreach my $subfield_code (keys(%witness)) {
161 for (my $i=0;$i<=$#big_array;$i++) {
162 $big_array[$i]{$subfield_code}=" " unless ($big_array[$i]{$subfield_code});
165 # now, construct template !
167 my @header_value_loop;
168 for (my $i=0;$i<=$#big_array; $i++) {
170 foreach my $subfield_code (sort keys(%witness)) {
171 $items_data .="<td>".$big_array[$i]{$subfield_code}."</td>";
174 $row_data{item_value} = $items_data;
175 $row_data{itemnum} = $itemnums[$i];
176 push(@item_value_loop,\%row_data);
178 foreach my $subfield_code (sort keys(%witness)) {
180 $header_value{header_value} = $witness{$subfield_code};
181 push(@header_value_loop, \%header_value);
187 my $authorised_values_sth = $dbh->prepare("select authorised_value,lib from authorised_values where category=? order by lib");
189 foreach my $tag (sort keys %{$tagslib}) {
190 my $previous_tag = '';
191 # loop through each subfield
192 foreach my $subfield (sort keys %{$tagslib->{$tag}}) {
193 next if subfield_is_koha_internal_p($subfield);
194 next if ($tagslib->{$tag}->{$subfield}->{'tab'} ne "10");
196 $subfield_data{tag}=$tag;
197 $subfield_data{subfield}=$subfield;
198 # $subfield_data{marc_lib}=$tagslib->{$tag}->{$subfield}->{lib};
199 $subfield_data{marc_lib}="<span id=\"error$i\">".$tagslib->{$tag}->{$subfield}->{lib}."</span>";
200 $subfield_data{mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
201 $subfield_data{repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
203 ($x,$value) = find_value($tag,$subfield,$itemrecord) if ($itemrecord);
204 # search for itemcallnumber if applicable
205 if ($tagslib->{$tag}->{$subfield}->{kohafield} eq 'items.itemcallnumber' && C4::Context->preference('itemcallnumber')) {
206 my $CNtag = substr(C4::Context->preference('itemcallnumber'),0,3);
207 my $CNsubfield = substr(C4::Context->preference('itemcallnumber'),3,1);
208 my $temp = $record->field($CNtag);
210 $value = $temp->subfield($CNsubfield);
213 if ($tagslib->{$tag}->{$subfield}->{authorised_value}) {
214 my @authorised_values;
216 # builds list, depending on authorised value...
218 if ($tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) {
219 my $sth=$dbh->prepare("select branchcode,branchname from branches order by branchname");
221 push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
222 while (my ($branchcode,$branchname) = $sth->fetchrow_array) {
223 push @authorised_values, $branchcode;
224 $authorised_lib{$branchcode}=$branchname;
227 } elsif ($tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes") {
228 my $sth=$dbh->prepare("select itemtype,description from itemtypes order by description");
230 push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
231 while (my ($itemtype,$description) = $sth->fetchrow_array) {
232 push @authorised_values, $itemtype;
233 $authorised_lib{$itemtype}=$description;
235 #---- "true" authorised value
237 $authorised_values_sth->execute($tagslib->{$tag}->{$subfield}->{authorised_value});
238 push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
239 while (my ($value,$lib) = $authorised_values_sth->fetchrow_array) {
240 push @authorised_values, $value;
241 $authorised_lib{$value}=$lib;
244 $subfield_data{marc_value}= CGI::scrolling_list(-name=>'field_value',
245 -values=> \@authorised_values,
247 -labels => \%authorised_lib,
251 } elsif ($tagslib->{$tag}->{$subfield}->{thesaurus_category}) {
252 $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>";
254 } elsif ($tagslib->{$tag}->{$subfield}->{'value_builder'}) {
255 my $plugin="../value_builder/".$tagslib->{$tag}->{$subfield}->{'value_builder'};
257 my $extended_param = plugin_parameters($dbh,$record,$tagslib,$i,0);
258 my ($function_name,$javascript) = plugin_javascript($dbh,$record,$tagslib,$i,0);
259 $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";
261 $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\" value=\"$value\" size=50 maxlength=255>";
263 # $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\">";
264 push(@loop_data, \%subfield_data);
268 my ($template, $loggedinuser, $cookie)
269 = get_template_and_user({template_name => "acqui.simple/additem.tmpl",
272 authnotrequired => 0,
273 flagsrequired => {editcatalogue => 1},
277 # what's the next op ? it's what we are not in : an add if we're editing, otherwise, and edit.
278 $template->param(item_loop => \@item_value_loop,
279 item_header_loop => \@header_value_loop,
281 biblionumber =>$oldbiblionumber,
284 itemtagfield => $itemtagfield,
285 itemtagsubfield =>$itemtagsubfield,
287 opisadd => ($nextop eq "saveitem")?0:1);
288 foreach my $error (@errors) {
289 $template->param($error => 1);
291 output_html_with_http_headers $input, $cookie, $template->output;