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