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