bugfixes : enable entering a biblio without isbn
[koha_gimpoz] / 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::Output;
25 use C4::Biblio;
26 use C4::Context;
27 use HTML::Template;
28 use MARC::File::USMARC;
29
30 sub find_value {
31         my ($tagfield,$insubfield,$record) = @_;
32 #       warn "$tagfield / $insubfield // ";
33         my $result;
34         my $indicator;
35         foreach my $field ($record->field($tagfield)) {
36                 my @subfields = $field->subfields();
37                 foreach my $subfield (@subfields) {
38                         if (@$subfield[0] eq $insubfield) {
39                                 $result .= @$subfield[1];
40                                 $indicator = $field->indicator(1).$field->indicator(2);
41                         }
42                 }
43         }
44         return($indicator,$result);
45 }
46 my $input = new CGI;
47 my $error = $input->param('error');
48 my $bibid = $input->param('bibid');
49 my $op = $input->param('op');
50 my $dbh = C4::Context->dbh;
51 my $itemnum = $input->param('itemnum');
52
53 my $tagslib = &MARCgettagslib($dbh,1);
54 my $record = MARCgetbiblio($dbh,$bibid);
55 my $itemrecord;
56 my $nextop="additem";
57 #------------------------------------------------------------------------------------------------------------------------------
58 if ($op eq "additem") {
59 #------------------------------------------------------------------------------------------------------------------------------
60         # rebuild
61         my @tags = $input->param('tag');
62         my @subfields = $input->param('subfield');
63         my @values = $input->param('field_value');
64         # build indicator hash.
65         my @ind_tag = $input->param('ind_tag');
66         my @indicator = $input->param('indicator');
67         my %indicators;
68         for (my $i=0;$i<=$#ind_tag;$i++) {
69                 $indicators{$ind_tag[$i]} = $indicator[$i];
70         }
71         my $record = MARChtml2marc($dbh,\@tags,\@subfields,\@values,%indicators);
72 # MARC::Record builded => now, record in DB
73         my ($bibid,$oldbibnum,$oldbibitemnum) = NEWnewitem($dbh,$record,$bibid);
74         $nextop = "additem";
75 #------------------------------------------------------------------------------------------------------------------------------
76 } elsif ($op eq "edititem") {
77 #------------------------------------------------------------------------------------------------------------------------------
78 # retrieve item if exist => then, it's a modif
79         $itemrecord = MARCgetitem($dbh,$bibid,$itemnum);
80         $nextop="saveitem";
81 #------------------------------------------------------------------------------------------------------------------------------
82 } elsif ($op eq "saveitem") {
83 #------------------------------------------------------------------------------------------------------------------------------
84         # rebuild
85         my @tags = $input->param('tag');
86         my @subfields = $input->param('subfield');
87         my @values = $input->param('field_value');
88         # build indicator hash.
89         my @ind_tag = $input->param('ind_tag');
90         my @indicator = $input->param('indicator');
91         my %indicators;
92         for (my $i=0;$i<=$#ind_tag;$i++) {
93                 $indicators{$ind_tag[$i]} = $indicator[$i];
94         }
95         my $record = MARChtml2marc($dbh,\@tags,\@subfields,\@values,%indicators);
96 # MARC::Record builded => now, record in DB
97         my ($bibid,$oldbibnum,$oldbibitemnum) = NEWmoditem($dbh,$record,$bibid,$itemnum,0);
98         $nextop="additem";
99 }
100
101 #
102 #------------------------------------------------------------------------------------------------------------------------------
103 # build screen with existing items. and "new" one
104 #------------------------------------------------------------------------------------------------------------------------------
105
106 my %indicators;
107 $indicators{995}='  ';
108 # now, build existiing item list
109 my $temp = MARCgetbiblio($dbh,$bibid);
110 my @fields = $temp->fields();
111 my %witness; #---- stores the list of subfields used at least once, with the "meaning" of the code
112 my @big_array;
113 #---- finds where items.itemnumber is stored
114 my ($itemtagfield,$itemtagsubfield) = &MARCfind_marc_from_kohafield($dbh,"items.itemnumber");
115 my @itemnums; # array to store itemnums
116 foreach my $field (@fields) {
117         my @subf=$field->subfields;
118         my %this_row;
119 # loop through each subfield
120         for my $i (0..$#subf) {
121                 next if ($tagslib->{$field->tag()}->{$subf[$i][0]}->{tab}  ne 10 && ($field->tag() ne $itemtagfield && $subf[$i][0] ne $itemtagsubfield));
122                 $witness{$subf[$i][0]} = $tagslib->{$field->tag()}->{$subf[$i][0]}->{lib} if ($tagslib->{$field->tag()}->{$subf[$i][0]}->{tab}  eq 10);
123                 $this_row{$subf[$i][0]} =$subf[$i][1] if ($tagslib->{$field->tag()}->{$subf[$i][0]}->{tab}  eq 10);
124                 push @itemnums,$this_row{$subf[$i][0]} =$subf[$i][1] if ($field->tag() eq $itemtagfield && $subf[$i][0] eq $itemtagsubfield);
125         }
126         if (%this_row) {
127                 push(@big_array, \%this_row);
128         }
129 }
130 #fill big_row with missing datas
131 foreach my $subfield_code  (keys(%witness)) {
132         for (my $i=0;$i<=$#big_array;$i++) {
133                 $big_array[$i]{$subfield_code}="&nbsp;" unless ($big_array[$i]{$subfield_code});
134         }
135 }
136 # now, construct template !
137 my @item_value_loop;
138 my @header_value_loop;
139 for (my $i=0;$i<=$#big_array; $i++) {
140         my $items_data;
141         foreach my $subfield_code (sort keys(%witness)) {
142                 $items_data .="<td>".$big_array[$i]{$subfield_code}."</td>";
143         }
144         my %row_data;
145         $row_data{item_value} = $items_data;
146         $row_data{itemnum} = $itemnums[$i];
147         push(@item_value_loop,\%row_data);
148 }
149 foreach my $subfield_code (sort keys(%witness)) {
150         my %header_value;
151         $header_value{header_value} = $witness{$subfield_code};
152         push(@header_value_loop, \%header_value);
153 }
154
155 # next item form
156 my @loop_data =();
157 my $i=0;
158 my $authorised_values_sth = $dbh->prepare("select authorised_value from authorised_values where category=?");
159
160 foreach my $tag (sort keys %{$tagslib}) {
161         my $previous_tag = '';
162 # loop through each subfield
163         foreach my $subfield (sort keys %{$tagslib->{$tag}}) {
164                 next if ($subfield eq 'lib');
165                 next if ($subfield eq 'tab');
166                 next if ($tagslib->{$tag}->{$subfield}->{'tab'}  ne "10");
167                 my %subfield_data;
168                 $subfield_data{tag}=$tag;
169                 $subfield_data{subfield}=$subfield;
170                 $subfield_data{marc_lib}=$tagslib->{$tag}->{$subfield}->{lib};
171                 $subfield_data{marc_mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
172                 $subfield_data{marc_repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
173                 my ($x,$value);
174                 ($x,$value) = find_value($tag,$subfield,$itemrecord) if ($itemrecord);
175                 if ($tagslib->{$tag}->{$subfield}->{authorised_value}) {
176                         my @authorised_values;
177                         # builds list, depending on authorised value...
178                         #---- branch
179                         if ($tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) {
180                                 my $sth=$dbh->prepare("select branchcode,branchname from branches");
181                                 $sth->execute;
182                                 push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
183                                 while (my ($branchcode,$branchname) = $sth->fetchrow_array) {
184                                         push @authorised_values, $branchcode;
185                                 }
186                         #----- itemtypes
187                         } elsif ($tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes") {
188                                 my $sth=$dbh->prepare("select itemtype,description from itemtypes");
189                                 $sth->execute;
190                                 push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
191                                 while (my ($itemtype,$description) = $sth->fetchrow_array) {
192                                         push @authorised_values, $itemtype;
193                                 }
194                         #---- "true" authorised value
195                         } else {
196                                 $authorised_values_sth->execute($tagslib->{$tag}->{$subfield}->{authorised_value});
197                                 push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
198                                 while ((my $value) = $authorised_values_sth->fetchrow_array) {
199                                         push @authorised_values, $value;
200                                 }
201                         }
202                         $subfield_data{marc_value}= CGI::scrolling_list(-name=>'field_value',
203                                                                                                                                                 -values=> \@authorised_values,
204                                                                                                                                                 -default=>"$value",
205                                                                                                                                                 -size=>1,
206                                                                                                                                                 -multiple=>0,
207                                                                                                                                                 );
208                 } elsif ($tagslib->{$tag}->{$subfield}->{thesaurus_category}) {
209                         $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\"  size=47 maxlength=255 DISABLE READONLY> <a href=\"javascript:Dopop('../thesaurus_popup.pl?category=$tagslib->{$tag}->{$subfield}->{thesaurus_category}&index=$i',$i)\">...</a>";
210                 } elsif ($tagslib->{$tag}->{$subfield}->{'value_builder'}) {
211                         my $plugin="../value_builder/".$tagslib->{$tag}->{$subfield}->{'value_builder'};
212                         require $plugin;
213                         my $extended_param = plugin_parameters($dbh,$record,$tagslib,$i,0);
214                         $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\"  size=47 maxlength=255 DISABLE READONLY> <a href=\"javascript:Dopop('../plugin_launcher.pl?plugin_name=$tagslib->{$tag}->{$subfield}->{value_builder}&index=$i$extended_param',$i)\">...</a>";
215                 } else {
216                         $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\" value=\"$value\" size=50 maxlength=255>";
217                 }
218 #               $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\">";
219                 push(@loop_data, \%subfield_data);
220                 $i++
221         }
222 }
223 my $template = gettemplate("acqui.simple/additem.tmpl");
224 # what's the next op ? it's what we are not in : an add if we're editing, otherwise, and edit.
225 $template->param(item_loop => \@item_value_loop,
226                                                 item_header_loop => \@header_value_loop,
227                                                 bibid => $bibid,
228                                                 item => \@loop_data,
229                                                 itemnum => $itemnum,
230                                                 op => $nextop);
231 print "Content-Type: text/html\n\n", $template->output;