Bug 3383 - Item due reminder digests - cannot display title information
[koha.git] / cataloguing / additem.pl
blob684e6f62df11e8e2471a2a43f9e2eef0779aa945
1 #!/usr/bin/perl
3 # Copyright 2000-2002 Katipo Communications
4 # Copyright 2004-2010 BibLibre
5 # Parts Copyright Catalyst IT 2011
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
12 # version.
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
19 # with Koha; if not, write to the Free Software Foundation, Inc.,
20 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 use strict;
23 #use warnings; FIXME - Bug 2505
24 use CGI;
25 use C4::Auth;
26 use C4::Output;
27 use C4::Biblio;
28 use C4::Items;
29 use C4::Context;
30 use C4::Koha; # XXX subfield_is_koha_internal_p
31 use C4::Branch; # XXX subfield_is_koha_internal_p
32 use C4::ClassSource;
33 use C4::Dates;
34 use List::MoreUtils qw/any/;
35 use C4::Search;
37 use MARC::File::XML;
38 use URI::Escape;
40 our $dbh = C4::Context->dbh;
42 sub find_value {
43 my ($tagfield,$insubfield,$record) = @_;
44 my $result;
45 my $indicator;
46 foreach my $field ($record->field($tagfield)) {
47 my @subfields = $field->subfields();
48 foreach my $subfield (@subfields) {
49 if (@$subfield[0] eq $insubfield) {
50 $result .= @$subfield[1];
51 $indicator = $field->indicator(1).$field->indicator(2);
55 return($indicator,$result);
58 sub get_item_from_barcode {
59 my ($barcode)=@_;
60 my $dbh=C4::Context->dbh;
61 my $result;
62 my $rq=$dbh->prepare("SELECT itemnumber from items where items.barcode=?");
63 $rq->execute($barcode);
64 ($result)=$rq->fetchrow;
65 return($result);
68 sub set_item_default_location {
69 my $itemnumber = shift;
70 my $item = GetItem( $itemnumber );
71 if ( C4::Context->preference('NewItemsDefaultLocation') ) {
72 $item->{'permanent_location'} = $item->{'location'};
73 $item->{'location'} = C4::Context->preference('NewItemsDefaultLocation');
74 ModItem( $item, undef, $itemnumber);
76 else {
77 $item->{'permanent_location'} = $item->{'location'} if !defined($item->{'permanent_location'});
78 ModItem( $item, undef, $itemnumber);
82 # NOTE: This code is subject to change in the future with the implemenation of ajax based autobarcode code
83 # NOTE: 'incremental' is the ONLY autoBarcode option available to those not using javascript
84 sub _increment_barcode {
85 my ($record, $frameworkcode) = @_;
86 my ($tagfield,$tagsubfield) = &GetMarcFromKohaField("items.barcode",$frameworkcode);
87 unless ($record->field($tagfield)->subfield($tagsubfield)) {
88 my $sth_barcode = $dbh->prepare("select max(abs(barcode)) from items");
89 $sth_barcode->execute;
90 my ($newbarcode) = $sth_barcode->fetchrow;
91 $newbarcode++;
92 # OK, we have the new barcode, now create the entry in MARC record
93 my $fieldItem = $record->field($tagfield);
94 $record->delete_field($fieldItem);
95 $fieldItem->add_subfields($tagsubfield => $newbarcode);
96 $record->insert_fields_ordered($fieldItem);
98 return $record;
102 sub generate_subfield_form {
103 my ($tag, $subfieldtag, $value, $tagslib,$subfieldlib, $branches, $today_iso, $biblionumber, $temp, $loop_data, $i) = @_;
105 my $frameworkcode = &GetFrameworkCode($biblionumber);
106 my %subfield_data;
107 my $dbh = C4::Context->dbh;
108 my $authorised_values_sth = $dbh->prepare("SELECT authorised_value,lib FROM authorised_values WHERE category=? ORDER BY lib");
110 my $index_subfield = int(rand(1000000));
111 if ($subfieldtag eq '@'){
112 $subfield_data{id} = "tag_".$tag."_subfield_00_".$index_subfield;
113 } else {
114 $subfield_data{id} = "tag_".$tag."_subfield_".$subfieldtag."_".$index_subfield;
117 $subfield_data{tag} = $tag;
118 $subfield_data{subfield} = $subfieldtag;
119 $subfield_data{random} = int(rand(1000000)); # why do we need 2 different randoms?
120 $subfield_data{marc_lib} ="<span id=\"error$i\" title=\"".$subfieldlib->{lib}."\">".$subfieldlib->{lib}."</span>";
121 $subfield_data{mandatory} = $subfieldlib->{mandatory};
122 $subfield_data{repeatable} = $subfieldlib->{repeatable};
123 $subfield_data{maxlength} = $subfieldlib->{maxlength};
125 $value =~ s/"/&quot;/g;
126 if ( ! defined( $value ) || $value eq '') {
127 $value = $subfieldlib->{defaultvalue};
128 # get today date & replace YYYY, MM, DD if provided in the default value
129 my ( $year, $month, $day ) = split ',', $today_iso; # FIXME: iso dates don't have commas!
130 $value =~ s/YYYY/$year/g;
131 $value =~ s/MM/$month/g;
132 $value =~ s/DD/$day/g;
135 $subfield_data{visibility} = "display:none;" if (($subfieldlib->{hidden} > 4) || ($subfieldlib->{hidden} < -4));
137 my $pref_itemcallnumber = C4::Context->preference('itemcallnumber');
138 if (!$value && $subfieldlib->{kohafield} eq 'items.itemcallnumber' && $pref_itemcallnumber) {
139 my $CNtag = substr($pref_itemcallnumber, 0, 3);
140 my $CNsubfield = substr($pref_itemcallnumber, 3, 1);
141 my $CNsubfield2 = substr($pref_itemcallnumber, 4, 1);
142 my $temp2 = $temp->field($CNtag);
143 if ($temp2) {
144 $value = ($temp2->subfield($CNsubfield)).' '.($temp2->subfield($CNsubfield2));
145 #remove any trailing space incase one subfield is used
146 $value =~ s/^\s+|\s+$//g;
150 if ($frameworkcode eq 'FA' && $subfieldlib->{kohafield} eq 'items.barcode' && !$value){
151 my $input = new CGI;
152 $value = $input->param('barcode');
154 my $attributes_no_value = qq(tabindex="1" id="$subfield_data{id}" name="field_value" class="input_marceditor" size="67" maxlength="$subfield_data{maxlength}" );
155 my $attributes = qq($attributes_no_value value="$value" );
157 if ( $subfieldlib->{authorised_value} ) {
158 my @authorised_values;
159 my %authorised_lib;
160 # builds list, depending on authorised value...
161 if ( $subfieldlib->{authorised_value} eq "branches" ) {
162 foreach my $thisbranch (@$branches) {
163 push @authorised_values, $thisbranch->{value};
164 $authorised_lib{$thisbranch->{value}} = $thisbranch->{branchname};
165 $value = $thisbranch->{value} if $thisbranch->{selected} && !$value;
168 elsif ( $subfieldlib->{authorised_value} eq "itemtypes" ) {
169 push @authorised_values, "" unless ( $subfieldlib->{mandatory} );
170 my $sth = $dbh->prepare("SELECT itemtype,description FROM itemtypes ORDER BY description");
171 $sth->execute;
172 while ( my ( $itemtype, $description ) = $sth->fetchrow_array ) {
173 push @authorised_values, $itemtype;
174 $authorised_lib{$itemtype} = $description;
177 unless ( $value ) {
178 my $itype_sth = $dbh->prepare("SELECT itemtype FROM biblioitems WHERE biblionumber = ?");
179 $itype_sth->execute( $biblionumber );
180 ( $value ) = $itype_sth->fetchrow_array;
183 #---- class_sources
185 elsif ( $subfieldlib->{authorised_value} eq "cn_source" ) {
186 push @authorised_values, "" unless ( $subfieldlib->{mandatory} );
188 my $class_sources = GetClassSources();
189 my $default_source = C4::Context->preference("DefaultClassificationSource");
191 foreach my $class_source (sort keys %$class_sources) {
192 next unless $class_sources->{$class_source}->{'used'} or
193 ($value and $class_source eq $value) or
194 ($class_source eq $default_source);
195 push @authorised_values, $class_source;
196 $authorised_lib{$class_source} = $class_sources->{$class_source}->{'description'};
198 $value = $default_source unless ($value);
200 #---- "true" authorised value
202 else {
203 push @authorised_values, "" unless ( $subfieldlib->{mandatory} );
204 $authorised_values_sth->execute( $subfieldlib->{authorised_value} );
205 while ( my ( $value, $lib ) = $authorised_values_sth->fetchrow_array ) {
206 push @authorised_values, $value;
207 $authorised_lib{$value} = $lib;
211 if ($subfieldlib->{'hidden'}) {
212 $subfield_data{marc_value} = qq(<input type="hidden" $attributes /> $authorised_lib{$value});
214 else {
215 $subfield_data{marc_value} =CGI::scrolling_list( # FIXME: factor out scrolling_list
216 -name => "field_value",
217 -values => \@authorised_values,
218 -default => $value,
219 -labels => \%authorised_lib,
220 -override => 1,
221 -size => 1,
222 -multiple => 0,
223 -tabindex => 1,
224 -id => "tag_".$tag."_subfield_".$subfieldtag."_".$index_subfield,
225 -class => "input_marceditor",
230 # it's a thesaurus / authority field
231 elsif ( $subfieldlib->{authtypecode} ) {
232 $subfield_data{marc_value} = "<input type=\"text\" $attributes />
233 <a href=\"#\" class=\"buttonDot\"
234 onclick=\"Dopop('/cgi-bin/koha/authorities/auth_finder.pl?authtypecode=".$subfieldlib->{authtypecode}."&index=$subfield_data{id}','$subfield_data{id}'); return false;\" title=\"Tag Editor\">...</a>
237 # it's a plugin field
238 elsif ( $subfieldlib->{value_builder} ) {
239 # opening plugin
240 my $plugin = C4::Context->intranetdir . "/cataloguing/value_builder/" . $subfieldlib->{'value_builder'};
241 if (do $plugin) {
242 my $extended_param = plugin_parameters( $dbh, $temp, $tagslib, $subfield_data{id}, $loop_data );
243 my ( $function_name, $javascript ) = plugin_javascript( $dbh, $temp, $tagslib, $subfield_data{id}, $loop_data );
244 my $change = index($javascript, 'function Change') > -1 ?
245 "return Change$function_name($subfield_data{random}, '$subfield_data{id}');" :
246 'return 1;';
247 $subfield_data{marc_value} = qq[<input $attributes
248 onfocus="Focus$function_name($subfield_data{random}, '$subfield_data{id}');"
249 onchange=" $change"
250 onblur=" Blur$function_name($subfield_data{random}, '$subfield_data{id}');" />
251 <a href="#" class="buttonDot" onclick="Clic$function_name('$subfield_data{id}'); return false;" title="Tag Editor">...</a>
252 $javascript];
253 } else {
254 warn "Plugin Failed: $plugin";
255 $subfield_data{marc_value} = "<input $attributes />"; # supply default input form
258 elsif ( $tag eq '' ) { # it's an hidden field
259 $subfield_data{marc_value} = qq(<input type="hidden" $attributes />);
261 elsif ( $subfieldlib->{'hidden'} ) { # FIXME: shouldn't input type be "hidden" ?
262 $subfield_data{marc_value} = qq(<input type="text" $attributes />);
264 elsif ( length($value) > 100
265 or (C4::Context->preference("marcflavour") eq "UNIMARC" and
266 300 <= $tag && $tag < 400 && $subfieldtag eq 'a' )
267 or (C4::Context->preference("marcflavour") eq "MARC21" and
268 500 <= $tag && $tag < 600 )
270 # oversize field (textarea)
271 $subfield_data{marc_value} = "<textarea $attributes_no_value>$value</textarea>\n";
272 } else {
273 # it's a standard field
274 $subfield_data{marc_value} = "<input $attributes />";
277 return \%subfield_data;
281 my $input = new CGI;
282 my $error = $input->param('error');
283 my $biblionumber = $input->param('biblionumber');
284 my $itemnumber = $input->param('itemnumber');
285 my $op = $input->param('op');
286 my $hostitemnumber = $input->param('hostitemnumber');
287 my $marcflavour = C4::Context->preference("marcflavour");
289 my $frameworkcode = &GetFrameworkCode($biblionumber);
291 # Defining which userflag is needing according to the framework currently used
292 my $userflags;
293 if (defined $input->param('frameworkcode')) {
294 $userflags = ($input->param('frameworkcode') eq 'FA') ? "fast_cataloging" : "edit_items";
297 if (not defined $userflags) {
298 $userflags = ($frameworkcode eq 'FA') ? "fast_cataloging" : "edit_items";
301 my ($template, $loggedinuser, $cookie)
302 = get_template_and_user({template_name => "cataloguing/additem.tt",
303 query => $input,
304 type => "intranet",
305 authnotrequired => 0,
306 flagsrequired => {editcatalogue => $userflags},
307 debug => 1,
311 my $today_iso = C4::Dates->today('iso');
312 my $tagslib = &GetMarcStructure(1,$frameworkcode);
313 my $record = GetMarcBiblio($biblionumber);
314 my $oldrecord = TransformMarcToKoha($dbh,$record);
315 my $itemrecord;
316 my $nextop="additem";
317 my @errors; # store errors found while checking data BEFORE saving item.
318 #-------------------------------------------------------------------------------
319 if ($op eq "additem") {
320 #-------------------------------------------------------------------------------
321 # rebuild
322 my @tags = $input->param('tag');
323 my @subfields = $input->param('subfield');
324 my @values = $input->param('field_value');
325 # build indicator hash.
326 my @ind_tag = $input->param('ind_tag');
327 my @indicator = $input->param('indicator');
328 my $xml = TransformHtmlToXml(\@tags,\@subfields,\@values,\@indicator,\@ind_tag, 'ITEM');
329 my $record = MARC::Record::new_from_xml($xml, 'UTF-8');
331 # type of add
332 my $add_submit = $input->param('add_submit');
333 my $add_duplicate_submit = $input->param('add_duplicate_submit');
334 my $add_multiple_copies_submit = $input->param('add_multiple_copies_submit');
335 my $number_of_copies = $input->param('number_of_copies');
337 if (C4::Context->preference('autoBarcode') eq 'incremental') {
338 $record = _increment_barcode($record, $frameworkcode);
341 my $addedolditem = TransformMarcToKoha($dbh,$record);
343 # If we have to add or add & duplicate, we add the item
344 if ($add_submit || $add_duplicate_submit) {
345 # check for item barcode # being unique
346 my $exist_itemnumber = get_item_from_barcode($addedolditem->{'barcode'});
347 push @errors,"barcode_not_unique" if($exist_itemnumber);
348 # if barcode exists, don't create, but report The problem.
349 unless ($exist_itemnumber) {
350 my ($oldbiblionumber,$oldbibnum,$oldbibitemnum) = AddItemFromMarc($record,$biblionumber);
351 set_item_default_location($oldbibitemnum);
353 $nextop = "additem";
354 if ($exist_itemnumber) {
355 $itemrecord = $record;
359 # If we have to add & duplicate
360 if ($add_duplicate_submit) {
361 $itemrecord = $record;
362 if (C4::Context->preference('autoBarcode') eq 'incremental') {
363 $itemrecord = _increment_barcode($itemrecord, $frameworkcode);
365 else {
366 # we have to clear the barcode field in the duplicate item record to make way for the new one generated by the javascript plugin
367 my ($tagfield,$tagsubfield) = &GetMarcFromKohaField("items.barcode",$frameworkcode);
368 my $fieldItem = $itemrecord->field($tagfield);
369 $itemrecord->delete_field($fieldItem);
370 $fieldItem->delete_subfields($tagsubfield);
371 $itemrecord->insert_fields_ordered($fieldItem);
375 # If we have to add multiple copies
376 if ($add_multiple_copies_submit) {
378 use C4::Barcodes;
379 my $barcodeobj = C4::Barcodes->new;
380 my $oldbarcode = $addedolditem->{'barcode'};
381 my ($tagfield,$tagsubfield) = &GetMarcFromKohaField("items.barcode",$frameworkcode);
383 # If there is a barcode and we can't find him new values, we can't add multiple copies
384 my $testbarcode;
385 $testbarcode = $barcodeobj->next_value($oldbarcode) if $barcodeobj;
386 if ($oldbarcode && !$testbarcode) {
388 push @errors, "no_next_barcode";
389 $itemrecord = $record;
391 } else {
392 # We add each item
394 # For the first iteration
395 my $barcodevalue = $oldbarcode;
396 my $exist_itemnumber;
399 for (my $i = 0; $i < $number_of_copies;) {
401 # If there is a barcode
402 if ($barcodevalue) {
404 # Getting a new barcode (if it is not the first iteration or the barcode we tried already exists)
405 $barcodevalue = $barcodeobj->next_value($oldbarcode) if ($i > 0 || $exist_itemnumber);
407 # Putting it into the record
408 if ($barcodevalue) {
409 $record->field($tagfield)->update($tagsubfield => $barcodevalue);
412 # Checking if the barcode already exists
413 $exist_itemnumber = get_item_from_barcode($barcodevalue);
416 # Adding the item
417 if (!$exist_itemnumber) {
418 my ($oldbiblionumber,$oldbibnum,$oldbibitemnum) = AddItemFromMarc($record,$biblionumber);
419 set_item_default_location($oldbibitemnum);
421 # We count the item only if it was really added
422 # That way, all items are added, even if there was some already existing barcodes
423 # FIXME : Please note that there is a risk of infinite loop here if we never find a suitable barcode
424 $i++;
427 # Preparing the next iteration
428 $oldbarcode = $barcodevalue;
430 undef($itemrecord);
433 if ($frameworkcode eq 'FA' && $input->param('borrowernumber')){
434 my $redirect_string = 'borrowernumber=' . uri_escape($input->param('borrowernumber')) .
435 '&barcode=' . uri_escape($input->param('barcode'));
436 $redirect_string .= '&duedatespec=' . uri_escape($input->param('duedatespec')) .
437 '&stickyduedate=1';
438 print $input->redirect("/cgi-bin/koha/circ/circulation.pl?" . $redirect_string);
439 exit;
443 #-------------------------------------------------------------------------------
444 } elsif ($op eq "edititem") {
445 #-------------------------------------------------------------------------------
446 # retrieve item if exist => then, it's a modif
447 $itemrecord = C4::Items::GetMarcItem($biblionumber,$itemnumber);
448 $nextop = "saveitem";
449 #-------------------------------------------------------------------------------
450 } elsif ($op eq "delitem") {
451 #-------------------------------------------------------------------------------
452 # check that there is no issue on this item before deletion.
453 $error = &DelItemCheck($dbh,$biblionumber,$itemnumber);
454 if($error == 1){
455 print $input->redirect("additem.pl?biblionumber=$biblionumber&frameworkcode=$frameworkcode");
456 }else{
457 push @errors,$error;
458 $nextop="additem";
460 #-------------------------------------------------------------------------------
461 } elsif ($op eq "delallitems") {
462 #-------------------------------------------------------------------------------
463 my @biblioitems = &GetBiblioItemByBiblioNumber($biblionumber);
464 my $errortest=0;
465 my $itemfail;
466 foreach my $biblioitem (@biblioitems) {
467 my $items = &GetItemsByBiblioitemnumber( $biblioitem->{biblioitemnumber} );
469 foreach my $item (@$items) {
470 $error =&DelItemCheck( $dbh, $biblionumber, $item->{itemnumber} );
471 $itemfail =$item;
472 if($error == 1){
473 next
475 else {
476 push @errors,$error;
477 $errortest++
480 if($errortest > 0){
481 $nextop="additem";
483 else {
484 my $defaultview = C4::Context->preference('IntranetBiblioDefaultView');
485 my $views = { C4::Search::enabled_staff_search_views };
486 if ($defaultview eq 'isbd' && $views->{can_view_ISBD}) {
487 print $input->redirect("/cgi-bin/koha/catalogue/ISBDdetail.pl?biblionumber=$biblionumber");
488 } elsif ($defaultview eq 'marc' && $views->{can_view_MARC}) {
489 print $input->redirect("/cgi-bin/koha/catalogue/MARCdetail.pl?biblionumber=$biblionumber");
490 } elsif ($defaultview eq 'labeled_marc' && $views->{can_view_labeledMARC}) {
491 print $input->redirect("/cgi-bin/koha/catalogue/labeledMARCdetail.pl?biblionumber=$biblionumber");
492 } else {
493 print $input->redirect("/cgi-bin/koha/catalogue/detail.pl?biblionumber=$biblionumber");
495 exit;
498 #-------------------------------------------------------------------------------
499 } elsif ($op eq "saveitem") {
500 #-------------------------------------------------------------------------------
501 # rebuild
502 my @tags = $input->param('tag');
503 my @subfields = $input->param('subfield');
504 my @values = $input->param('field_value');
505 # build indicator hash.
506 my @ind_tag = $input->param('ind_tag');
507 my @indicator = $input->param('indicator');
508 # my $itemnumber = $input->param('itemnumber');
509 my $xml = TransformHtmlToXml(\@tags,\@subfields,\@values,\@indicator,\@ind_tag,'ITEM');
510 my $itemtosave=MARC::Record::new_from_xml($xml, 'UTF-8');
511 # MARC::Record builded => now, record in DB
512 # warn "R: ".$record->as_formatted;
513 # check that the barcode don't exist already
514 my $addedolditem = TransformMarcToKoha($dbh,$itemtosave);
515 my $exist_itemnumber = get_item_from_barcode($addedolditem->{'barcode'});
516 if ($exist_itemnumber && $exist_itemnumber != $itemnumber) {
517 push @errors,"barcode_not_unique";
518 } else {
519 ModItemFromMarc($itemtosave,$biblionumber,$itemnumber);
520 $itemnumber="";
522 $nextop="additem";
523 } elsif ($op eq "delinkitem"){
524 my $analyticfield = '773';
525 if ($marcflavour eq 'MARC21' || $marcflavour eq 'NORMARC'){
526 $analyticfield = '773';
527 } elsif ($marcflavour eq 'UNIMARC') {
528 $analyticfield = '461';
530 foreach my $field ($record->field($analyticfield)){
531 if ($field->subfield('9') eq $hostitemnumber){
532 $record->delete_field($field);
533 last;
536 my $modbibresult = ModBiblio($record, $biblionumber,'');
540 #-------------------------------------------------------------------------------
541 # build screen with existing items. and "new" one
542 #-------------------------------------------------------------------------------
544 # now, build existiing item list
545 my $temp = GetMarcBiblio( $biblionumber );
546 #my @fields = $record->fields();
549 my %witness; #---- stores the list of subfields used at least once, with the "meaning" of the code
550 my @big_array;
551 #---- finds where items.itemnumber is stored
552 my ( $itemtagfield, $itemtagsubfield) = &GetMarcFromKohaField("items.itemnumber", $frameworkcode);
553 my ($branchtagfield, $branchtagsubfield) = &GetMarcFromKohaField("items.homebranch", $frameworkcode);
554 C4::Biblio::EmbedItemsInMarcBiblio($temp, $biblionumber);
555 my @fields = $temp->fields();
558 my @hostitemnumbers;
559 if ( C4::Context->preference('EasyAnalyticalRecords') ) {
560 my $analyticfield = '773';
561 if ($marcflavour eq 'MARC21' || $marcflavour eq 'NORMARC') {
562 $analyticfield = '773';
563 } elsif ($marcflavour eq 'UNIMARC') {
564 $analyticfield = '461';
566 foreach my $hostfield ($temp->field($analyticfield)){
567 my $hostbiblionumber = $hostfield->subfield('0');
568 if ($hostbiblionumber){
569 my $hostrecord = GetMarcBiblio($hostbiblionumber, 1);
570 if ($hostrecord) {
571 my ($itemfield, undef) = GetMarcFromKohaField( 'items.itemnumber', GetFrameworkCode($hostbiblionumber) );
572 foreach my $hostitem ($hostrecord->field($itemfield)){
573 if ($hostitem->subfield('9') eq $hostfield->subfield('9')){
574 push (@fields, $hostitem);
575 push (@hostitemnumbers, $hostfield->subfield('9'));
584 foreach my $field (@fields) {
585 next if ( $field->tag() < 10 );
587 my @subf = $field->subfields or (); # don't use ||, as that forces $field->subfelds to be interpreted in scalar context
588 my %this_row;
589 # loop through each subfield
590 my $i = 0;
591 foreach my $subfield (@subf){
592 my $subfieldcode = $subfield->[0];
593 my $subfieldvalue= $subfield->[1];
595 next if ($tagslib->{$field->tag()}->{$subfieldcode}->{tab} ne 10
596 && ($field->tag() ne $itemtagfield
597 && $subfieldcode ne $itemtagsubfield));
598 $witness{$subfieldcode} = $tagslib->{$field->tag()}->{$subfieldcode}->{lib} if ($tagslib->{$field->tag()}->{$subfieldcode}->{tab} eq 10);
599 if ($tagslib->{$field->tag()}->{$subfieldcode}->{tab} eq 10) {
600 $this_row{$subfieldcode} .= " | " if($this_row{$subfieldcode});
601 $this_row{$subfieldcode} .= GetAuthorisedValueDesc( $field->tag(),
602 $subfieldcode, $subfieldvalue, '', $tagslib)
603 || $subfieldvalue;
606 if (($field->tag eq $branchtagfield) && ($subfieldcode eq $branchtagsubfield) && C4::Context->preference("IndependantBranches")) {
607 #verifying rights
608 my $userenv = C4::Context->userenv();
609 unless (($userenv->{'flags'} == 1) or (($userenv->{'branch'} eq $subfieldvalue))){
610 $this_row{'nomod'} = 1;
613 $this_row{itemnumber} = $subfieldvalue if ($field->tag() eq $itemtagfield && $subfieldcode eq $itemtagsubfield);
615 if ( C4::Context->preference('EasyAnalyticalRecords') ) {
616 foreach my $hostitemnumber (@hostitemnumbers){
617 if ($this_row{itemnumber} eq $hostitemnumber){
618 $this_row{hostitemflag} = 1;
619 $this_row{hostbiblionumber}= GetBiblionumberFromItemnumber($hostitemnumber);
620 last;
624 # my $countanalytics=GetAnalyticsCount($this_row{itemnumber});
625 # if ($countanalytics > 0){
626 # $this_row{countanalytics} = $countanalytics;
631 if (%this_row) {
632 push(@big_array, \%this_row);
636 my ($holdingbrtagf,$holdingbrtagsubf) = &GetMarcFromKohaField("items.holdingbranch",$frameworkcode);
637 @big_array = sort {$a->{$holdingbrtagsubf} cmp $b->{$holdingbrtagsubf}} @big_array;
639 # now, construct template !
640 # First, the existing items for display
641 my @item_value_loop;
642 my @header_value_loop;
643 for my $row ( @big_array ) {
644 my %row_data;
645 my @item_fields = map +{ field => $_ || '' }, @$row{ sort keys(%witness) };
646 $row_data{item_value} = [ @item_fields ];
647 $row_data{itemnumber} = $row->{itemnumber};
648 #reporting this_row values
649 $row_data{'nomod'} = $row->{'nomod'};
650 $row_data{'hostitemflag'} = $row->{'hostitemflag'};
651 $row_data{'hostbiblionumber'} = $row->{'hostbiblionumber'};
652 # $row_data{'countanalytics'} = $row->{'countanalytics'};
653 push(@item_value_loop,\%row_data);
655 foreach my $subfield_code (sort keys(%witness)) {
656 my %header_value;
657 $header_value{header_value} = $witness{$subfield_code};
658 push(@header_value_loop, \%header_value);
661 # now, build the item form for entering a new item
662 my @loop_data =();
663 my $i=0;
665 my $pref_itemcallnumber = C4::Context->preference('itemcallnumber');
667 my $onlymine = C4::Context->preference('IndependantBranches') &&
668 C4::Context->userenv &&
669 C4::Context->userenv->{flags}!=1 &&
670 C4::Context->userenv->{branch};
671 my $branch = $input->param('branch') || C4::Context->userenv->{branch};
672 my $branches = GetBranchesLoop($branch,$onlymine); # build once ahead of time, instead of multiple times later.
674 # We generate form, from actuel record
675 @fields = ();
676 if($itemrecord){
677 foreach my $field ($itemrecord->fields()){
678 my $tag = $field->{_tag};
679 foreach my $subfield ( $field->subfields() ){
681 my $subfieldtag = $subfield->[0];
682 my $value = $subfield->[1];
683 my $subfieldlib = $tagslib->{$tag}->{$subfieldtag};
685 next if subfield_is_koha_internal_p($subfieldtag);
686 next if ($tagslib->{$tag}->{$subfieldtag}->{'tab'} ne "10");
688 my $subfield_data = generate_subfield_form($tag, $subfieldtag, $value, $tagslib, $subfieldlib, $branches, $today_iso, $biblionumber, $temp, \@loop_data, $i);
690 push @fields, "$tag$subfieldtag";
691 push (@loop_data, $subfield_data);
692 $i++;
697 # and now we add fields that are empty
699 foreach my $tag ( keys %{$tagslib}){
700 foreach my $subtag (keys %{$tagslib->{$tag}}){
701 next if subfield_is_koha_internal_p($subtag);
702 next if ($tagslib->{$tag}->{$subtag}->{'tab'} ne "10");
703 next if any { /^$tag$subtag$/ } @fields;
705 my @values = (undef);
706 @values = $itemrecord->field($tag)->subfield($subtag) if ($itemrecord && defined($itemrecord->field($tag)->subfield($subtag)));
707 for my $value (@values){
708 my $subfield_data = generate_subfield_form($tag, $subtag, $value, $tagslib, $tagslib->{$tag}->{$subtag}, $branches, $today_iso, $biblionumber, $temp, \@loop_data, $i);
709 push (@loop_data, $subfield_data);
710 $i++;
714 @loop_data = sort {$a->{subfield} cmp $b->{subfield} } @loop_data;
716 # what's the next op ? it's what we are not in : an add if we're editing, otherwise, and edit.
717 $template->param( title => $record->title() ) if ($record ne "-1");
718 $template->param(
719 biblionumber => $biblionumber,
720 title => $oldrecord->{title},
721 author => $oldrecord->{author},
722 item_loop => \@item_value_loop,
723 item_header_loop => \@header_value_loop,
724 item => \@loop_data,
725 itemnumber => $itemnumber,
726 barcode => GetBarcodeFromItemnumber($itemnumber),
727 itemtagfield => $itemtagfield,
728 itemtagsubfield => $itemtagsubfield,
729 op => $nextop,
730 opisadd => ($nextop eq "saveitem") ? 0 : 1,
731 popup => $input->param('popup') ? 1: 0,
732 C4::Search::enabled_staff_search_views,
735 if ($frameworkcode eq 'FA'){
736 $template->{VARS}->{'borrowernumber'}=$input->param('borrowernumber');
737 $template->{VARS}->{'barcode'}=$input->param('barcode');
738 $template->{VARS}->{'stickyduedate'}=$input->param('stickduedate');
739 $template->{VARS}->{'duedatespec'}=$input->param('duedatespec');
742 foreach my $error (@errors) {
743 $template->param($error => 1);
745 output_html_with_http_headers $input, $cookie, $template->output;