container.pm

filter: Statementstmt 77.7% 7 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 83.3% 10 / 12 CC 1 SCAR 0.9
CC
1
Cov
78%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« checkbox.pm ↑ Summary fieldset.pm »
1 package HTML::GUI::container;
2  
3 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 =head1 NAME
7  
8 HTML::GUI::container - Create and control a whole container for web application
9  
10 =head1 VERSION
11  
12 Version 0.01
13  
14 =cut
15  
16 our $VERSION = '0.01';
17  
18 0 ▶use HTML::GUI::widget;
Subroutine BEGIN: called CC=1 SCAR=2.6
19 use UNIVERSAL qw(isa);
20 our @ISA = qw(HTML::GUI::widget);
21 use Log::Log4perl qw(:easy);
22  
23  
24  
25 =head1 CONTAINER
26  
27 Manage a container : it loads its definition with a YAML file and create all the widgets it contains.
28 It can generate javascript code to check constraints on the web page.
29 It can test if constraints of each widget are OK.
30 It can generate the HTML of each widget for use with HTML::Template.
31  
32  
33 =cut
34  
35  
36  
37 =head1 PUBLIC METHODS
38  
39 =pod
40  
41 =head3 new
42  
43 create a new container
44  
45 =cut
46  
47 sub new
48 {
49 my($class, $params) = @_;
50  
51 my $this = $class->SUPER::new($params);
52 return undef unless defined $this;
53  
54 $this->{widgets} = [];
55 $this->{index} = {$this->getId() => $this};# an index of all widget ids
56
57 bless($this, $class);
58 if (exists $params->{childs} && ref $params->{childs} eq 'ARRAY'){
59 foreach my $widgetDefinition (@{$params->{childs}}){
60 $this->addChild($widgetDefinition);
61 }
62 }
63 return $this;
64 }
65  
66 =head3 addChild
67  
68 Parameters :
69 widget_def : hash ref : Parameters to define the widget inside the container.
70 The same parameters as to create a a widget but you can specify the cloneID (the id of the widget you want to clone from).
71  
72 Return :
73
74  
75 Description :
76 Create and add a widget to the container
77  
78 =cut
79  
80 sub addChild
81 {
82 my($self,
83 $widget_def, # hash ref : Parameters to define the widget inside the container.
84 #The same parameters as to create a widget object
85 # but you can specify the cloneID
86 # (the id of the widget you want to clone from).
87 # $widget_def can also a widget object
88 ) = @_;
89  
90 my $widget = undef;
91 SWITCH: {
92 if (isa($widget_def,'HTML::GUI::widget')){
93 $widget = $widget_def;
94 last SWITCH;
95 }
96 $widget = HTML::GUI::widget->instantiate($widget_def);
97 last SWITCH;
98 }
99 if (!defined $widget){
100 die "Impossible to define the widget";
101 }
102  
103 #get the list of the ids existing in the new widget
104 my @ids_list = $widget->getIds();
105  
106 foreach my $id (@ids_list){
107 if ($id ne '' && exists $self->{index}{$id} ){
108 $self->error({visibility => 'pub',
109 'error-type' => 'internal',
110 'message' => "An internal error occured while generating the screen."
111 ." Report your problem to the technical support." ,
112 });
113 ERROR "Impossible to add the child [$id]. Each widget id MUST be unique.";
114 return;
115 }
116 }
117 push @{$self->{widgets}} , $widget;
118  
119 foreach my $id (@ids_list){
120 #the widget with a void id are ignored
121 $self->setIndex($widget->getElementById($id)) unless $id eq '';
122 }
123 $widget->setParent($self);
124
125 }
126  
127  
128 =pod
129  
130 =head3 getDefinitionData
131  
132 Specialization of the widget.pm function
133  
134 =cut
135 sub getDefinitionData($;$$$)
136 {
137 my ($self,$paramPublicProp,$paramDefaultValue, $paramPublicPropList) = @_;
138
139 my @widgetsDefinitionList = () ;
140  
141 my $publicProp = $self->SUPER::getDefinitionData($paramPublicProp,$paramDefaultValue, $paramPublicPropList);
142 foreach my $widget (@{$self->{widgets}}){
143 push @widgetsDefinitionList, $widget->getDefinitionData();
144 }
145 $publicProp->{childs} = \@widgetsDefinitionList;
146  
147 return $publicProp;
148 }
149  
150 =pod
151  
152 =head3 getIds
153  
154 Return :
155 array
156  
157 Description :
158 return an array of the ids of the widgets which belong
159 to the container.
160  
161 =cut
162  
163 sub getIds
164 {
165 my($self) = @_;
166 if (!defined $self->{parent}){
167 return (keys %{$self->{index}});
168 }else{
169 my @idList = ();
170 foreach my $widget (@{$self->{widgets}}){
171 push @idList, $widget->getIds();
172 }
173 }
174
175 }
176  
177 =pod
178  
179 =head3 setIndex
180  
181 Parameters :
182 $widget : widget objet : the object to add to the index.
183  
184 Description :
185 update the index of the container and all of its parents in order to have all indexes "up-to-date"
186  
187  
188 =cut
189  
190 sub setIndex
191 {
192  
193 my($self,$newWidget) = @_;
194  
195 my $id = $newWidget->getId();
196 $self->{index}{$id} = $newWidget;
197  
198 if (defined $self->{parent}){
199 $self->{parent}->setIndex($newWidget);
200 }
201 }
202  
203 =pod
204  
205 =head3 getElementById
206  
207 Parameters :
208 id : string : id of the object to find.
209  
210 Description :
211 return the objet widget whose id is $id or undef if no object has this id
212  
213  
214 =cut
215  
216 sub getElementById
217 {
218  
219 my($self,$id) = @_;
220  
221 return undef unless (exists $self->{index}{$id});
222 return $self->{index}{$id};
223 }
224  
225  
226 =pod
227  
228 =head3 setDefaultField
229  
230 Parameters :
231 widgetObj : widget : The widget objet you want to use as a default widget.
232  
233 Return :
234
235  
236 Description :
237 The default widget defined with this function will be used anytime your create a new widget without specifiing options.
238
239  
240 =cut
241  
242 sub setDefaultField
243 {
244 my($self,
245 $widgetObj, # widget : The widget objet you want to use as a default widget.
246 ) = @_;
247 #UML_MODELER_BEGIN_PERSONAL_CODE_setDefaultField
248 #UML_MODELER_END_PERSONAL_CODE_setDefaultField
249 }
250  
251  
252 =pod
253  
254  
255 =pod
256  
257 =head3 ListError
258  
259 Return :
260 string
261  
262 Description :
263 Return a string describing all the public errors that occured in all the widget objects in order to explain to the user why his input cannot be recorded.
264  
265 =cut
266  
267 sub ListError
268 {
269 my($self ) = @_;
270 #UML_MODELER_BEGIN_PERSONAL_CODE_ListError
271 #UML_MODELER_END_PERSONAL_CODE_ListError
272 }
273  
274  
275 =pod
276  
277 =head3 validate
278  
279 Return :
280 return 1 if no field of the container break no constraint
281 return 0 if one or more field break constraint
282  
283 =cut
284  
285 sub validate
286 {
287 my($self) = @_;
288 my $OK = 1;
289 foreach my $widget (@{$self->{widgets}}){
290 if ( !$widget->validate()){
291 $OK = 0;
292 }
293 }
294 return $OK;
295 }
296  
297 =head3 getFiredBtn
298
299 Parameters :
300 $params : the hash ref containing the POST key-value pairs.
301  
302 Description :
303 Find the button the user clic (if one button was fired)
304  
305 Returns :
306 - The button object which was fired
307 - undef if no button was fired
308  
309 =cut
310 sub getFiredBtn
311 {
312 my($self,$params) = @_;
313 foreach my $key (keys %{$self->{index}}){
314  
315 my $widget = $self->{index}{$key};
316 if ($widget->fired($params)){
317 return $widget;
318 }
319 }
320 return undef;
321 }
322  
323 =pod
324  
325 =head3 getValueHash
326  
327 Description :
328 get all the values stored in the container
329 Return :
330 a ref to a hash containing the value associated to each input id
331  
332 =cut
333  
334 sub getValueHash
335 {
336 my($self) = @_;
337 my %values=();
338 foreach my $widget (@{$self->{widgets}}){
339 my $widgetValues = $widget->getValueHash();
340 if ($widgetValues){
341 foreach my $key (keys %$widgetValues){
342 $values{$key} =$widgetValues->{$key};
343 }
344 }
345 }
346 return \%values;
347 }
348  
349 =pod
350  
351 =head3 setValueFromParams
352  
353 Description :
354 set the value of the widgets of the container for which a value fits in $params hash;
355 Return :
356 nothing
357  
358 =cut
359  
360 sub setValueFromParams
361 {
362 my($self,$params) = @_;
363 foreach my $widget (@{$self->{widgets}}){
364 if (isa($widget,'HTML::GUI::input')
365 ||isa($widget,'HTML::GUI::container')){
366 $widget->setValueFromParams($params);
367 }
368 }
369 }
370  
371 =pod
372  
373 =head3 setValue
374  
375 Description :
376 set the value of the widgets of the container for which a value fits in $params hash;
377
378 Parameters :
379 $valueHash : a hash ref of the same form as the function getValueHash returns
380  
381 Return :
382 nothing
383  
384 =cut
385  
386 sub setValue
387 {
388 my($self,$valueHash) = @_;
389 foreach my $key (keys %{$valueHash}){
390 if (exists $self->{index}{$key}){
391 my $widget = $self->{index}{$key};
392 $widget->setValue($valueHash->{$key});
393 }
394 }
395 }
396  
397  
398 =head3 getHtml
399  
400 Return :
401 a string containing the html of the widget contained in the container.
402  
403 =cut
404  
405 sub getHtml
406 {
407 my($self ) = @_;
408 my $html = "";
409 foreach my $widget (@{$self->{widgets}}){
410 $html .= $widget->getHtml();
411 }
412 return $html;
413 }
414  
415 =pod
416  
417 =head3 DESTROY
418 The destructor to erase the ref to the parent
419 and avoid cycle references
420  
421 =cut
422  
423 sub DESTROY
424 {
425 my($self ) = @_;
426
427 delete $self->{parent};
428 }
429  
430 =head1 AUTHOR
431  
432 Jean-Christian Hassler, C<< <jhassler at free.fr> >>
433  
434 =head1 BUGS
435  
436 Please report any bugs or feature requests to
437 C<bug-gui-libhtml-container at rt.cpan.org>, or through the web interface at
438 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=HTML-GUI-widget>.
439 I will be notified, and then you'll automatically be notified of progress on
440 your bug as I make changes.
441  
442 =head1 SUPPORT
443  
444 You can find documentation for this module with the perldoc command.
445  
446 perldoc HTML::GUI::widget
447  
448 You can also look for information at:
449  
450 =over 4
451  
452 =item * AnnoCPAN: Annotated CPAN documentation
453  
454 L<http://annocpan.org/dist/HTML-GUI-widget>
455  
456 =item * CPAN Ratings
457  
458 L<http://cpanratings.perl.org/d/HTML-GUI-widget>
459  
460 =item * RT: CPAN's request tracker
461  
462 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=HTML-GUI-widget>
463  
464 =item * Search CPAN
465  
466 L<http://search.cpan.org/dist/HTML-GUI-widget>
467  
468 =back
469  
470 =head1 ACKNOWLEDGEMENTS
471  
472 =head1 COPYRIGHT & LICENSE
473  
474 Copyright 2007 Jean-Christian Hassler, all rights reserved.
475  
476 This program is free software; you can redistribute it and/or modify it
477 under the same terms as Perl itself.
478  
479 =cut
480  
481 1; # End of HTML::GUI::container
« checkbox.pm ↑ Summary fieldset.pm »