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 $biblionumber = $input->param('biblionumber');
53 my $biblioitemnumber = find_biblioitemnumber($dbh,$biblionumber);
54 my $itemnumber = $input->param('itemnumber');
55 my $op = $input->param('op');
58 my $itemtype = &MARCfind_frameworkcode($dbh,$biblionumber);
60 my $tagslib = &MARCgettagslib($dbh,1,$itemtype);
64 my @errors; # store errors found while checking data BEFORE saving item.
65 #------------------------------------------------------------------------------------------------------------------------------
66 if ($op eq "additem") {
67 #------------------------------------------------------------------------------------------------------------------------------
69 my @tags = $input->param('tag');
70 my @subfields = $input->param('subfield');
71 my @values = $input->param('field_value');
72 # build indicator hash.
73 my @ind_tag = $input->param('ind_tag');
74 my @indicator = $input->param('indicator');
76 for (my $i=0;$i<=$#ind_tag;$i++) {
77 $indicators{$ind_tag[$i]} = $indicator[$i];
79 my $addeditem = MARChtml2marc($dbh,\@tags,\@subfields,\@values,%indicators);
80 # check for item barcode # being unique
81 my $addedolditem = MARCmarc2koha($dbh,$addeditem);
82 my $exists = itemdata($addedolditem->{'barcode'});
83 push @errors,"barcode_not_unique" if($exists);
84 # if barcode exists, don't create, but report The problem.
85 $itemnumber = NEWnewitem($dbh,$addeditem,$biblionumber,$biblioitemnumber) unless ($exists);
87 #------------------------------------------------------------------------------------------------------------------------------
88 } elsif ($op eq "edititem") {
89 #------------------------------------------------------------------------------------------------------------------------------
90 # retrieve item if exist => then, it's a modif
91 $itemrecord = MARCgetitem($dbh,$biblionumber,$itemnumber);
93 #------------------------------------------------------------------------------------------------------------------------------
94 } elsif ($op eq "delitem") {
95 #------------------------------------------------------------------------------------------------------------------------------
96 # retrieve item if exist => then, it's a modif
97 &NEWdelitem($dbh,$biblionumber,$itemnumber);
99 #------------------------------------------------------------------------------------------------------------------------------
100 } elsif ($op eq "saveitem") {
101 #------------------------------------------------------------------------------------------------------------------------------
103 my @tags = $input->param('tag');
104 my @subfields = $input->param('subfield');
105 my @values = $input->param('field_value');
106 # build indicator hash.
107 my @ind_tag = $input->param('ind_tag');
108 my @indicator = $input->param('indicator');
109 # my $itemnum = $input->param('itemnum');
111 for (my $i=0;$i<=$#ind_tag;$i++) {
112 $indicators{$ind_tag[$i]} = $indicator[$i];
114 my $itemrecord = MARChtml2marc($dbh,\@tags,\@subfields,\@values,%indicators);
115 # MARC::Record builded => now, record in DB
116 # warn "ITEM TO MODIFY : ".$itemrecord->as_formatted;
117 NEWmoditem($dbh, $itemrecord, $biblionumber, $biblioitemnumber, $itemnumber,0);
123 #------------------------------------------------------------------------------------------------------------------------------
124 # build screen with existing items. and "new" one
125 #------------------------------------------------------------------------------------------------------------------------------
126 my ($template, $loggedinuser, $cookie)
127 = get_template_and_user({template_name => "acqui.simple/additem.tmpl",
130 authnotrequired => 0,
131 flagsrequired => {editcatalogue => 1},
135 # load the biblio to have the title, the biblioitemnumber & any other useful field
136 my $record = MARCgetbiblio($dbh,$biblionumber);
137 # warn $record ->as_formatted;
138 # warn "REC ".$record->as_formatted;
139 my $oldrecord = MARCmarc2koha($dbh,$record);
142 $indicators{995}=' ';
143 # now, build existiing item list
144 my @fields = $record->fields();
145 my %witness; #---- stores the list of subfields used at least once, with the "meaning" of the code
147 #---- finds where items.itemnumber is stored
148 my ($itemtagfield,$itemtagsubfield) = &MARCfind_marc_from_kohafield($dbh,"items.itemnumber",$itemtype);
149 my ($branchtagfield,$branchtagsubfield) = &MARCfind_marc_from_kohafield($dbh,"items.homebranch",$itemtype);
150 my @itemnums; # array to store itemnums
151 foreach my $field (@fields) {
152 next if ($field->tag()<10);
153 my @subf=$field->subfields;
155 # loop through each subfield
156 for my $i (0..$#subf) {
157 next if ($tagslib->{$field->tag()}->{$subf[$i][0]}->{tab} ne 10 && ($field->tag() ne $itemtagfield && $subf[$i][0] ne $itemtagsubfield));
158 $witness{$subf[$i][0]} = $tagslib->{$field->tag()}->{$subf[$i][0]}->{lib} if ($tagslib->{$field->tag()}->{$subf[$i][0]}->{tab} eq 10);
159 $this_row{$subf[$i][0]} =$subf[$i][1] if ($tagslib->{$field->tag()}->{$subf[$i][0]}->{tab} eq 10);
160 if (($field->tag eq $branchtagfield) && ($subf[$i][$0] eq $branchtagsubfield) && C4::Context->preference("IndependantBranches")) {
162 my $userenv = C4::Context->userenv;
163 unless ($userenv->{'flags'} == 1){
164 if (not ($userenv->{'branch'} eq $subf[$i][1])) {
165 $this_row{'nomod'}=1;
170 push @itemnums,$this_row{$subf[$i][0]} =$subf[$i][1] if ($field->tag() eq $itemtagfield && $subf[$i][0] eq $itemtagsubfield);
173 push(@big_array, \%this_row);
176 #fill big_row with missing datas
177 foreach my $subfield_code (keys(%witness)) {
178 for (my $i=0;$i<=$#big_array;$i++) {
179 $big_array[$i]{$subfield_code}=" " unless ($big_array[$i]{$subfield_code});
182 # now, construct template !
184 my @header_value_loop;
185 for (my $i=0;$i<=$#big_array; $i++) {
187 foreach my $subfield_code (sort keys(%witness)) {
188 $items_data .="<td>".$big_array[$i]{$subfield_code}."</td>";
191 $row_data{item_value} = $items_data;
192 $row_data{itemnumber} = $itemnums[$i];
193 #reporting this_row values
194 $row_data{'nomod'} = $big_array[$i]{'nomod'};
195 push(@item_value_loop,\%row_data);
197 foreach my $subfield_code (sort keys(%witness)) {
199 $header_value{header_value} = $witness{$subfield_code};
200 push(@header_value_loop, \%header_value);
206 my $authorised_values_sth = $dbh->prepare("select authorised_value,lib from authorised_values where category=? order by lib");
208 foreach my $tag (sort keys %{$tagslib}) {
209 my $previous_tag = '';
210 # loop through each subfield
211 foreach my $subfield (sort keys %{$tagslib->{$tag}}) {
212 next if subfield_is_koha_internal_p($subfield);
213 next if ($tagslib->{$tag}->{$subfield}->{'tab'} ne "10");
215 $subfield_data{tag}=$tag;
216 $subfield_data{subfield}=$subfield;
217 # $subfield_data{marc_lib}=$tagslib->{$tag}->{$subfield}->{lib};
218 $subfield_data{marc_lib}="<span id=\"error$i\">".$tagslib->{$tag}->{$subfield}->{lib}."</span>";
219 $subfield_data{mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
220 $subfield_data{repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
222 ($x,$value) = find_value($tag,$subfield,$itemrecord) if ($itemrecord);
224 #testing branch value if IndependantBranches.
225 my $test = (C4::Context->preference("IndependantBranches")) &&
226 ($tag==$branchtagfield) && ($subfield==$branchtagsubfield) &&
227 (C4::Context->userenv->{flags} != 1) && ($value != C4::Context->userenv->{branch}) ;
228 print $input->redirect("additem.pl?biblionumber=$biblionumber") if ($test);
230 # search for itemcallnumber if applicable
231 if ($tagslib->{$tag}->{$subfield}->{kohafield} eq 'items.itemcallnumber' && C4::Context->preference('itemcallnumber')) {
232 my $CNtag = substr(C4::Context->preference('itemcallnumber'),0,3);
233 my $CNsubfield = substr(C4::Context->preference('itemcallnumber'),3,1);
234 my $temp = $record->field($CNtag);
236 $value = $temp->subfield($CNsubfield);
239 if ($tagslib->{$tag}->{$subfield}->{authorised_value}) {
240 my @authorised_values;
242 # builds list, depending on authorised value...
244 if ($tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) {
245 if ((C4::Context->preference("IndependantBranches")) && (C4::Context->userenv->{flags} != 1)){
246 my $sth=$dbh->prepare("select branchcode,branchname from branches where branchcode = ? order by branchname");
247 $sth->execute(C4::Context->userenv->{branch});
248 push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
249 while (my ($branchcode,$branchname) = $sth->fetchrow_array) {
250 push @authorised_values, $branchcode;
251 $authorised_lib{$branchcode}=$branchname;
254 my $sth=$dbh->prepare("select branchcode,branchname from branches order by branchname");
256 push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
257 while (my ($branchcode,$branchname) = $sth->fetchrow_array) {
258 push @authorised_values, $branchcode;
259 $authorised_lib{$branchcode}=$branchname;
263 } elsif ($tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes") {
264 my $sth=$dbh->prepare("select itemtype,description from itemtypes order by description");
266 push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
267 while (my ($itemtype,$description) = $sth->fetchrow_array) {
268 push @authorised_values, $itemtype;
269 $authorised_lib{$itemtype}=$description;
271 #---- "true" authorised value
273 $authorised_values_sth->execute($tagslib->{$tag}->{$subfield}->{authorised_value});
274 push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
275 while (my ($value,$lib) = $authorised_values_sth->fetchrow_array) {
276 push @authorised_values, $value;
277 $authorised_lib{$value}=$lib;
280 $subfield_data{marc_value}= CGI::scrolling_list(-name=>'field_value',
281 -values=> \@authorised_values,
283 -labels => \%authorised_lib,
287 } elsif ($tagslib->{$tag}->{$subfield}->{thesaurus_category}) {
288 $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>";
290 } elsif ($tagslib->{$tag}->{$subfield}->{'value_builder'}) {
291 my $plugin="../value_builder/".$tagslib->{$tag}->{$subfield}->{'value_builder'};
293 my $extended_param = plugin_parameters($dbh,$record,$tagslib,$i,0);
294 my ($function_name,$javascript) = plugin_javascript($dbh,$record,$tagslib,$i,0);
295 $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";
297 $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\" value=\"$value\" size=50 maxlength=255>";
299 # $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\">";
300 push(@loop_data, \%subfield_data);
304 my ($template, $loggedinuser, $cookie)
305 = get_template_and_user({template_name => "acqui.simple/additem.tmpl",
308 authnotrequired => 0,
309 flagsrequired => {editcatalogue => 1},
313 # what's the next op ? it's what we are not in : an add if we're editing, otherwise, and edit.
314 $template->param(item_loop => \@item_value_loop,
315 item_header_loop => \@header_value_loop,
316 biblionumber =>$biblionumber,
317 biblioitemnumber => $oldrecord->{biblioitemnumber},
318 title => $oldrecord->{title},
319 author => $oldrecord->{author},
321 itemnumber => $itemnumber,
322 itemtagfield => $itemtagfield,
323 itemtagsubfield =>$itemtagsubfield,
325 opisadd => ($nextop eq "saveitem")?0:1);
326 foreach my $error (@errors) {
327 $template->param($error => 1);
329 output_html_with_http_headers $input, $cookie, $template->output;