QBit/WebInterface/Controller/Form.pm untested

filter: Statementstmt 0.0% 0 / 133 Branchbran 0.0% 0 / 44 Conditioncond 0.0% 0 / 37 MC/DCmcdc 0.0% 0 / 21 Subroutinesub 0.0% 0 / 21 Podpod 0.0% 0 / 16 totaltotal 0.0% 0 / 272 CC 23 SCAR 36.0
CC
23
Cov
0%
process
110.0
start_form_html
42.0
init
20.0
CRAP 36.4
1 package QBit::WebInterface::Controller::Form;
2 $QBit::WebInterface::Controller::Form::VERSION = '0.033';
3 use qbit;
4  
5 use base qw(QBit::Class);
6  
7 use Exception::Form;
8 use Exception::WebInterface::Controller::CSRF;
9  
10 __PACKAGE__->mk_ro_accessors(qw(controller));
11  
12 our $FIELDS_NAMESPACE = 'QBit::WebInterface::Controller::Form::Field::';
13  
14 sub init {
15 my ($self) = @_;
16  
17 weaken($self->{'controller'});
18  
19 my @default_fields = (
20 {type => 'hidden', name => 'save'},
21 $self->controller->request->param('retpath') ? {name => 'retpath', type => 'hidden'} : (),
22 $self->controller->app->form_fields(),
23 );
24  
25 $self->{'__FIELDS__'} = [];
26 foreach my $field (@default_fields, @{$self->{'fields'} || []}) {
27 my $field_class = "$FIELDS_NAMESPACE$field->{'type'}";
28 my $field_module = "$field_class.pm";
29 $field_module =~ s/::/\//g;
30  
31 if (require($field_module)) {
32 throw gettext('Field class must be QBit::WebInterface::Controller::Form::Field descendant')
33 unless $field_class->isa('QBit::WebInterface::Controller::Form::Field');
34 push(@{$self->{'__FIELDS__'}}, $field_class->new(%$field, form => $self));
35 } else {
36 throw gettext('Unknown field type "%s"', $_->{'type'});
37 }
38 }
39  
40 $self->{'__FIELDS_HS__'} = {};
41 foreach my $field (@{$self->{'__FIELDS__'}}) {
42 $self->{'__FIELDS_HS__'}{$field->{'name'} || ''} ||= [];
43 push(@{$self->{'__FIELDS_HS__'}{$field->{'name'} || ''}}, $field);
44 }
45 }
46  
47 sub get_fields {
48 return $_[0]->{'__FIELDS__'};
49 }
50  
51 sub get_fields_hs {
52 return $_[0]->{'__FIELDS_HS__'};
53 }
54  
55 sub get_field {
56 return $_[0]->{'__FIELDS_HS__'}->{$_[1]}[0];
57 }
58  
59 sub set_field_error {
60 my ($self, $field_name, $error) = @_;
61  
62 throw gettext('Error class must be Exception descendant.')
63 unless blessed($error) && $error->isa('Exception');
64  
65 $self->{'__FIELDS_ERROR__'}{$field_name} = $error;
66 return FALSE;
67 }
68  
69 sub get_field_names {
70 my ($self) = @_;
71  
72 return map {$_->{'name'}} @{$self->get_fields()};
73 }
74  
75 sub break {
76 my ($self, @data) = @_;
77  
78 $self->{'__BREAK_PROCESS__'} = 1;
79 return @data;
80 }
81  
82 sub process {
83 my ($self, %opts) = @_;
84  
85 return $self->denied($self->controller)
86 if exists($self->{'check_rights'}) && !$self->controller->check_rights(@{$self->{'check_rights'}});
87  
88 $self->{'__BREAK_PROCESS__'} = 0;
89 my @pre_process_data = $self->{'pre_process'}($self) if exists($self->{'pre_process'});
90 return @pre_process_data if $self->{'__BREAK_PROCESS__'};
91  
92 $self->{'__FIELDS_ERROR__'} = {};
93 delete($self->{'__ERROR__'});
94  
95 my $retpath = $self->controller->request->param('retpath');
96 $self->{'__FIELDS_HS__'}{'retpath'}[0]{'value'} = $retpath if $retpath;
97  
98 my $pseudo_url = $self->controller->get_option('cur_cmdpath') . '/' . $self->controller->get_option('cur_cmd');
99  
100 if ($self->controller->request->param('save') && !$opts{'no_save'}) {
101 if ($self->controller->check_anti_csrf_token($self->controller->request->param('save'), url => $pseudo_url)) {
102 foreach my $field (@{$self->{'__FIELDS__'}}) {
103 try {
104 $field->process($self->controller);
105 }
106 catch Exception::Form with {
107 $self->set_field_error($field->{'name'}, shift);
108 };
109 }
110  
111 if (!%{$self->{'__FIELDS_ERROR__'}} && exists($self->{'check'})) {
112 try {
113 $self->{'check'}($self);
114 }
115 catch Exception::Form with {
116 $self->{'__ERROR__'} = $_[0];
117 };
118 }
119  
120 if ( !%{$self->{'__FIELDS_ERROR__'}}
121 && !exists($self->{'__ERROR__'})
122 && exists($self->{'save'}))
123 {
124 try {
125 $self->{'save'}($self, $self->controller);
126 }
127 catch Exception::Form with {
128 $self->{'__ERROR__'} = $_[0];
129 };
130 }
131  
132 if (keys(%{$self->{'__FIELDS_ERROR__'}}) || $self->{'__ERROR__'}) {
133 return $self->_get_html($self->controller);
134 }
135  
136 return $self->_on_complete($self->controller);
137 } else {
138 throw Exception::WebInterface::Controller::CSRF gettext('CSRF has been detected');
139 }
140 } else {
141 $self->{'__FIELDS_HS__'}{'save'}[0]{'value'} = $self->controller->gen_anti_csrf_token(url => $pseudo_url);
142 return $self->_get_html($self->controller);
143 }
144 }
145  
146 sub get_value {
147 my ($self, $name) = @_;
148  
149 return exists($self->{'__FIELDS_HS__'}{$name || ''}[0]) ? $self->{'__FIELDS_HS__'}{$name || ''}[0]->value() : undef;
150 }
151  
152 sub start_form_html {
153 my ($self) = @_;
154  
155 my $html = '<form class="form-horizontal" method="post"'
156 . (defined($self->{'enctype'}) ? " enctype=\"$self->{'enctype'}\"" : '') . '>';
157 $html .= '<fieldset>';
158 $html .= '<legend>' . html_encode($self->{'title'}) . '</legend>' if defined($self->{'title'});
159  
160 $html .= '<p class="text-info">' . html_encode($self->{'description'}) . '</p>'
161 if defined($self->{'description'});
162  
163 $html .= '<p class="text-error">' . html_encode($self->{'__ERROR__'}->message()) . '</p>'
164 if exists($self->{'__ERROR__'});
165  
166 my $success_message = $self->{'controller'}->stash_delete($self->get_stash_form_name('complete_message'));
167 $html .= '<p class="text-success">' . html_encode($success_message) . '</p>' if defined($success_message);
168  
169 $html .= $_->as_html() foreach grep {$_->is_hidden()} @{$self->{'__FIELDS__'}};
170  
171 return $html;
172 }
173  
174 sub finish_form_html {
175 my ($self) = @_;
176  
177 my $html .= '</fieldset>';
178 $html .= '</form>';
179  
180 return $html;
181 }
182  
183 sub as_html {
184 my ($self) = @_;
185  
186 my $html = $self->start_form_html();
187 $html .= $_->as_html() foreach grep {!$_->is_hidden()} @{$self->{'__FIELDS__'}};
188 $html .= $self->finish_form_html();
189  
190 return $html;
191 }
192  
193 sub denied {return $_[1]->denied()}
194  
195 sub get_stash_form_name {
196 my ($self, $name) = @_;
197  
198 my $stash_name =
199 $self->controller->get_option('cur_cmdpath') . '_' . $self->controller->get_option('cur_cmd') . '_' . $name;
200  
201 return $stash_name;
202 }
203  
204 sub _on_complete {
205 my ($self, $controller) = @_;
206  
207 if (defined($self->{complete_message})) {
208 my $stash_name = $self->get_stash_form_name('complete_message');
209  
210 $controller->stash_set($stash_name, $self->{complete_message});
211 }
212  
213 my $retpath = $controller->request->param('retpath');
214 my %redirect_opts = $self->{'redirect_opts'} ? %{$self->{'redirect_opts'}} : ();
215 if ($retpath) {
216 return $controller->redirect2url_internal($retpath);
217 } else {
218 return $controller->redirect($self->{'redirect'}, %redirect_opts);
219 }
220 }
221  
222 sub _get_html {
223 my ($self, $controller) = @_;
224  
225 return $controller->from_template(($self->{'template'} || 'form.tt2'),
226 vars => {%{$self->{'vars'} || {}}, form => $self});
227 }
228  
229 TRUE;