5 # Copyright 2000-2002 Katipo Communications
7 # This file is part of Koha.
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
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.
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
26 use C4::Interface::CGI::Output;
29 use C4::Koha; # XXX subfield_is_koha_internal_p
32 use MARC::File::USMARC;
35 my ($tagfield,$insubfield,$record) = @_;
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);
47 return($indicator,$result);
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);
56 my $op = $input->param('op');
57 my $itemnum = $input->param('itemnum');
60 my $itemtype = &MARCfind_frameworkcode($dbh,$bibid);
62 my $tagslib = &MARCgettagslib($dbh,1,$itemtype);
63 my $record = MARCgetbiblio($dbh,$bibid);
64 my $oldrecord = MARCmarc2koha($dbh,$record);
67 my @errors; # store errors found while checking data BEFORE saving item.
68 #------------------------------------------------------------------------------------------------------------------------------
69 if ($op eq "additem") {
70 #------------------------------------------------------------------------------------------------------------------------------
72 my @tags = $input->param('tag');
73 my @subfields = $input->param('subfield');
74 my @values = $input->param('field_value');
75 # build indicator hash.
76 my @ind_tag = $input->param('ind_tag');
77 my @indicator = $input->param('indicator');
79 for (my $i=0;$i<=$#ind_tag;$i++) {
80 $indicators{$ind_tag[$i]} = $indicator[$i];
82 my $record = MARChtml2marc($dbh,\@tags,\@subfields,\@values,%indicators);
83 # check for item barcode # being unique
84 my $oldbibid = MARCmarc2koha($dbh,$record);
85 my $exists = itemdata($oldbibid->{'barcode'});
86 push @errors,"barcode_not_unique" if($exists);
87 # MARC::Record builded => now, record in DB
88 # if barcode exists, don't create, but report The problem.
89 my ($oldbiblionumber,$oldbibnum,$oldbibitemnum) = NEWnewitem($dbh,$record,$bibid) unless ($exists);
92 $itemrecord = $record;
96 #------------------------------------------------------------------------------------------------------------------------------
97 } elsif ($op eq "edititem") {
98 #------------------------------------------------------------------------------------------------------------------------------
99 # retrieve item if exist => then, it's a modif
100 $itemrecord = MARCgetitem($dbh,$bibid,$itemnum);
102 #------------------------------------------------------------------------------------------------------------------------------
103 } elsif ($op eq "delitem") {
104 #------------------------------------------------------------------------------------------------------------------------------
105 # retrieve item if exist => then, it's a modif
106 &NEWdelitem($dbh,$bibid,$itemnum);
108 #------------------------------------------------------------------------------------------------------------------------------
109 } elsif ($op eq "saveitem") {
110 #------------------------------------------------------------------------------------------------------------------------------
112 my @tags = $input->param('tag');
113 my @subfields = $input->param('subfield');
114 my @values = $input->param('field_value');
115 # build indicator hash.
116 my @ind_tag = $input->param('ind_tag');
117 my @indicator = $input->param('indicator');
118 # my $itemnum = $input->param('itemnum');
120 for (my $i=0;$i<=$#ind_tag;$i++) {
121 $indicators{$ind_tag[$i]} = $indicator[$i];
123 my $record = MARChtml2marc($dbh,\@tags,\@subfields,\@values,%indicators);
124 # MARC::Record builded => now, record in DB
125 # warn "R: ".$record->as_formatted;
126 my ($oldbiblionumber,$oldbibnum,$oldbibitemnum) = NEWmoditem($dbh,$record,$bibid,$itemnum,0);
132 #------------------------------------------------------------------------------------------------------------------------------
133 # build screen with existing items. and "new" one
134 #------------------------------------------------------------------------------------------------------------------------------
135 my ($template, $loggedinuser, $cookie)
136 = get_template_and_user({template_name => "acqui.simple/additem.tmpl",
139 authnotrequired => 0,
140 flagsrequired => {editcatalogue => 1},
145 $indicators{995}=' ';
146 # now, build existiing item list
147 # my $temp = MARCgetbiblio($dbh,$bibid);
148 # my @fields = $temp->fields();
149 my @fields = $record->fields();
150 my %witness; #---- stores the list of subfields used at least once, with the "meaning" of the code
152 #---- finds where items.itemnumber is stored
153 my ($itemtagfield,$itemtagsubfield) = &MARCfind_marc_from_kohafield($dbh,"items.itemnumber",$itemtype);
154 my ($branchtagfield,$branchtagsubfield) = &MARCfind_marc_from_kohafield($dbh,"items.homebranch",$itemtype);
155 my @itemnums; # array to store itemnums
156 foreach my $field (@fields) {
157 next if ($field->tag()<10);
158 my @subf=$field->subfields;
160 # loop through each subfield
161 for my $i (0..$#subf) {
162 next if ($tagslib->{$field->tag()}->{$subf[$i][0]}->{tab} ne 10 && ($field->tag() ne $itemtagfield && $subf[$i][0] ne $itemtagsubfield));
163 $witness{$subf[$i][0]} = $tagslib->{$field->tag()}->{$subf[$i][0]}->{lib} if ($tagslib->{$field->tag()}->{$subf[$i][0]}->{tab} eq 10);
164 $this_row{$subf[$i][0]} =$subf[$i][1] if ($tagslib->{$field->tag()}->{$subf[$i][0]}->{tab} eq 10);
165 if (($field->tag eq $branchtagfield) && ($subf[$i][$0] eq $branchtagsubfield) && C4::Context->preference("IndependantBranches")) {
167 my $userenv = C4::Context->userenv;
168 unless ($userenv->{'flags'} == 1){
169 if (not ($userenv->{'branch'} eq $subf[$i][1])) {
170 $this_row{'nomod'}=1;
175 push @itemnums,$this_row{$subf[$i][0]} =$subf[$i][1] if ($field->tag() eq $itemtagfield && $subf[$i][0] eq $itemtagsubfield);
178 push(@big_array, \%this_row);
181 #fill big_row with missing datas
182 foreach my $subfield_code (keys(%witness)) {
183 for (my $i=0;$i<=$#big_array;$i++) {
184 $big_array[$i]{$subfield_code}=" " unless ($big_array[$i]{$subfield_code});
187 # now, construct template !
189 my @header_value_loop;
190 for (my $i=0;$i<=$#big_array; $i++) {
192 foreach my $subfield_code (sort keys(%witness)) {
193 $items_data .="<td>".$big_array[$i]{$subfield_code}."</td>";
196 $row_data{item_value} = $items_data;
197 $row_data{itemnum} = $itemnums[$i];
198 #reporting this_row values
199 $row_data{'nomod'} = $big_array[$i]{'nomod'};
200 push(@item_value_loop,\%row_data);
202 foreach my $subfield_code (sort keys(%witness)) {
204 $header_value{header_value} = $witness{$subfield_code};
205 push(@header_value_loop, \%header_value);
211 my $authorised_values_sth = $dbh->prepare("select authorised_value,lib from authorised_values where category=? order by lib");
213 foreach my $tag (sort keys %{$tagslib}) {
214 my $previous_tag = '';
215 # loop through each subfield
216 foreach my $subfield (sort keys %{$tagslib->{$tag}}) {
217 next if subfield_is_koha_internal_p($subfield);
218 next if ($tagslib->{$tag}->{$subfield}->{'tab'} ne "10");
220 $subfield_data{tag}=$tag;
221 $subfield_data{subfield}=$subfield;
222 # $subfield_data{marc_lib}=$tagslib->{$tag}->{$subfield}->{lib};
223 $subfield_data{marc_lib}="<span id=\"error$i\">".$tagslib->{$tag}->{$subfield}->{lib}."</span>";
224 $subfield_data{mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
225 $subfield_data{repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
227 ($x,$value) = find_value($tag,$subfield,$itemrecord) if ($itemrecord);
229 #testing branch value if IndependantBranches.
230 my $test = (C4::Context->preference("IndependantBranches")) &&
231 ($tag==$branchtagfield) && ($subfield==$branchtagsubfield) &&
232 (C4::Context->userenv->{flags} != 1) && ($value != C4::Context->userenv->{branch}) ;
233 print $input->redirect("additem.pl?bibid=$bibid") if ($test);
235 # search for itemcallnumber if applicable
236 if ($tagslib->{$tag}->{$subfield}->{kohafield} eq 'items.itemcallnumber' && C4::Context->preference('itemcallnumber')) {
237 my $CNtag = substr(C4::Context->preference('itemcallnumber'),0,3);
238 my $CNsubfield = substr(C4::Context->preference('itemcallnumber'),3,1);
239 my $temp = $record->field($CNtag);
241 $value = $temp->subfield($CNsubfield);
244 if ($tagslib->{$tag}->{$subfield}->{authorised_value}) {
245 my @authorised_values;
247 # builds list, depending on authorised value...
249 if ($tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) {
250 if ((C4::Context->preference("IndependantBranches")) && (C4::Context->userenv->{flags} != 1)){
251 my $sth=$dbh->prepare("select branchcode,branchname from branches where branchcode = ? order by branchname");
252 $sth->execute(C4::Context->userenv->{branch});
253 push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
254 while (my ($branchcode,$branchname) = $sth->fetchrow_array) {
255 push @authorised_values, $branchcode;
256 $authorised_lib{$branchcode}=$branchname;
259 my $sth=$dbh->prepare("select branchcode,branchname from branches order by branchname");
261 push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
262 while (my ($branchcode,$branchname) = $sth->fetchrow_array) {
263 push @authorised_values, $branchcode;
264 $authorised_lib{$branchcode}=$branchname;
268 } elsif ($tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes") {
269 my $sth=$dbh->prepare("select itemtype,description from itemtypes order by description");
271 push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
272 while (my ($itemtype,$description) = $sth->fetchrow_array) {
273 push @authorised_values, $itemtype;
274 $authorised_lib{$itemtype}=$description;
276 #---- "true" authorised value
278 $authorised_values_sth->execute($tagslib->{$tag}->{$subfield}->{authorised_value});
279 push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
280 while (my ($value,$lib) = $authorised_values_sth->fetchrow_array) {
281 push @authorised_values, $value;
282 $authorised_lib{$value}=$lib;
285 $subfield_data{marc_value}= CGI::scrolling_list(-name=>'field_value',
286 -values=> \@authorised_values,
288 -labels => \%authorised_lib,
292 } elsif ($tagslib->{$tag}->{$subfield}->{thesaurus_category}) {
293 $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>";
295 } elsif ($tagslib->{$tag}->{$subfield}->{'value_builder'}) {
296 my $plugin="../value_builder/".$tagslib->{$tag}->{$subfield}->{'value_builder'};
298 my $extended_param = plugin_parameters($dbh,$record,$tagslib,$i,0);
299 my ($function_name,$javascript) = plugin_javascript($dbh,$record,$tagslib,$i,0);
300 $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";
302 $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\" value=\"$value\" size=50 maxlength=255>";
304 # $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\">";
305 push(@loop_data, \%subfield_data);
309 my ($template, $loggedinuser, $cookie)
310 = get_template_and_user({template_name => "acqui.simple/additem.tmpl",
313 authnotrequired => 0,
314 flagsrequired => {editcatalogue => 1},
318 # what's the next op ? it's what we are not in : an add if we're editing, otherwise, and edit.
319 $template->param(item_loop => \@item_value_loop,
320 item_header_loop => \@header_value_loop,
322 biblionumber =>$oldbiblionumber,
323 title => $oldrecord->{title},
324 author => $oldrecord->{author},
327 itemtagfield => $itemtagfield,
328 itemtagsubfield =>$itemtagsubfield,
330 opisadd => ($nextop eq "saveitem")?0:1);
331 foreach my $error (@errors) {
332 $template->param($error => 1);
334 output_html_with_http_headers $input, $cookie, $template->output;