sync with dev_week.
[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 use Smart::Comments;
34
35 sub find_value {
36         my ($tagfield,$insubfield,$record) = @_;
37         my $result;
38         my $indicator;
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);
45                         }
46                 }
47         }
48         return($indicator,$result);
49 }
50 my $input = new CGI;
51 my $dbh = C4::Context->dbh;
52 my $error = $input->param('error');
53 my $biblionumber = $input->param('biblionumber');
54 if (!$biblionumber){
55     $biblionumber=$input->param('bibid');
56 }
57 my $biblioitemnumber = find_biblioitemnumber($dbh,$biblionumber);
58 my $itemnumber = $input->param('itemnumber');
59 if (!$itemnumber){
60     $itemnumber=$input->param('itemnum');
61     }
62
63 my $op = $input->param('op');
64
65 # find itemtype
66 my $itemtype = &MARCfind_frameworkcode($dbh,$biblionumber);
67
68 my $tagslib = &MARCgettagslib($dbh,1,$itemtype);
69 my $record = MARCgetbiblio($dbh,$biblionumber);
70 # warn "==>".$record->as_formatted;
71 my $oldrecord = MARCmarc2koha($dbh,$record);
72 my $itemrecord;
73 my $nextop="additem";
74 my @errors; # store errors found while checking data BEFORE saving item.
75 #------------------------------------------------------------------------------------------------------------------------------
76 if ($op eq "additem") {
77 #------------------------------------------------------------------------------------------------------------------------------
78         # rebuild
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;
94                         $newbarcode++;
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);
100                 }
101         }
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);
108         $nextop = "additem";
109 #------------------------------------------------------------------------------------------------------------------------------
110 } elsif ($op eq "edititem") {
111 #------------------------------------------------------------------------------------------------------------------------------
112 # retrieve item if exist => then, it's a modif
113         $itemrecord = get_record($biblionumber);
114         $nextop="saveitem";
115 #------------------------------------------------------------------------------------------------------------------------------
116 } elsif ($op eq "delitem") {
117 #------------------------------------------------------------------------------------------------------------------------------
118 # retrieve item if exist => then, it's a modif
119         &NEWdelitem($dbh,$biblionumber,$itemnumber);
120         $nextop="additem";
121 #------------------------------------------------------------------------------------------------------------------------------
122 } elsif ($op eq "saveitem") {
123 #------------------------------------------------------------------------------------------------------------------------------
124         # rebuild
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);
137         $itemnumber="";
138         $nextop="additem";
139 }
140
141 #
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",
147                              query => $input,
148                              type => "intranet",
149                              authnotrequired => 0,
150                              flagsrequired => {editcatalogue => 1},
151                              debug => 1,
152                              });
153
154 my %indicators;
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
161 my @big_array;
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);
165
166 foreach my $field (@fields) {
167         next if ($field->tag()<10);
168         my @subf=$field->subfields;
169         my %this_row;
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")) {
176                         #verifying rights
177                         my $userenv = C4::Context->userenv;
178                         unless (($userenv->{'flags'} == 1) or (($userenv->{'branch'} eq $subf[$i][1]))){
179                                         $this_row{'nomod'}=1;
180                         }
181                 }
182                 $this_row{itemnum} = $subf[$i][1] if ($field->tag() eq $itemtagfield && $subf[$i][0] eq $itemtagsubfield);
183         }
184         if (%this_row) {
185                 push(@big_array, \%this_row);
186         }
187 }
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}="&nbsp;" unless ($big_array[$i]{$subfield_code});
192         }
193 }
194 my ($holdingbrtagf,$holdingbrtagsubf) = &MARCfind_marc_from_kohafield($dbh,"items.holdingbranch",$itemtype);
195 @big_array = sort {$a->{$holdingbrtagsubf} cmp $b->{$holdingbrtagsubf}} @big_array;
196
197 # now, construct template !
198 my @item_value_loop;
199 my @header_value_loop;
200 for (my $i=0;$i<=$#big_array; $i++) {
201         my $items_data;
202         foreach my $subfield_code (sort keys(%witness)) {
203                 $items_data .="<td>".$big_array[$i]{$subfield_code}."</td>";
204         }
205         my %row_data;
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);
211 }
212 foreach my $subfield_code (sort keys(%witness)) {
213         my %header_value;
214         $header_value{header_value} = $witness{$subfield_code};
215         push(@header_value_loop, \%header_value);
216 }
217
218 # next item form
219 my @loop_data =();
220 my $i=0;
221 my $authorised_values_sth = $dbh->prepare("select authorised_value,lib from authorised_values where category=? order by lib");
222
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");
229                 my %subfield_data;
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};
236                 my ($x,$value);
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);
248                         if ($temp) {
249                                 $value = $temp->subfield($CNsubfield);
250                         }
251                 }
252                 if ($tagslib->{$tag}->{$subfield}->{authorised_value}) {
253                         my @authorised_values;
254                         my %authorised_lib;
255                         # builds list, depending on authorised value...
256                         #---- branch
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;
265                                                 }
266                                 } else {
267                                         my $sth=$dbh->prepare("select branchcode,branchname from branches order by branchname");
268                                         $sth->execute;
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;
273                                         }
274                                 }
275                         #----- itemtypes
276                         } elsif ($tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes") {
277                                 my $sth=$dbh->prepare("select itemtype,description from itemtypes order by description");
278                                 $sth->execute;
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;
283                                 }
284                         #---- "true" authorised value
285                         } else {
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;
291                                 }
292                         }
293                         $subfield_data{marc_value}= CGI::scrolling_list(-name=>'field_value',
294                                                                                                                                                 -values=> \@authorised_values,
295                                                                                                                                                 -default=>"$value",
296                                                                                                                                                 -labels => \%authorised_lib,
297                                                                                                                                                 -size=>1,
298                                                                                                                                                 -multiple=>0,
299                                                                                                                                                 );
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>";
302                         #"
303                 } elsif ($tagslib->{$tag}->{$subfield}->{'value_builder'}) {
304                         my $plugin="../value_builder/".$tagslib->{$tag}->{$subfield}->{'value_builder'};
305                         require $plugin;
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";
309                 } else {
310                         $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\" value=\"$value\" size=50 maxlength=255>";
311                 }
312 #               $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\">";
313                 push(@loop_data, \%subfield_data);
314                 $i++
315         }
316 }
317
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},
325                                                 item => \@loop_data,
326                                                 itemnumber => $itemnumber,
327                                                 itemnum => $itemnumber,
328                                                 itemtagfield => $itemtagfield,
329                                                 itemtagsubfield =>$itemtagsubfield,
330                                                 op => $nextop,
331                                                 opisadd => ($nextop eq "saveitem")?0:1);
332 foreach my $error (@errors) {
333         $template->param($error => 1);
334 }
335 output_html_with_http_headers $input, $cookie, $template->output;