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 my $oldrecord = TransformMarcToKoha($dbh,$record);
74 my @errors; # store errors found while checking data BEFORE saving item.
75 #-------------------------------------------------------------------------------
76 if ($op eq "additem") {
77 #-------------------------------------------------------------------------------
79 my @tags = $input->param('tag');
80 my @subfields = $input->param('subfield');
81 my @values = $input->param('field_value');
82 # build indicator hash.
83 my @ind_tag = $input->param('ind_tag');
84 my @indicator = $input->param('indicator');
85 my $xml = TransformHtmlToXml(\@tags,\@subfields,\@values,\@indicator,\@ind_tag, 'ITEM');
86 my $record=MARC::Record::new_from_xml($xml, 'UTF-8');
87 # if autoBarcode is ON, calculate barcode...
88 if (C4::Context->preference('autoBarcode')) {
89 my ($tagfield,$tagsubfield) = &GetMarcFromKohaField("items.barcode",$frameworkcode);
90 unless ($record->field($tagfield)->subfield($tagsubfield)) {
91 my $sth_barcode = $dbh->prepare("select max(abs(barcode)) from items");
92 $sth_barcode->execute;
93 my ($newbarcode) = $sth_barcode->fetchrow;
95 # OK, we have the new barcode, now create the entry in MARC record
96 my $fieldItem = $record->field($tagfield);
97 $record->delete_field($fieldItem);
98 $fieldItem->add_subfields($tagsubfield => $newbarcode);
99 $record->insert_fields_ordered($fieldItem);
102 # check for item barcode # being unique
103 my $addedolditem = TransformMarcToKoha($dbh,$record);
104 my $exists = get_item_from_barcode($addedolditem->{'barcode'});
105 push @errors,"barcode_not_unique" if($exists);
106 # if barcode exists, don't create, but report The problem.
107 my ($oldbiblionumber,$oldbibnum,$oldbibitemnum) = AddItem($record,$biblionumber) unless ($exists);
110 $itemrecord = $record;
114 #-------------------------------------------------------------------------------
115 } elsif ($op eq "edititem") {
116 #-------------------------------------------------------------------------------
117 # retrieve item if exist => then, it's a modif
118 $itemrecord = GetMarcItem($biblionumber,$itemnumber);
120 #-------------------------------------------------------------------------------
121 } elsif ($op eq "delitem") {
122 #-------------------------------------------------------------------------------
123 # check that there is no issue on this item before deletion.
124 my $sth=$dbh->prepare("select * from issues i where i.returndate is null and i.itemnumber=?");
125 $sth->execute($itemnumber);
126 my $onloan=$sth->fetchrow;
127 push @errors,"book_on_loan" if ($onloan); ##error book_on_loan added to template as well
131 &DelItem($biblionumber,$itemnumber);
132 print $input->redirect("additem.pl?biblionumber=$biblionumber&frameworkcode=$frameworkcode");
135 #-------------------------------------------------------------------------------
136 } elsif ($op eq "saveitem") {
137 #-------------------------------------------------------------------------------
139 my @tags = $input->param('tag');
140 my @subfields = $input->param('subfield');
141 my @values = $input->param('field_value');
142 # build indicator hash.
143 my @ind_tag = $input->param('ind_tag');
144 my @indicator = $input->param('indicator');
145 # my $itemnumber = $input->param('itemnumber');
146 my $xml = TransformHtmlToXml(\@tags,\@subfields,\@values,\@indicator,\@ind_tag,'ITEM');
147 my $itemrecord=MARC::Record::new_from_xml($xml, 'UTF-8');
148 # MARC::Record builded => now, record in DB
149 # warn "R: ".$record->as_formatted;
150 my ($oldbiblionumber,$oldbibnum,$oldbibitemnum) = ModItem($itemrecord,$biblionumber,$itemnumber,0);
156 #-------------------------------------------------------------------------------
157 # build screen with existing items. and "new" one
158 #-------------------------------------------------------------------------------
159 my ($template, $loggedinuser, $cookie)
160 = get_template_and_user({template_name => "cataloguing/additem.tmpl",
163 authnotrequired => 0,
164 flagsrequired => {editcatalogue => 1},
169 $indicators{995}=' ';
170 # now, build existiing item list
171 my $temp = GetMarcBiblio( $biblionumber );
172 my @fields = $temp->fields();
173 #my @fields = $record->fields();
174 my %witness; #---- stores the list of subfields used at least once, with the "meaning" of the code
176 #---- finds where items.itemnumber is stored
177 my ($itemtagfield,$itemtagsubfield) = &GetMarcFromKohaField("items.itemnumber",$frameworkcode);
178 my ($branchtagfield,$branchtagsubfield) = &GetMarcFromKohaField("items.homebranch",$frameworkcode);
180 foreach my $field (@fields) {
181 next if ($field->tag()<10);
182 my @subf=$field->subfields;
184 # loop through each subfield
185 for my $i (0..$#subf) {
186 next if ($tagslib->{$field->tag()}->{$subf[$i][0]}->{tab} ne 10 && ($field->tag() ne $itemtagfield && $subf[$i][0] ne $itemtagsubfield));
187 $witness{$subf[$i][0]} = $tagslib->{$field->tag()}->{$subf[$i][0]}->{lib} if ($tagslib->{$field->tag()}->{$subf[$i][0]}->{tab} eq 10);
188 $this_row{$subf[$i][0]} =$subf[$i][1] if ($tagslib->{$field->tag()}->{$subf[$i][0]}->{tab} eq 10);
189 if (($field->tag eq $branchtagfield) && ($subf[$i][$0] eq $branchtagsubfield) && C4::Context->preference("IndependantBranches")) {
191 my $userenv = C4::Context->userenv;
192 unless (($userenv->{'flags'} == 1) or (($userenv->{'branch'} eq $subf[$i][1]))){
193 $this_row{'nomod'}=1;
196 $this_row{itemnumber} = $subf[$i][1] if ($field->tag() eq $itemtagfield && $subf[$i][0] eq $itemtagsubfield);
199 push(@big_array, \%this_row);
202 #fill big_row with missing datas
203 foreach my $subfield_code (keys(%witness)) {
204 for (my $i=0;$i<=$#big_array;$i++) {
205 $big_array[$i]{$subfield_code}=" " unless ($big_array[$i]{$subfield_code});
208 my ($holdingbrtagf,$holdingbrtagsubf) = &GetMarcFromKohaField("items.holdingbranch",$frameworkcode);
209 @big_array = sort {$a->{$holdingbrtagsubf} cmp $b->{$holdingbrtagsubf}} @big_array;
211 # now, construct template !
213 my @header_value_loop;
214 for (my $i=0;$i<=$#big_array; $i++) {
216 foreach my $subfield_code (sort keys(%witness)) {
217 $items_data .="<td>".$big_array[$i]{$subfield_code}."</td>";
220 $items_data =~ s/"/"/g;
221 $row_data{item_value} = $items_data;
222 $row_data{itemnumber} = $big_array[$i]->{itemnumber};
223 #reporting this_row values
224 $row_data{'nomod'} = $big_array[$i]{'nomod'};
225 push(@item_value_loop,\%row_data);
227 foreach my $subfield_code (sort keys(%witness)) {
229 $header_value{header_value} = $witness{$subfield_code};
230 push(@header_value_loop, \%header_value);
236 my $authorised_values_sth = $dbh->prepare("select authorised_value,lib from authorised_values where category=? order by lib");
238 foreach my $tag (sort keys %{$tagslib}) {
239 my $previous_tag = '';
240 # loop through each subfield
241 foreach my $subfield (sort keys %{$tagslib->{$tag}}) {
242 next if subfield_is_koha_internal_p($subfield);
243 next if ($tagslib->{$tag}->{$subfield}->{'tab'} ne "10");
245 $subfield_data{tag}=$tag;
246 $subfield_data{subfield}=$subfield;
247 # $subfield_data{marc_lib}=$tagslib->{$tag}->{$subfield}->{lib};
248 $subfield_data{marc_lib}="<span id=\"error$i\" title=\"".$tagslib->{$tag}->{$subfield}->{lib}."\">".substr($tagslib->{$tag}->{$subfield}->{lib},0,12)."</span>";
249 $subfield_data{mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
250 $subfield_data{repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
251 $subfield_data{hidden}= "display:none" if $tagslib->{$tag}->{$subfield}->{hidden};
253 ($x,$value) = find_value($tag,$subfield,$itemrecord) if ($itemrecord);
254 $value =~ s/"/"/g;
255 #testing branch value if IndependantBranches.
256 my $test = (C4::Context->preference("IndependantBranches")) &&
257 ($tag eq $branchtagfield) && ($subfield eq $branchtagsubfield) &&
258 (C4::Context->userenv->{flags} != 1) && ($value) && ($value ne C4::Context->userenv->{branch}) ;
259 # print $input->redirect(".pl?biblionumber=$biblionumber") if ($test);
260 # search for itemcallnumber if applicable
261 if (!$value && $tagslib->{$tag}->{$subfield}->{kohafield} eq 'items.itemcallnumber' && C4::Context->preference('itemcallnumber')) {
262 my $CNtag = substr(C4::Context->preference('itemcallnumber'),0,3);
263 my $CNsubfield = substr(C4::Context->preference('itemcallnumber'),3,1);
264 my $CNsubfield2 = substr(C4::Context->preference('itemcallnumber'),4,1);
265 my $temp2 = $temp->field($CNtag);
267 $value = ($temp2->subfield($CNsubfield)).' '.($temp2->subfield($CNsubfield2));
268 #remove any trailing space incase one subfield is used
269 $value=~s/^\s+|\s+$//g;
272 if ($tagslib->{$tag}->{$subfield}->{authorised_value}) {
273 my @authorised_values;
275 # builds list, depending on authorised value...
277 if ($tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) {
278 if ((C4::Context->preference("IndependantBranches")) && (C4::Context->userenv->{flags} != 1)){
279 my $sth=$dbh->prepare("select branchcode,branchname from branches where branchcode = ? order by branchname");
280 $sth->execute(C4::Context->userenv->{branch});
281 push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
282 while (my ($branchcode,$branchname) = $sth->fetchrow_array) {
283 push @authorised_values, $branchcode;
284 $authorised_lib{$branchcode}=$branchname;
287 my $sth=$dbh->prepare("select branchcode,branchname from branches order by branchname");
289 push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
290 while (my ($branchcode,$branchname) = $sth->fetchrow_array) {
291 push @authorised_values, $branchcode;
292 $authorised_lib{$branchcode}=$branchname;
296 } elsif ($tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes") {
297 my $sth=$dbh->prepare("select itemtype,description from itemtypes order by description");
299 push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
300 while (my ($itemtype,$description) = $sth->fetchrow_array) {
301 push @authorised_values, $itemtype;
302 $authorised_lib{$itemtype}=$description;
304 #---- "true" authorised value
306 $authorised_values_sth->execute($tagslib->{$tag}->{$subfield}->{authorised_value});
307 push @authorised_values, "" unless ($tagslib->{$tag}->{$subfield}->{mandatory});
308 while (my ($authvalue,$lib) = $authorised_values_sth->fetchrow_array) {
309 push @authorised_values, $authvalue;
310 $authorised_lib{$authvalue}=$lib;
313 $subfield_data{marc_value}= CGI::scrolling_list(-name=>'field_value',
314 -values=> \@authorised_values,
316 -labels => \%authorised_lib,
321 } elsif ($tagslib->{$tag}->{$subfield}->{thesaurus_category}) {
322 $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>";
324 } elsif ($tagslib->{$tag}->{$subfield}->{'value_builder'}) {
325 my $plugin="value_builder/".$tagslib->{$tag}->{$subfield}->{'value_builder'};
327 my $extended_param = plugin_parameters($dbh,$record,$tagslib,$i,0);
328 my ($function_name,$javascript) = plugin_javascript($dbh,$record,$tagslib,$i,0);
329 $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";
331 $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\" value=\"$value\" size=\"50\" maxlength=\"255\" />";
333 # $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\">";
334 push(@loop_data, \%subfield_data);
339 # what's the next op ? it's what we are not in : an add if we're editing, otherwise, and edit.
340 $template->param( title => $record->title() ) if ($record ne "-1");
341 $template->param(item_loop => \@item_value_loop,
342 item_header_loop => \@header_value_loop,
343 biblionumber => $biblionumber,
344 title => $oldrecord->{title},
345 author => $oldrecord->{author},
347 itemnumber => $itemnumber,
348 itemtagfield => $itemtagfield,
349 itemtagsubfield =>$itemtagsubfield,
351 opisadd => ($nextop eq "saveitem")?0:1);
352 foreach my $error (@errors) {
353 $template->param($error => 1);
355 output_html_with_http_headers $input, $cookie, $template->output;