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