lib/Strehler/Forms.pm

filter: Statementstmt 92.5% 136 / 147 Branchbran 66.6% 16 / 24 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 13 / 13 Podpod 66.6% 6 / 9 totaltotal 87.8% 174 / 198 CC 20 SCAR 14.3
CC
20
Cov
89%
add_multilang_fields
7.0
tags_for_form
6.7
form_user
4.1
CRAP 4.2
« lib/Strehler/FormFu/Validator/CategoryUnique.pm ↑ Summary lib/Strehler/FormFu/Element/SubmitBlockMulti.pm »
1 package Strehler::Forms;
2 12 $Strehler::Forms::VERSION = '1.6.8';
3 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 12 ▶use Cwd 'abs_path';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 12 ▶use HTML::FormFu;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 12 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 12 my $module_file_path = __FILE__;
9 12 my $root_path = abs_path($module_file_path);
10 12 $root_path =~ s/Forms\.pm//;
11  
12 12 my $form_path = $root_path . 'forms';
13  
14  
15  
16 sub form_login
17 6 ▶{
Subroutine form_login: called CC=1 SCAR=0.0
Pod documented
18 6 my $form = HTML::FormFu->new;
19 6 $form->auto_error_class('error-msg');
20 6 $form->load_config_file( $form_path . '/admin/login.yml' );
21 6 return $form;
22 }
23  
24 sub form_category
25 15 ▶{
Subroutine form_category: called CC=1 SCAR=0.0
Pod documented
26 15 my $form = HTML::FormFu->new;
27 15 $form->auto_error_class('error-msg');
28 15 $form->load_config_file( $form_path . '/admin/category.yml' );
29 15 $form = add_dynamic_fields_for_category($form);
30 15 my $prev_name = $form->element( { type => 'Hidden', name => 'prev-name'} );
31 15 my $prev_parent = $form->element( { type => 'Hidden', name => 'prev-parent'} );
32 15 my $position = $form->get_element({ name => 'save' });
33 15 $form->insert_before($prev_name, $position);
34 15 $form->insert_before($prev_parent, $position);
35 15 return $form;
36 }
37  
38 sub form_category_fast
39 1 ▶{
Subroutine form_category_fast: called CC=1 SCAR=0.0
Pod documented
40 1 my $form = HTML::FormFu->new;
41 1 $form->auto_error_class('error-msg');
42 1 $form->load_config_file( $form_path . '/admin/category_fast.yml' );
43 1 return $form;
44 }
45  
46 sub form_user
47 5 ▶{
Subroutine form_user: called CC=4 SCAR=14.2
Pod documented
48 5 my $action = shift;
49 5 my $form = HTML::FormFu->new;
50 5 $form->auto_error_class('error-msg');
51 5 $form->load_config_file( $form_path . '/admin/user.yml' );
52 5 ▶ if($action eq 'add')
Branch: 3 branches
BranchTrueFalseTotal
if ($action eq 'add') { } 3 2 5
elsif ($action eq 'edit') { } 0 2 2
elsif ($action eq 'password') { } 2 0 2
53 {
54 3 $form->constraint({ name => 'password', type => 'Required' });
55 3 $form->constraint({ name => 'password-confirm', type => 'Required' });
56 3 $form->constraint({ name => 'user', type => 'Required' });
57 }
58 elsif($action eq 'edit')
59 {
60 0 my $user = $form->get_element({ name => 'user' });
61 0 $user->attributes->{readonly} = 'readonly';
62 0 $user->attributes->{disabled} = 'disabled';
63 }
64 elsif($action eq 'password')
65 {
66 2 my $user = $form->get_element({ name => 'user' });
67 2 $user->attributes->{readonly} = 'readonly';
68 2 $user->attributes->{disabled} = 'disabled';
69 2 my $role = $form->get_element({ name => 'role' });
70 2 $role->attributes->{readonly} = 'readonly';
71 2 $role->attributes->{disabled} = 'disabled';
72 }
73 5 return $form;
74 }
75  
76 sub form_generic
77 18 ▶{
Subroutine form_generic: called CC=3 SCAR=11.0
Pod documented
78 18 my $form_conf = shift;
79 18 my $multilang_conf = shift;
80 18 my $dancer_conf = shift;
81  
82 18 my $languages = $dancer_conf->{'languages'};
83 18 my $default_language = $dancer_conf->{'default_language'};
84 18 my $only_default_required = $dancer_conf->{'only_default_required'};
85  
86 18 ▶ if(! $form_conf)
Branch: 1 branch
BranchTrueFalseTotal
unless ($form_conf) 0 18 18
87 {
88 0 return undef;
89 }
90  
91 18 my $form = HTML::FormFu->new;
92 18 $form->auto_error_class('error-msg');
93 18 $form->load_config_file( $form_conf );
94 18 my $action_block_conf;
95 18 $action_block_conf->{'type'} = "Hidden";
96 18 $action_block_conf->{'name'} = "strehl-action";
97 18 $form->element($action_block_conf);
98 18 ▶ if($multilang_conf)
Branch: 1 branch
BranchTrueFalseTotal
if ($multilang_conf) 17 1 18
99 {
100 17 $form = add_multilang_fields($form, $languages, $multilang_conf, $default_language, $only_default_required);
101 }
102 18 return $form;
103 }
104  
105 sub form_filter
106 6 ▶{
Subroutine form_filter: called CC=3 SCAR=11.0
Pod documented
107 6 my $conf = shift;
108 6 my $multilang = shift;
109 6 my $selected_language = shift;
110 6 my $languages = shift;
111 6 my @languages_options;
112 6 for my $l (@{$languages})
113 {
114 12 push @languages_options, [ $l, $l ];
115 }
116 6 my @selected_languages = split( ',', $selected_language );
117 6 my $form = HTML::FormFu->new;
118 6 $form->load_config_file( $conf );
119 6 ▶ if($multilang)
Branch: 1 branch
BranchTrueFalseTotal
if ($multilang) 5 1 6
120 {
121 5 my $languages_block = $form->get_element({ name => 'language-block'});
122 5 my $position = $languages_block->get_elements()->[0];
123 5 $languages_block->insert_after($languages_block->element(
124 { label => "Languages:", type => 'Checkboxgroup', name => 'language', options => \@languages_options, default => $selected_language, attributes => { class => 'languages-filter'}}), $position);
125 }
126 6 return $form;
127 }
128  
129 sub tags_for_form
130 18 ▶{
Subroutine tags_for_form: called CC=4 SCAR=19.1
Pod undocumented
131 18 my $form = shift;
132 18 my $params_hashref = shift;
133 18 ▶ if($params_hashref->{'configured-tag'})
Branch: 2 branches
BranchTrueFalseTotal
if ($params_hashref->{'configured-tag'}) { } 0 18 18
elsif ($params_hashref->{'tags'}) { } 18 0 18
134 {
135 0 ▶ if(ref($params_hashref->{'configured-tag'}) eq 'ARRAY')
Branch: 1 branch
BranchTrueFalseTotal
if (ref $params_hashref->{'configured-tag'} eq 'ARRAY') { } 0 0 0
136 {
137 0 $params_hashref->{'tags'} = join(',', @{$params_hashref->{'configured-tag'}});
138 }
139 else
140 {
141 0 $params_hashref->{'tags'} = $params_hashref->{'configured-tag'};
142 }
143 0 my $position = $form->get_elements()->[0];
144 0 $form->insert_after($form->element({ type => 'Text', name => 'tags'}), $position);
145 }
146 elsif($params_hashref->{'tags'})
147 {
148 18 my $position = $form->get_elements()->[0];
149 18 $form->insert_after($form->element({ type => 'Text', name => 'tags'}), $position);
150 }
151 18 return $form;
152 }
153  
154 sub add_multilang_fields
155 17 ▶{
Subroutine add_multilang_fields: called CC=7 SCAR=19.5
Pod undocumented
156 17 my $form = shift;
157 17 my $lan_ref = shift;
158 17 my $config = shift;
159 17 my $default_language = shift;
160 17 my $only_default_required = shift;
161 17 my $position = $form->get_element({ name => 'save' });
162 17 for(@{$lan_ref})
163 {
164 34 my $lan = $_;
165 34 my $form_multilan = HTML::FormFu->new;
166 34 $form_multilan->load_config_file($config);
167 34 for(@{$form_multilan->get_elements()})
168 {
169 96 my $el = $_;
170 96 my @el_constraints = ();
171 96 foreach my $const (@{$el->_constraints()})
172 {
173 28 ▶ if($const->type() eq 'Required')
Branch: 1 branch
BranchTrueFalseTotal
if ($const->type eq 'Required') { } 28 0 28
174 {
175 28 ▶ if($lan eq $default_language || ! $only_default_required)
Branch: 1 branch
BranchTrueFalseTotal
if ($lan eq $default_language or not $only_default_required) 14 14 28
Condition66%2/3
$lan eq $default_language or not $only_default_required
l!l&&r!l&&!r
14014
MC/DC50%1/2
$lan eq $default_language or not $only_default_required
$lan eq $default_language not $only_default_required
Truth table66%2/3
A or not(B)
A= $lan eq $default_languageB= not $only_default_required
ABresult
1X1
011
000
176 {
177 14 push @el_constraints, $const;
178 }
179 }
180 else
181 {
182 0 push @el_constraints, $const;
183 }
184 }
185 96 $el->_constraints(\@el_constraints);
186 96 $el->name($el->name() . '_' . $lan);
187 96 $el->label($el->label . " (" . $lan . ")");
188 96 $form->insert_before($_->clone(), $position);
189 }
190 }
191 17 return $form;
192 }
193  
194 sub add_dynamic_fields_for_category
195 15 ▶{
Subroutine add_dynamic_fields_for_category: called CC=4 SCAR=13.9
Pod undocumented
196 15 my $form = shift;
197 15 my $config = $form_path . '/admin/category_dynamic.yml';
198 15 my $position = $form->get_element({ name => 'save' });
199 15 for(Strehler::Helpers::get_categorized_entities())
200 {
201 60 my $ent = $_;
202 60 my $form_dyna = HTML::FormFu->new;
203 60 my $fieldset = HTML::FormFu::Element::Fieldset->new;
204 60 $fieldset->element( { name => 'placeholder', type => 'Blank' } );
205 60 my $f_position = $fieldset->get_element( { name => 'placeholder' } );
206 60 $form_dyna->load_config_file($config);
207 60 for(@{$form_dyna->get_elements()})
208 {
209 180 my $el = $_;
210 180 ▶ if(ref($el) eq "HTML::FormFu::Element::Block")
Branch: 1 branch
BranchTrueFalseTotal
if (ref $el eq 'HTML::FormFu::Element::Block') { } 60 120 180
211 {
212 60 $el->content("Per " . $ent);
213 60 $el->name($el->name() . '-' . $ent);
214 60 $fieldset->insert_before($el->clone(), $f_position);
215 }
216 else
217 {
218 120 $el->name($el->name() . '-' . $ent);
219 120 $fieldset->insert_before($el->clone(), $f_position);
220 }
221 }
222 60 $form->insert_before($fieldset->clone(), $position);
223 }
224 15 return $form;
225 }
226  
227 12 1;
228  
229 =encoding utf8
230  
231 =head1 NAME
232  
233 Strehler::Forms - Functions to manage forms
234  
235 =head1 DESCRIPTION
236  
237 Functions used as "Forms Factory" by L<Strehler::Admin>, the module where all the CRUD navigation resides.
238  
239 =head1 FUNCTIONS
240  
241 Functions that generate forms:
242  
243 =over 4
244  
245 =item form_login
246  
247 =item form_category/form_category_fast
248  
249 =item form_user
250  
251 =item form_generic
252  
253 =item form_filter
254  
255 =back
256  
257 tags_for_form is used to manage tags field because it has two different behaviours: as text field and as list of checkboxes.
258  
259 add_multilang_fields manages multilang forms.
260  
261 add_dynamic_fields_for_category manipulate category form.
262  
263  
264  
265 =cut
266  
« lib/Strehler/FormFu/Validator/CategoryUnique.pm ↑ Summary lib/Strehler/FormFu/Element/SubmitBlockMulti.pm »