re-adding oldbiblionumber for the sake of the plugin
[koha.git] / cataloguing / additem.pl
1 #!/usr/bin/perl
2
3 # $Id$
4
5 # Copyright 2000-2002 Katipo Communications
6 #
7 # This file is part of Koha.
8 #
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
12 # version.
13 #
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.
17 #
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
21
22 use CGI;
23 use strict;
24 use C4::Auth;
25 use C4::Output;
26 use C4::Interface::CGI::Output;
27 use C4::Biblio;
28 use C4::Context;
29 use C4::Koha; # XXX subfield_is_koha_internal_p
30 use C4::Search;
31 use HTML::Template;
32 use MARC::File::USMARC;
33
34 sub find_value {
35         my ($tagfield,$insubfield,$record) = @_;
36         my $result;
37         my $indicator;
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);
44                         }
45                 }
46         }
47         return($indicator,$result);
48 }
49 my $input = new CGI;
50 my $dbh = C4::Context->dbh;
51 my $error = $input->param('error');
52 my $biblionumber = $input->param('biblionumber');
53 if (!$biblionumber){
54     $biblionumber=$input->param('bibid');
55 }
56 my $biblioitemnumber = find_biblioitemnumber($dbh,$biblionumber);
57 my $itemnumber = $input->param('itemnumber');
58 my $op = $input->param('op');
59
60 # find itemtype
61 my $itemtype = &MARCfind_frameworkcode($dbh,$biblionumber);
62
63 my $tagslib = &MARCgettagslib($dbh,1,$itemtype);
64 my $record = MARCgetbiblio($dbh,$biblionumber);
65 # warn "==>".$record->as_formatted;
66 my $oldrecord = MARCmarc2koha($dbh,$record);
67 my $itemrecord;
68 my $nextop="additem";
69 my @errors; # store errors found while checking data BEFORE saving item.
70 #------------------------------------------------------------------------------------------------------------------------------
71 if ($op eq "additem") {
72 #------------------------------------------------------------------------------------------------------------------------------
73         # rebuild
74         my @tags = $input->param('tag');
75         my @subfields = $input->param('subfield');
76         my @values = $input->param('field_value');
77         # build indicator hash.
78         my @ind_tag = $input->param('ind_tag');
79         my @indicator = $input->param('indicator');
80         my $xml = MARChtml2xml(\@tags,\@subfields,\@values,\@indicator,\@ind_tag);
81         my $record=MARC::Record::new_from_xml($xml, 'UTF-8');
82         # if autoBarcode is ON, calculate barcode...
83         if (C4::Context->preference('autoBarcode')) {
84                 my ($tagfield,$tagsubfield) = &MARCfind_marc_from_kohafield($dbh,"items.barcode");
85                 unless ($record->field($tagfield)->subfield($tagsubfield)) {
86                         my $sth_barcode = $dbh->prepare("select max(abs(barcode)) from items");
87                         $sth_barcode->execute;
88                         my ($newbarcode) = $sth_barcode->fetchrow;
89                         $newbarcode++;
90                         # OK, we have the new barcode, now create the entry in MARC record
91                         my $fieldItem = $record->field($tagfield);
92                         $record->delete_field($fieldItem);
93                         $fieldItem->add_subfields($tagsubfield => $newbarcode);
94                         $record->insert_fields_ordered($fieldItem);
95                 }
96         }
97 # check for item barcode # being unique
98         my $addedolditem = MARCmarc2koha($dbh,$record);
99         my $exists = get_item_from_barcode($addedolditem->{'barcode'});
100         push @errors,"barcode_not_unique" if($exists);
101         # if barcode exists, don't create, but report The problem.
102         $itemnumber = NEWnewitem($dbh,$record,$biblionumber,$biblioitemnumber) unless ($exists);
103         $nextop = "additem";
104 #------------------------------------------------------------------------------------------------------------------------------
105 } elsif ($op eq "edititem") {
106 #------------------------------------------------------------------------------------------------------------------------------
107 # retrieve item if exist => then, it's a modif
108         $itemrecord = MARCgetitem($dbh,$biblionumber,$itemnumber);
109         $nextop="saveitem";
110 #------------------------------------------------------------------------------------------------------------------------------
111 } elsif ($op eq "delitem") {
112 #------------------------------------------------------------------------------------------------------------------------------
113 # retrieve item if exist => then, it's a modif
114         &NEWdelitem($dbh,$biblionumber,$itemnumber);
115         $nextop="additem";
116 #------------------------------------------------------------------------------------------------------------------------------
117 } elsif ($op eq "saveitem") {
118 #------------------------------------------------------------------------------------------------------------------------------
119         # rebuild
120         my @tags = $input->param('tag');
121         my @subfields = $input->param('subfield');
122         my @values = $input->param('field_value');
123         # build indicator hash.
124         my @ind_tag = $input->param('ind_tag');
125         my @indicator = $input->param('indicator');
126 #       my $itemnumber = $input->param('itemnumber');
127         my $xml = MARChtml2xml(\@tags,\@subfields,\@values,\@indicator,\@ind_tag);
128         my $itemrecord=MARC::Record::new_from_xml($xml, 'UTF-8');
129 # MARC::Record builded => now, record in DB
130 # warn "R: ".$record->as_formatted;
131         my ($oldbiblionumber,$oldbibnum,$oldbibitemnum) = NEWmoditem($dbh,$record,$biblionumber,$itemnumber,0);
132         $itemnumber="";
133         $nextop="additem";
134 }
135
136 #
137 #------------------------------------------------------------------------------------------------------------------------------
138 # build screen with existing items. and "new" one
139 #------------------------------------------------------------------------------------------------------------------------------
140 my ($template, $loggedinuser, $cookie)
141     = get_template_and_user({template_name => "acqui.simple/additem.tmpl",
142                              query => $input,
143                              type => "intranet",
144                              authnotrequired => 0,
145                              flagsrequired => {editcatalogue => 1},
146                              debug => 1,
147                              });
148
149 my %indicators;
150 $indicators{995}='  ';
151 # now, build existiing item list
152 my $temp = MARCgetbiblio($dbh,$biblionumber);
153 my @fields = $temp->fields();
154 #my @fields = $record->fields();
155 my %witness; #---- stores the list of subfields used at least once, with the "meaning" of the code
156 my @big_array;
157 #---- finds where items.itemnumber is stored
158 my ($itemtagfield,$itemtagsubfield) = &MARCfind_marc_from_kohafield($dbh,"items.itemnumber",$itemtype);
159 my ($branchtagfield,$branchtagsubfield) = &MARCfind_marc_from_kohafield($dbh,"items.homebranch",$itemtype);
160
161 foreach my $field (@fields) {
162         next if ($field->tag()<10);
163         my @subf=$field->subfields;
164         my %this_row;
165 # loop through each subfield
166         for my $i (0..$#subf) {
167                 next if ($tagslib->{$field->tag()}->{$subf[$i][0]}->{tab}  ne 10 && ($field->tag() ne $itemtagfield && $subf[$i][0] ne $itemtagsubfield));
168                 $witness{$subf[$i][0]} = $tagslib->{$field->tag()}->{$subf[$i][0]}->{lib} if ($tagslib->{$field->tag()}->{$subf[$i][0]}->{tab}  eq 10);
169                 $this_row{$subf[$i][0]} =$subf[$i][1] if ($tagslib->{$field->tag()}->{$subf[$i][0]}->{tab}  eq 10);
170                 if (($field->tag eq $branchtagfield) && ($subf[$i][$0] eq $branchtagsubfield) && C4::Context->preference("IndependantBranches")) {
171                         #verifying rights
172                         my $userenv = C4::Context->userenv;
173                         unless (($userenv->{'flags'} == 1) or (($userenv->{'branch'} eq $subf[$i][1]))){
174                                         $this_row{'nomod'}=1;
175                         }
176                 }
177                 $this_row{itemnum} = $subf[$i][1] if ($field->tag() eq $itemtagfield && $subf[$i][0] eq $itemtagsubfield);
178         }
179         if (%this_row) {
180                 push(@big_array, \%this_row);
181         }
182 }
183 #fill big_row with missing datas
184 foreach my $subfield_code  (keys(%witness)) {
185         for (my $i=0;$i<=$#big_array;$i++) {
186                 $big_array[$i]{$subfield_code}="&nbsp;" unless ($big_array[$i]{$subfield_code});
187         }
188 }
189 my ($holdingbrtagf,$holdingbrtagsubf) = &MARCfind_marc_from_kohafield($dbh,"items.holdingbranch",$itemtype);
190 @big_array = sort {$a->{$holdingbrtagsubf} cmp $b->{$holdingbrtagsubf}} @big_array;
191
192 # now, construct template !
193 my @item_value_loop;
194 my @header_value_loop;
195 for (my $i=0;$i<=$#big_array; $i++) {
196         my $items_data;
197         foreach my $subfield_code (sort keys(%witness)) {
198                 $items_data .="<td>".$big_array[$i]{$subfield_code}."</td>";
199         }
200         my %row_data;
201         $row_data{item_value} = $items_data;
202         $row_data{itemnum} = $big_array[$i]->{itemnum};
203         #reporting this_row values
204         $row_data{'nomod'} = $big_array[$i]{'nomod'};
205         push(@item_value_loop,\%row_data);
206 }
207 foreach my $subfield_code (sort keys(%witness)) {
208         my %header_value;
209         $header_value{header_value} = $witness{$subfield_code};
210         push(@header_value_loop, \%header_value);
211 }
212
213 # next item form
214 my @loop_data =();
215 my $i=0;
216 my $authorised_values_sth = $dbh->prepare("select authorised_value,lib from authorised_values where category=? order by lib");
217
218 foreach my $tag (sort keys %{$tagslib}) {
219         my $previous_tag = '';
220 # loop through each subfield
221         foreach my $subfield (sort keys %{$tagslib->{$tag}}) {
222                 next if subfield_is_koha_internal_p($subfield);
223                 next if ($tagslib->{$tag}->{$subfield}->{'tab'}  ne "10");
224                 my %subfield_data;
225                 $subfield_data{tag}=$tag;
226                 $subfield_data{subfield}=$subfield;
227 #               $subfield_data{marc_lib}=$tagslib->{$tag}->{$subfield}->{lib};
228                 $subfield_data{marc_lib}="<span id=\"error$i\">".$tagslib->{$tag}->{$subfield}->{lib}."</span>";
229                 $subfield_data{mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
230                 $subfield_data{repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
231                 my ($x,$value);
232                 ($x,$value) = find_value($tag,$subfield,$itemrecord) if ($itemrecord);
233                 #testing branch value if IndependantBranches.
234                 my $test = (C4::Context->preference("IndependantBranches")) && 
235                                         ($tag eq $branchtagfield) && ($subfield eq $branchtagsubfield) &&
236                                         (C4::Context->userenv->{flags} != 1) && ($value) && ($value ne C4::Context->userenv->{branch}) ;
237 #               print $input->redirect(".pl?biblionumber=$biblionumber") if ($test);
238                 # search for itemcallnumber if applicable
239                 if ($tagslib->{$tag}->{$subfield}->{kohafield} eq 'items.itemcallnumber' && C4::Context->preference('itemcallnumber')) {
240                         my $CNtag = substr(C4::Context->preference('itemcallnumber'),0,3);
241                         my $CNsubfield = substr(C4::Context->preference('itemcallnumber'),3,1);
242                         my $temp = $record->field($CNtag);
243                         if ($temp) {
244                                 $value = $temp->subfield($CNsubfield);
245                         }
246                 }
247                 if ($tagslib->{$tag}->{$subfield}->{authorised_value}) {
248                         my @authorised_values;
249                         my %authorised_lib;
250                         # builds list, depending on authorised value...
251                         #---- branch
252                         if ($tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) {
253                                 if ((C4::Context->preference("IndependantBranches")) && (C4::Context->userenv->{flags} != 1)){
254                                                 my $sth=$dbh->prepare("select branchcode,branchname from branches where branchcode = ? order by branchname");
255                                                 $sth->execute(C4::Context->userenv->{branch});
256                                                 push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
257                                                 while (my ($branchcode,$branchname) = $sth->fetchrow_array) {
258                                                         push @authorised_values, $branchcode;
259                                                         $authorised_lib{$branchcode}=$branchname;
260                                                 }
261                                 } else {
262                                         my $sth=$dbh->prepare("select branchcode,branchname from branches order by branchname");
263                                         $sth->execute;
264                                         push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
265                                         while (my ($branchcode,$branchname) = $sth->fetchrow_array) {
266                                                 push @authorised_values, $branchcode;
267                                                 $authorised_lib{$branchcode}=$branchname;
268                                         }
269                                 }
270                         #----- itemtypes
271                         } elsif ($tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes") {
272                                 my $sth=$dbh->prepare("select itemtype,description from itemtypes order by description");
273                                 $sth->execute;
274                                 push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
275                                 while (my ($itemtype,$description) = $sth->fetchrow_array) {
276                                         push @authorised_values, $itemtype;
277                                         $authorised_lib{$itemtype}=$description;
278                                 }
279                         #---- "true" authorised value
280                         } else {
281                                 $authorised_values_sth->execute($tagslib->{$tag}->{$subfield}->{authorised_value});
282                                 push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
283                                 while (my ($value,$lib) = $authorised_values_sth->fetchrow_array) {
284                                         push @authorised_values, $value;
285                                         $authorised_lib{$value}=$lib;
286                                 }
287                         }
288                         $subfield_data{marc_value}= CGI::scrolling_list(-name=>'field_value',
289                                                                                                                                                 -values=> \@authorised_values,
290                                                                                                                                                 -default=>"$value",
291                                                                                                                                                 -labels => \%authorised_lib,
292                                                                                                                                                 -size=>1,
293                                                                                                                                                 -multiple=>0,
294                                                                                                                                                 );
295                 } elsif ($tagslib->{$tag}->{$subfield}->{thesaurus_category}) {
296                         $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>";
297                         #"
298                 } elsif ($tagslib->{$tag}->{$subfield}->{'value_builder'}) {
299                         my $plugin="../value_builder/".$tagslib->{$tag}->{$subfield}->{'value_builder'};
300                         require $plugin;
301                         my $extended_param = plugin_parameters($dbh,$record,$tagslib,$i,0);
302                         my ($function_name,$javascript) = plugin_javascript($dbh,$record,$tagslib,$i,0);
303                         $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";
304                 } else {
305                         $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\" value=\"$value\" size=50 maxlength=255>";
306                 }
307 #               $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\">";
308                 push(@loop_data, \%subfield_data);
309                 $i++
310         }
311 }
312
313 # what's the next op ? it's what we are not in : an add if we're editing, otherwise, and edit.
314 $template->param(item_loop => \@item_value_loop,
315                                                 item_header_loop => \@header_value_loop,
316                                                 biblionumber => $biblionumber,
317                                                 bibid        => $biblionumber, 
318                                                 title => $oldrecord->{title},
319                                                 author => $oldrecord->{author},
320                                                 item => \@loop_data,
321                                                 itemnumber => $itemnumber,
322                                                 itemtagfield => $itemtagfield,
323                                                 itemtagsubfield =>$itemtagsubfield,
324                                                 op => $nextop,
325                                                 opisadd => ($nextop eq "saveitem")?0:1);
326 foreach my $error (@errors) {
327         $template->param($error => 1);
328 }
329 output_html_with_http_headers $input, $cookie, $template->output;