| 1 | package HTML::Shakan; | ||||||||||||||||||||
| 2 | 22 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 3 | 22 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 22 | ▶ | use Mouse 0.9; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 22 | our $VERSION = '2.10'; | |||||||||||||||||||
| 6 | 22 | ▶ | use Carp (); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 22 | ▶ | use 5.008001; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | 22 | ▶ | use FormValidator::Lite 0.24 'Email', 'URL', 'Date', 'File'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 22 | ▶ | use Hash::MultiValue; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | 22 | ▶ | use HTML::Shakan::Renderer::HTML; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 13 | 22 | ▶ | use HTML::Shakan::Filters; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | 22 | ▶ | use HTML::Shakan::Widgets::Simple; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 15 | 22 | ▶ | use HTML::Shakan::Fields; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 16 | 22 | ▶ | use HTML::Shakan::Field::Input; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 17 | 22 | ▶ | use HTML::Shakan::Field::Date; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 18 | 22 | ▶ | use HTML::Shakan::Field::Choice; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 19 | 22 | ▶ | use HTML::Shakan::Field::File; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 20 | 22 | ▶ | use List::MoreUtils 0.22 'uniq'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | 21 | ▶ | sub import { | ||||||||||||||||||
|
Subroutine
import: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 23 | 21 | HTML::Shakan::Fields->export_to_level(1); | |||||||||||||||||||
| 24 | } | ||||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | has '_fvl' => ( | ||||||||||||||||||||
| 27 | is => 'ro', | ||||||||||||||||||||
| 28 | isa => 'FormValidator::Lite', | ||||||||||||||||||||
| 29 | lazy => 1, | ||||||||||||||||||||
| 30 | handles => [qw/has_error load_function_message get_error_messages is_error is_valid set_error set_message/], | ||||||||||||||||||||
| 31 | 0 | ▶ | default => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 32 | 0 | my $self = shift; | |||||||||||||||||||
| 33 | 0 | $self->params(); # build laziness data | |||||||||||||||||||
| 34 | |||||||||||||||||||||
| 35 | 0 | FormValidator::Lite->new($self); | |||||||||||||||||||
| 36 | } | ||||||||||||||||||||
| 37 | 22 | ); | |||||||||||||||||||
| 38 | |||||||||||||||||||||
| 39 | 0 | ▶ | sub BUILD { | ||||||||||||||||||
|
Subroutine
BUILD: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||
| 40 | 0 | my $self = shift; | |||||||||||||||||||
| 41 | |||||||||||||||||||||
| 42 | 0 | my $fvl = $self->_fvl; | |||||||||||||||||||
| 43 | |||||||||||||||||||||
| 44 | # simple check | ||||||||||||||||||||
| 45 | 0 | $fvl->check(do { | |||||||||||||||||||
| 46 | 0 | my @c; | |||||||||||||||||||
| 47 | 0 | for my $field (@{ $self->fields }) { | |||||||||||||||||||
| 48 | 0 | push @c, $field->get_constraints(); | |||||||||||||||||||
| 49 | } | ||||||||||||||||||||
| 50 | 0 | @c; | |||||||||||||||||||
| 51 | }); | ||||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | # run custom validation | ||||||||||||||||||||
| 54 | 0 | ▶ | if (my $cv = $self->custom_validation) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 55 | 0 | $cv->( $self ); | |||||||||||||||||||
| 56 | } | ||||||||||||||||||||
| 57 | 0 | for my $field ($self->fields) { | |||||||||||||||||||
| 58 | 0 | ▶ | if (my $cv = $field->custom_validation) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 59 | 0 | $cv->($self, $field); | |||||||||||||||||||
| 60 | } | ||||||||||||||||||||
| 61 | } | ||||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | 0 | ▶ | if ($fvl->is_valid) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 64 | 0 | $self->_inflate_values(); | |||||||||||||||||||
| 65 | } else { | ||||||||||||||||||||
| 66 | 0 | $fvl->set_param_message( | |||||||||||||||||||
| 67 | $self->_set_error_messages() | ||||||||||||||||||||
| 68 | ); | ||||||||||||||||||||
| 69 | } | ||||||||||||||||||||
| 70 | } | ||||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | 22 | has custom_validation => ( | |||||||||||||||||||
| 73 | is => 'ro', | ||||||||||||||||||||
| 74 | isa => 'CodeRef', | ||||||||||||||||||||
| 75 | ); | ||||||||||||||||||||
| 76 | |||||||||||||||||||||
| 77 | 0 | ▶ | sub _set_error_messages { | ||||||||||||||||||
|
Subroutine
_set_error_messages: not called CC=3 SCAR=24.8
| |||||||||||||||||||||
| 78 | 0 | my ($self, ) = @_; | |||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | 0 | my %x; | |||||||||||||||||||
| 81 | 0 | for my $field ($self->fields) { | |||||||||||||||||||
| 82 | 0 | ▶ | $x{$field->name} = $field->label || $field->name; | ||||||||||||||||||
|
Condition0%0/3
$field->label || $field->name
MC/DC0%0/2
$field->label || $field->name
$field->label
$field->name
Truth table0%0/3
A || B
A= $field->labelB= $field->name
| |||||||||||||||||||||
| 83 | } | ||||||||||||||||||||
| 84 | 0 | %x; | |||||||||||||||||||
| 85 | } | ||||||||||||||||||||
| 86 | |||||||||||||||||||||
| 87 | 0 | ▶ | sub _inflate_values { | ||||||||||||||||||
|
Subroutine
_inflate_values: not called CC=4 SCAR=30.0
| |||||||||||||||||||||
| 88 | 0 | my $self = shift; | |||||||||||||||||||
| 89 | |||||||||||||||||||||
| 90 | # inflate values | ||||||||||||||||||||
| 91 | 0 | my $params = $self->params; | |||||||||||||||||||
| 92 | 0 | for my $field (@{ $self->fields }) { | |||||||||||||||||||
| 93 | 0 | ▶ | if (my $inf = $field->inflator) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 94 | 0 | my $v = $params->{$field->name}; | |||||||||||||||||||
| 95 | 0 | ▶ | if (defined $v) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 96 | 0 | $params->{$field->name} = $inf->inflate($v); | |||||||||||||||||||
| 97 | } | ||||||||||||||||||||
| 98 | } | ||||||||||||||||||||
| 99 | } | ||||||||||||||||||||
| 100 | } | ||||||||||||||||||||
| 101 | |||||||||||||||||||||
| 102 | 22 | has 'submitted' => ( | |||||||||||||||||||
| 103 | is => 'ro', | ||||||||||||||||||||
| 104 | isa => 'Bool', | ||||||||||||||||||||
| 105 | lazy => 1, | ||||||||||||||||||||
| 106 | builder => '_build_submitted', | ||||||||||||||||||||
| 107 | ); | ||||||||||||||||||||
| 108 | 0 | ▶ | sub _build_submitted { | ||||||||||||||||||
|
Subroutine
_build_submitted: not called CC=3 SCAR=24.8
| |||||||||||||||||||||
| 109 | 0 | my ($self, ) = @_; | |||||||||||||||||||
| 110 | |||||||||||||||||||||
| 111 | 0 | my $r = $self->request; | |||||||||||||||||||
| 112 | my $submitted_field = ( | ||||||||||||||||||||
| 113 | scalar | ||||||||||||||||||||
| 114 | 0 | ▶ | grep { defined $r->param($_) || defined $r->upload($_) } | ||||||||||||||||||
|
Condition0%0/3
defined $r->param($_) || defined $r->upload($_)
MC/DC0%0/2
defined $r->param($_) || defined $r->upload($_)
defined $r->param($_)
defined $r->upload($_)
Truth table0%0/3
A || B
A= defined $r->param($_)B= defined $r->upload($_)
| |||||||||||||||||||||
| 115 | uniq | ||||||||||||||||||||
| 116 | 0 | map { $_->name } | |||||||||||||||||||
| 117 | $self->fields | ||||||||||||||||||||
| 118 | ); | ||||||||||||||||||||
| 119 | 0 | ▶ | return $submitted_field > 0 ? 1 : 0; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 120 | } | ||||||||||||||||||||
| 121 | |||||||||||||||||||||
| 122 | 0 | ▶ | sub submitted_and_valid { | ||||||||||||||||||
|
Subroutine
submitted_and_valid: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 123 | 0 | my $self = shift; | |||||||||||||||||||
| 124 | 0 | ▶ | $self->submitted && $self->is_valid; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 125 | } | ||||||||||||||||||||
| 126 | |||||||||||||||||||||
| 127 | has model => ( | ||||||||||||||||||||
| 128 | is => 'rw', | ||||||||||||||||||||
| 129 | isa => 'Object', | ||||||||||||||||||||
| 130 | 0 | ▶ | trigger => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 131 | 0 | my ($self, $model) = @_; | |||||||||||||||||||
| 132 | 0 | $model->form($self); | |||||||||||||||||||
| 133 | 0 | $model; | |||||||||||||||||||
| 134 | }, | ||||||||||||||||||||
| 135 | 22 | ); | |||||||||||||||||||
| 136 | |||||||||||||||||||||
| 137 | 22 | has renderer => ( | |||||||||||||||||||
| 138 | is => 'rw', | ||||||||||||||||||||
| 139 | isa => 'Object', | ||||||||||||||||||||
| 140 | builder => '_build_renderer', | ||||||||||||||||||||
| 141 | ); | ||||||||||||||||||||
| 142 | 0 | ▶ | sub _build_renderer { | ||||||||||||||||||
|
Subroutine
_build_renderer: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 143 | 0 | HTML::Shakan::Renderer::HTML->new(); | |||||||||||||||||||
| 144 | } | ||||||||||||||||||||
| 145 | 0 | ▶ | sub render { | ||||||||||||||||||
|
Subroutine
render: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 146 | 0 | my $self = shift; | |||||||||||||||||||
| 147 | 0 | $self->renderer()->render($self); | |||||||||||||||||||
| 148 | } | ||||||||||||||||||||
| 149 | |||||||||||||||||||||
| 150 | 0 | ▶ | sub render_field { | ||||||||||||||||||
|
Subroutine
render_field: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 151 | 0 | my ( $self, $name ) = @_; | |||||||||||||||||||
| 152 | 0 | my ( $field, ) = grep { $_->name eq $name } $self->fields; | |||||||||||||||||||
| 153 | 0 | ▶ | return unless $field; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 154 | 0 | return $self->widgets->render( $self, $field ); | |||||||||||||||||||
| 155 | } | ||||||||||||||||||||
| 156 | |||||||||||||||||||||
| 157 | 0 | ▶ | sub fillin_param { | ||||||||||||||||||
|
Subroutine
fillin_param: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 158 | 0 | my ($self, $key) = @_; | |||||||||||||||||||
| 159 | 0 | $self->fillin_params->{$key}; | |||||||||||||||||||
| 160 | } | ||||||||||||||||||||
| 161 | has fillin_params => ( | ||||||||||||||||||||
| 162 | is => 'ro', | ||||||||||||||||||||
| 163 | isa => 'HashRef', | ||||||||||||||||||||
| 164 | lazy => 1, | ||||||||||||||||||||
| 165 | 0 | ▶ | default => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=4 SCAR=30.0
| |||||||||||||||||||||
| 166 | 0 | my $self = shift; | |||||||||||||||||||
| 167 | 0 | my $fp = {}; | |||||||||||||||||||
| 168 | 0 | for my $name ($self->request->param) { | |||||||||||||||||||
| 169 | 0 | my @v = $self->request->param($name); | |||||||||||||||||||
| 170 | 0 | ▶ | if (@v) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 171 | 0 | ▶ | $fp->{$name} = @v==1 ? $v[0] : \@v; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 172 | } | ||||||||||||||||||||
| 173 | } | ||||||||||||||||||||
| 174 | 0 | $fp; | |||||||||||||||||||
| 175 | }, | ||||||||||||||||||||
| 176 | 22 | ); | |||||||||||||||||||
| 177 | |||||||||||||||||||||
| 178 | 22 | has fields => ( | |||||||||||||||||||
| 179 | is => 'ro', | ||||||||||||||||||||
| 180 | isa => 'ArrayRef', | ||||||||||||||||||||
| 181 | required => 1, | ||||||||||||||||||||
| 182 | auto_deref => 1, | ||||||||||||||||||||
| 183 | ); | ||||||||||||||||||||
| 184 | |||||||||||||||||||||
| 185 | 22 | has request => ( | |||||||||||||||||||
| 186 | is => 'ro', | ||||||||||||||||||||
| 187 | isa => 'Object', | ||||||||||||||||||||
| 188 | required => 1, | ||||||||||||||||||||
| 189 | ); | ||||||||||||||||||||
| 190 | |||||||||||||||||||||
| 191 | 22 | has 'widgets' => ( | |||||||||||||||||||
| 192 | is => 'ro', | ||||||||||||||||||||
| 193 | isa => 'Str', | ||||||||||||||||||||
| 194 | default => 'HTML::Shakan::Widgets::Simple', | ||||||||||||||||||||
| 195 | ); | ||||||||||||||||||||
| 196 | |||||||||||||||||||||
| 197 | 22 | has 'params' => ( | |||||||||||||||||||
| 198 | is => 'rw', | ||||||||||||||||||||
| 199 | isa => 'Hash::MultiValue', | ||||||||||||||||||||
| 200 | lazy => 1, | ||||||||||||||||||||
| 201 | builder => '_build_params', | ||||||||||||||||||||
| 202 | ); | ||||||||||||||||||||
| 203 | |||||||||||||||||||||
| 204 | has 'uploads' => ( | ||||||||||||||||||||
| 205 | is => 'rw', | ||||||||||||||||||||
| 206 | isa => 'HashRef', | ||||||||||||||||||||
| 207 | 0 | ▶ | default => sub { +{} }, | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 208 | 22 | ); | |||||||||||||||||||
| 209 | 0 | ▶ | sub upload { | ||||||||||||||||||
|
Subroutine
upload: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 210 | 0 | my ($self, $name) = @_; | |||||||||||||||||||
| 211 | 0 | $self->uploads->{$name}; | |||||||||||||||||||
| 212 | } | ||||||||||||||||||||
| 213 | |||||||||||||||||||||
| 214 | # code taken from MooseX::Param and changed a bit | ||||||||||||||||||||
| 215 | 0 | ▶ | sub param { | ||||||||||||||||||
|
Subroutine
param: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||
| 216 | 0 | my $self = shift; | |||||||||||||||||||
| 217 | |||||||||||||||||||||
| 218 | 0 | my $params = $self->params; | |||||||||||||||||||
| 219 | |||||||||||||||||||||
| 220 | # if they want the list of keys ... | ||||||||||||||||||||
| 221 | 0 | ▶ | return $params->keys if scalar @_ == 0; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 222 | |||||||||||||||||||||
| 223 | # if they want to fetch a particular key ... | ||||||||||||||||||||
| 224 | 0 | ▶ | if (scalar @_ == 1) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 225 | 0 | ▶ | return wantarray ? $params->get_all($_[0]) : $params->get($_[0]); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 226 | } | ||||||||||||||||||||
| 227 | |||||||||||||||||||||
| 228 | 0 | ▶ | ( ( scalar @_ % 2 ) == 0 ) || confess "parameter assignment must be an even numbered list"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 229 | |||||||||||||||||||||
| 230 | 0 | my %new = @_; | |||||||||||||||||||
| 231 | 0 | ▶ | while ( my ( $key, $value ) = each %new ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 232 | 0 | ▶ | my @values = ref $value eq 'ARRAY' ? @$value : ($value); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 233 | 0 | $self->params->set($key, @values); | |||||||||||||||||||
| 234 | } | ||||||||||||||||||||
| 235 | 0 | return; | |||||||||||||||||||
| 236 | } | ||||||||||||||||||||
| 237 | |||||||||||||||||||||
| 238 | 0 | ▶ | sub _build_params { | ||||||||||||||||||
|
Subroutine
_build_params: not called CC=7 SCAR=40.3
| |||||||||||||||||||||
| 239 | 0 | my $self = shift; | |||||||||||||||||||
| 240 | 0 | my $params = {}; | |||||||||||||||||||
| 241 | 0 | for my $field (@{$self->fields}) { | |||||||||||||||||||
| 242 | 0 | ▶ | if ($self->widgets->can('field_filter')) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 243 | # e.g. DateField | ||||||||||||||||||||
| 244 | 0 | $self->widgets->field_filter($self, $field, $params); | |||||||||||||||||||
| 245 | } | ||||||||||||||||||||
| 246 | 0 | ▶ | if ($field->can('field_filter')) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 247 | # e.g. FileField | ||||||||||||||||||||
| 248 | 0 | $field->field_filter($self, $params); | |||||||||||||||||||
| 249 | } | ||||||||||||||||||||
| 250 | |||||||||||||||||||||
| 251 | 0 | my $name = $field->name; | |||||||||||||||||||
| 252 | |||||||||||||||||||||
| 253 | 0 | my @val = $self->request->param($name); | |||||||||||||||||||
| 254 | 0 | ▶ | if (@val != 0) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 255 | 0 | ▶ | if ( my $filters = $field->{filters} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 256 | 0 | @val = map { HTML::Shakan::Filters->filter( $filters, $_ ) } @val; | |||||||||||||||||||
| 257 | } | ||||||||||||||||||||
| 258 | 0 | ▶ | $params->{$name} = @val==1 ? $val[0] : \@val; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 259 | } | ||||||||||||||||||||
| 260 | } | ||||||||||||||||||||
| 261 | |||||||||||||||||||||
| 262 | 0 | Hash::MultiValue->from_mixed($params); | |||||||||||||||||||
| 263 | } | ||||||||||||||||||||
| 264 | |||||||||||||||||||||
| 265 | 22 | ▶ | no Mouse; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 266 | 22 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||
| 267 | __END__ | ||||||||||||||||||||