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