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