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
28 use Date::Calc qw(Today);
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");
246 my $index_subfield= int(rand(1000000));
247 if($subfield eq '@'){
248 $subfield_data{id} = "tag_".$tag."_subfield_0_".$index_subfield;
250 $subfield_data{id} = "tag_".$tag."_subfield_".$subfield."_".$index_subfield;
252 $subfield_data{tag}=$tag;
253 $subfield_data{subfield}=$subfield;
254 $subfield_data{random}=int(rand(1000000));
255 # $subfield_data{marc_lib}=$tagslib->{$tag}->{$subfield}->{lib};
256 $subfield_data{marc_lib}="<span id=\"error$i\" title=\"".$tagslib->{$tag}->{$subfield}->{lib}."\">".$tagslib->{$tag}->{$subfield}->{lib}."</span>";
257 $subfield_data{mandatory}=$tagslib->{$tag}->{$subfield}->{mandatory};
258 $subfield_data{repeatable}=$tagslib->{$tag}->{$subfield}->{repeatable};
260 ($x,$value) = find_value($tag,$subfield,$itemrecord) if ($itemrecord);
261 $value =~ s/"/"/g;
263 $value = $tagslib->{$tag}->{$subfield}->{defaultvalue};
265 # get today date & replace YYYY, MM, DD if provided in the default value
266 my ( $year, $month, $day ) = Today();
267 $month = sprintf( "%02d", $month );
268 $day = sprintf( "%02d", $day );
269 $value =~ s/YYYY/$year/g;
270 $value =~ s/MM/$month/g;
271 $value =~ s/DD/$day/g;
273 $subfield_data{visibility} = "display:none;" if (($tagslib->{$tag}->{$subfield}->{hidden} % 2 == 1) and $value ne '');
274 #testing branch value if IndependantBranches.
275 my $test = (C4::Context->preference("IndependantBranches")) &&
276 ($tag eq $branchtagfield) && ($subfield eq $branchtagsubfield) &&
277 (C4::Context->userenv->{flags} != 1) && ($value) && ($value ne C4::Context->userenv->{branch}) ;
278 # print $input->redirect(".pl?biblionumber=$biblionumber") if ($test);
279 # search for itemcallnumber if applicable
280 if (!$value && $tagslib->{$tag}->{$subfield}->{kohafield} eq 'items.itemcallnumber' && C4::Context->preference('itemcallnumber')) {
281 my $CNtag = substr(C4::Context->preference('itemcallnumber'),0,3);
282 my $CNsubfield = substr(C4::Context->preference('itemcallnumber'),3,1);
283 my $CNsubfield2 = substr(C4::Context->preference('itemcallnumber'),4,1);
284 my $temp2 = $temp->field($CNtag);
286 $value = ($temp2->subfield($CNsubfield)).' '.($temp2->subfield($CNsubfield2));
287 #remove any trailing space incase one subfield is used
288 $value=~s/^\s+|\s+$//g;
291 if ( $tagslib->{$tag}->{$subfield}->{authorised_value} ) {
292 my @authorised_values;
294 my $dbh=C4::Context->dbh;
296 # builds list, depending on authorised value...
299 if ( $tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) {
302 "select branchcode,branchname from branches order by branchname");
304 push @authorised_values, ""
305 unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
307 while ( my ( $branchcode, $branchname ) = $sth->fetchrow_array ) {
308 push @authorised_values, $branchcode;
309 $authorised_lib{$branchcode} = $branchname;
314 elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes" ) {
317 "select itemtype,description from itemtypes order by description");
319 push @authorised_values, ""
320 unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
324 while ( my ( $itemtype, $description ) = $sth->fetchrow_array ) {
325 push @authorised_values, $itemtype;
326 $authorised_lib{$itemtype} = $description;
328 $value = $itemtype unless ($value);
330 #---- "true" authorised value
333 $authorised_values_sth->execute(
334 $tagslib->{$tag}->{$subfield}->{authorised_value} );
336 push @authorised_values, ""
337 unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
339 while ( my ( $value, $lib ) = $authorised_values_sth->fetchrow_array ) {
340 push @authorised_values, $value;
341 $authorised_lib{$value} = $lib;
344 $subfield_data{marc_value} =CGI::scrolling_list(
345 -name => "tag_".$tag."_subfield_".$subfield."_".$index_subfield,
346 -values => \@authorised_values,
348 -labels => \%authorised_lib,
353 -id => "tag_".$tag."_subfield_".$subfield."_".$index_subfield,
354 -class => "input_marceditor",
356 # it's a thesaurus / authority field
358 elsif ( $tagslib->{$tag}->{$subfield}->{authtypecode} ) {
359 $subfield_data{marc_value} =
360 "<input type=\"text\"
361 id=\"".$subfield_data{id}."\"
362 name=\"".$subfield_data{id}."\"
364 class=\"input_marceditor\"
369 <a href=\"#\" class=\"buttonDot\"
370 onclick=\"Dopop('/cgi-bin/koha/authorities/auth_finder.pl?authtypecode=".$tagslib->{$tag}->{$subfield}->{authtypecode}."&index=$subfield_data{id}','$subfield_data{id}'); return false;\" title=\"Tag Editor\">...</a>
372 # it's a plugin field
374 elsif ( $tagslib->{$tag}->{$subfield}->{'value_builder'} ) {
376 # opening plugin. Just check wether we are on a developper computer on a production one
377 # (the cgidir differs)
378 my $cgidir = C4::Context->intranetdir . "/cgi-bin/cataloguing/value_builder";
379 unless ( opendir( DIR, "$cgidir" ) ) {
380 $cgidir = C4::Context->intranetdir . "/cataloguing/value_builder";
382 my $plugin = $cgidir . "/" . $tagslib->{$tag}->{$subfield}->{'value_builder'};
383 do $plugin || die "Plugin Failed: ".$plugin;
384 my $extended_param = plugin_parameters( $dbh, $temp, $tagslib, $subfield_data{id}, \@loop_data );
385 my ( $function_name, $javascript ) = plugin_javascript( $dbh, $temp, $tagslib, $subfield_data{id}, \@loop_data );
386 # my ( $function_name, $javascript,$extended_param );
388 $subfield_data{marc_value} =
389 "<input tabindex=\"1\"
391 id=\"".$subfield_data{id}."\"
392 name=\"".$subfield_data{id}."\"
394 class=\"input_marceditor\"
395 onfocus=\"Focus$function_name(".$subfield_data{random}.")\"
398 onblur=\"Blur$function_name(".$subfield_data{random}."); \" \/>
399 <a href=\"#\" class=\"buttonDot\" onclick=\"Clic$function_name('$subfield_data{id}'; return false;)\" title=\"Tag Editor\">...</a>
401 # it's an hidden field
403 elsif ( $tag eq '' ) {
404 $subfield_data{marc_value} =
405 "<input tabindex=\"1\"
407 id=\"".$subfield_data{id}."\"
408 name=\"".$subfield_data{id}."\"
414 elsif ( $tagslib->{$tag}->{$subfield}->{'hidden'} ) {
415 $subfield_data{marc_value} =
416 "<input type=\"text\"
417 id=\"".$subfield_data{id}."\"
418 name=\"".$subfield_data{id}."\"
419 class=\"input_marceditor\"
426 # it's a standard field
432 ( C4::Context->preference("marcflavour") eq "UNIMARC" && $tag >= 300
433 and $tag < 400 && $subfield eq 'a' )
436 && C4::Context->preference("marcflavour") eq "MARC21" )
439 $subfield_data{marc_value} =
440 "<textarea cols=\"70\"
442 id=\"".$subfield_data{id}."\"
443 name=\"".$subfield_data{id}."\"
444 class=\"input_marceditor\"
452 $subfield_data{marc_value} =
453 "<input type=\"text\"
454 id=\"".$subfield_data{id}."\"
455 name=\"".$subfield_data{id}."\"
460 class=\"input_marceditor\"
465 # $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\">";
466 push(@loop_data, \%subfield_data);
471 # what's the next op ? it's what we are not in : an add if we're editing, otherwise, and edit.
472 $template->param( title => $record->title() ) if ($record ne "-1");
473 $template->param(item_loop => \@item_value_loop,
474 item_header_loop => \@header_value_loop,
475 biblionumber => $biblionumber,
476 title => $oldrecord->{title},
477 author => $oldrecord->{author},
479 itemnumber => $itemnumber,
480 itemtagfield => $itemtagfield,
481 itemtagsubfield =>$itemtagsubfield,
483 opisadd => ($nextop eq "saveitem")?0:1);
484 foreach my $error (@errors) {
485 $template->param($error => 1);
487 output_html_with_http_headers $input, $cookie, $template->output;