line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package CGI::Lazy::Widget::Controller; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
5
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
38
|
|
4
|
|
|
|
|
|
|
|
5
|
1
|
|
|
1
|
|
6
|
use JSON; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
6
|
|
6
|
1
|
|
|
1
|
|
111
|
use JavaScript::Minifier qw(minify); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
41
|
|
7
|
1
|
|
|
1
|
|
4
|
use CGI::Lazy::Globals; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
76
|
|
8
|
1
|
|
|
1
|
|
4
|
use base qw(CGI::Lazy::Widget); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
1391
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
#---------------------------------------------------------------------------------------- |
11
|
|
|
|
|
|
|
sub buildCheckbox { |
12
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
13
|
0
|
|
|
|
|
|
my $fieldname = shift; |
14
|
0
|
|
|
|
|
|
my $webcontrol = shift; |
15
|
0
|
|
|
|
|
|
my $value = shift; |
16
|
|
|
|
|
|
|
|
17
|
0
|
0
|
|
|
|
|
if ($webcontrol->{value}) { |
|
|
0
|
|
|
|
|
|
18
|
0
|
0
|
|
|
|
|
if ($value eq $webcontrol->{value}) { |
19
|
0
|
|
|
|
|
|
return ($webcontrol->{value}, ' checked '); |
20
|
|
|
|
|
|
|
} else { |
21
|
0
|
|
|
|
|
|
return ($webcontrol->{value}); |
22
|
|
|
|
|
|
|
} |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
} elsif ($webcontrol->{sql}) { |
25
|
0
|
|
|
|
|
|
my ($query, @binds) = @{$webcontrol->{sql}}; |
|
0
|
|
|
|
|
|
|
26
|
0
|
|
|
|
|
|
my $lookupvalue = $self->q->db->get($query, @binds); |
27
|
|
|
|
|
|
|
|
28
|
0
|
0
|
|
|
|
|
if ($value eq $lookupvalue) { |
29
|
0
|
|
|
|
|
|
return ($lookupvalue, ' checked '); |
30
|
|
|
|
|
|
|
} else { |
31
|
0
|
|
|
|
|
|
return ($lookupvalue); |
32
|
|
|
|
|
|
|
} |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
#---------------------------------------------------------------------------------------- |
37
|
|
|
|
|
|
|
sub buildSelect { |
38
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
39
|
0
|
|
|
|
|
|
my $fieldname = shift; |
40
|
0
|
|
|
|
|
|
my $webcontrol = shift; |
41
|
0
|
|
|
|
|
|
my $value = shift; |
42
|
|
|
|
|
|
|
|
43
|
0
|
|
|
|
|
|
my $list = []; |
44
|
|
|
|
|
|
|
|
45
|
0
|
|
|
|
|
|
my $vals = {}; |
46
|
0
|
|
|
|
|
|
tie %$vals, 'Tie::IxHash'; |
47
|
|
|
|
|
|
|
|
48
|
0
|
0
|
|
|
|
|
if ($webcontrol->{values} ) { |
|
|
0
|
|
|
|
|
|
49
|
0
|
0
|
|
|
|
|
if (ref $webcontrol->{values} eq 'HASH') { |
|
|
0
|
|
|
|
|
|
50
|
0
|
0
|
|
|
|
|
$vals->{''} = '' unless $webcontrol->{notNull}; |
51
|
0
|
|
|
|
|
|
$vals = $webcontrol->{values}; |
52
|
|
|
|
|
|
|
} elsif (ref $webcontrol->{values} eq 'ARRAY') { |
53
|
0
|
0
|
|
|
|
|
$vals->{''} = '' unless $webcontrol->{notNull}; |
54
|
0
|
|
|
|
|
|
$vals->{$_} = $_ for @{$webcontrol->{values}}; |
|
0
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
} else { |
56
|
0
|
|
|
|
|
|
return; |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
} elsif ($webcontrol->{sql}) { |
61
|
0
|
|
|
|
|
|
my ($query, @binds) = @{$webcontrol->{sql}}; |
|
0
|
|
|
|
|
|
|
62
|
0
|
0
|
|
|
|
|
$vals->{''} = '' unless $webcontrol->{notNull}; |
63
|
0
|
|
|
|
|
|
$vals->{$_->[0]} = $_->[1] for @{$self->q->db->getarray($query, @binds)}; |
|
0
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
0
|
|
|
|
|
|
foreach (keys %$vals) { |
68
|
0
|
0
|
|
|
|
|
if ($vals->{$_} eq $value) { |
69
|
0
|
|
|
|
|
|
push @$list, {'ITEM.LABEL' => $_, 'ITEM.VALUE' => $vals->{$_}, 'ITEM.SELECTED' => ' selected '}; |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
} else { |
72
|
0
|
|
|
|
|
|
push @$list, {'ITEM.LABEL' => $_, 'ITEM.VALUE' => $vals->{$_}}; |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
0
|
|
|
|
|
|
return $list; |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
#---------------------------------------------------------------------------------------- |
80
|
|
|
|
|
|
|
sub buildRadio { |
81
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
82
|
0
|
|
|
|
|
|
my $fieldname = shift; |
83
|
0
|
|
|
|
|
|
my $webcontrol = shift; |
84
|
0
|
|
|
|
|
|
my $webname = shift; |
85
|
0
|
|
|
|
|
|
my $webID = shift; |
86
|
0
|
|
|
|
|
|
my $value = shift; |
87
|
|
|
|
|
|
|
|
88
|
0
|
|
|
|
|
|
my $list = []; |
89
|
0
|
|
|
|
|
|
my $vals = {}; |
90
|
0
|
|
|
|
|
|
tie %$vals, 'Tie::IxHash'; |
91
|
|
|
|
|
|
|
|
92
|
0
|
0
|
|
|
|
|
if ($webcontrol->{values} ) { |
|
|
0
|
|
|
|
|
|
93
|
|
|
|
|
|
|
|
94
|
0
|
0
|
|
|
|
|
if (ref $webcontrol->{values} eq 'HASH') { |
|
|
0
|
|
|
|
|
|
95
|
0
|
|
|
|
|
|
$vals = $webcontrol->{values}; |
96
|
|
|
|
|
|
|
} elsif (ref $webcontrol->{values} eq 'ARRAY') { |
97
|
0
|
|
|
|
|
|
$vals->{$_} = $_ for @{$webcontrol->{values}}; |
|
0
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
} else { |
99
|
0
|
|
|
|
|
|
return; |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
} elsif ($webcontrol->{sql} ) { |
104
|
0
|
|
|
|
|
|
my ($query, @binds) = @{$webcontrol->{sql}}; |
|
0
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
|
106
|
0
|
|
|
|
|
|
$vals->{$_->[0]} = $_->[1] for @{$self->q->db->getarray($query, @binds)}; |
|
0
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
|
110
|
0
|
|
|
|
|
|
foreach (sort keys %$vals) { |
111
|
0
|
0
|
|
|
|
|
if ($vals->{$_} eq $value) { |
112
|
0
|
|
|
|
|
|
push @$list, { |
113
|
|
|
|
|
|
|
"ID.".$fieldname => $webID."-$_", |
114
|
|
|
|
|
|
|
'NAME.'.$fieldname => $webname, |
115
|
|
|
|
|
|
|
'VALUELABEL.'.$fieldname => $_, |
116
|
|
|
|
|
|
|
'VALUE.'.$fieldname => $vals->{$_}, |
117
|
|
|
|
|
|
|
'CHECKED.'.$fieldname => ' checked ', |
118
|
|
|
|
|
|
|
}; |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
} else { |
121
|
0
|
|
|
|
|
|
push @$list, { |
122
|
|
|
|
|
|
|
"ID.".$fieldname => $webID."-$_", |
123
|
|
|
|
|
|
|
'NAME.'.$fieldname => $webname, |
124
|
|
|
|
|
|
|
'VALUELABEL.'.$fieldname => $_, |
125
|
|
|
|
|
|
|
'VALUE.'.$fieldname => $vals->{$_}, |
126
|
|
|
|
|
|
|
}; |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
} |
129
|
|
|
|
|
|
|
} |
130
|
|
|
|
|
|
|
|
131
|
0
|
|
|
|
|
|
return $list; |
132
|
|
|
|
|
|
|
} |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
#---------------------------------------------------------------------------------------- |
135
|
|
|
|
|
|
|
sub contents { |
136
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
137
|
0
|
|
|
|
|
|
my %args = @_; |
138
|
|
|
|
|
|
|
|
139
|
0
|
|
|
|
|
|
my $incoming = $args{incoming}; |
140
|
0
|
|
|
|
|
|
my $template = $self->vars->{template}; |
141
|
0
|
|
|
|
|
|
my $widgetID = $self->widgetID; |
142
|
0
|
|
|
|
|
|
my $containerID = $self->vars->{containerId}; |
143
|
|
|
|
|
|
|
|
144
|
0
|
0
|
|
|
|
|
my $divopen = $args{nodiv} ? '' : " "; |
145
|
0
|
0
|
|
|
|
|
my $divclose = $args{nodiv} ? '' : ""; |
146
|
|
|
|
|
|
|
|
147
|
0
|
|
|
|
|
|
my $tmplvars; |
148
|
|
|
|
|
|
|
|
149
|
0
|
|
|
|
|
|
foreach my $control (@{$self->controls}) { |
|
0
|
|
|
|
|
|
|
150
|
0
|
|
|
|
|
|
my $fieldname = $control->{name}; |
151
|
0
|
|
|
|
|
|
my $value = $incoming->{$fieldname}; |
152
|
|
|
|
|
|
|
|
153
|
0
|
|
|
|
|
|
my $webname = "NAME.$fieldname"; |
154
|
0
|
|
|
|
|
|
my $webID = "ID.$fieldname"; |
155
|
|
|
|
|
|
|
|
156
|
0
|
|
|
|
|
|
my $type = $control->{type}; |
157
|
0
|
|
|
|
|
|
$tmplvars->{"LABEL.".$fieldname} = $control->{label}; |
158
|
|
|
|
|
|
|
|
159
|
0
|
|
|
|
|
|
$tmplvars->{$webname} = $fieldname; |
160
|
0
|
|
|
|
|
|
$tmplvars->{$webID} = $fieldname; |
161
|
|
|
|
|
|
|
|
162
|
0
|
0
|
|
|
|
|
if ($type eq 'select') { #build variables for web controls |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
163
|
0
|
|
|
|
|
|
$tmplvars->{"LOOP.".$fieldname} = $self->buildSelect($fieldname, $control, $value); |
164
|
|
|
|
|
|
|
} elsif ($type eq 'checkbox') { |
165
|
0
|
|
|
|
|
|
($tmplvars->{"VALUE.".$fieldname}, $tmplvars->{"CHECKED.".$fieldname}) = $self->buildCheckbox($fieldname, $control, $value); |
166
|
|
|
|
|
|
|
} elsif ($type eq 'radio') { |
167
|
0
|
|
|
|
|
|
$tmplvars->{"LOOP.".$fieldname} = $self->buildRadio($fieldname, $control, $webname, $webID, $value ); |
168
|
|
|
|
|
|
|
} else { |
169
|
0
|
|
|
|
|
|
$tmplvars->{"VALUE.".$fieldname} = $value; |
170
|
|
|
|
|
|
|
} |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
} |
173
|
|
|
|
|
|
|
|
174
|
0
|
|
|
|
|
|
my $jscontrollername = $widgetID."Controller"; |
175
|
|
|
|
|
|
|
|
176
|
0
|
|
|
|
|
|
my $selectObject = to_json([map {{name => $_->{name}, required => $_->{required}}} @{$self->controls}]); |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
|
178
|
0
|
|
|
|
|
|
my $javascript = <
|
179
|
|
|
|
|
|
|
var $jscontrollername = new controllerController('$widgetID', '$containerID', $selectObject); |
180
|
|
|
|
|
|
|
END |
181
|
|
|
|
|
|
|
|
182
|
0
|
0
|
|
|
|
|
if ($javascript) { |
183
|
0
|
0
|
|
|
|
|
$javascript = minify(input => $javascript) unless $self->q->config->noMinify; |
184
|
|
|
|
|
|
|
} |
185
|
|
|
|
|
|
|
|
186
|
0
|
|
|
|
|
|
my $js = $self->q->jswrap($javascript); |
187
|
|
|
|
|
|
|
|
188
|
0
|
|
|
|
|
|
return $divopen. |
189
|
|
|
|
|
|
|
$js. |
190
|
|
|
|
|
|
|
$self->q->template($template)->process($tmplvars). |
191
|
|
|
|
|
|
|
$divclose; |
192
|
|
|
|
|
|
|
} |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
#---------------------------------------------------------------------------------------- |
195
|
|
|
|
|
|
|
sub controls { |
196
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
197
|
|
|
|
|
|
|
|
198
|
0
|
|
|
|
|
|
return $self->{_controls}; |
199
|
|
|
|
|
|
|
} |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
#---------------------------------------------------------------------------------------- |
202
|
|
|
|
|
|
|
sub display { |
203
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
204
|
0
|
|
|
|
|
|
my %args = @_; |
205
|
|
|
|
|
|
|
|
206
|
0
|
|
|
|
|
|
return $self->contents(%args); |
207
|
|
|
|
|
|
|
} |
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
#---------------------------------------------------------------------------------------- |
210
|
|
|
|
|
|
|
sub new { |
211
|
0
|
|
|
0
|
0
|
|
my $class = shift; |
212
|
0
|
|
|
|
|
|
my $q = shift; |
213
|
0
|
|
|
|
|
|
my $vars = shift; |
214
|
|
|
|
|
|
|
|
215
|
0
|
|
|
|
|
|
my $widgetID = $vars->{id}; |
216
|
|
|
|
|
|
|
|
217
|
0
|
|
|
|
|
|
my $self = { |
218
|
|
|
|
|
|
|
_q => $q, |
219
|
|
|
|
|
|
|
_vars => $vars, |
220
|
|
|
|
|
|
|
_widgetID => $widgetID, |
221
|
|
|
|
|
|
|
_controls => $vars->{controls}, |
222
|
|
|
|
|
|
|
}; |
223
|
|
|
|
|
|
|
|
224
|
0
|
|
|
|
|
|
bless $self, $class; |
225
|
|
|
|
|
|
|
|
226
|
0
|
|
|
|
|
|
return $self; |
227
|
|
|
|
|
|
|
} |
228
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
1 |
230
|
|
|
|
|
|
|
|
231
|
|
|
|
|
|
|
__END__ |