| 1 |
package HTML::FormHandler::BuildFields; |
| 2 |
# ABSTRACT: role to build field array |
| 3 |
$HTML::FormHandler::BuildFields::VERSION = '0.410002'; |
| 4 |
use Moose::Role; |
| 5 |
use Try::Tiny; |
| 6 |
use Class::Load qw/ load_optional_class /; |
| 7 |
use namespace::autoclean; |
| 8 |
use HTML::FormHandler::Merge ('merge'); |
| 9 |
use Data::Clone; |
| 10 |
|
| 11 |
|
| 12 |
has 'fields_from_model' => ( isa => 'Bool', is => 'rw' ); |
| 13 |
|
| 14 |
has 'field_list' => ( isa => 'HashRef|ArrayRef', is => 'rw', default => sub { {} } ); |
| 15 |
|
| 16 |
has 'build_include_method' => ( is => 'ro', isa => 'CodeRef', traits => ['Code'], |
| 17 |
default => sub { \&default_build_include }, handles => { build_include => 'execute_method' } ); |
| 18 |
has 'include' => ( is => 'rw', isa => 'ArrayRef', traits => ['Array'], builder => 'build_include', |
| 19 |
lazy => 1, handles => { has_include => 'count' } ); |
| 20 |
sub default_build_include { [] } |
| 21 |
|
| 22 |
sub has_field_list { |
| 23 |
my ( $self, $field_list ) = @_; |
| 24 |
$field_list ||= $self->field_list; |
| 25 |
if ( ref $field_list eq 'HASH' ) { |
| 26 |
return $field_list if ( scalar keys %{$field_list} ); |
| 27 |
} |
| 28 |
elsif ( ref $field_list eq 'ARRAY' ) { |
| 29 |
return $field_list if ( scalar @{$field_list} ); |
| 30 |
} |
| 31 |
return; |
| 32 |
} |
| 33 |
|
| 34 |
|
| 35 |
# This is the only entry point for this file. It processes the |
| 36 |
# various methods of field definition (has_field plus the attrs above), |
| 37 |
# creates objects for fields and writes them into the 'fields' attr |
| 38 |
# on the base object. |
| 39 |
# |
| 40 |
# calls routines to process various field lists |
| 41 |
# orders the fields after processing in order to skip |
| 42 |
# fields which have had the 'order' attribute set |
| 43 |
sub _build_fields { |
| 44 |
my $self = shift; |
| 45 |
|
| 46 |
my $meta_flist = $self->_build_meta_field_list; |
| 47 |
|
| 48 |
$self->_process_field_array( $meta_flist, 0 ) if $meta_flist; |
| 49 |
my $flist = $self->has_field_list; |
| 50 |
if( $flist ) { |
| 51 |
if( ref($flist) eq 'ARRAY' && ref( $flist->[0] ) eq 'HASH' ) { |
| 52 |
$self->_process_field_array( $flist ); |
| 53 |
} |
| 54 |
else { |
| 55 |
$self->_process_field_list( $flist ); |
| 56 |
} |
| 57 |
} |
| 58 |
if ( $self->fields_from_model && (my $mlist = $self->model_fields) ) { |
| 59 |
$self->_process_field_list( $mlist ); |
| 60 |
} |
| 61 |
|
| 62 |
return unless $self->has_fields; |
| 63 |
|
| 64 |
$self->_order_fields; |
| 65 |
|
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
# loops through all inherited classes and composed roles |
| 70 |
# to find fields specified with 'has_field' |
| 71 |
sub _build_meta_field_list { |
| 72 |
my $self = shift; |
| 73 |
my $field_list = []; |
| 74 |
|
| 75 |
foreach my $sc ( reverse $self->meta->linearized_isa ) { |
| 76 |
my $meta = $sc->meta; |
| 77 |
if ( $meta->can('calculate_all_roles') ) { |
| 78 |
foreach my $role ( reverse $meta->calculate_all_roles ) { |
| 79 |
if ( $role->can('field_list') && $role->has_field_list ) { |
| 80 |
foreach my $fld_def ( @{ $role->field_list } ) { |
| 81 |
push @$field_list, $fld_def; |
| 82 |
} |
| 83 |
} |
| 84 |
} |
| 85 |
} |
| 86 |
if ( $meta->can('field_list') && $meta->has_field_list ) { |
| 87 |
foreach my $fld_def ( @{ $meta->field_list } ) { |
| 88 |
push @$field_list, $fld_def; |
| 89 |
} |
| 90 |
} |
| 91 |
} |
| 92 |
return $field_list if scalar @$field_list; |
| 93 |
} |
| 94 |
|
| 95 |
sub _process_field_list { |
| 96 |
my ( $self, $flist ) = @_; |
| 97 |
|
| 98 |
if ( ref $flist eq 'ARRAY' ) { |
| 99 |
$self->_process_field_array( $self->_array_fields( $flist ) ); |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
# munges the field_list array into an array of field attributes |
| 104 |
sub _array_fields { |
| 105 |
my ( $self, $fields ) = @_; |
| 106 |
|
| 107 |
$fields = clone( $fields ); |
| 108 |
my @new_fields; |
| 109 |
while (@$fields) { |
| 110 |
my $name = shift @$fields; |
| 111 |
my $attr = shift @$fields; |
| 112 |
unless ( ref $attr eq 'HASH' ) { |
| 113 |
$attr = { type => $attr }; |
| 114 |
} |
| 115 |
push @new_fields, { name => $name, %$attr }; |
| 116 |
} |
| 117 |
return \@new_fields; |
| 118 |
} |
| 119 |
|
| 120 |
# loop through array of field hashrefs |
| 121 |
sub _process_field_array { |
| 122 |
my ( $self, $fields ) = @_; |
| 123 |
|
| 124 |
# clone and, optionally, filter fields |
| 125 |
$fields = $self->clean_fields( $fields ); |
| 126 |
# the point here is to process fields in the order parents |
| 127 |
# before children, so we process all fields with no dots |
| 128 |
# first, then one dot, then two dots... |
| 129 |
my $num_fields = scalar @$fields; |
| 130 |
my $num_dots = 0; |
| 131 |
my $count_fields = 0; |
| 132 |
while ( $count_fields < $num_fields ) { |
| 133 |
foreach my $field (@$fields) { |
| 134 |
my $count = ( $field->{name} =~ tr/\.// ); |
| 135 |
next unless $count == $num_dots; |
| 136 |
$self->_make_field($field); |
| 137 |
$count_fields++; |
| 138 |
} |
| 139 |
$num_dots++; |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
sub clean_fields { |
| 144 |
my ( $self, $fields ) = @_; |
| 145 |
if( $self->has_include ) { |
| 146 |
my @fields; |
| 147 |
my %include = map { $_ => 1 } @{ $self->include }; |
| 148 |
foreach my $fld ( @$fields ) { |
| 149 |
push @fields, clone($fld) if exists $include{$fld->{name}}; |
| 150 |
} |
| 151 |
return \@fields; |
| 152 |
} |
| 153 |
return clone( $fields ); |
| 154 |
} |
| 155 |
|
| 156 |
# Maps the field type to a field class, finds the parent, |
| 157 |
# sets the 'form' attribute, calls update_or_create |
| 158 |
# The 'field_attr' hashref must have a 'name' key |
| 159 |
sub _make_field { |
| 160 |
my ( $self, $field_attr ) = @_; |
| 161 |
|
| 162 |
my $type = $field_attr->{type} ||= 'Text'; |
| 163 |
my $name = $field_attr->{name}; |
| 164 |
|
| 165 |
my $do_update; |
| 166 |
if ( $name =~ /^\+(.*)/ ) { |
| 167 |
$field_attr->{name} = $name = $1; |
| 168 |
$do_update = 1; |
| 169 |
} |
| 170 |
|
| 171 |
my $class = $self->_find_field_class( $type, $name ); |
| 172 |
|
| 173 |
my $parent = $self->_find_parent( $field_attr ); |
| 174 |
|
| 175 |
$field_attr = $self->_merge_updates( $field_attr, $class ) unless $do_update; |
| 176 |
|
| 177 |
my $field = $self->_update_or_create( $parent, $field_attr, $class, $do_update ); |
| 178 |
|
| 179 |
$self->form->add_to_index( $field->full_name => $field ) if $self->form; |
| 180 |
} |
| 181 |
|
| 182 |
sub _make_adhoc_field { |
| 183 |
my ( $self, $class, $field_attr ) = @_; |
| 184 |
|
| 185 |
# remove and save form & parent, because if the form class has a 'clone' |
| 186 |
# method, Data::Clone::clone will clone the form |
| 187 |
my $parent = delete $field_attr->{parent}; |
| 188 |
my $form = delete $field_attr->{form}; |
| 189 |
$field_attr = $self->_merge_updates( $field_attr, $class ); |
| 190 |
$field_attr->{parent} = $parent; |
| 191 |
$field_attr->{form} = $form; |
| 192 |
my $field = $self->new_field_with_traits( $class, $field_attr ); |
| 193 |
return $field; |
| 194 |
} |
| 195 |
|
| 196 |
sub _find_field_class { |
| 197 |
my ( $self, $type, $name ) = @_; |
| 198 |
|
| 199 |
my $field_ns = $self->field_name_space; |
| 200 |
my @classes; |
| 201 |
# '+'-prefixed fields could be full namespaces |
| 202 |
if ( $type =~ s/^\+// ) |
| 203 |
{ |
| 204 |
push @classes, $type; |
| 205 |
} |
| 206 |
foreach my $ns ( @$field_ns, 'HTML::FormHandler::Field', 'HTML::FormHandlerX::Field' ) |
| 207 |
{ |
| 208 |
push @classes, $ns . "::" . $type; |
| 209 |
} |
| 210 |
# look for Field in possible namespaces |
| 211 |
my $class; |
| 212 |
foreach my $try ( @classes ) { |
| 213 |
last if $class = load_optional_class($try) ? $try : undef; |
| 214 |
} |
| 215 |
die "Could not load field class '$type' for field '$name'" |
| 216 |
unless $class; |
| 217 |
|
| 218 |
return $class; |
| 219 |
} |
| 220 |
|
| 221 |
sub _find_parent { |
| 222 |
my ( $self, $field_attr ) = @_; |
| 223 |
|
| 224 |
# parent and name correction for names with dots |
| 225 |
my $parent; |
| 226 |
if ( $field_attr->{name} =~ /\./ ) { |
| 227 |
my @names = split /\./, $field_attr->{name}; |
| 228 |
my $simple_name = pop @names; |
| 229 |
my $parent_name = join '.', @names; |
| 230 |
# use special 'field' method call that starts from |
| 231 |
# $self, because names aren't always starting from |
| 232 |
# the form |
| 233 |
$parent = $self->field($parent_name, undef, $self); |
| 234 |
if ($parent) { |
| 235 |
die "The parent of field " . $field_attr->{name} . " is not a Compound Field" |
| 236 |
unless $parent->isa('HTML::FormHandler::Field::Compound'); |
| 237 |
$field_attr->{name} = $simple_name; |
| 238 |
} |
| 239 |
else { |
| 240 |
die "did not find parent for field " . $field_attr->{name}; |
| 241 |
} |
| 242 |
} |
| 243 |
elsif ( !( $self->form && $self == $self->form ) ) { |
| 244 |
# set parent |
| 245 |
$parent = $self; |
| 246 |
} |
| 247 |
|
| 248 |
# get full_name |
| 249 |
my $full_name = $field_attr->{name}; |
| 250 |
$full_name = $parent->full_name . "." . $field_attr->{name} |
| 251 |
if $parent; |
| 252 |
$field_attr->{full_name} = $full_name; |
| 253 |
return $parent; |
| 254 |
|
| 255 |
} |
| 256 |
|
| 257 |
sub _merge_updates { |
| 258 |
my ( $self, $field_attr, $class ) = @_; |
| 259 |
|
| 260 |
# If there are field_traits at the form level, prepend them |
| 261 |
my $field_updates; |
| 262 |
unshift @{$field_attr->{traits}}, @{$self->form->field_traits} if $self->form; |
| 263 |
# use full_name for updates from form, name for updates from compound field |
| 264 |
my $full_name = delete $field_attr->{full_name} || $field_attr->{name}; |
| 265 |
my $name = $field_attr->{name}; |
| 266 |
|
| 267 |
my $single_updates = {}; # updates that apply to a single field |
| 268 |
my $all_updates = {}; # updates that apply to all fields |
| 269 |
# get updates from form update_subfields and widget_tags |
| 270 |
if ( $self->form ) { |
| 271 |
$field_updates = $self->form->update_subfields; |
| 272 |
if ( keys %$field_updates ) { |
| 273 |
$all_updates = $field_updates->{all} || {}; |
| 274 |
$single_updates = $field_updates->{$full_name}; |
| 275 |
if ( exists $field_updates->{by_flag} ) { |
| 276 |
$all_updates = $self->by_flag_updates( $field_attr, $class, $field_updates, $all_updates ); |
| 277 |
} |
| 278 |
if ( exists $field_updates->{by_type} && |
| 279 |
exists $field_updates->{by_type}->{$field_attr->{type}} ) { |
| 280 |
$all_updates = merge( $field_updates->{by_type}->{$field_attr->{type}}, $all_updates ); |
| 281 |
} |
| 282 |
} |
| 283 |
# merge widget tags into 'all' updates |
| 284 |
if( $self->form->has_widget_tags ) { |
| 285 |
$all_updates = merge( $all_updates, { tags => $self->form->widget_tags } ); |
| 286 |
} |
| 287 |
} |
| 288 |
# get updates from compound field update_subfields and widget_tags |
| 289 |
if ( $self->has_flag('is_compound') ) { |
| 290 |
my $comp_field_updates = $self->update_subfields; |
| 291 |
my $comp_all_updates = {}; |
| 292 |
my $comp_single_updates = {}; |
| 293 |
# -- compound 'all' updates -- |
| 294 |
if ( keys %$comp_field_updates ) { |
| 295 |
$comp_all_updates = $comp_field_updates->{all} || {}; |
| 296 |
# don't use full_name. varies depending on parent field name |
| 297 |
$comp_single_updates = $comp_field_updates->{$name} || {}; |
| 298 |
if ( exists $field_updates->{by_flag} ) { |
| 299 |
$comp_all_updates = $self->by_flag_updates( $field_attr, $class, $comp_field_updates, $comp_all_updates ); |
| 300 |
} |
| 301 |
if ( exists $comp_field_updates->{by_type} && |
| 302 |
exists $comp_field_updates->{by_type}->{$field_attr->{type}} ) { |
| 303 |
$comp_all_updates = merge( $comp_field_updates->{by_type}->{$field_attr->{type}}, $comp_all_updates ); |
| 304 |
} |
| 305 |
} |
| 306 |
if( $self->has_widget_tags ) { |
| 307 |
$comp_all_updates = merge( $comp_all_updates, { tags => $self->widget_tags } ); |
| 308 |
} |
| 309 |
|
| 310 |
# merge form 'all' updates, compound field higher precedence |
| 311 |
$all_updates = merge( $comp_all_updates, $all_updates ) |
| 312 |
if keys %$comp_all_updates; |
| 313 |
# merge single field updates, compound field higher precedence |
| 314 |
$single_updates = merge( $comp_single_updates, $single_updates ) |
| 315 |
if keys %$comp_single_updates; |
| 316 |
} |
| 317 |
|
| 318 |
# attributes set on a specific field through update_subfields override has_fields |
| 319 |
# attributes set by 'all' only happen if no field attributes |
| 320 |
$field_attr = merge( $field_attr, $all_updates ) if keys %$all_updates; |
| 321 |
$field_attr = merge( $single_updates, $field_attr ) if keys %$single_updates; |
| 322 |
|
| 323 |
# get the widget and widget_wrapper from form |
| 324 |
unless( $self->form && $self->form->no_widgets ) { |
| 325 |
# widget |
| 326 |
my $widget = $field_attr->{widget}; |
| 327 |
unless( $widget ) { |
| 328 |
my $attr = $class->meta->find_attribute_by_name( 'widget' ); |
| 329 |
$widget = $attr->default if $attr; |
| 330 |
} |
| 331 |
$widget = '' if $widget eq 'None'; |
| 332 |
# widget wrapper |
| 333 |
my $widget_wrapper = $field_attr->{widget_wrapper}; |
| 334 |
unless( $widget_wrapper ) { |
| 335 |
my $attr = $class->meta->get_attribute('widget_wrapper'); |
| 336 |
$widget_wrapper = $attr->default if $attr; |
| 337 |
$widget_wrapper ||= $self->form->widget_wrapper if $self->form; |
| 338 |
$widget_wrapper ||= 'Simple'; |
| 339 |
$field_attr->{widget_wrapper} = $widget_wrapper; |
| 340 |
} |
| 341 |
# add widget and wrapper roles to field traits |
| 342 |
if ( $widget ) { |
| 343 |
my $widget_role = $self->get_widget_role( $widget, 'Field' ); |
| 344 |
push @{$field_attr->{traits}}, $widget_role; |
| 345 |
} |
| 346 |
if ( $widget_wrapper ) { |
| 347 |
my $wrapper_role = $self->get_widget_role( $widget_wrapper, 'Wrapper' ); |
| 348 |
push @{$field_attr->{traits}}, $wrapper_role; |
| 349 |
} |
| 350 |
} |
| 351 |
return $field_attr; |
| 352 |
} |
| 353 |
|
| 354 |
sub by_flag_updates { |
| 355 |
my ( $self, $field_attr, $class, $field_updates, $all_updates ) = @_; |
| 356 |
|
| 357 |
my $by_flag = $field_updates->{by_flag}; |
| 358 |
if ( exists $by_flag->{contains} && $field_attr->{is_contains} ) { |
| 359 |
$all_updates = merge( $field_updates->{by_flag}->{contains}, $all_updates ); |
| 360 |
} |
| 361 |
elsif ( exists $by_flag->{repeatable} && $class->meta->find_attribute_by_name('is_repeatable') ) { |
| 362 |
$all_updates = merge( $field_updates->{by_flag}->{repeatable}, $all_updates ); |
| 363 |
} |
| 364 |
elsif ( exists $by_flag->{compound} && $class->meta->find_attribute_by_name('is_compound') ) { |
| 365 |
$all_updates = merge( $field_updates->{by_flag}->{compound}, $all_updates ); |
| 366 |
} |
| 367 |
return $all_updates; |
| 368 |
} |
| 369 |
|
| 370 |
# update, replace, or create field |
| 371 |
# Create makes the field object and passes in the properties as constructor args. |
| 372 |
# Update changed properties on a previously created object. |
| 373 |
# Replace overwrites a field with a different configuration. |
| 374 |
# (The update/replace business is much the same as you'd see with inheritance.) |
| 375 |
# This function populates/updates the base object's 'field' array. |
| 376 |
sub _update_or_create { |
| 377 |
my ( $self, $parent, $field_attr, $class, $do_update ) = @_; |
| 378 |
|
| 379 |
$parent ||= $self->form; |
| 380 |
$field_attr->{parent} = $parent; |
| 381 |
$field_attr->{form} = $self->form if $self->form; |
| 382 |
my $index = $parent->field_index( $field_attr->{name} ); |
| 383 |
my $field; |
| 384 |
if ( defined $index ) { |
| 385 |
if ($do_update) # this field started with '+'. Update. |
| 386 |
{ |
| 387 |
$field = $parent->field( $field_attr->{name} ); |
| 388 |
die "Field to update for " . $field_attr->{name} . " not found" |
| 389 |
unless $field; |
| 390 |
foreach my $key ( keys %{$field_attr} ) { |
| 391 |
next if $key eq 'name' || $key eq 'form' || $key eq 'parent' || |
| 392 |
$key eq 'full_name' || $key eq 'type'; |
| 393 |
$field->$key( $field_attr->{$key} ) |
| 394 |
if $field->can($key); |
| 395 |
} |
| 396 |
} |
| 397 |
else # replace existing field |
| 398 |
{ |
| 399 |
$field = $self->new_field_with_traits( $class, $field_attr); |
| 400 |
$parent->set_field_at( $index, $field ); |
| 401 |
} |
| 402 |
} |
| 403 |
else # new field |
| 404 |
{ |
| 405 |
$field = $self->new_field_with_traits( $class, $field_attr); |
| 406 |
$parent->add_field($field); |
| 407 |
} |
| 408 |
$field->form->add_repeatable_field($field) |
| 409 |
if ( $field->form && $field->has_flag('is_repeatable') ); |
| 410 |
return $field; |
| 411 |
} |
| 412 |
|
| 413 |
sub new_field_with_traits { |
| 414 |
my ( $self, $class, $field_attr ) = @_; |
| 415 |
|
| 416 |
my $traits = delete $field_attr->{traits} || []; |
| 417 |
if( @$traits ) { |
| 418 |
$class = $class->with_traits( @$traits ); |
| 419 |
$class->meta->make_immutable; |
| 420 |
} |
| 421 |
my $field = $class->new( %{$field_attr} ); |
| 422 |
|
| 423 |
return $field; |
| 424 |
} |
| 425 |
|
| 426 |
sub _order_fields { |
| 427 |
my $self = shift; |
| 428 |
|
| 429 |
# order the fields |
| 430 |
# There's a hole in this... if child fields are defined at |
| 431 |
# a level above the containing parent, then they won't |
| 432 |
# exist when this routine is called and won't be ordered. |
| 433 |
# This probably needs to be moved out of here into |
| 434 |
# a separate recursive step that's called after build_fields. |
| 435 |
|
| 436 |
# get highest order number |
| 437 |
my $order = 0; |
| 438 |
foreach my $field ( $self->all_fields ) { |
| 439 |
$order++ if $field->order > $order; |
| 440 |
} |
| 441 |
$order++; |
| 442 |
# number all unordered fields |
| 443 |
foreach my $field ( $self->all_fields ) { |
| 444 |
$field->order($order) unless $field->order; |
| 445 |
$order++; |
| 446 |
} |
| 447 |
} |
| 448 |
|
| 449 |
use namespace::autoclean; |
| 450 |
1; |
| 451 |
|
| 452 |
__END__ |
| 453 |
|
| 454 |
=pod |
| 455 |
|
| 456 |
=encoding UTF-8 |
| 457 |
|
| 458 |
=head1 NAME |
| 459 |
|
| 460 |
HTML::FormHandler::BuildFields - role to build field array |
| 461 |
|
| 462 |
=head1 VERSION |
| 463 |
|
| 464 |
version 0.410002 |
| 465 |
|
| 466 |
=head1 SYNOPSIS |
| 467 |
|
| 468 |
These are the methods that are necessary to build the fields arrays |
| 469 |
in a form. This is a role which is composed into L<HTML::FormHandler>. |
| 470 |
|
| 471 |
Internal code only. This role has no user interfaces. |
| 472 |
|
| 473 |
=head1 AUTHOR |
| 474 |
|
| 475 |
FormHandler Contributors - see HTML::FormHandler |
| 476 |
|
| 477 |
=head1 COPYRIGHT AND LICENSE |
| 478 |
|
| 479 |
This software is copyright (c) 2017 by Gerda Shank. |
| 480 |
|
| 481 |
This is free software; you can redistribute it and/or modify it under |
| 482 |
the same terms as the Perl 5 programming language system itself. |
| 483 |
|
| 484 |
=cut |