1st commit to go to zebra.
[koha-ffzg.git] / 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::Interface::CGI::Output;
27 use C4::Biblio;
28 use C4::Context;
29 use C4::Koha; # XXX subfield_is_koha_internal_p
30 use C4::Search;
31 use HTML::Template;
32 use MARC::File::USMARC;
33
34 sub find_value {
35         my ($tagfield,$insubfield,$record) = @_;
36         my $result;
37         my $indicator;
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);
44                         }
45                 }
46         }
47         return($indicator,$result);
48 }
49 my $input = new CGI;
50 my $dbh = C4::Context->dbh;
51 my $error = $input->param('error');
52 my $biblionumber = $input->param('biblionumber');
53
54 my $op = $input->param('op');
55 my $itemnum = $input->param('itemnum');
56
57 # find itemtype
58 my $itemtype = &MARCfind_frameworkcode($dbh,$biblionumber);
59
60 my $tagslib = &MARCgettagslib($dbh,1,$itemtype);
61 my $record = MARCgetbiblio($dbh,$biblionumber);
62 my $oldrecord = MARCmarc2koha($dbh,$record);
63 my $itemrecord;
64 my $nextop="additem";
65 my @errors; # store errors found while checking data BEFORE saving item.
66 #------------------------------------------------------------------------------------------------------------------------------
67 if ($op eq "additem") {
68 #------------------------------------------------------------------------------------------------------------------------------
69         # rebuild
70         my @tags = $input->param('tag');
71         my @subfields = $input->param('subfield');
72         my @values = $input->param('field_value');
73         # build indicator hash.
74         my @ind_tag = $input->param('ind_tag');
75         my @indicator = $input->param('indicator');
76         my %indicators;
77         for (my $i=0;$i<=$#ind_tag;$i++) {
78                 $indicators{$ind_tag[$i]} = $indicator[$i];
79         }
80         my $record = MARChtml2marc($dbh,\@tags,\@subfields,\@values,%indicators);
81 # check for item barcode # being unique
82         my $oldbibid = MARCmarc2koha($dbh,$record);
83         my $exists = itemdata($oldbibid->{'barcode'});
84         push @errors,"barcode_not_unique" if($exists);
85 # MARC::Record builded => now, record in DB
86         # if barcode exists, don't create, but report The problem.
87         my ($biblionumber,$oldbibnum,$oldbibitemnum) = NEWnewitem($dbh,$record,$biblionumber) unless ($exists);
88         if ($exists) {
89                 $nextop = "additem";
90                 $itemrecord = $record;
91         } else {
92                 $nextop = "additem";
93         }
94 #------------------------------------------------------------------------------------------------------------------------------
95 } elsif ($op eq "edititem") {
96 #------------------------------------------------------------------------------------------------------------------------------
97 # retrieve item if exist => then, it's a modif
98         $itemrecord = MARCgetitem($dbh,$biblionumber,$itemnum);
99         $nextop="saveitem";
100 #------------------------------------------------------------------------------------------------------------------------------
101 } elsif ($op eq "delitem") {
102 #------------------------------------------------------------------------------------------------------------------------------
103 # retrieve item if exist => then, it's a modif
104         &NEWdelitem($dbh,$biblionumber,$itemnum);
105         $nextop="additem";
106 #------------------------------------------------------------------------------------------------------------------------------
107 } elsif ($op eq "saveitem") {
108 #------------------------------------------------------------------------------------------------------------------------------
109         # rebuild
110         my @tags = $input->param('tag');
111         my @subfields = $input->param('subfield');
112         my @values = $input->param('field_value');
113         # build indicator hash.
114         my @ind_tag = $input->param('ind_tag');
115         my @indicator = $input->param('indicator');
116 #       my $itemnum = $input->param('itemnum');
117         my %indicators;
118         for (my $i=0;$i<=$#ind_tag;$i++) {
119                 $indicators{$ind_tag[$i]} = $indicator[$i];
120         }
121         my $record = MARChtml2marc($dbh,\@tags,\@subfields,\@values,%indicators);
122 # MARC::Record builded => now, record in DB
123 # warn "R: ".$record->as_formatted;
124         my ($biblionumber,$oldbibnum,$oldbibitemnum) = NEWmoditem($dbh,$record,$biblionumber,$itemnum,0);
125         $itemnum="";
126         $nextop="additem";
127 }
128
129 #
130 #------------------------------------------------------------------------------------------------------------------------------
131 # build screen with existing items. and "new" one
132 #------------------------------------------------------------------------------------------------------------------------------
133 my ($template, $loggedinuser, $cookie)
134     = get_template_and_user({template_name => "acqui.simple/additem.tmpl",
135                              query => $input,
136                              type => "intranet",
137                              authnotrequired => 0,
138                              flagsrequired => {editcatalogue => 1},
139                              debug => 1,
140                              });
141
142 my %indicators;
143 $indicators{995}='  ';
144 # now, build existiing item list
145 # my $temp = MARCgetbiblio($dbh,$bibid);
146 # my @fields = $temp->fields();
147 my @fields = $record->fields();
148 my %witness; #---- stores the list of subfields used at least once, with the "meaning" of the code
149 my @big_array;
150 #---- finds where items.itemnumber is stored
151 my ($itemtagfield,$itemtagsubfield) = &MARCfind_marc_from_kohafield($dbh,"items.itemnumber",$itemtype);
152 my ($branchtagfield,$branchtagsubfield) = &MARCfind_marc_from_kohafield($dbh,"items.homebranch",$itemtype);
153 my @itemnums; # array to store itemnums
154 foreach my $field (@fields) {
155         next if ($field->tag()<10);
156         my @subf=$field->subfields;
157         my %this_row;
158 # loop through each subfield
159         for my $i (0..$#subf) {
160                 next if ($tagslib->{$field->tag()}->{$subf[$i][0]}->{tab}  ne 10 && ($field->tag() ne $itemtagfield && $subf[$i][0] ne $itemtagsubfield));
161                 $witness{$subf[$i][0]} = $tagslib->{$field->tag()}->{$subf[$i][0]}->{lib} if ($tagslib->{$field->tag()}->{$subf[$i][0]}->{tab}  eq 10);
162                 $this_row{$subf[$i][0]} =$subf[$i][1] if ($tagslib->{$field->tag()}->{$subf[$i][0]}->{tab}  eq 10);
163                 if (($field->tag eq $branchtagfield) && ($subf[$i][$0] eq $branchtagsubfield) && C4::Context->preference("IndependantBranches")) {
164                         #verifying rights
165                         my $userenv = C4::Context->userenv;
166                         unless ($userenv->{'flags'} == 1){
167                                 if (not ($userenv->{'branch'} eq $subf[$i][1])) {
168                                         $this_row{'nomod'}=1;
169                                         warn "nomod"
170                                 }
171                         }
172                 }
173                 push @itemnums,$this_row{$subf[$i][0]} =$subf[$i][1] if ($field->tag() eq $itemtagfield && $subf[$i][0] eq $itemtagsubfield);
174         }
175         if (%this_row) {
176                 push(@big_array, \%this_row);
177         }
178 }
179 #fill big_row with missing datas
180 foreach my $subfield_code  (keys(%witness)) {
181         for (my $i=0;$i<=$#big_array;$i++) {
182                 $big_array[$i]{$subfield_code}="&nbsp;" unless ($big_array[$i]{$subfield_code});
183         }
184 }
185 # now, construct template !
186 my @item_value_loop;
187 my @header_value_loop;
188 for (my $i=0;$i<=$#big_array; $i++) {
189         my $items_data;
190         foreach my $subfield_code (sort keys(%witness)) {
191                 $items_data .="<td>".$big_array[$i]{$subfield_code}."</td>";
192         }
193         my %row_data;
194         $row_data{item_value} = $items_data;
195         $row_data{itemnum} = $itemnums[$i];
196         #reporting this_row values
197         $row_data{'nomod'} = $big_array[$i]{'nomod'};
198         push(@item_value_loop,\%row_data);
199 }
200 foreach my $subfield_code (sort keys(%witness)) {
201         my %header_value;
202         $header_value{header_value} = $witness{$subfield_code};
203         push(@header_value_loop, \%header_value);
204 }
205
206 # next item form
207 my @loop_data =();
208 my $i=0;
209 my $authorised_values_sth = $dbh->prepare("select authorised_value,lib from authorised_values where category=? order by lib");
210
211 foreach my $tag (sort keys %{$tagslib}) {
212         my $previous_tag = '';
213 # loop through each subfield
214         foreach my $subfield (sort keys %{$tagslib->{$tag}}) {
215                 next if subfield_is_koha_internal_p($subfield);
216                 next if ($tagslib->{$tag}->{$subfield}->{'tab'}  ne "10");
217                 my %subfield_data;
218                 $subfield_data{tag}=$tag;
219                 $subfield_data{subfield}=$subfield;
220 #               $subfield_data{marc_lib}=$tagslib->{$tag}->{$subfield}->{lib};
221                 $subfield_data{marc_lib}="<span id=\"error$i\">".$tagslib->{$tag}->{$subfield}->{lib}."</span>";
222                 $subfield_data{mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
223                 $subfield_data{repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
224                 my ($x,$value);
225                 ($x,$value) = find_value($tag,$subfield,$itemrecord) if ($itemrecord);
226                 
227                 #testing branch value if IndependantBranches.
228                 my $test = (C4::Context->preference("IndependantBranches")) && 
229                                         ($tag==$branchtagfield) && ($subfield==$branchtagsubfield) &&
230                                         (C4::Context->userenv->{flags} != 1) && ($value != C4::Context->userenv->{branch}) ;
231                 print $input->redirect("additem.pl?biblionumber=$biblionumber") if ($test);
232                 
233                 # search for itemcallnumber if applicable
234                 if ($tagslib->{$tag}->{$subfield}->{kohafield} eq 'items.itemcallnumber' && C4::Context->preference('itemcallnumber')) {
235                         my $CNtag = substr(C4::Context->preference('itemcallnumber'),0,3);
236                         my $CNsubfield = substr(C4::Context->preference('itemcallnumber'),3,1);
237                         my $temp = $record->field($CNtag);
238                         if ($temp) {
239                                 $value = $temp->subfield($CNsubfield);
240                         }
241                 }
242                 if ($tagslib->{$tag}->{$subfield}->{authorised_value}) {
243                         my @authorised_values;
244                         my %authorised_lib;
245                         # builds list, depending on authorised value...
246                         #---- branch
247                         if ($tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) {
248                                 if ((C4::Context->preference("IndependantBranches")) && (C4::Context->userenv->{flags} != 1)){
249                                                 my $sth=$dbh->prepare("select branchcode,branchname from branches where branchcode = ? order by branchname");
250                                                 $sth->execute(C4::Context->userenv->{branch});
251                                                 push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
252                                                 while (my ($branchcode,$branchname) = $sth->fetchrow_array) {
253                                                         push @authorised_values, $branchcode;
254                                                         $authorised_lib{$branchcode}=$branchname;
255                                                 }
256                                 } else {
257                                         my $sth=$dbh->prepare("select branchcode,branchname from branches order by branchname");
258                                         $sth->execute;
259                                         push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
260                                         while (my ($branchcode,$branchname) = $sth->fetchrow_array) {
261                                                 push @authorised_values, $branchcode;
262                                                 $authorised_lib{$branchcode}=$branchname;
263                                         }
264                                 }
265                         #----- itemtypes
266                         } elsif ($tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes") {
267                                 my $sth=$dbh->prepare("select itemtype,description from itemtypes order by description");
268                                 $sth->execute;
269                                 push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
270                                 while (my ($itemtype,$description) = $sth->fetchrow_array) {
271                                         push @authorised_values, $itemtype;
272                                         $authorised_lib{$itemtype}=$description;
273                                 }
274                         #---- "true" authorised value
275                         } else {
276                                 $authorised_values_sth->execute($tagslib->{$tag}->{$subfield}->{authorised_value});
277                                 push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
278                                 while (my ($value,$lib) = $authorised_values_sth->fetchrow_array) {
279                                         push @authorised_values, $value;
280                                         $authorised_lib{$value}=$lib;
281                                 }
282                         }
283                         $subfield_data{marc_value}= CGI::scrolling_list(-name=>'field_value',
284                                                                                                                                                 -values=> \@authorised_values,
285                                                                                                                                                 -default=>"$value",
286                                                                                                                                                 -labels => \%authorised_lib,
287                                                                                                                                                 -size=>1,
288                                                                                                                                                 -multiple=>0,
289                                                                                                                                                 );
290                 } elsif ($tagslib->{$tag}->{$subfield}->{thesaurus_category}) {
291                         $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>";
292                         #"
293                 } elsif ($tagslib->{$tag}->{$subfield}->{'value_builder'}) {
294                         my $plugin="../value_builder/".$tagslib->{$tag}->{$subfield}->{'value_builder'};
295                         require $plugin;
296                         my $extended_param = plugin_parameters($dbh,$record,$tagslib,$i,0);
297                         my ($function_name,$javascript) = plugin_javascript($dbh,$record,$tagslib,$i,0);
298                         $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";
299                 } else {
300                         $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\" value=\"$value\" size=50 maxlength=255>";
301                 }
302 #               $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\">";
303                 push(@loop_data, \%subfield_data);
304                 $i++
305         }
306 }
307 my ($template, $loggedinuser, $cookie)
308     = get_template_and_user({template_name => "acqui.simple/additem.tmpl",
309                              query => $input,
310                              type => "intranet",
311                              authnotrequired => 0,
312                              flagsrequired => {editcatalogue => 1},
313                              debug => 1,
314                              });
315
316 # what's the next op ? it's what we are not in : an add if we're editing, otherwise, and edit.
317 $template->param(item_loop => \@item_value_loop,
318                                                 item_header_loop => \@header_value_loop,
319                                                 biblionumber =>$biblionumber,
320                                                 title => $oldrecord->{title},
321                                                 author => $oldrecord->{author},
322                                                 item => \@loop_data,
323                                                 itemnum => $itemnum,
324                                                 itemtagfield => $itemtagfield,
325                                                 itemtagsubfield =>$itemtagsubfield,
326                                                 op => $nextop,
327                                                 opisadd => ($nextop eq "saveitem")?0:1);
328 foreach my $error (@errors) {
329         $template->param($error => 1);
330 }
331 output_html_with_http_headers $input, $cookie, $template->output;