| 1 | package Strehler::FormFu::Element::EntityTypeSelect; | ||
| 2 | $Strehler::FormFu::Element::EntityTypeSelect::VERSION = '1.6.8'; | ||
| 3 | use strict; | ||
| 4 | use Moose; | ||
| 5 | use Strehler::Helpers; | ||
| 6 | use Data::Dumper; | ||
| 7 | |||
| 8 | extends 'HTML::FormFu::Element::Select'; | ||
| 9 | |||
| 10 | use Carp qw( croak ); | ||
| 11 | |||
| 12 | has _excluded => ( | ||
| 13 | is => 'rw', | ||
| 14 | default => sub { [] }, | ||
| 15 | lazy => 1, | ||
| 16 | ); | ||
| 17 | |||
| 18 | after BUILD => sub { | ||
| 19 | my $self = shift; | ||
| 20 | |||
| 21 | $self->filename('input'); | ||
| 22 | $self->field_filename('select_tag'); | ||
| 23 | $self->multi_value(1); | ||
| 24 | $self->generate_options(undef); | ||
| 25 | return; | ||
| 26 | }; | ||
| 27 | |||
| 28 | sub excluded { ## no critic qw(Subroutines::RequireArgUnpacking) | ||
| 29 | my ( $self, $arg ) = @_; | ||
| 30 | |||
| 31 | return $self->_element if @_ == 1; | ||
| 32 | |||
| 33 | |||
| 34 | if ( defined $arg ) { | ||
| 35 | $self->generate_options($arg); | ||
| 36 | } | ||
| 37 | return $self; | ||
| 38 | } | ||
| 39 | |||
| 40 | sub generate_options | ||
| 41 | { | ||
| 42 | my $self = shift; | ||
| 43 | my $excluded = shift; | ||
| 44 | my %blacklist = (); | ||
| 45 | %blacklist = map { $_ => 1 } @{$excluded} if $excluded; | ||
| 46 | my @entities = Strehler::Helpers::entities_list(); | ||
| 47 | my @elements_of_select; | ||
| 48 | push @elements_of_select, { value => undef, label => "-- select --"}; | ||
| 49 | foreach my $e (@entities) | ||
| 50 | { | ||
| 51 | if(! exists $blacklist{$e}) | ||
| 52 | { | ||
| 53 | my $c = Strehler::Helpers::class_from_entity($e); | ||
| 54 | push @elements_of_select, { value => $e, label => $c->label() } | ||
| 55 | } | ||
| 56 | } | ||
| 57 | $self->options(\@elements_of_select); | ||
| 58 | |||
| 59 | return; | ||
| 60 | } | ||
| 61 | |||
| 62 | =encoding utf8 | ||
| 63 | |||
| 64 | =head1 NAME | ||
| 65 | |||
| 66 | Strehler::FormFu::Element::EntityTypeSelect - FormFu Element to allow to select a Strehler entity. | ||
| 67 | |||
| 68 | =head1 DESCRIPTION | ||
| 69 | |||
| 70 | A Strehler element that you can include in your form configuration to allow user to select a Strehler entity. | ||
| 71 | |||
| 72 | This element hasn't the standard HTML::FormFu elements namespace because it makes sense only in a Strehler system. | ||
| 73 | |||
| 74 | =head1 SYNOPSIS | ||
| 75 | |||
| 76 | In custom Strehler entity form: | ||
| 77 | |||
| 78 | - type: '+Strehler::FormFu::Element::EntityTypeSelect' | ||
| 79 | name: entitytype | ||
| 80 | label: Entity Type | ||
| 81 | excluded: [ 'log', 'image' ] | ||
| 82 | |||
| 83 | =head1 PARAMETERS | ||
| 84 | |||
| 85 | =over 4 | ||
| 86 | |||
| 87 | =item excluded | ||
| 88 | |||
| 89 | Entities that you don't want to be listed in the select. For example, to avoid Strehler admin entities to be listed: | ||
| 90 | |||
| 91 | excluded: ['log', 'user'] | ||
| 92 | |||
| 93 | =back | ||
| 94 | |||
| 95 | =cut | ||
| 96 | |||
| 97 | 1; |