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
28 use C4::Koha; # XXX subfield_is_koha_internal_p
33 my ($tagfield,$insubfield,$record) = @_;
36 foreach my $field ($record->field($tagfield)) {
37 my @subfields = $field->subfields();
38 foreach my $subfield (@subfields) {
39 if (@$subfield[0] eq $insubfield) {
40 $result .= @$subfield[1];
41 $indicator = $field->indicator(1).$field->indicator(2);
45 return($indicator,$result);
48 sub get_item_from_barcode {
50 my $dbh=C4::Context->dbh;
52 my $rq=$dbh->prepare("SELECT itemnumber from items where items.barcode=?");
53 $rq->execute($barcode);
54 ($result)=$rq->fetchrow;
59 my $dbh = C4::Context->dbh;
60 my $error = $input->param('error');
61 my $biblionumber = $input->param('biblionumber');
62 my $itemnumber = $input->param('itemnumber');
64 my $op = $input->param('op');
67 my $frameworkcode = &GetFrameworkCode($biblionumber);
69 my $tagslib = &GetMarcStructure(1,$frameworkcode);
70 my $record = GetMarcBiblio($biblionumber);
71 # warn "==>".$record->as_formatted;
72 my $oldrecord = TransformMarcToKoha($dbh,$record);
75 my @errors; # store errors found while checking data BEFORE saving item.
76 #-------------------------------------------------------------------------------
77 if ($op eq "additem") {
78 #-------------------------------------------------------------------------------
80 my @tags = $input->param('tag');
81 my @subfields = $input->param('subfield');
82 my @values = $input->param('field_value');
83 # build indicator hash.
84 my @ind_tag = $input->param('ind_tag');
85 my @indicator = $input->param('indicator');
86 my $xml = TransformHtmlToXml(\@tags,\@subfields,\@values,\@indicator,\@ind_tag, 'ITEM');
87 my $record=MARC::Record::new_from_xml($xml, 'UTF-8');
88 # if autoBarcode is ON, calculate barcode...
89 if (C4::Context->preference('autoBarcode')) {
90 my ($tagfield,$tagsubfield) = &GetMarcFromKohaField("items.barcode",$frameworkcode);
91 unless ($record->field($tagfield)->subfield($tagsubfield)) {
92 my $sth_barcode = $dbh->prepare("select max(abs(barcode)) from items");
93 $sth_barcode->execute;
94 my ($newbarcode) = $sth_barcode->fetchrow;
96 # OK, we have the new barcode, now create the entry in MARC record
97 my $fieldItem = $record->field($tagfield);
98 $record->delete_field($fieldItem);
99 $fieldItem->add_subfields($tagsubfield => $newbarcode);
100 $record->insert_fields_ordered($fieldItem);
103 # check for item barcode # being unique
104 my $addedolditem = TransformMarcToKoha($dbh,$record);
105 my $exists = get_item_from_barcode($addedolditem->{'barcode'});
106 push @errors,"barcode_not_unique" if($exists);
107 # if barcode exists, don't create, but report The problem.
108 my ($oldbiblionumber,$oldbibnum,$oldbibitemnum) = AddItem($record,$biblionumber) unless ($exists);
111 $itemrecord = $record;
115 #-------------------------------------------------------------------------------
116 } elsif ($op eq "edititem") {
117 #-------------------------------------------------------------------------------
118 # retrieve item if exist => then, it's a modif
119 $itemrecord = GetMarcItem($biblionumber,$itemnumber);
121 #-------------------------------------------------------------------------------
122 } elsif ($op eq "delitem") {
123 #-------------------------------------------------------------------------------
124 # check that there is no issue on this item before deletion.
125 my $sth=$dbh->prepare("select * from issues i where i.returndate is null and i.itemnumber=?");
126 $sth->execute($itemnumber);
127 my $onloan=$sth->fetchrow;
128 push @errors,"book_on_loan" if ($onloan); ##error book_on_loan added to template as well
132 &DelItem($biblionumber,$itemnumber);
133 print $input->redirect("additem.pl?biblionumber=$biblionumber&frameworkcode=$frameworkcode");
136 #-------------------------------------------------------------------------------
137 } elsif ($op eq "saveitem") {
138 #-------------------------------------------------------------------------------
140 my @tags = $input->param('tag');
141 my @subfields = $input->param('subfield');
142 my @values = $input->param('field_value');
143 # build indicator hash.
144 my @ind_tag = $input->param('ind_tag');
145 my @indicator = $input->param('indicator');
146 # my $itemnumber = $input->param('itemnumber');
147 my $xml = TransformHtmlToXml(\@tags,\@subfields,\@values,\@indicator,\@ind_tag,'ITEM');
148 my $itemrecord=MARC::Record::new_from_xml($xml, 'UTF-8');
149 # MARC::Record builded => now, record in DB
150 # warn "R: ".$record->as_formatted;
151 my ($oldbiblionumber,$oldbibnum,$oldbibitemnum) = ModItem($itemrecord,$biblionumber,$itemnumber,0);
157 #-------------------------------------------------------------------------------
158 # build screen with existing items. and "new" one
159 #-------------------------------------------------------------------------------
160 my ($template, $loggedinuser, $cookie)
161 = get_template_and_user({template_name => "cataloguing/additem.tmpl",
164 authnotrequired => 0,
165 flagsrequired => {editcatalogue => 1},
170 $indicators{995}=' ';
171 # now, build existiing item list
172 my $temp = GetMarcBiblio( $biblionumber );
173 my @fields = $temp->fields();
174 #my @fields = $record->fields();
175 my %witness; #---- stores the list of subfields used at least once, with the "meaning" of the code
177 #---- finds where items.itemnumber is stored
178 my ($itemtagfield,$itemtagsubfield) = &GetMarcFromKohaField("items.itemnumber",$frameworkcode);
179 my ($branchtagfield,$branchtagsubfield) = &GetMarcFromKohaField("items.homebranch",$frameworkcode);
181 foreach my $field (@fields) {
182 next if ($field->tag()<10);
183 my @subf=$field->subfields;
185 # loop through each subfield
186 for my $i (0..$#subf) {
187 next if ($tagslib->{$field->tag()}->{$subf[$i][0]}->{tab} ne 10 && ($field->tag() ne $itemtagfield && $subf[$i][0] ne $itemtagsubfield));
188 $witness{$subf[$i][0]} = $tagslib->{$field->tag()}->{$subf[$i][0]}->{lib} if ($tagslib->{$field->tag()}->{$subf[$i][0]}->{tab} eq 10);
189 $this_row{$subf[$i][0]} =$subf[$i][1] if ($tagslib->{$field->tag()}->{$subf[$i][0]}->{tab} eq 10);
190 if (($field->tag eq $branchtagfield) && ($subf[$i][$0] eq $branchtagsubfield) && C4::Context->preference("IndependantBranches")) {
192 my $userenv = C4::Context->userenv;
193 unless (($userenv->{'flags'} == 1) or (($userenv->{'branch'} eq $subf[$i][1]))){
194 $this_row{'nomod'}=1;
197 $this_row{itemnumber} = $subf[$i][1] if ($field->tag() eq $itemtagfield && $subf[$i][0] eq $itemtagsubfield);
200 push(@big_array, \%this_row);
203 #fill big_row with missing datas
204 foreach my $subfield_code (keys(%witness)) {
205 for (my $i=0;$i<=$#big_array;$i++) {
206 $big_array[$i]{$subfield_code}=" " unless ($big_array[$i]{$subfield_code});
209 my ($holdingbrtagf,$holdingbrtagsubf) = &GetMarcFromKohaField("items.holdingbranch",$frameworkcode);
210 @big_array = sort {$a->{$holdingbrtagsubf} cmp $b->{$holdingbrtagsubf}} @big_array;
212 # now, construct template !
214 my @header_value_loop;
215 for (my $i=0;$i<=$#big_array; $i++) {
217 foreach my $subfield_code (sort keys(%witness)) {
218 $items_data .="<td>".$big_array[$i]{$subfield_code}."</td>";
221 $items_data =~ s/"/"/g;
222 $row_data{item_value} = $items_data;
223 $row_data{itemnumber} = $big_array[$i]->{itemnumber};
224 #reporting this_row values
225 $row_data{'nomod'} = $big_array[$i]{'nomod'};
226 push(@item_value_loop,\%row_data);
228 foreach my $subfield_code (sort keys(%witness)) {
230 $header_value{header_value} = $witness{$subfield_code};
231 push(@header_value_loop, \%header_value);
237 my $authorised_values_sth = $dbh->prepare("select authorised_value,lib from authorised_values where category=? order by lib");
239 foreach my $tag (sort keys %{$tagslib}) {
240 my $previous_tag = '';
241 # loop through each subfield
242 foreach my $subfield (sort keys %{$tagslib->{$tag}}) {
243 next if subfield_is_koha_internal_p($subfield);
244 next if ($tagslib->{$tag}->{$subfield}->{'tab'} ne "10");
246 $subfield_data{tag}=$tag;
247 $subfield_data{subfield}=$subfield;
248 # $subfield_data{marc_lib}=$tagslib->{$tag}->{$subfield}->{lib};
249 $subfield_data{marc_lib}="<span id=\"error$i\" title=\"".$tagslib->{$tag}->{$subfield}->{lib}."\">".substr($tagslib->{$tag}->{$subfield}->{lib},0,12)."</span>";
250 $subfield_data{mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
251 $subfield_data{repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
252 $subfield_data{hidden}= "display:none" if $tagslib->{$tag}->{$subfield}->{hidden};
254 ($x,$value) = find_value($tag,$subfield,$itemrecord) if ($itemrecord);
255 $value =~ s/"/"/g;
256 #testing branch value if IndependantBranches.
257 my $test = (C4::Context->preference("IndependantBranches")) &&
258 ($tag eq $branchtagfield) && ($subfield eq $branchtagsubfield) &&
259 (C4::Context->userenv->{flags} != 1) && ($value) && ($value ne C4::Context->userenv->{branch}) ;
260 # print $input->redirect(".pl?biblionumber=$biblionumber") if ($test);
261 # search for itemcallnumber if applicable
262 if (!$value && $tagslib->{$tag}->{$subfield}->{kohafield} eq 'items.itemcallnumber' && C4::Context->preference('itemcallnumber')) {
263 my $CNtag = substr(C4::Context->preference('itemcallnumber'),0,3);
264 my $CNsubfield = substr(C4::Context->preference('itemcallnumber'),3,1);
265 my $CNsubfield2 = substr(C4::Context->preference('itemcallnumber'),4,1);
266 my $temp2 = $temp->field($CNtag);
268 $value = ($temp2->subfield($CNsubfield)).' '.($temp2->subfield($CNsubfield2));
269 #remove any trailing space incase one subfield is used
270 $value=~s/^\s+|\s+$//g;
273 if ($tagslib->{$tag}->{$subfield}->{authorised_value}) {
274 my @authorised_values;
276 # builds list, depending on authorised value...
278 if ($tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) {
279 if ((C4::Context->preference("IndependantBranches")) && (C4::Context->userenv->{flags} != 1)){
280 my $sth=$dbh->prepare("select branchcode,branchname from branches where branchcode = ? order by branchname");
281 $sth->execute(C4::Context->userenv->{branch});
282 push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
283 while (my ($branchcode,$branchname) = $sth->fetchrow_array) {
284 push @authorised_values, $branchcode;
285 $authorised_lib{$branchcode}=$branchname;
288 my $sth=$dbh->prepare("select branchcode,branchname from branches order by branchname");
290 push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
291 while (my ($branchcode,$branchname) = $sth->fetchrow_array) {
292 push @authorised_values, $branchcode;
293 $authorised_lib{$branchcode}=$branchname;
297 } elsif ($tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes") {
298 my $sth=$dbh->prepare("select itemtype,description from itemtypes order by description");
300 push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
301 while (my ($itemtype,$description) = $sth->fetchrow_array) {
302 push @authorised_values, $itemtype;
303 $authorised_lib{$itemtype}=$description;
305 #---- "true" authorised value
307 $authorised_values_sth->execute($tagslib->{$tag}->{$subfield}->{authorised_value});
308 push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
309 while (my ($value,$lib) = $authorised_values_sth->fetchrow_array) {
310 push @authorised_values, $value;
311 $authorised_lib{$value}=$lib;
314 $subfield_data{marc_value}= CGI::scrolling_list(-name=>'field_value',
315 -values=> \@authorised_values,
317 -labels => \%authorised_lib,
322 } elsif ($tagslib->{$tag}->{$subfield}->{thesaurus_category}) {
323 $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>";
325 } elsif ($tagslib->{$tag}->{$subfield}->{'value_builder'}) {
326 my $plugin="value_builder/".$tagslib->{$tag}->{$subfield}->{'value_builder'};
328 my $extended_param = plugin_parameters($dbh,$record,$tagslib,$i,0);
329 my ($function_name,$javascript) = plugin_javascript($dbh,$record,$tagslib,$i,0);
330 $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";
332 $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\" value=\"$value\" size=50 maxlength=255>";
334 # $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\">";
335 push(@loop_data, \%subfield_data);
340 # what's the next op ? it's what we are not in : an add if we're editing, otherwise, and edit.
341 $template->param( title => $record->title() ) if ($record ne "-1");
342 $template->param(item_loop => \@item_value_loop,
343 item_header_loop => \@header_value_loop,
344 biblionumber => $biblionumber,
345 title => $oldrecord->{title},
346 author => $oldrecord->{author},
348 itemnumber => $itemnumber,
349 itemtagfield => $itemtagfield,
350 itemtagsubfield =>$itemtagsubfield,
352 opisadd => ($nextop eq "saveitem")?0:1);
353 foreach my $error (@errors) {
354 $template->param($error => 1);
356 output_html_with_http_headers $input, $cookie, $template->output;