1 package Koha::UI::Form::Builder::Item;
3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
21 use C4::Biblio qw( GetFrameworkCode GetMarcBiblio GetMarcStructure IsMarcStructureInternal );
22 use C4::Koha qw( GetAuthorisedValues );
23 use C4::ClassSource qw( GetClassSources );
25 use Koha::DateUtils qw( dt_from_string );
30 Koha::UI::Form::Builder::Item
32 Helper to build a form to add or edit a new item.
42 my $form = Koha::UI::Form::Builder::Item->new(
44 biblionumber => $biblionumber,
45 item => $current_item,
50 biblionumber should be passed if we are creating a new item.
51 For edition, an hashref representing the item to edit item must be passed.
57 my ( $class, $params ) = @_;
60 $self->{biblionumber} = $params->{biblionumber};
61 $self->{item} = $params->{item};
67 =head3 generate_subfield_form
69 Generate subfield's info for given tag, subfieldtag, etc.
73 sub generate_subfield_form {
75 my ($self, $params) = @_;
76 my $tag = $params->{tag};
77 my $subfieldtag = $params->{subfieldtag};
78 my $value = $params->{value};
79 my $tagslib = $params->{tagslib};
80 my $libraries = $params->{libraries};
81 my $marc_record = $params->{marc_record};
82 my $restricted_edition = $params->{restricted_editition};
83 my $prefill_with_default_values = $params->{prefill_with_default_values};
84 my $branch_limit = $params->{branch_limit};
85 my $default_branches_empty = $params->{default_branches_empty};
86 my $readonly = $params->{readonly};
88 my $item = $self->{item};
89 my $subfield = $tagslib->{$tag}{$subfieldtag};
90 my $biblionumber = $self->{biblionumber};
92 my $frameworkcode = $biblionumber ? GetFrameworkCode($biblionumber) : q{};
95 my $dbh = C4::Context->dbh;
97 my $index_subfield = int( rand(1000000) );
98 if ( $subfieldtag eq '@' ) {
99 $subfield_data{id} = "tag_" . $tag . "_subfield_00_" . $index_subfield;
103 "tag_" . $tag . "_subfield_" . $subfieldtag . "_" . $index_subfield;
106 $subfield_data{tag} = $tag;
107 $subfield_data{subfield} = $subfieldtag;
108 $subfield_data{marc_lib} =
110 . $subfield->{lib} . "\">"
113 $subfield_data{mandatory} = $subfield->{mandatory};
114 $subfield_data{important} = $subfield->{important};
115 $subfield_data{repeatable} = $subfield->{repeatable};
116 $subfield_data{maxlength} = $subfield->{maxlength};
117 $subfield_data{display_order} = $subfield->{display_order};
118 $subfield_data{kohafield} =
119 $subfield->{kohafield} || 'items.more_subfields_xml';
121 if ( $prefill_with_default_values && ( !defined($value) || $value eq '' ) ) {
122 $value = $subfield->{defaultvalue} if !$item->{itemnumber}; # apply defaultvalue only to new items
125 # get today date & replace <<YYYY>>, <<YY>>, <<MM>>, <<DD>> if provided in the default value
126 my $today_dt = dt_from_string;
127 my $year = $today_dt->strftime('%Y');
128 my $shortyear = $today_dt->strftime('%y');
129 my $month = $today_dt->strftime('%m');
130 my $day = $today_dt->strftime('%d');
131 $value =~ s/<<YYYY>>/$year/g;
132 $value =~ s/<<YY>>/$shortyear/g;
133 $value =~ s/<<MM>>/$month/g;
134 $value =~ s/<<DD>>/$day/g;
136 # And <<USER>> with surname (?)
139 ? C4::Context->userenv->{'surname'}
142 $value =~ s/<<USER>>/$username/g;
146 $subfield_data{visibility} = "display:none;"
147 if ( ( $subfield->{hidden} > 4 ) || ( $subfield->{hidden} <= -4 ) );
149 my $pref_itemcallnumber = C4::Context->preference('itemcallnumber');
150 if ( $prefill_with_default_values
152 && $subfield->{kohafield}
153 && $subfield->{kohafield} eq 'items.itemcallnumber'
154 && $pref_itemcallnumber )
157 my $pref_itemcallnumber_part ( split( /,/, $pref_itemcallnumber ) )
160 substr( $pref_itemcallnumber_part, 0, 3 ); # 3-digit tag number
162 substr( $pref_itemcallnumber_part, 3 ); # Any and all subfields
163 $CNsubfields = undef if $CNsubfields eq '';
164 my $temp2 = $marc_record->field($CNtag);
167 $value = $temp2->as_string( $CNsubfields, ' ' );
172 if ( $subfield->{authorised_value} ) {
173 my @authorised_values;
176 # builds list, depending on authorised value...
177 if ( $subfield->{authorised_value} eq "LOST" ) {
178 my $ClaimReturnedLostValue =
179 C4::Context->preference('ClaimReturnedLostValue');
180 my $item_is_return_claim =
181 $ClaimReturnedLostValue
182 && exists $item->{itemlost}
183 && $ClaimReturnedLostValue eq $item->{itemlost};
184 $subfield_data{IS_RETURN_CLAIM} = $item_is_return_claim;
186 $subfield_data{IS_LOST_AV} = 1;
188 push @authorised_values, qq{};
189 my $av = GetAuthorisedValues( $subfield->{authorised_value} );
191 push @authorised_values, $r->{authorised_value};
192 $authorised_lib{ $r->{authorised_value} } = $r->{lib};
195 elsif ( $subfield->{authorised_value} eq "branches" ) {
196 push @authorised_values, "" if $default_branches_empty;
197 foreach my $thisbranch (@$libraries) {
198 push @authorised_values, $thisbranch->{branchcode};
199 $authorised_lib{ $thisbranch->{branchcode} } =
200 $thisbranch->{branchname};
201 $value = $thisbranch->{branchcode}
202 if $thisbranch->{selected} && !$value;
205 elsif ( $subfield->{authorised_value} eq "itemtypes" ) {
206 push @authorised_values, "";
209 $itemtypes = Koha::ItemTypes->search_with_localization(
210 { branchcode => $branch_limit } );
213 $itemtypes = Koha::ItemTypes->search_with_localization;
215 while ( my $itemtype = $itemtypes->next ) {
216 push @authorised_values, $itemtype->itemtype;
217 $authorised_lib{ $itemtype->itemtype } =
218 $itemtype->translated_description;
221 if (!$value && $biblionumber) {
222 my $itype_sth = $dbh->prepare(
223 "SELECT itemtype FROM biblioitems WHERE biblionumber = ?");
224 $itype_sth->execute($biblionumber);
225 ($value) = $itype_sth->fetchrow_array;
230 elsif ( $subfield->{authorised_value} eq "cn_source" ) {
231 push @authorised_values, "";
233 my $class_sources = GetClassSources();
235 C4::Context->preference("DefaultClassificationSource");
237 foreach my $class_source ( sort keys %$class_sources ) {
239 unless $class_sources->{$class_source}->{'used'}
240 or ( $value and $class_source eq $value )
241 or ( $class_source eq $default_source );
242 push @authorised_values, $class_source;
243 $authorised_lib{$class_source} =
244 $class_sources->{$class_source}->{'description'};
246 $value = $default_source if !$value && $prefill_with_default_values;
248 #---- "true" authorised value
251 push @authorised_values, qq{};
252 my $av = GetAuthorisedValues( $subfield->{authorised_value} );
254 push @authorised_values, $r->{authorised_value};
255 $authorised_lib{ $r->{authorised_value} } = $r->{lib};
259 if ( $subfield->{hidden} > 4 or $subfield->{hidden} <= -4 ) {
260 $subfield_data{marc_value} = {
262 id => $subfield_data{id},
263 maxlength => $subfield_data{maxlength},
267 grep { $_ eq $subfield->{authorised_value} }
268 (qw(branches itemtypes cn_source))
269 ) ? () : ( category => $subfield->{authorised_value} )
274 $subfield_data{marc_value} = {
281 values => \@authorised_values,
282 labels => \%authorised_lib,
286 grep { $_ eq $subfield->{authorised_value} }
287 (qw(branches itemtypes cn_source))
288 ) ? () : ( category => $subfield->{authorised_value} )
294 # it's a thesaurus / authority field
295 elsif ( $subfield->{authtypecode} ) {
296 $subfield_data{marc_value} = {
298 id => $subfield_data{id},
299 maxlength => $subfield_data{maxlength},
301 authtypecode => $subfield->{authtypecode},
305 # it's a plugin field
306 elsif ( $subfield->{value_builder} ) { # plugin
307 require Koha::FrameworkPlugin;
308 my $plugin = Koha::FrameworkPlugin->new(
310 name => $subfield->{'value_builder'},
316 record => $marc_record,
318 id => $subfield_data{id},
320 $plugin->build($pars);
321 if ( !$plugin->errstr ) {
322 my $class = 'buttonDot' . ( $plugin->noclick ? ' disabled' : '' );
323 $subfield_data{marc_value} = {
324 type => 'text_plugin',
325 id => $subfield_data{id},
326 maxlength => $subfield_data{maxlength},
329 nopopup => $plugin->noclick,
330 javascript => $plugin->javascript,
334 warn $plugin->errstr;
335 $subfield_data{marc_value} = {
337 id => $subfield_data{id},
338 maxlength => $subfield_data{maxlength},
340 }; # supply default input form
343 elsif ( $tag eq '' ) { # it's an hidden field
344 $subfield_data{marc_value} = {
346 id => $subfield_data{id},
347 maxlength => $subfield_data{maxlength},
351 elsif ( $subfield->{'hidden'} )
352 { # FIXME: shouldn't input type be "hidden" ?
353 $subfield_data{marc_value} = {
355 id => $subfield_data{id},
356 maxlength => $subfield_data{maxlength},
361 ( $value and length($value) > 100 )
362 or ( C4::Context->preference("marcflavour") eq "UNIMARC"
363 and 300 <= $tag && $tag < 400 && $subfieldtag eq 'a' )
364 or ( C4::Context->preference("marcflavour") eq "MARC21"
365 and 500 <= $tag && $tag < 600 )
368 # oversize field (textarea)
369 $subfield_data{marc_value} = {
371 id => $subfield_data{id},
376 # it's a standard field
377 $subfield_data{marc_value} = {
379 id => $subfield_data{id},
380 maxlength => $subfield_data{maxlength},
385 # If we're on restricted editing, and our field is not in the list of subfields to allow,
386 # then it is read-only
387 $subfield_data{marc_value}->{readonly} = $readonly;
389 return \%subfield_data;
395 Koha::UI::Form::Builder::Item->new(
396 { biblionumber => $biblionumber, item => $current_item } )->edit_form(
398 branchcode => $branchcode,
399 restricted_editition => $restrictededition,
401 @subfields_to_prefill
402 ? ( subfields_to_prefill => \@subfields_to_prefill )
405 prefill_with_default_values => 1,
406 branch_limit => C4::Context->userenv->{"branch"},
410 Returns the list of subfields to display on the add/edit item form.
412 Use it in the view with:
413 [% PROCESS subfields_for_item subfields => subfields %]
421 Pre-select a library (for logged in user)
423 =item restricted_editition
425 Flag to restrict the edition if the user does not have necessary permissions.
427 =item subfields_to_prefill
429 List of subfields to prefill (value of syspref SubfieldsToUseWhenPrefill)
431 =item subfields_to_allow
433 List of subfields to allow (value of syspref SubfieldsToAllowForRestrictedBatchmod or SubfieldsToAllowForRestrictedEditing)
435 =item ignore_not_allowed_subfields
437 If set, the subfields in subfields_to_allow will be ignored (ie. they will not be part of the subfield list.
438 If not set, the subfields in subfields_to_allow will be marked as readonly.
440 =item kohafields_to_ignore
442 List of subfields to ignore/skip
444 =item prefill_with_default_values
446 Flag to prefill with the default values defined in the framework.
450 Limit info depending on the library (so far only item types).
452 =item default_branches_empty
454 Flag to add an empty option to the library list.
456 =item ignore_invisible_subfields
458 Skip the subfields that are not visible on the editor.
460 When duplicating an item we do not want to retrieve the subfields that are hidden.
467 my ( $self, $params ) = @_;
469 my $branchcode = $params->{branchcode};
470 my $restricted_edition = $params->{restricted_editition};
471 my $subfields_to_prefill = $params->{subfields_to_prefill} || [];
472 my $subfields_to_allow = $params->{subfields_to_allow} || [];
473 my $ignore_not_allowed_subfields = $params->{ignore_not_allowed_subfields};
474 my $kohafields_to_ignore = $params->{kohafields_to_ignore} || [];
475 my $prefill_with_default_values = $params->{prefill_with_default_values};
476 my $branch_limit = $params->{branch_limit};
477 my $default_branches_empty = $params->{default_branches_empty};
478 my $ignore_invisible_subfields = $params->{ignore_invisible_subfields} || 0;
481 Koha::Libraries->search( {}, { order_by => ['branchname'] } )->unblessed;
482 for my $library (@$libraries) {
483 $library->{selected} = 1 if $branchcode && $library->{branchcode} eq $branchcode;
486 my $item = $self->{item};
487 my $marc_more_subfields = $item->{more_subfields_xml}
489 # FIXME Use Maybe MARC::Record::new_from_xml if encoding issues on subfield (??)
490 MARC::Record->new_from_xml( $item->{more_subfields_xml}, 'UTF-8' )
493 my $biblionumber = $self->{biblionumber};
494 my $frameworkcode = $biblionumber ? GetFrameworkCode($biblionumber) : q{};
495 my $marc_record = $biblionumber ? GetMarcBiblio( { biblionumber => $biblionumber } ) : undef;
497 my $tagslib = GetMarcStructure( 1, $frameworkcode );
498 foreach my $tag ( keys %{$tagslib} ) {
500 foreach my $subfieldtag ( keys %{ $tagslib->{$tag} } ) {
502 my $subfield = $tagslib->{$tag}{$subfieldtag};
504 next if IsMarcStructureInternal($subfield);
505 next if $subfield->{tab} ne "10";
507 if grep { $subfield->{kohafield} && $subfield->{kohafield} eq $_ }
508 @$kohafields_to_ignore;
511 if $ignore_invisible_subfields
512 && ( $subfield->{hidden} > 4 || $subfield->{hidden} <= -4 );
516 @$subfields_to_allow && !grep {
517 sprintf( "%s\$%s", $subfield->{tagfield}, $subfield->{tagsubfield} ) eq $_
518 } @$subfields_to_allow
521 next if $ignore_not_allowed_subfields;
522 $readonly = 1 if $restricted_edition;
530 !@$subfields_to_prefill
531 || ( @$subfields_to_prefill && grep { $_ eq $subfieldtag }
532 @$subfields_to_prefill )
535 my $kohafield = $subfield->{kohafield};
538 # This is a mapped field
539 ( my $attribute = $kohafield ) =~ s|^items\.||;
540 push @values, $subfield->{repeatable}
541 ? split '\s\|\s', $item->{$attribute}
542 : $item->{$attribute}
543 if defined $item->{$attribute};
546 # Not mapped, picked the values from more_subfields_xml's MARC
547 if ($marc_more_subfields) {
548 for my $f ( $marc_more_subfields->fields($tag) ) {
549 push @values, $f->subfield($subfieldtag);
555 @values = ('') unless @values;
557 for my $value (@values) {
558 my $subfield_data = $self->generate_subfield_form(
561 subfieldtag => $subfieldtag,
564 libraries => $libraries,
565 marc_record => $marc_record,
566 restricted_edition => $restricted_edition,
567 prefill_with_default_values => $prefill_with_default_values,
568 branch_limit => $branch_limit,
569 default_branches_empty => $default_branches_empty,
570 readonly => $readonly
573 push @subfields, $subfield_data;
579 $a->{display_order} <=> $b->{display_order}
580 || $a->{subfield} cmp $b->{subfield}