Patch from Joe Atzberger to remove $Id$ and $Log$ from scripts
[koha.git] / cataloguing / additem.pl
1 #!/usr/bin/perl
2
3
4 # Copyright 2000-2002 Katipo Communications
5 #
6 # This file is part of Koha.
7 #
8 # Koha is free software; you can redistribute it and/or modify it under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
11 # version.
12 #
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License along with
18 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
19 # Suite 330, Boston, MA  02111-1307 USA
20
21 use CGI;
22 use strict;
23 use C4::Auth;
24 use C4::Output;
25 use C4::Biblio;
26 use C4::Context;
27 use C4::Koha; # XXX subfield_is_koha_internal_p
28
29 use MARC::File::XML;
30
31 sub find_value {
32     my ($tagfield,$insubfield,$record) = @_;
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
47 sub get_item_from_barcode {
48     my ($barcode)=@_;
49     my $dbh=C4::Context->dbh;
50     my $result;
51     my $rq=$dbh->prepare("SELECT itemnumber from items where items.barcode=?");
52     $rq->execute($barcode);
53     ($result)=$rq->fetchrow;
54     return($result);
55 }
56
57 my $input = new CGI;
58 my $dbh = C4::Context->dbh;
59 my $error = $input->param('error');
60 my $biblionumber = $input->param('biblionumber');
61 my $itemnumber = $input->param('itemnumber');
62
63 my $op = $input->param('op');
64
65 # find itemtype
66 my $frameworkcode = &GetFrameworkCode($biblionumber);
67
68 my $tagslib = &GetMarcStructure(1,$frameworkcode);
69 my $record = GetMarcBiblio($biblionumber);
70 my $oldrecord = TransformMarcToKoha($dbh,$record);
71 my $itemrecord;
72 my $nextop="additem";
73 my @errors; # store errors found while checking data BEFORE saving item.
74 #-------------------------------------------------------------------------------
75 if ($op eq "additem") {
76 #-------------------------------------------------------------------------------
77     # rebuild
78     my @tags = $input->param('tag');
79     my @subfields = $input->param('subfield');
80     my @values = $input->param('field_value');
81     # build indicator hash.
82     my @ind_tag = $input->param('ind_tag');
83     my @indicator = $input->param('indicator');
84     my $xml = TransformHtmlToXml(\@tags,\@subfields,\@values,\@indicator,\@ind_tag, 'ITEM');
85         my $record=MARC::Record::new_from_xml($xml, 'UTF-8');
86     # if autoBarcode is ON, calculate barcode...
87     if (C4::Context->preference('autoBarcode')) {
88         my ($tagfield,$tagsubfield) = &GetMarcFromKohaField("items.barcode",$frameworkcode);
89         unless ($record->field($tagfield)->subfield($tagsubfield)) {
90             my $sth_barcode = $dbh->prepare("select max(abs(barcode)) from items");
91             $sth_barcode->execute;
92             my ($newbarcode) = $sth_barcode->fetchrow;
93             $newbarcode++;
94             # OK, we have the new barcode, now create the entry in MARC record
95             my $fieldItem = $record->field($tagfield);
96             $record->delete_field($fieldItem);
97             $fieldItem->add_subfields($tagsubfield => $newbarcode);
98             $record->insert_fields_ordered($fieldItem);
99         }
100     }
101 # check for item barcode # being unique
102     my $addedolditem = TransformMarcToKoha($dbh,$record);
103     my $exists = get_item_from_barcode($addedolditem->{'barcode'});
104     push @errors,"barcode_not_unique" if($exists);
105     # if barcode exists, don't create, but report The problem.
106     my ($oldbiblionumber,$oldbibnum,$oldbibitemnum) = AddItem($record,$biblionumber) unless ($exists);
107     if ($exists) {
108     $nextop = "additem";
109         $itemrecord = $record;
110     } else {
111         $nextop = "additem";
112     }
113 #-------------------------------------------------------------------------------
114 } elsif ($op eq "edititem") {
115 #-------------------------------------------------------------------------------
116 # retrieve item if exist => then, it's a modif
117     $itemrecord = GetMarcItem($biblionumber,$itemnumber);
118     $nextop="saveitem";
119 #-------------------------------------------------------------------------------
120 } elsif ($op eq "delitem") {
121 #-------------------------------------------------------------------------------
122     # check that there is no issue on this item before deletion.
123     my $sth=$dbh->prepare("select * from issues i where i.returndate is null and i.itemnumber=?");
124     $sth->execute($itemnumber);
125     my $onloan=$sth->fetchrow;
126     push @errors,"book_on_loan" if ($onloan); ##error book_on_loan added to template as well
127     if ($onloan){
128     $nextop="additem";
129     } else {
130         &DelItem($biblionumber,$itemnumber);
131         print $input->redirect("additem.pl?biblionumber=$biblionumber&frameworkcode=$frameworkcode");
132         #$nextop="additem";
133     }
134 #-------------------------------------------------------------------------------
135 } elsif ($op eq "saveitem") {
136 #-------------------------------------------------------------------------------
137     # rebuild
138     my @tags = $input->param('tag');
139     my @subfields = $input->param('subfield');
140     my @values = $input->param('field_value');
141     # build indicator hash.
142     my @ind_tag = $input->param('ind_tag');
143     my @indicator = $input->param('indicator');
144 #    my $itemnumber = $input->param('itemnumber');
145     my $xml = TransformHtmlToXml(\@tags,\@subfields,\@values,\@indicator,\@ind_tag,'ITEM');
146         my $itemrecord=MARC::Record::new_from_xml($xml, 'UTF-8');
147 # MARC::Record builded => now, record in DB
148 # warn "R: ".$record->as_formatted;
149     my ($oldbiblionumber,$oldbibnum,$oldbibitemnum) = ModItem($itemrecord,$biblionumber,$itemnumber,0);
150     $itemnumber="";
151     $nextop="additem";
152 }
153
154 #
155 #-------------------------------------------------------------------------------
156 # build screen with existing items. and "new" one
157 #-------------------------------------------------------------------------------
158 my ($template, $loggedinuser, $cookie)
159     = get_template_and_user({template_name => "cataloguing/additem.tmpl",
160                  query => $input,
161                  type => "intranet",
162                  authnotrequired => 0,
163                  flagsrequired => {editcatalogue => 1},
164                  debug => 1,
165                  });
166
167 my %indicators;
168 $indicators{995}='  ';
169 # now, build existiing item list
170 my $temp = GetMarcBiblio( $biblionumber );
171 my @fields = $temp->fields();
172 #my @fields = $record->fields();
173 my %witness; #---- stores the list of subfields used at least once, with the "meaning" of the code
174 my @big_array;
175 #---- finds where items.itemnumber is stored
176 my ($itemtagfield,$itemtagsubfield) = &GetMarcFromKohaField("items.itemnumber",$frameworkcode);
177 my ($branchtagfield,$branchtagsubfield) = &GetMarcFromKohaField("items.homebranch",$frameworkcode);
178
179 foreach my $field (@fields) {
180     next if ($field->tag()<10);
181     my @subf=$field->subfields;
182     my %this_row;
183 # loop through each subfield
184     for my $i (0..$#subf) {
185         next if ($tagslib->{$field->tag()}->{$subf[$i][0]}->{tab}  ne 10 && ($field->tag() ne $itemtagfield && $subf[$i][0] ne $itemtagsubfield));
186         $witness{$subf[$i][0]} = $tagslib->{$field->tag()}->{$subf[$i][0]}->{lib} if ($tagslib->{$field->tag()}->{$subf[$i][0]}->{tab}  eq 10);
187         $this_row{$subf[$i][0]} =$subf[$i][1] if ($tagslib->{$field->tag()}->{$subf[$i][0]}->{tab}  eq 10);
188         if (($field->tag eq $branchtagfield) && ($subf[$i][$0] eq $branchtagsubfield) && C4::Context->preference("IndependantBranches")) {
189             #verifying rights
190             my $userenv = C4::Context->userenv;
191             unless (($userenv->{'flags'} == 1) or (($userenv->{'branch'} eq $subf[$i][1]))){
192                     $this_row{'nomod'}=1;
193             }
194         }
195         $this_row{itemnumber} = $subf[$i][1] if ($field->tag() eq $itemtagfield && $subf[$i][0] eq $itemtagsubfield);
196     }
197     if (%this_row) {
198         push(@big_array, \%this_row);
199     }
200 }
201 #fill big_row with missing datas
202 foreach my $subfield_code  (keys(%witness)) {
203     for (my $i=0;$i<=$#big_array;$i++) {
204         $big_array[$i]{$subfield_code}="&nbsp;" unless ($big_array[$i]{$subfield_code});
205     }
206 }
207 my ($holdingbrtagf,$holdingbrtagsubf) = &GetMarcFromKohaField("items.holdingbranch",$frameworkcode);
208 @big_array = sort {$a->{$holdingbrtagsubf} cmp $b->{$holdingbrtagsubf}} @big_array;
209
210 # now, construct template !
211 my @item_value_loop;
212 my @header_value_loop;
213 for (my $i=0;$i<=$#big_array; $i++) {
214     my $items_data;
215     foreach my $subfield_code (sort keys(%witness)) {
216         $items_data .="<td>".$big_array[$i]{$subfield_code}."</td>";
217     }
218     my %row_data;
219     $items_data =~ s/"/&quot;/g;
220     $row_data{item_value} = $items_data;
221     $row_data{itemnumber} = $big_array[$i]->{itemnumber};
222     #reporting this_row values
223     $row_data{'nomod'} = $big_array[$i]{'nomod'};
224     push(@item_value_loop,\%row_data);
225 }
226 foreach my $subfield_code (sort keys(%witness)) {
227     my %header_value;
228     $header_value{header_value} = $witness{$subfield_code};
229     push(@header_value_loop, \%header_value);
230 }
231
232 # next item form
233 my @loop_data =();
234 my $i=0;
235 my $authorised_values_sth = $dbh->prepare("select authorised_value,lib from authorised_values where category=? order by lib");
236
237 foreach my $tag (sort keys %{$tagslib}) {
238   my $previous_tag = '';
239 # loop through each subfield
240   foreach my $subfield (sort keys %{$tagslib->{$tag}}) {
241     next if subfield_is_koha_internal_p($subfield);
242     next if ($tagslib->{$tag}->{$subfield}->{'tab'}  ne "10");
243     my %subfield_data;
244     $subfield_data{tag}=$tag;
245     $subfield_data{subfield}=$subfield;
246 #        $subfield_data{marc_lib}=$tagslib->{$tag}->{$subfield}->{lib};
247     $subfield_data{marc_lib}="<span id=\"error$i\" title=\"".$tagslib->{$tag}->{$subfield}->{lib}."\">".$tagslib->{$tag}->{$subfield}->{lib}."</span>";
248     $subfield_data{mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
249     $subfield_data{repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
250     $subfield_data{hidden}= "display:none" if $tagslib->{$tag}->{$subfield}->{hidden};
251     my ($x,$value);
252     ($x,$value) = find_value($tag,$subfield,$itemrecord) if ($itemrecord);
253     $value =~ s/"/&quot;/g;
254     #testing branch value if IndependantBranches.
255     my $test = (C4::Context->preference("IndependantBranches")) &&
256               ($tag eq $branchtagfield) && ($subfield eq $branchtagsubfield) &&
257               (C4::Context->userenv->{flags} != 1) && ($value) && ($value ne C4::Context->userenv->{branch}) ;
258 #         print $input->redirect(".pl?biblionumber=$biblionumber") if ($test);
259         # search for itemcallnumber if applicable
260     if (!$value && $tagslib->{$tag}->{$subfield}->{kohafield} eq 'items.itemcallnumber' && C4::Context->preference('itemcallnumber')) {
261       my $CNtag = substr(C4::Context->preference('itemcallnumber'),0,3);
262       my $CNsubfield = substr(C4::Context->preference('itemcallnumber'),3,1);
263                         my $CNsubfield2 = substr(C4::Context->preference('itemcallnumber'),4,1);
264                         my $temp2 = $temp->field($CNtag);
265                         if ($temp2) {
266                                 $value = ($temp2->subfield($CNsubfield)).' '.($temp2->subfield($CNsubfield2));
267 #remove any trailing space incase one subfield is used
268         $value=~s/^\s+|\s+$//g;
269       }
270     }
271     if ($tagslib->{$tag}->{$subfield}->{authorised_value}) {
272       my @authorised_values;
273       my %authorised_lib;
274       # builds list, depending on authorised value...
275       #---- branch
276       if ($tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) {
277         if ((C4::Context->preference("IndependantBranches")) && (C4::Context->userenv->{flags} != 1)){
278           my $sth=$dbh->prepare("select branchcode,branchname from branches where branchcode = ? order by branchname");
279           $sth->execute(C4::Context->userenv->{branch});
280           push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
281           while (my ($branchcode,$branchname) = $sth->fetchrow_array) {
282               push @authorised_values, $branchcode;
283               $authorised_lib{$branchcode}=$branchname;
284           }
285         } else {
286           my $sth=$dbh->prepare("select branchcode,branchname from branches order by branchname");
287           $sth->execute;
288           push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
289           while (my ($branchcode,$branchname) = $sth->fetchrow_array) {
290               push @authorised_values, $branchcode;
291               $authorised_lib{$branchcode}=$branchname;
292           }
293         }
294         #----- itemtypes
295         } elsif ($tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes") {
296             my $sth=$dbh->prepare("select itemtype,description from itemtypes order by description");
297             $sth->execute;
298             push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
299             while (my ($itemtype,$description) = $sth->fetchrow_array) {
300                 push @authorised_values, $itemtype;
301                 $authorised_lib{$itemtype}=$description;
302             }
303       #---- "true" authorised value
304       } else {
305           $authorised_values_sth->execute($tagslib->{$tag}->{$subfield}->{authorised_value});
306           push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
307           while (my ($authvalue,$lib) = $authorised_values_sth->fetchrow_array) {
308               push @authorised_values, $authvalue;
309               $authorised_lib{$authvalue}=$lib;
310           }
311       }
312       $subfield_data{marc_value}= CGI::scrolling_list(-name=>'field_value',
313                                                                   -values=> \@authorised_values,
314                                                                   -default=>"$value",
315                                                                   -labels => \%authorised_lib,
316                                                                   -size=>1,
317                                                                   -tabindex=>'',
318                                                                   -multiple=>0,
319                                                                   );
320     } elsif ($tagslib->{$tag}->{$subfield}->{thesaurus_category}) {
321       $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>";
322           #"
323     } elsif ($tagslib->{$tag}->{$subfield}->{'value_builder'}) {
324       my $plugin="value_builder/".$tagslib->{$tag}->{$subfield}->{'value_builder'};
325       require $plugin;
326       my $extended_param = plugin_parameters($dbh,$record,$tagslib,$i,0);
327       my ($function_name,$javascript) = plugin_javascript($dbh,$record,$tagslib,$i,0);
328       $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";
329     } else {
330       $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\" value=\"$value\" size=\"50\" maxlength=\"255\" />";
331     }
332 #        $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\">";
333         push(@loop_data, \%subfield_data);
334         $i++
335     }
336 }
337
338 # what's the next op ? it's what we are not in : an add if we're editing, otherwise, and edit.
339 $template->param( title => $record->title() ) if ($record ne "-1");
340 $template->param(item_loop => \@item_value_loop,
341                         item_header_loop => \@header_value_loop,
342                         biblionumber => $biblionumber,
343                         title => $oldrecord->{title},
344                         author => $oldrecord->{author},
345                         item => \@loop_data,
346                         itemnumber => $itemnumber,
347                         itemtagfield => $itemtagfield,
348                         itemtagsubfield =>$itemtagsubfield,
349                         op => $nextop,
350                         opisadd => ($nextop eq "saveitem")?0:1);
351 foreach my $error (@errors) {
352     $template->param($error => 1);
353 }
354 output_html_with_http_headers $input, $cookie, $template->output;