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
34 my ($tagfield,$insubfield,$record) = @_;
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);
46 return($indicator,$result);
49 sub get_item_from_barcode {
51 my $dbh=C4::Context->dbh;
53 my $rq=$dbh->prepare("SELECT itemnumber from items where items.barcode=?");
54 $rq->execute($barcode);
55 ($result)=$rq->fetchrow;
60 my $dbh = C4::Context->dbh;
61 my $error = $input->param('error');
62 my $biblionumber = $input->param('biblionumber');
63 my $itemnumber = $input->param('itemnumber');
65 my $op = $input->param('op');
68 my $frameworkcode = &MARCfind_frameworkcode($biblionumber);
70 my $tagslib = &MARCgettagslib($dbh,1,$frameworkcode);
71 my $record = GetMarcBiblio($biblionumber);
72 # warn "==>".$record->as_formatted;
73 my $oldrecord = MARCmarc2koha($dbh,$record);
76 my @errors; # store errors found while checking data BEFORE saving item.
77 #-------------------------------------------------------------------------------
78 if ($op eq "additem") {
79 #-------------------------------------------------------------------------------
81 my @tags = $input->param('tag');
82 my @subfields = $input->param('subfield');
83 my @values = $input->param('field_value');
84 # build indicator hash.
85 my @ind_tag = $input->param('ind_tag');
86 my @indicator = $input->param('indicator');
87 my $xml = MARChtml2xml(\@tags,\@subfields,\@values,\@indicator,\@ind_tag);
88 my $record=MARC::Record::new_from_xml($xml, 'UTF-8');
89 # if autoBarcode is ON, calculate barcode...
90 if (C4::Context->preference('autoBarcode')) {
91 my ($tagfield,$tagsubfield) = &MARCfind_marc_from_kohafield($dbh,"items.barcode");
92 unless ($record->field($tagfield)->subfield($tagsubfield)) {
93 my $sth_barcode = $dbh->prepare("select max(abs(barcode)) from items");
94 $sth_barcode->execute;
95 my ($newbarcode) = $sth_barcode->fetchrow;
97 # OK, we have the new barcode, now create the entry in MARC record
98 my $fieldItem = $record->field($tagfield);
99 $record->delete_field($fieldItem);
100 $fieldItem->add_subfields($tagsubfield => $newbarcode);
101 $record->insert_fields_ordered($fieldItem);
104 # check for item barcode # being unique
105 my $addedolditem = MARCmarc2koha($dbh,$record);
106 my $exists = get_item_from_barcode($addedolditem->{'barcode'});
107 push @errors,"barcode_not_unique" if($exists);
108 # if barcode exists, don't create, but report The problem.
109 my ($oldbiblionumber,$oldbibnum,$oldbibitemnum) = AddItem($record,$biblionumber) unless ($exists);
112 $itemrecord = $record;
116 #-------------------------------------------------------------------------------
117 } elsif ($op eq "edititem") {
118 #-------------------------------------------------------------------------------
119 # retrieve item if exist => then, it's a modif
120 $itemrecord = MARCgetitem($biblionumber,$itemnumber);
122 #-------------------------------------------------------------------------------
123 } elsif ($op eq "delitem") {
124 #-------------------------------------------------------------------------------
125 # check that there is no issue on this item before deletion.
126 my $sth=$dbh->prepare("select * from issues i where i.returndate is null and i.itemnumber=?");
127 $sth->execute($itemnumber);
128 my $onloan=$sth->fetchrow;
129 push @errors,"book_on_loan" if ($onloan); ##error book_on_loan added to template as well
133 &DelItem($biblionumber,$itemnumber);
134 print $input->redirect("additem.pl?biblionumber=$biblionumber&frameworkcode=$frameworkcode");
137 #-------------------------------------------------------------------------------
138 } elsif ($op eq "saveitem") {
139 #-------------------------------------------------------------------------------
141 my @tags = $input->param('tag');
142 my @subfields = $input->param('subfield');
143 my @values = $input->param('field_value');
144 # build indicator hash.
145 my @ind_tag = $input->param('ind_tag');
146 my @indicator = $input->param('indicator');
147 # my $itemnumber = $input->param('itemnumber');
148 my $xml = MARChtml2xml(\@tags,\@subfields,\@values,\@indicator,\@ind_tag);
149 my $itemrecord=MARC::Record::new_from_xml($xml, 'UTF-8');
150 # MARC::Record builded => now, record in DB
151 # warn "R: ".$record->as_formatted;
152 my ($oldbiblionumber,$oldbibnum,$oldbibitemnum) = ModItem($itemrecord,$biblionumber,$itemnumber,0);
158 #-------------------------------------------------------------------------------
159 # build screen with existing items. and "new" one
160 #-------------------------------------------------------------------------------
161 my ($template, $loggedinuser, $cookie)
162 = get_template_and_user({template_name => "cataloguing/additem.tmpl",
165 authnotrequired => 0,
166 flagsrequired => {editcatalogue => 1},
171 $indicators{995}=' ';
172 # now, build existiing item list
173 my $temp = GetMarcBiblio( $biblionumber );
174 my @fields = $temp->fields();
175 #my @fields = $record->fields();
176 my %witness; #---- stores the list of subfields used at least once, with the "meaning" of the code
178 #---- finds where items.itemnumber is stored
179 my ($itemtagfield,$itemtagsubfield) = &MARCfind_marc_from_kohafield($dbh,"items.itemnumber",$frameworkcode);
180 my ($branchtagfield,$branchtagsubfield) = &MARCfind_marc_from_kohafield($dbh,"items.homebranch",$frameworkcode);
182 foreach my $field (@fields) {
183 next if ($field->tag()<10);
184 my @subf=$field->subfields;
186 # loop through each subfield
187 for my $i (0..$#subf) {
188 next if ($tagslib->{$field->tag()}->{$subf[$i][0]}->{tab} ne 10 && ($field->tag() ne $itemtagfield && $subf[$i][0] ne $itemtagsubfield));
189 $witness{$subf[$i][0]} = $tagslib->{$field->tag()}->{$subf[$i][0]}->{lib} if ($tagslib->{$field->tag()}->{$subf[$i][0]}->{tab} eq 10);
190 $this_row{$subf[$i][0]} =$subf[$i][1] if ($tagslib->{$field->tag()}->{$subf[$i][0]}->{tab} eq 10);
191 if (($field->tag eq $branchtagfield) && ($subf[$i][$0] eq $branchtagsubfield) && C4::Context->preference("IndependantBranches")) {
193 my $userenv = C4::Context->userenv;
194 unless (($userenv->{'flags'} == 1) or (($userenv->{'branch'} eq $subf[$i][1]))){
195 $this_row{'nomod'}=1;
198 $this_row{itemnumber} = $subf[$i][1] if ($field->tag() eq $itemtagfield && $subf[$i][0] eq $itemtagsubfield);
201 push(@big_array, \%this_row);
204 #fill big_row with missing datas
205 foreach my $subfield_code (keys(%witness)) {
206 for (my $i=0;$i<=$#big_array;$i++) {
207 $big_array[$i]{$subfield_code}=" " unless ($big_array[$i]{$subfield_code});
210 my ($holdingbrtagf,$holdingbrtagsubf) = &MARCfind_marc_from_kohafield($dbh,"items.holdingbranch",$frameworkcode);
211 @big_array = sort {$a->{$holdingbrtagsubf} cmp $b->{$holdingbrtagsubf}} @big_array;
213 # now, construct template !
215 my @header_value_loop;
216 for (my $i=0;$i<=$#big_array; $i++) {
218 foreach my $subfield_code (sort keys(%witness)) {
219 $items_data .="<td>".$big_array[$i]{$subfield_code}."</td>";
222 $items_data =~ s/"/"/g;
223 $row_data{item_value} = $items_data;
224 $row_data{itemnumber} = $big_array[$i]->{itemnumber};
225 #reporting this_row values
226 $row_data{'nomod'} = $big_array[$i]{'nomod'};
227 push(@item_value_loop,\%row_data);
229 foreach my $subfield_code (sort keys(%witness)) {
231 $header_value{header_value} = $witness{$subfield_code};
232 push(@header_value_loop, \%header_value);
238 my $authorised_values_sth = $dbh->prepare("select authorised_value,lib from authorised_values where category=? order by lib");
240 foreach my $tag (sort keys %{$tagslib}) {
241 my $previous_tag = '';
242 # loop through each subfield
243 foreach my $subfield (sort keys %{$tagslib->{$tag}}) {
244 next if subfield_is_koha_internal_p($subfield);
245 next if ($tagslib->{$tag}->{$subfield}->{'tab'} ne "10");
247 $subfield_data{tag}=$tag;
248 $subfield_data{subfield}=$subfield;
249 # $subfield_data{marc_lib}=$tagslib->{$tag}->{$subfield}->{lib};
250 $subfield_data{marc_lib}="<span id=\"error$i\" title=\"".$tagslib->{$tag}->{$subfield}->{lib}."\">".substr($tagslib->{$tag}->{$subfield}->{lib},0,12)."</span>";
251 $subfield_data{mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
252 $subfield_data{repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
253 $subfield_data{hidden}= "display:none" if $tagslib->{$tag}->{$subfield}->{hidden};
255 ($x,$value) = find_value($tag,$subfield,$itemrecord) if ($itemrecord);
256 $value =~ s/"/"/g;
257 #testing branch value if IndependantBranches.
258 my $test = (C4::Context->preference("IndependantBranches")) &&
259 ($tag eq $branchtagfield) && ($subfield eq $branchtagsubfield) &&
260 (C4::Context->userenv->{flags} != 1) && ($value) && ($value ne C4::Context->userenv->{branch}) ;
261 # print $input->redirect(".pl?biblionumber=$biblionumber") if ($test);
262 # search for itemcallnumber if applicable
263 if (!$value && $tagslib->{$tag}->{$subfield}->{kohafield} eq 'items.itemcallnumber' && C4::Context->preference('itemcallnumber')) {
264 my $CNtag = substr(C4::Context->preference('itemcallnumber'),0,3);
265 my $CNsubfield = substr(C4::Context->preference('itemcallnumber'),3,1);
266 my $CNsubfield2 = substr(C4::Context->preference('itemcallnumber'),4,1);
267 my $temp2 = $temp->field($CNtag);
269 $value = ($temp2->subfield($CNsubfield)).' '.($temp2->subfield($CNsubfield2));
270 #remove any trailing space incase one subfield is used
271 $value=~s/^\s+|\s+$//g;
274 if ($tagslib->{$tag}->{$subfield}->{authorised_value}) {
275 my @authorised_values;
277 # builds list, depending on authorised value...
279 if ($tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) {
280 if ((C4::Context->preference("IndependantBranches")) && (C4::Context->userenv->{flags} != 1)){
281 my $sth=$dbh->prepare("select branchcode,branchname from branches where branchcode = ? order by branchname");
282 $sth->execute(C4::Context->userenv->{branch});
283 push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
284 while (my ($branchcode,$branchname) = $sth->fetchrow_array) {
285 push @authorised_values, $branchcode;
286 $authorised_lib{$branchcode}=$branchname;
289 my $sth=$dbh->prepare("select branchcode,branchname from branches order by branchname");
291 push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
292 while (my ($branchcode,$branchname) = $sth->fetchrow_array) {
293 push @authorised_values, $branchcode;
294 $authorised_lib{$branchcode}=$branchname;
298 } elsif ($tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes") {
299 my $sth=$dbh->prepare("select itemtype,description from itemtypes order by description");
301 push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
302 while (my ($itemtype,$description) = $sth->fetchrow_array) {
303 push @authorised_values, $itemtype;
304 $authorised_lib{$itemtype}=$description;
306 #---- "true" authorised value
308 $authorised_values_sth->execute($tagslib->{$tag}->{$subfield}->{authorised_value});
309 push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
310 while (my ($value,$lib) = $authorised_values_sth->fetchrow_array) {
311 push @authorised_values, $value;
312 $authorised_lib{$value}=$lib;
315 $subfield_data{marc_value}= CGI::scrolling_list(-name=>'field_value',
316 -values=> \@authorised_values,
318 -labels => \%authorised_lib,
323 } elsif ($tagslib->{$tag}->{$subfield}->{thesaurus_category}) {
324 $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\" size=47 maxlength=255> <a href=\"javascript:Dopop('cataloguing/thesaurus_popup.pl?category=$tagslib->{$tag}->{$subfield}->{thesaurus_category}&index=$i',$i)\">...</a>";
326 } elsif ($tagslib->{$tag}->{$subfield}->{'value_builder'}) {
327 my $plugin="value_builder/".$tagslib->{$tag}->{$subfield}->{'value_builder'};
329 my $extended_param = plugin_parameters($dbh,$record,$tagslib,$i,0);
330 my ($function_name,$javascript) = plugin_javascript($dbh,$record,$tagslib,$i,0);
331 $subfield_data{marc_value}="<input type=\"text\" value=\"$value\" name=\"field_value\" size=47 maxlength=255 OnFocus=\"javascript:Focus$function_name($i)\" OnBlur=\"javascript:Blur$function_name($i)\"> <a href=\"javascript:Clic$function_name($i)\">...</a> $javascript";
333 $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\" value=\"$value\" size=50 maxlength=255>";
335 # $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\">";
336 push(@loop_data, \%subfield_data);
341 # what's the next op ? it's what we are not in : an add if we're editing, otherwise, and edit.
342 $template->param( title => $record->title() ) if ($record ne "-1");
343 $template->param(item_loop => \@item_value_loop,
344 item_header_loop => \@header_value_loop,
345 biblionumber => $biblionumber,
346 title => $oldrecord->{title},
347 author => $oldrecord->{author},
349 itemnumber => $itemnumber,
350 itemtagfield => $itemtagfield,
351 itemtagsubfield =>$itemtagsubfield,
353 opisadd => ($nextop eq "saveitem")?0:1);
354 foreach my $error (@errors) {
355 $template->param($error => 1);
357 output_html_with_http_headers $input, $cookie, $template->output;