Debian Codegraph @

Embperl/Form/ControlMultValue.pm

raw
1
2 ###################################################################################
3 #
4 # Embperl - Copyright (c) 1997-2008 Gerald Richter / ecos gmbh www.ecos.de
5 # Embperl - Copyright (c) 2008-2014 Gerald Richter
6 #
7 # You may distribute under the terms of either the GNU General Public
8 # License or the Artistic License, as specified in the Perl README file.
9 #
10 # THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
11 # IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
12 # WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
13 #
14 # $Id$
15 #
16 ###################################################################################
17
18 package Embperl::Form::ControlMultValue ;
19
20 use strict ;
21 use vars qw{%fdat} ;
22
23 use base 'Embperl::Form::Control' ;
24
25 use Embperl::Inline ;
26
27 # ---------------------------------------------------------------------------
28 #
29 # init - Init the new control
30 #
31
32
33 sub init
34
35 {
36 my ($self) = @_ ;
37
38 if ($self -> {datasrc})
39 {
40 my $name = $self -> {datasrc} ;
41 $name =~ s/[#\/].+$// ;
42 my $form = $self -> form ;
43 my $packages = $form -> get_datasrc_packages ;
44 $self -> {datasrcobj} = $form -> new_object ($packages, $name, $self, { datarsc => $self -> {datasrc}}) ;
45 }
46
47 $self -> SUPER::init ;
48
49 return $self ;
50 }
51
52 # ---------------------------------------------------------------------------
53 #
54 # constrain_attrs - returns attrs that might change the form layout
55 # if there value changes
56 #
57
58 sub constrain_attrs
59
60 {
61 my ($self, $req) = @_ ;
62
63 return if (!$self -> {datasrcobj}) ;
64
65 return $self -> {datasrcobj} -> constrain_attrs ($req) ;
66 }
67
68 # ---------------------------------------------------------------------------
69 #
70 # get_all_values - returns all values and options, including addtop and addbottom
71 #
72
73 sub get_all_values
74
75 {
76 my ($self, $req) = @_ ;
77
78 my $key = "all_values:$self" ;
79 my $v ;
80 return @$v if ($v = $req -> {$key}) ;
81
82 my $addtop = $self -> {addtop} ;
83 my $addbottom = $self -> {addbottom} ;
84
85 my $values ;
86 my $options ;
87 my $nocache = 0 ;
88
89 if ($self -> {datasrcobj})
90 {
91 my $key = "all_values_datasrc:$self->{datasrcobj}" ;
92 if (my $v = $req -> {$key})
93 {
94 ($values, $options) = @$v ;
95 }
96 else
97 {
98 ($values, $options) = $self -> {datasrcobj} -> get_values ($req, $self) ;
99 $options ||= $values ;
100 $nocache = $self -> {datasrcobj} -> values_no_cache ;
101 $req -> {$key} = [$values, $options] if (!$nocache) ;
102 }
103 }
104 else
105 {
106 $values = $self -> {values} ;
107 $options = $self -> {options} || $values ;
108 $options = $self -> form -> convert_options ($self, $self -> {values}, $options, $req)
109 if (!$self -> {showoptions}) ;
110 }
111
112 if (!$addtop && !$addbottom)
113 {
114 $req -> {$key} = [$values, $options] ;
115 return ($values, $options)
116 }
117 my @values ;
118 my @options ;
119 if ($addtop)
120 {
121 push @values, map { ref $_?$_ -> [0]:$_ } @$addtop ;
122 push @options, map { ref $_?$_ -> [1]:$_ } @$addtop ;
123 }
124
125 if ($values)
126 {
127 if ($addtop && $values -> [0] eq '' && $options -> [0] eq '---')
128 {
129 push @values, @{$values}[1..$#$values] ;
130 push @options, @{$options}[1..$#$options] ;
131 }
132 else
133 {
134 push @values, @$values ;
135 push @options, @$options ;
136 }
137 }
138
139 if ($addbottom)
140 {
141 push @values, map { $_ -> [0] } @$addbottom ;
142 push @options, map { $_ -> [1] } @$addbottom ;
143 }
144
145 $req -> {$key} = [\@values, \@options] if (!$nocache) ;
146 return (\@values, \@options) ;
147 }
148
149 # ---------------------------------------------------------------------------
150 #
151 # get_values - returns values and options, possibly filter applied
152 #
153
154 sub get_values
155
156 {
157 my ($self, $req) = @_ ;
158
159
160 my ($values, $options) = $self -> get_all_values ($req) ;
161 my $filter = $self -> {filter} ;
162 return ($values, $options) if (!$filter) ;
163
164 my @values ;
165 my @options ;
166 my $i = 0 ;
167 foreach (@$values)
168 {
169 if (/$filter/)
170 {
171 push @values, $_ ;
172 push @options, $options -> [$i] ;
173 }
174 $i++ ;
175 }
176 return (\@values, \@options) ;
177 }
178
179
180 # ---------------------------------------------------------------------------
181 #
182 # get_datasource_controls - returns additional controls provided by the
183 # datasource object e.g. a browse button
184 #
185
186 sub get_datasource_controls
187
188 {
189 my ($self, $req) = @_ ;
190
191 return $self -> {datasrcobj} -> get_datasource_controls ($req, $self) if ($self -> {datasrcobj}) ;
192 return ;
193 }
194
195 # ---------------------------------------------------------------------------
196 #
197 # get_id_from_value - returns id for a given value
198 #
199
200 sub get_id_from_value
201
202 {
203 my ($self, $value) = @_ ;
204
205 return if (!$self -> {datasrcobj}) ;
206 return $self -> {datasrcobj} -> get_id_from_value ($value) ;
207 }
208
209 # ---------------------------------------------------------------------------
210 #
211 # get_option_form_value - returns the option for a given value
212 #
213 # in $value value
214 # ret option
215 #
216
217 sub get_option_from_value
218
219 {
220 my ($self, $value, $req) = @_ ;
221
222 my $addtop = $self -> {addtop} ;
223 if ($addtop)
224 {
225 foreach (@$addtop)
226 {
227 if ($_ -> [0] eq $value)
228 {
229 return $_ -> [1] ;
230 }
231 }
232 }
233
234 if ($self->{datasrc})
235 {
236 my $option = $self -> {datasrcobj} -> get_option_from_value ($value, $req, $self) ;
237
238 return $option if (defined ($option)) ;
239 }
240 elsif (ref $self -> {values})
241 {
242 my $i = 0 ;
243 foreach (@{$self -> {values}})
244 {
245 if ($_ eq $value)
246 {
247 my $options = [$self -> {options}[$i] || $value] ;
248 $options = $self -> form -> convert_options ($self, [$value], $options, $req)
249 if (!$self -> {showoptions}) ;
250 return $options -> [0] ;
251 }
252 $i++ ;
253 }
254 }
255
256 my $addbottom = $self -> {addbottom} ;
257 if ($addbottom)
258 {
259 foreach (@$addbottom)
260 {
261 if ($_ -> [0] eq $value)
262 {
263 return $_ -> [1] ;
264 }
265 }
266 }
267
268 return ;
269 }
270
271 # ---------------------------------------------------------------------------
272 #
273 # get_active_id - get the id of the value which is currently active
274 #
275
276 sub get_active_id
277
278 {
279 my ($self, $req) = @_ ;
280
281 my $key = "active_id:$self" ;
282 my $id ;
283 return $id if ($id = $req -> {$key}) ;
284
285 my ($values, $options) = $self -> get_values ($req) ;
286 my $name = $self -> {name} ;
287 my $dataval = $fdat{$name} || $values -> [0] ;
288 my $activeid ;
289
290 my $i = 0 ;
291 foreach my $val (@$values)
292 {
293 if ($val eq $dataval)
294 {
295 $activeid = "$name-$i" ;
296 last ;
297 }
298 $i++ ;
299 }
300
301 return $req -> {$key} = $activeid ;
302 }
303
304 # ---------------------------------------------------------------------------
305 #
306 # is_with_id - returns true if the control shows something that has an internal id
307 #
308
309 sub is_with_id
310
311 {
312 my ($self, $req) = @_ ;
313
314 return 1 ;
315 }
316
317 # ------------------------------------------------------------------------------------------
318 #
319 # get_display_text - returns the text that should be displayed
320 #
321
322 sub get_display_text
323 {
324 my ($self, $req, $value) = @_ ;
325
326 $value = $self -> get_value ($req) if (!defined ($value)) ;
327 $value = $self -> get_option_from_value ($value, $req) ;
328
329 return $value ;
330 }
331
332 # ---------------------------------------------------------------------------
333 #
334 # init_markup - add any dynamic markup to the form data
335 #
336
337 sub init_markup
338
339 {
340 my ($self, $req, $parentctl, $method) = @_ ;
341
342 return if (!$self -> is_readonly($req) && (! $parentctl || ! $parentctl -> is_readonly($req))) ;
343
344 my $val = $self -> get_value ($req) ;
345 if ($val ne '')
346 {
347 my $name = $self -> {name} ;
348 my $fdat = $req -> {docdata} || \%Embperl::fdat ;
349 $fdat -> {'_opt_' . $name} = $self -> get_option_from_value ($val, $req) ;
350 $fdat -> {'_id_' . $name} = $self -> get_id_from_value ($val, $req) ;
351 }
352 }
353
354 1 ;
355
356 # damit %fdat etc definiert ist
357 __EMBPERL__
358
359
360 [# ---------------------------------------------------------------------------
361 #
362 # show_control_readonly - output the control as readonly
363 #]
364
365 [$ sub show_control_readonly ($self, $req, $value)
366
367 my $text = $self -> get_display_text ($req, $value) ;
368 my $id = $self -> get_id_from_value ($val, $req) ;
369 my $name = $self -> {force_name} || $self -> {name} ;
370 $]
371 <div [+ do { local $escmode = 0 ; $self -> get_std_control_attr($req, '', 'readonly', 'ef-control-with-id') } +] _ef_divname="_opt_[+ $name +]">[+ $text +]</div>
372 [$ if $self->{hidden} $]
373 <input type="hidden" name="[+ $name +]" value="[+ $value +]">
374 [$endif$]
375 <input type="hidden" name="_id_[+ $name +]" value="[+ $id +]">
376 [$endsub$]
377
378 [# ---------------------------------------------------------------------------
379 #
380 # show_control_addons - output additional things after the control
381 #]
382
383 [$ sub show_control_addons ($self, $req)
384
385 my $datasrc_ctrls ;
386 $datasrc_ctrls = $self -> get_datasource_controls ($req)
387 unless ($self -> {no_datasource_controls}) ;
388
389 $][$if $datasrc_ctrls $]
390 [$foreach my $ctrl (@$datasrc_ctrls) $]
391 <a class="cControlAddonA" href="[+ $ctrl->{href} +]" onClick="[+ $ctrl->{onclick} +]">[$if $ctrl -> {icon} $]<img class="cControlAddonImg" src="[+ $ctrl -> {icon} +]" title="[+ $ctrl -> {text} +]" alt="[+ $ctrl -> {text} +]">[$else$][+ $ctrl -> {text} +][$endif$]</a>
392 [$endforeach$]
393 [$endif$]
394 [$endsub$]
395
396
397 __END__
398
399 =pod
400
401 =head1 NAME
402
403 Embperl::Form::ControlMultValue - Base class for controls inside
404 an Embperl Form which have multiple values to select from, like
405 a select box or radio buttons.
406
407
408 =head1 SYNOPSIS
409
410 Do not use directly, instead derive a class
411
412 =head1 DESCRIPTION
413
414 This class is not used directly, it is used as a base class for
415 all controls which have multiple values to select from inside
416 an Embperl Form. It provides a set of methods
417 that could be overwritten to customize the behaviour of your controls.
418
419 =head1 METHODS
420
421 =head2 get_values
422
423 returns the values and options
424
425 =head2 get_datasource_controls
426
427 returns additional controls provided by the
428 datasource object e.g. a browse button
429
430 =head2 get_active_id
431
432 get the id of the value which is currently active
433
434 =head1 PARAMETERS
435
436 =head3 values
437
438 Arrayref with the values to select from. This is what gets
439 submited back to the server.
440
441 =head3 options
442
443 Arrayref with the options to select from. This is what the user sees.
444
445 =head3 datasrc
446
447 Name of an class which provides the values for the
448 values and options parameters. Either a full package name or
449 a name, in which case all packages which are returned
450 by Embperl::Form::get_datasrc_packages are searched.
451 Everything after '#' is ignored and can be used by the
452 DataSource module to do further selections.
453
454 =head3 no_datasource_controls
455
456 Disables the output of the additional controls
457
458 =head1 AUTHOR
459
460 G. Richter (richter at embperl dot org)
461
462 =head1 SEE ALSO
463
464 perl(1), Embperl, Embperl::Form, Embperl::From::Control, Embperl::Form::DataSource
465