french version
[koha.git] / acqui.simple / 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 $bibid = $input->param('bibid');
53 my $oldbiblionumber = &MARCfind_oldbiblionumber_from_MARCbibid($dbh,$bibid);
54
55 my $op = $input->param('op');
56 my $itemnum = $input->param('itemnum');
57
58 my $tagslib = &MARCgettagslib($dbh,1);
59 my $record = MARCgetbiblio($dbh,$bibid);
60 my $itemrecord;
61 my $nextop="additem";
62 my @errors; # store errors found while checking data BEFORE saving item.
63 #------------------------------------------------------------------------------------------------------------------------------
64 if ($op eq "additem") {
65 #------------------------------------------------------------------------------------------------------------------------------
66         # rebuild
67         my @tags = $input->param('tag');
68         my @subfields = $input->param('subfield');
69         my @values = $input->param('field_value');
70         # build indicator hash.
71         my @ind_tag = $input->param('ind_tag');
72         my @indicator = $input->param('indicator');
73         my %indicators;
74         for (my $i=0;$i<=$#ind_tag;$i++) {
75                 $indicators{$ind_tag[$i]} = $indicator[$i];
76         }
77         my $record = MARChtml2marc($dbh,\@tags,\@subfields,\@values,%indicators);
78 # check for item barcode # being unique
79         my $oldbibid = MARCmarc2koha($dbh,$record);
80         my $exists = itemdata($oldbibid->{'barcode'});
81         push @errors,"barcode_not_unique" if($exists);
82 # MARC::Record builded => now, record in DB
83         # if barcode exists, don't create, but report The problem.
84         my ($oldbiblionumber,$oldbibnum,$oldbibitemnum) = NEWnewitem($dbh,$record,$bibid) unless ($exists);
85         if ($exists) {
86                 $nextop = "additem";
87                 $itemrecord = $record;
88         } else {
89                 $nextop = "additem";
90         }
91 #------------------------------------------------------------------------------------------------------------------------------
92 } elsif ($op eq "edititem") {
93 #------------------------------------------------------------------------------------------------------------------------------
94 # retrieve item if exist => then, it's a modif
95         $itemrecord = MARCgetitem($dbh,$bibid,$itemnum);
96         $nextop="saveitem";
97 #------------------------------------------------------------------------------------------------------------------------------
98 } elsif ($op eq "saveitem") {
99 #------------------------------------------------------------------------------------------------------------------------------
100         # rebuild
101         my @tags = $input->param('tag');
102         my @subfields = $input->param('subfield');
103         my @values = $input->param('field_value');
104         # build indicator hash.
105         my @ind_tag = $input->param('ind_tag');
106         my @indicator = $input->param('indicator');
107 #       my $itemnum = $input->param('itemnum');
108         my %indicators;
109         for (my $i=0;$i<=$#ind_tag;$i++) {
110                 $indicators{$ind_tag[$i]} = $indicator[$i];
111         }
112         my $record = MARChtml2marc($dbh,\@tags,\@subfields,\@values,%indicators);
113 # MARC::Record builded => now, record in DB
114         my ($oldbiblionumber,$oldbibnum,$oldbibitemnum) = NEWmoditem($dbh,$record,$bibid,$itemnum,0);
115         $itemnum="";
116         $nextop="additem";
117 }
118
119 #
120 #------------------------------------------------------------------------------------------------------------------------------
121 # build screen with existing items. and "new" one
122 #------------------------------------------------------------------------------------------------------------------------------
123
124 my %indicators;
125 $indicators{995}='  ';
126 # now, build existiing item list
127 my $temp = MARCgetbiblio($dbh,$bibid);
128 my @fields = $temp->fields();
129 my %witness; #---- stores the list of subfields used at least once, with the "meaning" of the code
130 my @big_array;
131 #---- finds where items.itemnumber is stored
132 my ($itemtagfield,$itemtagsubfield) = &MARCfind_marc_from_kohafield($dbh,"items.itemnumber");
133 my @itemnums; # array to store itemnums
134 foreach my $field (@fields) {
135         next if ($field->tag()<10);
136         my @subf=$field->subfields;
137         my %this_row;
138 # loop through each subfield
139         for my $i (0..$#subf) {
140                 next if ($tagslib->{$field->tag()}->{$subf[$i][0]}->{tab}  ne 10 && ($field->tag() ne $itemtagfield && $subf[$i][0] ne $itemtagsubfield));
141                 $witness{$subf[$i][0]} = $tagslib->{$field->tag()}->{$subf[$i][0]}->{lib} if ($tagslib->{$field->tag()}->{$subf[$i][0]}->{tab}  eq 10);
142                 $this_row{$subf[$i][0]} =$subf[$i][1] if ($tagslib->{$field->tag()}->{$subf[$i][0]}->{tab}  eq 10);
143                 push @itemnums,$this_row{$subf[$i][0]} =$subf[$i][1] if ($field->tag() eq $itemtagfield && $subf[$i][0] eq $itemtagsubfield);
144         }
145         if (%this_row) {
146                 push(@big_array, \%this_row);
147         }
148 }
149 #fill big_row with missing datas
150 foreach my $subfield_code  (keys(%witness)) {
151         for (my $i=0;$i<=$#big_array;$i++) {
152                 $big_array[$i]{$subfield_code}="&nbsp;" unless ($big_array[$i]{$subfield_code});
153         }
154 }
155 # now, construct template !
156 my @item_value_loop;
157 my @header_value_loop;
158 for (my $i=0;$i<=$#big_array; $i++) {
159         my $items_data;
160         foreach my $subfield_code (sort keys(%witness)) {
161                 $items_data .="<td>".$big_array[$i]{$subfield_code}."</td>";
162         }
163         my %row_data;
164         $row_data{item_value} = $items_data;
165         $row_data{itemnum} = $itemnums[$i];
166         push(@item_value_loop,\%row_data);
167 }
168 foreach my $subfield_code (sort keys(%witness)) {
169         my %header_value;
170         $header_value{header_value} = $witness{$subfield_code};
171         push(@header_value_loop, \%header_value);
172 }
173
174 # next item form
175 my @loop_data =();
176 my $i=0;
177 my $authorised_values_sth = $dbh->prepare("select authorised_value,lib from authorised_values where category=? order by authorised_value");
178
179 foreach my $tag (sort keys %{$tagslib}) {
180         my $previous_tag = '';
181 # loop through each subfield
182         foreach my $subfield (sort keys %{$tagslib->{$tag}}) {
183                 next if subfield_is_koha_internal_p($subfield);
184                 next if ($tagslib->{$tag}->{$subfield}->{'tab'}  ne "10");
185                 my %subfield_data;
186                 $subfield_data{tag}=$tag;
187                 $subfield_data{subfield}=$subfield;
188 #               $subfield_data{marc_lib}=$tagslib->{$tag}->{$subfield}->{lib};
189                 $subfield_data{marc_lib}="<DIV id=\"error$i\">".$tagslib->{$tag}->{$subfield}->{lib}."</div>";
190                 $subfield_data{mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
191                 $subfield_data{repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
192                 my ($x,$value);
193                 ($x,$value) = find_value($tag,$subfield,$itemrecord) if ($itemrecord);
194                 if ($tagslib->{$tag}->{$subfield}->{authorised_value}) {
195                         my @authorised_values;
196                         my %authorised_lib;
197                         # builds list, depending on authorised value...
198                         #---- branch
199                         if ($tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) {
200                                 my $sth=$dbh->prepare("select branchcode,branchname from branches");
201                                 $sth->execute;
202                                 push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
203                                 while (my ($branchcode,$branchname) = $sth->fetchrow_array) {
204                                         push @authorised_values, $branchcode;
205                                         $authorised_lib{$branchcode}=$branchname;
206                                 }
207                         #----- itemtypes
208                         } elsif ($tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes") {
209                                 my $sth=$dbh->prepare("select itemtype,description from itemtypes");
210                                 $sth->execute;
211                                 push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
212                                 while (my ($itemtype,$description) = $sth->fetchrow_array) {
213                                         push @authorised_values, $itemtype;
214                                         $authorised_lib{$itemtype}=$description;
215                                 }
216                         #---- "true" authorised value
217                         } else {
218                                 $authorised_values_sth->execute($tagslib->{$tag}->{$subfield}->{authorised_value});
219                                 push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
220                                 while (my ($value,$lib) = $authorised_values_sth->fetchrow_array) {
221                                         push @authorised_values, $value;
222                                         $authorised_lib{$value}=$lib;
223                                 }
224                         }
225                         $subfield_data{marc_value}= CGI::scrolling_list(-name=>'field_value',
226                                                                                                                                                 -values=> \@authorised_values,
227                                                                                                                                                 -default=>"$value",
228                                                                                                                                                 -labels => \%authorised_lib,
229                                                                                                                                                 -size=>1,
230                                                                                                                                                 -multiple=>0,
231                                                                                                                                                 );
232                 } elsif ($tagslib->{$tag}->{$subfield}->{thesaurus_category}) {
233                         $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>";
234                 } elsif ($tagslib->{$tag}->{$subfield}->{'value_builder'}) {
235                         my $plugin="../value_builder/".$tagslib->{$tag}->{$subfield}->{'value_builder'};
236                         require $plugin;
237                         my $extended_param = plugin_parameters($dbh,$record,$tagslib,$i,0);
238                         my ($function_name,$javascript) = plugin_javascript($dbh,$record,$tagslib,$i,0);
239                         $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";
240                 } else {
241                         $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\" value=\"$value\" size=50 maxlength=255>";
242                 }
243 #               $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\">";
244                 push(@loop_data, \%subfield_data);
245                 $i++
246         }
247 }
248 my ($template, $loggedinuser, $cookie)
249     = get_template_and_user({template_name => "acqui.simple/additem.tmpl",
250                              query => $input,
251                              type => "intranet",
252                              authnotrequired => 0,
253                              flagsrequired => {parameters => 1},
254                              debug => 1,
255                              });
256
257 # what's the next op ? it's what we are not in : an add if we're editing, otherwise, and edit.
258 $template->param(item_loop => \@item_value_loop,
259                                                 item_header_loop => \@header_value_loop,
260                                                 bibid => $bibid,
261                                                 biblionumber =>$oldbiblionumber,
262                                                 item => \@loop_data,
263                                                 itemnum => $itemnum,
264                                                 itemtagfield => $itemtagfield,
265                                                 itemtagsubfield =>$itemtagsubfield,
266                                                 op => $nextop,
267                                                 opisadd => ($nextop eq "saveitem")?0:1);
268 foreach my $error (@errors) {
269         $template->param($error => 1);
270 }
271 output_html_with_http_headers $input, $cookie, $template->output;