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;
36 my ($tagfield,$insubfield,$record) = @_;
39 foreach my $field ($record->field($tagfield)) {
40 my @subfields = $field->subfields();
41 foreach my $subfield (@subfields) {
42 if (@$subfield[0] eq $insubfield) {
43 $result .= @$subfield[1];
44 $indicator = $field->indicator(1).$field->indicator(2);
48 return($indicator,$result);
51 my $dbh = C4::Context->dbh;
52 my $error = $input->param('error');
53 my $biblionumber = $input->param('biblionumber');
55 $biblionumber=$input->param('bibid');
57 my $biblioitemnumber = find_biblioitemnumber($dbh,$biblionumber);
58 my $itemnumber = $input->param('itemnumber');
60 $itemnumber=$input->param('itemnum');
63 my $op = $input->param('op');
66 my $itemtype = &MARCfind_frameworkcode($dbh,$biblionumber);
68 my $tagslib = &MARCgettagslib($dbh,1,$itemtype);
69 my $record = MARCgetbiblio($dbh,$biblionumber);
70 # warn "==>".$record->as_formatted;
71 my $oldrecord = MARCmarc2koha($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 = MARChtml2xml(\@tags,\@subfields,\@values,\@indicator,\@ind_tag);
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) = &MARCfind_marc_from_kohafield($dbh,"items.barcode");
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 = MARCmarc2koha($dbh,$record);
104 my $exists = get_item_from_barcode($addedolditem->{'barcode'});
105 push @errors,"barcode_not_unique" if($exists);
106 # if barcode exists, don't create, but report The problem.
107 $itemnumber = NEWnewitem($dbh,$record,$biblionumber,$biblioitemnumber) unless ($exists);
109 #------------------------------------------------------------------------------------------------------------------------------
110 } elsif ($op eq "edititem") {
111 #------------------------------------------------------------------------------------------------------------------------------
112 # retrieve item if exist => then, it's a modif
113 $itemrecord = get_record($biblionumber);
115 #------------------------------------------------------------------------------------------------------------------------------
116 } elsif ($op eq "delitem") {
117 #------------------------------------------------------------------------------------------------------------------------------
118 # retrieve item if exist => then, it's a modif
119 &NEWdelitem($dbh,$biblionumber,$itemnumber);
121 #------------------------------------------------------------------------------------------------------------------------------
122 } elsif ($op eq "saveitem") {
123 #------------------------------------------------------------------------------------------------------------------------------
125 my @tags = $input->param('tag');
126 my @subfields = $input->param('subfield');
127 my @values = $input->param('field_value');
128 # build indicator hash.
129 my @ind_tag = $input->param('ind_tag');
130 my @indicator = $input->param('indicator');
131 # my $itemnumber = $input->param('itemnumber');
132 my $xml = MARChtml2xml(\@tags,\@subfields,\@values,\@indicator,\@ind_tag);
133 my $itemrecord=MARC::Record::new_from_xml($xml, 'UTF-8');
134 # MARC::Record builded => now, record in DB
135 # warn "R: ".$record->as_formatted;
136 my ($oldbiblionumber,$oldbibnum,$oldbibitemnum) = NEWmoditem($dbh,$itemrecord,$biblionumber,$itemnumber,0);
142 #------------------------------------------------------------------------------------------------------------------------------
143 # build screen with existing items. and "new" one
144 #------------------------------------------------------------------------------------------------------------------------------
145 my ($template, $loggedinuser, $cookie)
146 = get_template_and_user({template_name => "acqui.simple/additem.tmpl",
149 authnotrequired => 0,
150 flagsrequired => {editcatalogue => 1},
155 $indicators{995}=' ';
156 # now, build existiing item list
157 my $temp = get_record($biblionumber);
158 my @fields = $temp->fields();
159 #my @fields = $record->fields();
160 my %witness; #---- stores the list of subfields used at least once, with the "meaning" of the code
162 #---- finds where items.itemnumber is stored
163 my ($itemtagfield,$itemtagsubfield) = &MARCfind_marc_from_kohafield($dbh,"items.itemnumber",$itemtype);
164 my ($branchtagfield,$branchtagsubfield) = &MARCfind_marc_from_kohafield($dbh,"items.homebranch",$itemtype);
166 foreach my $field (@fields) {
167 next if ($field->tag()<10);
168 my @subf=$field->subfields;
170 # loop through each subfield
171 for my $i (0..$#subf) {
172 next if ($tagslib->{$field->tag()}->{$subf[$i][0]}->{tab} ne 10 && ($field->tag() ne $itemtagfield && $subf[$i][0] ne $itemtagsubfield));
173 $witness{$subf[$i][0]} = $tagslib->{$field->tag()}->{$subf[$i][0]}->{lib} if ($tagslib->{$field->tag()}->{$subf[$i][0]}->{tab} eq 10);
174 $this_row{$subf[$i][0]} =$subf[$i][1] if ($tagslib->{$field->tag()}->{$subf[$i][0]}->{tab} eq 10);
175 if (($field->tag eq $branchtagfield) && ($subf[$i][$0] eq $branchtagsubfield) && C4::Context->preference("IndependantBranches")) {
177 my $userenv = C4::Context->userenv;
178 unless (($userenv->{'flags'} == 1) or (($userenv->{'branch'} eq $subf[$i][1]))){
179 $this_row{'nomod'}=1;
182 $this_row{itemnum} = $subf[$i][1] if ($field->tag() eq $itemtagfield && $subf[$i][0] eq $itemtagsubfield);
185 push(@big_array, \%this_row);
188 #fill big_row with missing datas
189 foreach my $subfield_code (keys(%witness)) {
190 for (my $i=0;$i<=$#big_array;$i++) {
191 $big_array[$i]{$subfield_code}=" " unless ($big_array[$i]{$subfield_code});
194 my ($holdingbrtagf,$holdingbrtagsubf) = &MARCfind_marc_from_kohafield($dbh,"items.holdingbranch",$itemtype);
195 @big_array = sort {$a->{$holdingbrtagsubf} cmp $b->{$holdingbrtagsubf}} @big_array;
197 # now, construct template !
199 my @header_value_loop;
200 for (my $i=0;$i<=$#big_array; $i++) {
202 foreach my $subfield_code (sort keys(%witness)) {
203 $items_data .="<td>".$big_array[$i]{$subfield_code}."</td>";
206 $row_data{item_value} = $items_data;
207 $row_data{itemnum} = $big_array[$i]->{itemnum};
208 #reporting this_row values
209 $row_data{'nomod'} = $big_array[$i]{'nomod'};
210 push(@item_value_loop,\%row_data);
212 foreach my $subfield_code (sort keys(%witness)) {
214 $header_value{header_value} = $witness{$subfield_code};
215 push(@header_value_loop, \%header_value);
221 my $authorised_values_sth = $dbh->prepare("select authorised_value,lib from authorised_values where category=? order by lib");
223 foreach my $tag (sort keys %{$tagslib}) {
224 my $previous_tag = '';
225 # loop through each subfield
226 foreach my $subfield (sort keys %{$tagslib->{$tag}}) {
227 next if subfield_is_koha_internal_p($subfield);
228 next if ($tagslib->{$tag}->{$subfield}->{'tab'} ne "10");
230 $subfield_data{tag}=$tag;
231 $subfield_data{subfield}=$subfield;
232 # $subfield_data{marc_lib}=$tagslib->{$tag}->{$subfield}->{lib};
233 $subfield_data{marc_lib}="<span id=\"error$i\">".$tagslib->{$tag}->{$subfield}->{lib}."</span>";
234 $subfield_data{mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
235 $subfield_data{repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
237 ($x,$value) = find_value($tag,$subfield,$itemrecord) if ($itemrecord);
238 #testing branch value if IndependantBranches.
239 my $test = (C4::Context->preference("IndependantBranches")) &&
240 ($tag eq $branchtagfield) && ($subfield eq $branchtagsubfield) &&
241 (C4::Context->userenv->{flags} != 1) && ($value) && ($value ne C4::Context->userenv->{branch}) ;
242 # print $input->redirect(".pl?biblionumber=$biblionumber") if ($test);
243 # search for itemcallnumber if applicable
244 if ($tagslib->{$tag}->{$subfield}->{kohafield} eq 'items.itemcallnumber' && C4::Context->preference('itemcallnumber')) {
245 my $CNtag = substr(C4::Context->preference('itemcallnumber'),0,3);
246 my $CNsubfield = substr(C4::Context->preference('itemcallnumber'),3,1);
247 my $temp = $record->field($CNtag);
249 $value = $temp->subfield($CNsubfield);
252 if ($tagslib->{$tag}->{$subfield}->{authorised_value}) {
253 my @authorised_values;
255 # builds list, depending on authorised value...
257 if ($tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) {
258 if ((C4::Context->preference("IndependantBranches")) && (C4::Context->userenv->{flags} != 1)){
259 my $sth=$dbh->prepare("select branchcode,branchname from branches where branchcode = ? order by branchname");
260 $sth->execute(C4::Context->userenv->{branch});
261 push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
262 while (my ($branchcode,$branchname) = $sth->fetchrow_array) {
263 push @authorised_values, $branchcode;
264 $authorised_lib{$branchcode}=$branchname;
267 my $sth=$dbh->prepare("select branchcode,branchname from branches order by branchname");
269 push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
270 while (my ($branchcode,$branchname) = $sth->fetchrow_array) {
271 push @authorised_values, $branchcode;
272 $authorised_lib{$branchcode}=$branchname;
276 } elsif ($tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes") {
277 my $sth=$dbh->prepare("select itemtype,description from itemtypes order by description");
279 push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
280 while (my ($itemtype,$description) = $sth->fetchrow_array) {
281 push @authorised_values, $itemtype;
282 $authorised_lib{$itemtype}=$description;
284 #---- "true" authorised value
286 $authorised_values_sth->execute($tagslib->{$tag}->{$subfield}->{authorised_value});
287 push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
288 while (my ($value,$lib) = $authorised_values_sth->fetchrow_array) {
289 push @authorised_values, $value;
290 $authorised_lib{$value}=$lib;
293 $subfield_data{marc_value}= CGI::scrolling_list(-name=>'field_value',
294 -values=> \@authorised_values,
296 -labels => \%authorised_lib,
300 } elsif ($tagslib->{$tag}->{$subfield}->{thesaurus_category}) {
301 $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>";
303 } elsif ($tagslib->{$tag}->{$subfield}->{'value_builder'}) {
304 my $plugin="../value_builder/".$tagslib->{$tag}->{$subfield}->{'value_builder'};
306 my $extended_param = plugin_parameters($dbh,$record,$tagslib,$i,0);
307 my ($function_name,$javascript) = plugin_javascript($dbh,$record,$tagslib,$i,0);
308 $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";
310 $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\" value=\"$value\" size=50 maxlength=255>";
312 # $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\">";
313 push(@loop_data, \%subfield_data);
318 # what's the next op ? it's what we are not in : an add if we're editing, otherwise, and edit.
319 $template->param(item_loop => \@item_value_loop,
320 item_header_loop => \@header_value_loop,
321 biblionumber => $biblionumber,
322 bibid => $biblionumber,
323 title => $oldrecord->{title},
324 author => $oldrecord->{author},
326 itemnumber => $itemnumber,
327 itemnum => $itemnumber,
328 itemtagfield => $itemtagfield,
329 itemtagsubfield =>$itemtagsubfield,
331 opisadd => ($nextop eq "saveitem")?0:1);
332 foreach my $error (@errors) {
333 $template->param($error => 1);
335 output_html_with_http_headers $input, $cookie, $template->output;