4 # Copyright 2000-2002 Katipo Communications
6 # This file is part of Koha.
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
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.
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
27 use C4::Koha; # XXX subfield_is_koha_internal_p
32 my ($tagfield,$insubfield,$record) = @_;
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);
44 return($indicator,$result);
47 sub get_item_from_barcode {
49 my $dbh=C4::Context->dbh;
51 my $rq=$dbh->prepare("SELECT itemnumber from items where items.barcode=?");
52 $rq->execute($barcode);
53 ($result)=$rq->fetchrow;
58 my $dbh = C4::Context->dbh;
59 my $error = $input->param('error');
60 my $biblionumber = $input->param('biblionumber');
61 my $itemnumber = $input->param('itemnumber');
63 my $op = $input->param('op');
66 my $frameworkcode = &GetFrameworkCode($biblionumber);
68 my $tagslib = &GetMarcStructure(1,$frameworkcode);
69 my $record = GetMarcBiblio($biblionumber);
70 my $oldrecord = TransformMarcToKoha($dbh,$record);
73 my @errors; # store errors found while checking data BEFORE saving item.
74 #-------------------------------------------------------------------------------
75 if ($op eq "additem") {
76 #-------------------------------------------------------------------------------
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;
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);
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);
109 $itemrecord = $record;
113 #-------------------------------------------------------------------------------
114 } elsif ($op eq "edititem") {
115 #-------------------------------------------------------------------------------
116 # retrieve item if exist => then, it's a modif
117 $itemrecord = GetMarcItem($biblionumber,$itemnumber);
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
130 &DelItem($biblionumber,$itemnumber);
131 print $input->redirect("additem.pl?biblionumber=$biblionumber&frameworkcode=$frameworkcode");
134 #-------------------------------------------------------------------------------
135 } elsif ($op eq "saveitem") {
136 #-------------------------------------------------------------------------------
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);
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",
162 authnotrequired => 0,
163 flagsrequired => {editcatalogue => 1},
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
175 #---- finds where items.itemnumber is stored
176 my ($itemtagfield,$itemtagsubfield) = &GetMarcFromKohaField("items.itemnumber",$frameworkcode);
177 my ($branchtagfield,$branchtagsubfield) = &GetMarcFromKohaField("items.homebranch",$frameworkcode);
179 foreach my $field (@fields) {
180 next if ($field->tag()<10);
181 my @subf=$field->subfields;
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")) {
190 my $userenv = C4::Context->userenv;
191 unless (($userenv->{'flags'} == 1) or (($userenv->{'branch'} eq $subf[$i][1]))){
192 $this_row{'nomod'}=1;
195 $this_row{itemnumber} = $subf[$i][1] if ($field->tag() eq $itemtagfield && $subf[$i][0] eq $itemtagsubfield);
198 push(@big_array, \%this_row);
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}=" " unless ($big_array[$i]{$subfield_code});
207 my ($holdingbrtagf,$holdingbrtagsubf) = &GetMarcFromKohaField("items.holdingbranch",$frameworkcode);
208 @big_array = sort {$a->{$holdingbrtagsubf} cmp $b->{$holdingbrtagsubf}} @big_array;
210 # now, construct template !
212 my @header_value_loop;
213 for (my $i=0;$i<=$#big_array; $i++) {
215 foreach my $subfield_code (sort keys(%witness)) {
216 $items_data .="<td>".$big_array[$i]{$subfield_code}."</td>";
219 $items_data =~ s/"/"/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);
226 foreach my $subfield_code (sort keys(%witness)) {
228 $header_value{header_value} = $witness{$subfield_code};
229 push(@header_value_loop, \%header_value);
235 my $authorised_values_sth = $dbh->prepare("select authorised_value,lib from authorised_values where category=? order by lib");
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");
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};
252 ($x,$value) = find_value($tag,$subfield,$itemrecord) if ($itemrecord);
253 $value =~ s/"/"/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);
266 $value = ($temp2->subfield($CNsubfield)).' '.($temp2->subfield($CNsubfield2));
267 #remove any trailing space incase one subfield is used
268 $value=~s/^\s+|\s+$//g;
271 if ($tagslib->{$tag}->{$subfield}->{authorised_value}) {
272 my @authorised_values;
274 # builds list, depending on authorised value...
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;
286 my $sth=$dbh->prepare("select branchcode,branchname from branches order by branchname");
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;
295 } elsif ($tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes") {
296 my $sth=$dbh->prepare("select itemtype,description from itemtypes order by description");
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;
303 #---- "true" authorised value
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;
312 $subfield_data{marc_value}= CGI::scrolling_list(-name=>'field_value',
313 -values=> \@authorised_values,
315 -labels => \%authorised_lib,
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>";
323 } elsif ($tagslib->{$tag}->{$subfield}->{'value_builder'}) {
324 my $plugin="value_builder/".$tagslib->{$tag}->{$subfield}->{'value_builder'};
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";
330 $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\" value=\"$value\" size=\"50\" maxlength=\"255\" />";
332 # $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\">";
333 push(@loop_data, \%subfield_data);
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},
346 itemnumber => $itemnumber,
347 itemtagfield => $itemtagfield,
348 itemtagsubfield =>$itemtagsubfield,
350 opisadd => ($nextop eq "saveitem")?0:1);
351 foreach my $error (@errors) {
352 $template->param($error => 1);
354 output_html_with_http_headers $input, $cookie, $template->output;