| 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::Control::mult ; |
| 19 |
|
| 20 |
use strict ; |
| 21 |
use base 'Embperl::Form::Control::grid' ; |
| 22 |
|
| 23 |
use vars qw{%fdat $epreq} ; |
| 24 |
|
| 25 |
use Embperl::Inline ; |
| 26 |
|
| 27 |
# --------------------------------------------------------------------------- |
| 28 |
# |
| 29 |
# new - create a new control |
| 30 |
# |
| 31 |
|
| 32 |
|
| 33 |
sub new |
| 34 |
|
| 35 |
{ |
| 36 |
my ($class, $args) = @_ ; |
| 37 |
|
| 38 |
my $self = Embperl::Form::ControlMultValue -> new($args) ; |
| 39 |
bless $self, $class ; |
| 40 |
|
| 41 |
$self -> init ; |
| 42 |
|
| 43 |
return $self ; |
| 44 |
} |
| 45 |
|
| 46 |
# --------------------------------------------------------------------------- |
| 47 |
# |
| 48 |
# init - init the new control |
| 49 |
# |
| 50 |
|
| 51 |
sub init |
| 52 |
|
| 53 |
{ |
| 54 |
my ($self) = @_ ; |
| 55 |
|
| 56 |
my $form = $self -> form ; |
| 57 |
$self -> {fields} ||= [$self -> {field}] ; |
| 58 |
$self -> {class} ||= 'ef-control-mult' ; |
| 59 |
my $options = $form -> {options} ; |
| 60 |
$form -> new_controls ($self -> {fields}, $options, undef, undef, $options -> {masks}, $options -> {defaults}, 1) ; |
| 61 |
|
| 62 |
return $self ; |
| 63 |
} |
| 64 |
|
| 65 |
# ------------------------------------------------------------------------------------------ |
| 66 |
# |
| 67 |
# init_data - daten aufteilen |
| 68 |
# |
| 69 |
|
| 70 |
sub init_data |
| 71 |
{ |
| 72 |
my ($self, $req) = @_ ; |
| 73 |
|
| 74 |
my $fdat = $req -> {docdata} || \%fdat ; |
| 75 |
my $name = $self->{name} ; |
| 76 |
my @entries = ref $fdat->{$name} eq 'ARRAY'?@{$fdat->{$name}}:split("\t",$fdat->{$name}); |
| 77 |
|
| 78 |
my $field = $self -> {fields}[0] ; |
| 79 |
my $i = 0 ; |
| 80 |
foreach my $entry (@entries) |
| 81 |
{ |
| 82 |
$fdat->{"__${name}__$i"} = $entry ; |
| 83 |
if ($field -> can ('init_data')) |
| 84 |
{ |
| 85 |
local $field->{name} = "__${name}__$i" ; |
| 86 |
local $field -> {fullid} = "$self->{fullid}__$i" ; |
| 87 |
$field -> init_data ($req, $self) ; |
| 88 |
} |
| 89 |
|
| 90 |
$i++ ; |
| 91 |
} |
| 92 |
$fdat->{"__${name}_max"} = $i?$i:1; |
| 93 |
} |
| 94 |
|
| 95 |
# ------------------------------------------------------------------------------------------ |
| 96 |
# |
| 97 |
# prepare_fdat - daten zusammenfuehren |
| 98 |
# |
| 99 |
|
| 100 |
sub prepare_fdat |
| 101 |
{ |
| 102 |
my ($self, $req) = @_ ; |
| 103 |
my $fdat = $req -> {form} || \%fdat ; |
| 104 |
my $name = $self->{name} ; |
| 105 |
my $max = $fdat->{"__${name}_max"} || 1 ; |
| 106 |
|
| 107 |
my $field = $self -> {fields}[0] ; |
| 108 |
my @rows; |
| 109 |
my $val ; |
| 110 |
for (my $i = 0; $i < $max; $i++) |
| 111 |
{ |
| 112 |
if ((ref ($field) =~ /::/) && $field -> can ('prepare_fdat')) |
| 113 |
{ |
| 114 |
local $field->{name} = "__${name}__$i" ; |
| 115 |
local $field -> {fullid} = "$self->{fullid}__$i" ; |
| 116 |
$field -> prepare_fdat ($req) ; |
| 117 |
} |
| 118 |
|
| 119 |
$val = $fdat->{"__${name}__$i"} ; |
| 120 |
push @rows, $val if ($val ne '') ; |
| 121 |
} |
| 122 |
$fdat->{$name} = \@rows if (@rows > 1 || defined ($rows[0]) || $fdat->{"__${name}_max"} > 0) ; |
| 123 |
|
| 124 |
foreach my $key (keys %$fdat) |
| 125 |
{ |
| 126 |
delete $fdat->{$key} if ($key =~ /^__\Q$name\E__/) ; |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
# ------------------------------------------------------------------------------------------ |
| 131 |
# |
| 132 |
# get_display_text - returns the text that should be displayed |
| 133 |
# |
| 134 |
|
| 135 |
sub get_display_text |
| 136 |
{ |
| 137 |
my ($self, $req, $value) = @_ ; |
| 138 |
|
| 139 |
my $field = $self -> {fields}[0] ; |
| 140 |
return if (!$field) ; |
| 141 |
|
| 142 |
return $field -> get_display_text ($req, $value) ; |
| 143 |
} |
| 144 |
|
| 145 |
# ------------------------------------------------------------------------------------------ |
| 146 |
|
| 147 |
sub show |
| 148 |
{ |
| 149 |
$_[0] -> {fullid} = $_[1] -> {uuid} . '_' . $_[0] -> {id} ; |
| 150 |
Embperl::Form::Control::show (@_) |
| 151 |
} |
| 152 |
|
| 153 |
#sub show_control_readonly { my $self = shift ; $self -> show_control (@_) } |
| 154 |
|
| 155 |
1 ; |
| 156 |
|
| 157 |
__EMBPERL__ |
| 158 |
|
| 159 |
[# --------------------------------------------------------------------------- |
| 160 |
# |
| 161 |
# show - output the whole control including the label |
| 162 |
#] |
| 163 |
|
| 164 |
[$sub show ($self, $req) |
| 165 |
|
| 166 |
$fdat{$self -> {name}} = $self -> {default} if ($fdat{$self -> {name}} eq '' && exists ($self -> {default})) ; |
| 167 |
my $span = 0 ; |
| 168 |
|
| 169 |
$]<table class="ef-element ef-element-width-[+ $self -> {width_percent} +] ef-element-[+ $self -> {type} +] [+ $self -> {state} +]" |
| 170 |
[$ if (!$self -> is_readonly($req) ) $]_ef_attach="ef_mult"[$endif$] > |
| 171 |
<tr> |
| 172 |
[- |
| 173 |
$span += $self -> show_label_cell ($req, $span); |
| 174 |
$self -> show_control_cell ($req, $span) ; |
| 175 |
-] |
| 176 |
</tr> |
| 177 |
</table>[$ |
| 178 |
endsub $] |
| 179 |
|
| 180 |
[# --------------------------------------------------------------------------- |
| 181 |
# |
| 182 |
# show_control - output the control |
| 183 |
#] |
| 184 |
|
| 185 |
[$ sub show_control ($self, $req) |
| 186 |
|
| 187 |
my $name = $self -> {name} ; |
| 188 |
my $max = $fdat{"__${name}_max"} ||= 1 ; |
| 189 |
|
| 190 |
my $span = ($self->{width_percent}) ; |
| 191 |
my $nsprefix = $self -> form -> {jsnamespace} ; |
| 192 |
my $jsname = $name ; |
| 193 |
$jsname =~ s/[^a-zA-Z0-9]/_/g ; |
| 194 |
$jsname .= 'Grid' ; |
| 195 |
$] |
| 196 |
[$ if ($max == 1 && $self -> is_readonly($req) ) $] |
| 197 |
[- |
| 198 |
my $field = $self -> {fields}[0] ; |
| 199 |
local $field -> {name} = "__${name}__0" ; |
| 200 |
$field -> show_control_readonly ($req) ; |
| 201 |
-] |
| 202 |
[$else$] |
| 203 |
|
| 204 |
[- |
| 205 |
$fdat{$name} = $self -> {default} if ($fdat{$name} eq '' && exists ($self -> {default})) ; |
| 206 |
my $span = 0 ; |
| 207 |
-] |
| 208 |
<div [+ do { local $escmode = 0 ; $self -> get_std_control_attr($req) } +]> |
| 209 |
<input type="hidden" class="ef-control-mult-max" name="__[+ $self -> {name} +]_max" > |
| 210 |
<table class="ef-control-mult-table" > |
| 211 |
[- $self -> show_grid_table ($req) ; -] |
| 212 |
</table> |
| 213 |
<table class="ef-control-mult-newrow" style="display: none"> |
| 214 |
[- |
| 215 |
local $req -> {epf_no_script} = 1 ; |
| 216 |
$self -> show_grid_table_row ($req, '%row%') ; |
| 217 |
-] |
| 218 |
</table> |
| 219 |
</div> |
| 220 |
[$endif$] |
| 221 |
[$endsub$] |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
[# --------------------------------------------------------------------------- |
| 226 |
# |
| 227 |
# show_grid_table_row Erzeugt eine Grid-Tabelle-Zeile |
| 228 |
#] |
| 229 |
|
| 230 |
[$ sub show_grid_table_row ($self, $req, $i) |
| 231 |
|
| 232 |
$field = $self -> {fields}[0] ; |
| 233 |
$id = $self -> {fullid}; |
| 234 |
$name = $self -> {name} ; |
| 235 |
my $jsname = $name ; |
| 236 |
$jsname =~ s/[^a-zA-Z0-9]/_/g ; |
| 237 |
$jsname .= 'Grid' ; |
| 238 |
my $ro = $self -> is_readonly ($req) ; |
| 239 |
$] |
| 240 |
|
| 241 |
<tr class="cGridRow" id="[+ "${id}_mult-row-$i" +]"> |
| 242 |
|
| 243 |
<td class="[+ $self -> {class} +]Cell"> |
| 244 |
[- |
| 245 |
local $field -> {name} = "__${name}__$i" ; |
| 246 |
if ($ro) |
| 247 |
{ |
| 248 |
$field -> show_control_readonly ($req) |
| 249 |
} |
| 250 |
else |
| 251 |
{ |
| 252 |
$field -> show_control ($req) |
| 253 |
} |
| 254 |
-] |
| 255 |
</td> |
| 256 |
</tr> |
| 257 |
[$ endsub $] |
| 258 |
|
| 259 |
|
| 260 |
[$ sub show_label_icon ($self) |
| 261 |
$name = $self -> {name} ; |
| 262 |
my $jsname = $name ; |
| 263 |
$jsname =~ s/[^a-zA-Z0-9]/_/g ; |
| 264 |
$jsname .= 'Grid' ; |
| 265 |
|
| 266 |
|
| 267 |
$] |
| 268 |
[$if (! $self -> is_readonly ($req)) $] |
| 269 |
<span class="ui-icon ui-icon-circle-plus ef-icon ef-control-mult-add" title="Zeile Hinzufügen"></span> |
| 270 |
<span class="ui-icon ui-icon-circle-minus ef-icon ef-control-mult-del" title="Zeile Löschen"></span> |
| 271 |
|
| 272 |
[$endif$] |
| 273 |
[$endsub$] |
| 274 |
|
| 275 |
[# --------------------------------------------------------------------------- |
| 276 |
# |
| 277 |
# show_grid_table Erzeugt eine Grid-Tabelle |
| 278 |
#] |
| 279 |
|
| 280 |
[$ sub show_grid_table ($self, $req) |
| 281 |
my $name = $self->{name} ; |
| 282 |
my $fields = $self -> {fields} ; |
| 283 |
my $id = $self -> {fullid}; |
| 284 |
my $i = 0 ; |
| 285 |
my $max = $fdat{"__${name}_max"} || 1 ; |
| 286 |
$] |
| 287 |
|
| 288 |
[* for ($i = 0; $i < $max ; $i++ ) { *] |
| 289 |
[- $self -> show_grid_table_row ($req, $i) ; -] |
| 290 |
[* } *] |
| 291 |
|
| 292 |
[$endsub$] |
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
__END__ |
| 297 |
|
| 298 |
=pod |
| 299 |
|
| 300 |
=head1 NAME |
| 301 |
|
| 302 |
Embperl::Form::Control::grid - A grid control inside an Embperl Form |
| 303 |
|
| 304 |
|
| 305 |
=head1 SYNOPSIS |
| 306 |
|
| 307 |
|
| 308 |
=head1 DESCRIPTION |
| 309 |
|
| 310 |
Used to create a grid control inside an Embperl Form. |
| 311 |
See Embperl::Form on how to specify parameters. |
| 312 |
|
| 313 |
=head2 PARAMETER |
| 314 |
|
| 315 |
=head3 type |
| 316 |
|
| 317 |
Needs to be 'grid' |
| 318 |
|
| 319 |
=head3 fields |
| 320 |
|
| 321 |
Array ref with field definitions |
| 322 |
|
| 323 |
=head3 header_bottom |
| 324 |
|
| 325 |
If grid has more rows as given in this parameter, |
| 326 |
a header line is also displayed at the bottom of the |
| 327 |
grid. Default is 10. Set to -1 to always get a |
| 328 |
header at the bottom. |
| 329 |
|
| 330 |
=head2 Example |
| 331 |
|
| 332 |
{ |
| 333 |
name => 'provider-path', |
| 334 |
text => 'Suchpfad', |
| 335 |
type => 'grid', |
| 336 |
fields => |
| 337 |
[ |
| 338 |
{ name => 'active', text => 'Aktiv', type => 'checkbox', width => '30' }, |
| 339 |
{ name => 'path', text => 'Pfad' }, |
| 340 |
], |
| 341 |
}, |
| 342 |
|
| 343 |
=head1 Author |
| 344 |
|
| 345 |
G. Richter (richter at embperl dot org) |
| 346 |
|
| 347 |
=head1 See Also |
| 348 |
|
| 349 |
perl(1), Embperl, Embperl::Form |
| 350 |
|
| 351 |
|