1 package Koha::UI::Form::Builder::Biblio;
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>.
20 use C4::ClassSource qw( GetClassSources );
21 use Koha::DateUtils qw( dt_from_string );
27 Koha::UI::Form::Builder::Biblio
29 Helper to build a form to add or edit a new biblio
39 my $form = Koha::UI::Form::Builder::Biblio->new(
41 biblionumber => $biblionumber,
49 my ( $class, $params ) = @_;
53 $self->{biblionumber} = $params->{biblionumber} =~ s/\D//gr;
54 # just in case biblionumber obtained from CGI and passed directly here contains weird characters like spaces
60 =head3 generate_subfield_form
62 Generate subfield's info for given tag, subfieldtag, etc.
66 sub generate_subfield_form {
67 my ($self, $params) = @_;
69 my $tag = $params->{tag};
70 my $subfield = $params->{subfield};
71 my $value = $params->{value} // '';
72 my $index_tag = $params->{index_tag};
73 my $rec = $params->{record};
74 my $hostitemnumber = $params->{hostitemnumber};
75 my $op = $params->{op} // '';
76 my $changed_framework = $params->{changed_framework};
77 my $breedingid = $params->{breedingid};
78 my $tagslib = $params->{tagslib};
79 my $mandatory_z3950 = $params->{mandatory_z3950} // {};
81 my $index_subfield = $self->create_key(); # create a specific key for each subfield
83 # Apply optional framework default value when it is a new record,
84 # or when editing as new (duplicating a record),
85 # or when changing a record's framework,
86 # or when importing a record,
87 # based on the ApplyFrameworkDefaults setting.
88 # Substitute date parts, user name
89 my $applydefaults = C4::Context->preference('ApplyFrameworkDefaults');
90 if ( $value eq '' && (
91 ( $applydefaults =~ /new/ && !$self->{biblionumber} ) ||
92 ( $applydefaults =~ /duplicate/ && $op eq 'duplicate' ) ||
93 ( $applydefaults =~ /changed/ && $changed_framework ) ||
94 ( $applydefaults =~ /imported/ && $breedingid )
96 $value = $tagslib->{$tag}->{$subfield}->{defaultvalue} // q{};
98 # get today date & replace <<YYYY>>, <<YY>>, <<MM>>, <<DD>> if provided in the default value
99 my $today_dt = dt_from_string;
100 my $year = $today_dt->strftime('%Y');
101 my $shortyear = $today_dt->strftime('%y');
102 my $month = $today_dt->strftime('%m');
103 my $day = $today_dt->strftime('%d');
104 $value =~ s/<<YYYY>>/$year/g;
105 $value =~ s/<<YY>>/$shortyear/g;
106 $value =~ s/<<MM>>/$month/g;
107 $value =~ s/<<DD>>/$day/g;
108 # And <<USER>> with surname (?)
109 my $username=(C4::Context->userenv?C4::Context->userenv->{'surname'}:"superlibrarian");
110 $value=~s/<<USER>>/$username/g;
113 my $dbh = C4::Context->dbh;
115 # map '@' as "subfield" label for fixed fields
116 # to something that's allowed in a div id.
117 my $id_subfield = $subfield;
118 $id_subfield = "00" if $id_subfield eq "@";
120 my %subfield_data = (
122 subfield => $id_subfield,
123 marc_lib => $tagslib->{$tag}->{$subfield}->{lib},
124 tag_mandatory => $tagslib->{$tag}->{mandatory},
125 mandatory => $tagslib->{$tag}->{$subfield}->{mandatory},
126 important => $tagslib->{$tag}->{$subfield}->{important},
127 repeatable => $tagslib->{$tag}->{$subfield}->{repeatable},
128 kohafield => $tagslib->{$tag}->{$subfield}->{kohafield},
130 id => "tag_".$tag."_subfield_".$id_subfield."_".$index_tag."_".$index_subfield,
132 maxlength => $tagslib->{$tag}->{$subfield}->{maxlength},
133 random => $self->create_key(),
136 if (exists $mandatory_z3950->{$tag.$subfield}){
137 $subfield_data{z3950_mandatory} = $mandatory_z3950->{$tag.$subfield};
139 # Subfield is hidden depending of hidden and mandatory flag, and is always
140 # shown if it contains anything or if its field is mandatory or important.
141 my $tdef = $tagslib->{$tag};
142 $subfield_data{visibility} = "display:none;"
143 if $tdef->{$subfield}->{hidden} % 2 == 1 &&
145 !$tdef->{$subfield}->{mandatory} &&
146 !$tdef->{mandatory} &&
147 !$tdef->{$subfield}->{important} &&
149 # expand all subfields of 773 if there is a host item provided in the input
150 $subfield_data{visibility} = '' if ($tag eq '773' and $hostitemnumber);
153 # it's an authorised field
154 if ( $tagslib->{$tag}->{$subfield}->{authorised_value} ) {
155 $subfield_data{marc_value} = $self->build_authorized_values_list(
158 subfield => $subfield,
160 index_tag => $index_tag,
161 index_subfield => $index_subfield,
166 # it's a subfield $9 linking to an authority record - see bug 2206 and 28022
167 elsif ($subfield eq "9" and
168 exists($tagslib->{$tag}->{'a'}->{authtypecode}) and
169 defined($tagslib->{$tag}->{'a'}->{authtypecode}) and
170 $tagslib->{$tag}->{'a'}->{authtypecode} ne '' and
171 $tagslib->{$tag}->{'a'}->{hidden} > -4 and
172 $tagslib->{$tag}->{'a'}->{hidden} < 5) {
173 $subfield_data{marc_value} = {
175 id => $subfield_data{id},
176 name => $subfield_data{id},
179 maxlength => $subfield_data{maxlength},
183 # it's a thesaurus / authority field
185 elsif ( $tagslib->{$tag}->{$subfield}->{authtypecode} ) {
186 # when authorities auto-creation is allowed, do not set readonly
187 my $is_readonly = C4::Context->preference('RequireChoosingExistingAuthority');
189 $subfield_data{marc_value} = {
191 id => $subfield_data{id},
192 name => $subfield_data{id},
195 maxlength => $subfield_data{maxlength},
196 readonly => ($is_readonly) ? 1 : 0,
197 authtype => $tagslib->{$tag}->{$subfield}->{authtypecode},
200 # it's a plugin field
201 } elsif ( $tagslib->{$tag}->{$subfield}->{'value_builder'} ) {
202 require Koha::FrameworkPlugin;
203 my $plugin = Koha::FrameworkPlugin->new( {
204 name => $tagslib->{$tag}->{$subfield}->{'value_builder'},
206 my $pars= { dbh => $dbh, record => $rec, tagslib => $tagslib,
207 id => $subfield_data{id} };
208 $plugin->build( $pars );
209 if( !$plugin->errstr ) {
210 $subfield_data{marc_value} = {
211 type => 'text_complex',
212 id => $subfield_data{id},
213 name => $subfield_data{id},
216 maxlength => $subfield_data{maxlength},
217 javascript => $plugin->javascript,
218 plugin => $plugin->name,
219 noclick => $plugin->noclick,
222 warn $plugin->errstr;
223 # supply default input form
224 $subfield_data{marc_value} = {
226 id => $subfield_data{id},
227 name => $subfield_data{id},
230 maxlength => $subfield_data{maxlength},
235 # it's an hidden field
236 } elsif ( $tag eq '' ) {
237 $subfield_data{marc_value} = {
239 id => $subfield_data{id},
240 name => $subfield_data{id},
243 maxlength => $subfield_data{maxlength},
248 # it's a standard field
252 ( C4::Context->preference("marcflavour") eq "UNIMARC" && $tag >= 300
253 and $tag < 400 && $subfield eq 'a' )
256 && C4::Context->preference("marcflavour") eq "MARC21" )
259 $subfield_data{marc_value} = {
261 id => $subfield_data{id},
262 name => $subfield_data{id},
268 $subfield_data{marc_value} = {
270 id => $subfield_data{id},
271 name => $subfield_data{id},
274 maxlength => $subfield_data{maxlength},
280 $subfield_data{'index_subfield'} = $index_subfield;
282 return \%subfield_data;
285 =head3 build_authorized_values_list
287 Return list of authorized values for given tag, subfield
291 sub build_authorized_values_list {
292 my ($self, $params) = @_;
294 my $tag = $params->{tag};
295 my $subfield = $params->{subfield};
296 my $value = $params->{value};
297 my $index_tag = $params->{index_tag};
298 my $index_subfield = $params->{index_subfield};
299 my $tagslib = $params->{tagslib};
301 my @authorised_values;
304 # builds list, depending on authorised value...
307 my $category = $tagslib->{$tag}->{$subfield}->{authorised_value};
308 if ( $category eq "branches" ) {
309 my $libraries = Koha::Libraries->search_filtered({}, {order_by => ['branchname']});
310 while ( my $l = $libraries->next ) {
311 push @authorised_values, $l->branchcode;
312 $authorised_lib{$l->branchcode} = $l->branchname;
315 elsif ( $category eq "itemtypes" ) {
316 push @authorised_values, "";
319 my $itemtypes = Koha::ItemTypes->search_with_localization;
320 while ( $itemtype = $itemtypes->next ) {
321 push @authorised_values, $itemtype->itemtype;
322 $authorised_lib{$itemtype->itemtype} = $itemtype->translated_description;
324 $value = $itemtype unless ($value);
326 elsif ( $category eq "cn_source" ) {
327 push @authorised_values, "";
329 my $class_sources = GetClassSources();
331 my $default_source = C4::Context->preference("DefaultClassificationSource");
333 foreach my $class_source (sort keys %$class_sources) {
334 next unless $class_sources->{$class_source}->{'used'} or
335 ($value and $class_source eq $value) or
336 ($class_source eq $default_source);
337 push @authorised_values, $class_source;
338 $authorised_lib{$class_source} = $class_sources->{$class_source}->{'description'};
340 $value = $default_source unless $value;
343 my $branch_limit = C4::Context->userenv ? C4::Context->userenv->{branch} : '';
344 my $query = 'SELECT authorised_value, lib FROM authorised_values';
345 $query .= ' LEFT JOIN authorised_values_branches ON ( id = av_id )' if $branch_limit;
346 $query .= ' WHERE category = ?';
347 $query .= ' AND ( branchcode = ? OR branchcode IS NULL )' if $branch_limit;
348 $query .= ' GROUP BY authorised_value,lib ORDER BY lib, lib_opac';
349 my $authorised_values_sth = C4::Context->dbh->prepare($query);
351 $authorised_values_sth->execute(
352 $tagslib->{$tag}->{$subfield}->{authorised_value},
353 $branch_limit ? $branch_limit : (),
356 push @authorised_values, "";
358 while ( my ( $value, $lib ) = $authorised_values_sth->fetchrow_array ) {
359 push @authorised_values, $value;
360 $authorised_lib{$value} = $lib;
366 id => "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield,
367 name => "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield,
369 values => \@authorised_values,
370 labels => \%authorised_lib,
371 ( ( grep { $_ eq $category } ( qw(branches itemtypes cn_source) ) ) ? () : ( category => $category ) ),
378 Create unique key for subfields
383 return int(rand(1000000));