lib/CallBackery/GuiPlugin/AbstractForm.pm

filter: Statementstmt 29.3% 34 / 116 Branchbran 7.5% 3 / 40 Conditioncond 0.0% 0 / 11 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 28.5% 6 / 21 Podpod 100% 7 / 7 totaltotal 24.8% 50 / 201 CC 31 SCAR 29.8
CC
31
Cov
22%
validateData
42.0
processData
42.0
getAllFieldValues
30.0
CRAP 19.6
« lib/CallBackery/GuiPlugin/Users.pm ↑ Summary lib/CallBackery/Config.pm »
1 package CallBackery::GuiPlugin::AbstractForm;
2 1 ▶use Carp qw(carp croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use CallBackery::Translate qw(trm);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use CallBackery::Exception qw(mkerror);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Mojo::Promise;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 =head1 NAME
8  
9 CallBackery::GuiPlugin::AbstractForm - form base class
10  
11 =head1 SYNOPSIS
12  
13 use Mojo::Base 'CallBackery::GuiPlugin::AbstractForm';
14  
15 =head1 DESCRIPTION
16  
17 The base class for gui forms.
18  
19 =cut
20  
21 1 ▶use Mojo::Base 'CallBackery::GuiPlugin::Abstract';
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 =head1 ATTRIBUTES
24  
25 The attributes of the L<CallBackery::GuiPlugin::Abstract> class plus:
26  
27 =head2 screenCfg
28  
29 Returns a configuration structure for the form. The output from this
30 method is fed to the callbackery.ui.form.Auto object to build the
31 Qooxdoo form.
32  
33 =cut
34  
35 0 ▶has screenCfg => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
36 0 my $self = shift;
37 return {
38 0 type => 'form',
39 options => $self->screenOpts,
40 form => $self->formCfg,
41 action => $self->actionCfg,
42 }
43 1 };
44  
45 =head2 screenOpts
46  
47 Returns a hash of options for the screen Options
48  
49 =cut
50  
51 0 ▶has screenOpts => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
52 {
53 0 warnAboutUnsavedData => 1
54 }
55 1 };
56  
57 =head2 actionCfg
58  
59 Returns a list of action buttons to place at the top of the form.
60  
61 =cut
62  
63 0 ▶has actionCfg => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
64 0 [];
65 1 };
66  
67 =head2 actionCfgMap
68  
69 TODOC
70  
71 =cut
72  
73 0 ▶has actionCfgMap => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
74 0 my $self = shift;
75 0 my %map;
76 0 for my $row (@{$self->actionCfg}){
77 0 ▶ next unless $row->{action} =~ /^(submit|upload|download)/;
Branch: 1 branch
BranchTrueFalseTotal
unless $row->{"action"} =~ /^(submit|upload|download)/u 0 0 0
78 0 ▶ next unless $row->{key};
Branch: 1 branch
BranchTrueFalseTotal
unless $row->{"key"} 0 0 0
79 0 $map{$row->{key}} = $row;
80 }
81 0 return \%map;
82 1 };
83  
84 =head2 formCfg
85  
86 Returns the content of the form.
87  
88 =cut
89  
90 0 ▶has formCfg => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
91 0 [];
92 1 };
93  
94 =head2 formCfg
95  
96 TODOC
97  
98 =cut
99  
100 0 ▶has formCfgMap => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
101 0 my $self = shift;
102 0 my %map;
103 0 for my $row (@{$self->formCfg}){
104 0 ▶ next unless $row->{key};
Branch: 1 branch
BranchTrueFalseTotal
unless $row->{"key"} 0 0 0
105 0 $map{$row->{key}} = $row;
106 }
107 0 return \%map;
108 1 };
109  
110  
111 =head1 METHODS
112  
113 All the methods of L<CallBackery::GuiPlugin::Abstract> plus:
114  
115 =cut
116  
117 =head2 validateData(fieldName,formData)
118  
119 If the given value is valid for the field, return undef else return
120 an error message.
121  
122 =cut
123  
124 0 ▶sub validateData {
Subroutine validateData: not called CC=6 SCAR=37.4
Pod documented
125 0 my $self = shift;
126 0 my $fieldName = shift;
127 0 my $formData = shift;
128 0 my $entry = $self->formCfgMap->{$fieldName};
129 0 ▶ if (not ref $entry){
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $entry) 0 0 0
130 0 die mkerror(4095,trm("sorry, don't know the field you are talking about"));
131 }
132 0 ▶ return undef if not $entry->{set}{required} and (not defined $formData->{$fieldName} or length($formData->{$fieldName}) == 0);
Branch: 1 branch
BranchTrueFalseTotal
if not $entry->{"set"}{"required"} and !defined($formData->{$fieldName}) || length $formData->{$fieldName} == 0 0 0 0
Condition0%0/6
not $entry->{"set"}{"required"} and !defined($formData->{$fieldName}) || length $formData->{$fieldName} == 0
!ll&&!rl&&r
000
not defined $formData->{$fieldName} or length $formData->{$fieldName} == 0
l!l&&r!l&&!r
000
MC/DC0%0/3
not $entry->{"set"}{"required"} and !defined($formData->{$fieldName}) || length $formData->{$fieldName} == 0
not $entry->{"set"}{"required"} not defined $formData->{$fieldName} length $formData->{$fieldName} == 0
Truth table0%0/4
not(A) and not(B) or C
A= not $entry->{"set"}{"required"}B= not defined $formData->{$fieldName}C= length $formData->{$fieldName} == 0
ABCresult
0XX0
1000
11X1
1011
133 0 ▶ return ($entry->{validator} ? $entry->{validator}->($formData->{$fieldName},$fieldName,$formData) : undef);
Branch: 1 branch
BranchTrueFalseTotal
$entry->{'validator'} ? : 0 0 0
134 }
135  
136 =head2 processData($args)
137  
138 The default behavior of the method is to validate all the form fields
139 and then store the data into the config database.
140  
141 =cut
142  
143 0 ▶sub processData {
Subroutine processData: not called CC=6 SCAR=37.4
Pod documented
144 0 my $self = shift;
145 0 my $args = shift;
146 0 my $form = $self->formCfgMap;
147 0 my $formData = $args->{formData};
148 # this is only to be sure ... data should be pre-validated
149 0 for my $key (keys %$form){
150 0 ▶ if (my $error = $self->validateData($key,$formData)){
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $self->validateData($key, $formData)) 0 0 0
151 0 die mkerror(7492,"Value for $key: $error");
152 }
153 }
154 0 ▶ if ($args->{key}){
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"key"}) 0 0 0
155 0 my $handler = $self->actionCfgMap->{$args->{key}}{actionHandler};
156 0 ▶ if (ref $handler eq 'CODE'){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $handler eq "CODE") 0 0 0
157 0 return $handler->($self,$formData);
158 }
159 0 $handler = $self->actionCfgMap->{$args->{key}}{handler};
160 0 ▶ if (ref $handler eq 'CODE'){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $handler eq "CODE") 0 0 0
161 0 $self->log->warn("Using handler properties in actionCfg is deprecated. User actionHandler instead.");
162 0 return $handler->($formData);
163 }
164 0 $self->log->error('Plugin instance '.$self->name." action $args->{key} has a broken handler");
165 0 die mkerror(7623,'Plugin instance '.$self->name." action $args->{key} has a broken handler");
166 }
167 }
168  
169  
170 =head2 saveFormDataToConfig(data)
171  
172 Save all the form fields for which is available to the config
173 database. Keys will be prefixed by the plugin instance name
174 (C<PluginInstance::keyName>).
175  
176 =cut
177  
178 0 ▶sub saveFormDataToConfig {
Subroutine saveFormDataToConfig: not called CC=3 SCAR=24.8
Pod documented
179 0 my $self = shift;
180 0 my $formData = shift;
181 0 my $form = $self->formCfgMap;
182 0 for my $key (keys %$form){
183 0 ▶ next if not exists $formData->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $formData->{$key} 0 0 0
184 0 $self->setConfigValue($self->name.'::'.$key,$formData->{$key});
185 }
186 }
187  
188 =head2 getFieldValue(field)
189  
190 Fetch the current value of the field. This will either use the getter
191 method supplied in the form config or try to fetch the value from the
192 config database.
193  
194 =cut
195  
196 0 ▶sub getFieldValue {
Subroutine getFieldValue: not called CC=4 SCAR=30.0
Pod documented
197 0 my $self = shift;
198 0 my $field = shift;
199 0 my $entry = $self->formCfgMap->{$field};
200 0 ▶ return undef unless ref $entry eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $entry eq "HASH" 0 0 0
201 0 ▶ if ($entry->{getter}){
Branch: 1 branch
BranchTrueFalseTotal
if ($entry->{"getter"}) 0 0 0
202 0 ▶ if (ref $entry->{getter} eq 'CODE'){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $entry->{'getter'} eq 'CODE') { } 0 0 0
203 0 return $entry->{getter}->($self);
204 }
205 else {
206 0 warn 'Plugin instance'.$self->name." field $field has a broken getter\n";
207 }
208 }
209 0 return $self->getConfigValue($self->name.'::'.$field);
210 }
211  
212 =head2 getAllFieldValues
213  
214 Return all field values of the form.
215  
216 =cut
217  
218 0 ▶sub getAllFieldValues {
Subroutine getAllFieldValues: not called CC=5 SCAR=34.0
Pod documented
219 0 my $self = shift;
220 0 my %map;
221 my @promises;
222 0 for my $key (keys %{$self->formCfgMap}){
223 0 my $value = $self->getFieldValue($key);
224 0 ▶ if (eval { blessed $value && $value->isa('Mojo::Promise')}){
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { $value->blessed and $value->isa('Mojo::Promise') } }) { } 0 0 0
Condition0%0/3
$value->blessed && $value->isa('Mojo::Promise')
!ll&&!rl&&r
000
MC/DC0%0/2
$value->blessed && $value->isa('Mojo::Promise')
$value->blessed $value->isa('Mojo::Promise')
Truth table0%0/3
A && B
A= $value->blessedB= $value->isa('Mojo::Promise')
ABresult
0X0
100
111
225 0 push @promises, $value;
226 $value->then(
227 0 ▶ sub{
Subroutine __ANON__: not called CC=1 SCAR=6.9
228 0 $map{$key} = shift;
229 },
230 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
231 0 die shift;
232 }
233 0 );
234 }
235 else {
236 0 $map{$key} = $self->getFieldValue($key);
237 }
238 }
239 0 ▶ if (@promises){
Branch: 1 branch
BranchTrueFalseTotal
if (@promises) 0 0 0
240 0 ▶ return Mojo::Promise->new->all(@promises)->then(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
241 0 return \%map;
242 0 });
243 }
244 0 return \%map;
245 }
246  
247 =head2 getData (type,field)
248  
249 Return the value of the given field. If no field name is specified
250 return a hash with all the current data known to the plugin.
251  
252 =cut
253  
254 0 ▶sub getData {
Subroutine getData: not called CC=4 SCAR=30.0
Pod documented
255 0 my $self = shift;
256 0 my $type = shift;
257 0 ▶ if ($type eq 'field'){
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'field') { } 0 0 0
elsif ($type eq 'allFields') { } 0 0 0
258 0 return $self->getFieldValue(@_);
259 }
260 elsif ($type eq 'allFields') {
261 0 return $self->getAllFieldValues(@_);
262 }
263 else {
264 0 ▶ die mkerror(38334, 'Requested unknown data type' . ($type // 'unknown'));
Condition0%0/2
$type // "unknown"
l!l
00
MC/DC0%0/1
$type // "unknown"
$type
Truth table0%0/2
A
A= $type
Aresult
11
00
265 }
266 }
267  
268 =head2 massageConfig
269  
270 Function to integrate the plugin configuration recursively into the main config
271 hash.
272  
273 =cut
274  
275 4 ▶sub massageConfig {
Subroutine massageConfig: called CC=4 SCAR=13.9
Pod documented
276 4 my $self = shift;
277 4 my $cfg = shift;
278 4 my $actionCfg = $self->actionCfg;
279 4 for my $button (@$actionCfg){
280 6 ▶ if ($button->{action} eq 'popup'){
Branch: 1 branch
BranchTrueFalseTotal
if ($button->{"action"} eq "popup") 2 4 6
281 2 my $name = $button->{name};
282 die "Plugin instance name $name is not unique\n"
283 2 ▶ if $cfg->{PLUGIN}{prototype}{$name};
Branch: 1 branch
BranchTrueFalseTotal
if $cfg->{"PLUGIN"}{"prototype"}{$name} 0 2 2
284 my $popup = $cfg->{PLUGIN}{prototype}{$name}
285 2 = $self->app->config->loadAndNewPlugin($button->{backend}{plugin});
286 2 $popup->config($button->{backend}{config});
287 2 $popup->name($button->{name});
288 2 $popup->app($self->app);
289 2 $popup->massageConfig($cfg);
290 }
291 }
292 }
293  
294 1 1;
295 __END__
« lib/CallBackery/GuiPlugin/Users.pm ↑ Summary lib/CallBackery/Config.pm »