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