| 1 | package Form::Sensible::Renderer::HTML; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | 3 | ▶ | use Moose; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 3 | ▶ | use namespace::autoclean; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 3 | ▶ | use Template; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | 3 | ▶ | use Data::Dumper; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | 3 | ▶ | use Form::Sensible::Renderer::HTML::RenderedForm; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | 3 | extends 'Form::Sensible::Renderer'; | |||||||||||||
| 9 | |||||||||||||||
| 10 | 3 | has 'include_paths' => ( | |||||||||||||
| 11 | is => 'rw', | ||||||||||||||
| 12 | isa => 'ArrayRef[Str]', | ||||||||||||||
| 13 | required => 1, | ||||||||||||||
| 14 | lazy => 1, | ||||||||||||||
| 15 | builder => '_build_include_path', | ||||||||||||||
| 16 | ); | ||||||||||||||
| 17 | |||||||||||||||
| 18 | has 'additional_include_paths' => ( | ||||||||||||||
| 19 | is => 'rw', | ||||||||||||||
| 20 | isa => 'ArrayRef[Str]', | ||||||||||||||
| 21 | required => 1, | ||||||||||||||
| 22 | lazy => 1, | ||||||||||||||
| 23 | 5 | ▶ | default => sub { return []; }, | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 24 | # additional options | ||||||||||||||
| 25 | 3 | ); | |||||||||||||
| 26 | |||||||||||||||
| 27 | |||||||||||||||
| 28 | 3 | has 'base_theme' => ( | |||||||||||||
| 29 | is => 'rw', | ||||||||||||||
| 30 | isa => 'Str', | ||||||||||||||
| 31 | required => 1, | ||||||||||||||
| 32 | default => 'default' | ||||||||||||||
| 33 | ); | ||||||||||||||
| 34 | |||||||||||||||
| 35 | |||||||||||||||
| 36 | has 'tt_config' => ( | ||||||||||||||
| 37 | is => 'rw', | ||||||||||||||
| 38 | isa => 'HashRef', | ||||||||||||||
| 39 | required => 1, | ||||||||||||||
| 40 | 5 | ▶ | default => sub { | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 41 | 5 | my $self = shift; | |||||||||||||
| 42 | return { | ||||||||||||||
| 43 | 5 | INCLUDE_PATH => $self->complete_include_path(), | |||||||||||||
| 44 | WRAPPER => 'pre_process.tt' | ||||||||||||||
| 45 | }; | ||||||||||||||
| 46 | }, | ||||||||||||||
| 47 | 3 | lazy => 1, | |||||||||||||
| 48 | ); | ||||||||||||||
| 49 | |||||||||||||||
| 50 | has 'fs_template_dir' => ( | ||||||||||||||
| 51 | is => 'rw', | ||||||||||||||
| 52 | isa => 'Str', | ||||||||||||||
| 53 | required => 1, | ||||||||||||||
| 54 | 1 | ▶ | default => sub { File::ShareDir::dist_dir('Form-Sensible') . '/templates' }, | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 55 | 3 | lazy => 1, | |||||||||||||
| 56 | ); | ||||||||||||||
| 57 | |||||||||||||||
| 58 | |||||||||||||||
| 59 | ## if template is provided, it will be re-used. | ||||||||||||||
| 60 | ## otherwise, a new one is generated for each form render. | ||||||||||||||
| 61 | 3 | has 'template' => ( | |||||||||||||
| 62 | is => 'rw', | ||||||||||||||
| 63 | isa => 'Template', | ||||||||||||||
| 64 | ); | ||||||||||||||
| 65 | |||||||||||||||
| 66 | has 'default_options' => ( | ||||||||||||||
| 67 | is => 'rw', | ||||||||||||||
| 68 | isa => 'HashRef', | ||||||||||||||
| 69 | required => 1, | ||||||||||||||
| 70 | 7 | ▶ | default => sub { return {}; }, | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 71 | 3 | lazy => 1, | |||||||||||||
| 72 | ); | ||||||||||||||
| 73 | |||||||||||||||
| 74 | |||||||||||||||
| 75 | 9 | ▶ | sub render { | ||||||||||||
|
Subroutine
render: called CC=9 SCAR=22.2
Pod
documented
| |||||||||||||||
| 76 | 9 | my ($self, $form, $stash_prefill, $options) = @_; | |||||||||||||
| 77 | |||||||||||||||
| 78 | 9 | my $template_options = $self->default_options; | |||||||||||||
| 79 | |||||||||||||||
| 80 | # steps | ||||||||||||||
| 81 | # use or create Template object with options | ||||||||||||||
| 82 | # merge stash prefill | ||||||||||||||
| 83 | # create RenderedForm object | ||||||||||||||
| 84 | # setup RenderedForm object | ||||||||||||||
| 85 | # return renderedForm object | ||||||||||||||
| 86 | |||||||||||||||
| 87 | 9 | ▶ | if (!defined($stash_prefill)) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 88 | 8 | $stash_prefill = {}; | |||||||||||||
| 89 | } | ||||||||||||||
| 90 | 9 | my $form_specific_stash = { %{$stash_prefill} }; | |||||||||||||
| 91 | |||||||||||||||
| 92 | 9 | my $template = $self->template; | |||||||||||||
| 93 | |||||||||||||||
| 94 | ## if there is no $self->template - we have to | ||||||||||||||
| 95 | ## create one, but we don't keep it if we create it, | ||||||||||||||
| 96 | ## we just use it for this render. | ||||||||||||||
| 97 | 9 | ▶ | if (!defined($template)) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 98 | 9 | $template = $self->new_template( $options->{additional_tt_options} ); | |||||||||||||
| 99 | } | ||||||||||||||
| 100 | |||||||||||||||
| 101 | 9 | my %args = ( | |||||||||||||
| 102 | template => $template, | ||||||||||||||
| 103 | form => $form, | ||||||||||||||
| 104 | stash => $form_specific_stash, | ||||||||||||||
| 105 | ); | ||||||||||||||
| 106 | |||||||||||||||
| 107 | # load up default options. | ||||||||||||||
| 108 | 9 | foreach my $key (keys %{$template_options}) { | |||||||||||||
| 109 | 0 | $args{$key} = $template_options->{$key}; | |||||||||||||
| 110 | } | ||||||||||||||
| 111 | |||||||||||||||
| 112 | 9 | ▶ | if (ref($options) eq 'HASH') { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 113 | 9 | foreach my $key (keys %{$options}) { | |||||||||||||
| 114 | 1 | $args{$key} = $options->{$key}; | |||||||||||||
| 115 | } | ||||||||||||||
| 116 | } | ||||||||||||||
| 117 | |||||||||||||||
| 118 | ## take care of any subforms we have in this form. | ||||||||||||||
| 119 | 9 | my $subform_init_hash = { %args }; | |||||||||||||
| 120 | 9 | $args{'subform_renderers'} = {}; | |||||||||||||
| 121 | 9 | foreach my $field ($form->get_fields()) { | |||||||||||||
| 122 | 31 | my $fieldname = $field->name(); | |||||||||||||
| 123 | 31 | ▶ | if ($field->isa('Form::Sensible::Field::SubForm')) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 124 | 1 | $subform_init_hash->{'form'} = $field->form; | |||||||||||||
| 125 | #print "FOO!! $fieldname\n"; | ||||||||||||||
| 126 | 1 | $args{'subform_renderers'}{$fieldname} = Form::Sensible::Renderer::HTML::RenderedForm->new( $subform_init_hash ); | |||||||||||||
| 127 | #print Dumper($args{'subform_renderers'}{$fieldname}); | ||||||||||||||
| 128 | |||||||||||||||
| 129 | ## dirty hack for now. If we have subforms, then we automatically assume we have to be | ||||||||||||||
| 130 | ## multipart/form-data. What we should do is check all the subforms... but we aren't doing that at this point. | ||||||||||||||
| 131 | 1 | $args{'stash'}{'form_enctype'} = 'multipart/form-data' | |||||||||||||
| 132 | } elsif ($field->isa('Form::Sensible::Field::FileSelector')) { | ||||||||||||||
| 133 | 0 | $args{'stash'}{'form_enctype'} = 'multipart/form-data'; | |||||||||||||
| 134 | } | ||||||||||||||
| 135 | } | ||||||||||||||
| 136 | |||||||||||||||
| 137 | 9 | my $rendered_form = Form::Sensible::Renderer::HTML::RenderedForm->new( %args ); | |||||||||||||
| 138 | |||||||||||||||
| 139 | 9 | return $rendered_form; | |||||||||||||
| 140 | } | ||||||||||||||
| 141 | |||||||||||||||
| 142 | 5 | ▶ | sub _build_include_path { | ||||||||||||
|
Subroutine
_build_include_path: called CC=2 SCAR=6.9
| |||||||||||||||
| 143 | 5 | my $self = shift; | |||||||||||||
| 144 | |||||||||||||||
| 145 | 5 | my $path = []; | |||||||||||||
| 146 | 5 | ▶ | if ($self->base_theme ne 'default') { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 147 | 1 | push @{$path}, $self->path_to_theme(); | |||||||||||||
| 148 | } | ||||||||||||||
| 149 | 5 | push @{$path}, $self->path_to_theme('default'); | |||||||||||||
| 150 | 5 | return $path; | |||||||||||||
| 151 | } | ||||||||||||||
| 152 | |||||||||||||||
| 153 | 5 | ▶ | sub complete_include_path { | ||||||||||||
|
Subroutine
complete_include_path: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 154 | 5 | my $self = shift; | |||||||||||||
| 155 | |||||||||||||||
| 156 | 5 | return [ @{$self->additional_include_paths}, @{$self->include_paths()} ]; | |||||||||||||
| 157 | } | ||||||||||||||
| 158 | |||||||||||||||
| 159 | 6 | ▶ | sub path_to_theme { | ||||||||||||
|
Subroutine
path_to_theme: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||
| 160 | 6 | my ($self, $theme) = @_; | |||||||||||||
| 161 | |||||||||||||||
| 162 | 6 | ▶ | if (!$theme) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 163 | 1 | $theme = $self->base_theme; | |||||||||||||
| 164 | } | ||||||||||||||
| 165 | |||||||||||||||
| 166 | 6 | return $self->fs_template_dir . '/' . $theme; | |||||||||||||
| 167 | } | ||||||||||||||
| 168 | |||||||||||||||
| 169 | # create a new Template instance with the provided options. | ||||||||||||||
| 170 | 9 | ▶ | sub new_template { | ||||||||||||
|
Subroutine
new_template: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||
| 171 | 9 | my ($self, $additional_tt_options ) = @_; | |||||||||||||
| 172 | |||||||||||||||
| 173 | 9 | ▶ | $additional_tt_options ||= {}; | ||||||||||||
|
Condition100%2/2
$additional_tt_options ||= {}
MC/DC100%1/1
$additional_tt_options ||= {}
$additional_tt_options
Truth table100%2/2
A
A= $additional_tt_options
| |||||||||||||||
| 174 | 9 | my %template_options = ( %{$self->tt_config}, %$additional_tt_options ); | |||||||||||||
| 175 | |||||||||||||||
| 176 | 9 | return Template->new( \%template_options ); | |||||||||||||
| 177 | } | ||||||||||||||
| 178 | |||||||||||||||
| 179 | 3 | __PACKAGE__->meta->make_immutable; | |||||||||||||
| 180 | 3 | 1; | |||||||||||||
| 181 | |||||||||||||||
| 182 | __END__ | ||||||||||||||