| 1 | |||
| 2 | ################################################################################### | ||
| 3 | # | ||
| 4 | # Embperl - Copyright (c) 1997-2008 Gerald Richter / ecos gmbh www.ecos.de | ||
| 5 | # Embperl - Copyright (c) 2008-2015 Gerald Richter | ||
| 6 | # Embperl - Copyright (c) 2015-2023 actevy.io | ||
| 7 | # | ||
| 8 | # You may distribute under the terms of either the GNU General Public | ||
| 9 | # License or the Artistic License, as specified in the Perl README file. | ||
| 10 | # | ||
| 11 | # THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR | ||
| 12 | # IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED | ||
| 13 | # WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. | ||
| 14 | # | ||
| 15 | # $Id$ | ||
| 16 | # | ||
| 17 | ################################################################################### | ||
| 18 | |||
| 19 | package Embperl::Form::Control::mult ; | ||
| 20 | |||
| 21 | use strict ; | ||
| 22 | use base 'Embperl::Form::Control::grid' ; | ||
| 23 | |||
| 24 | use vars qw{%fdat $epreq} ; | ||
| 25 | |||
| 26 | use Embperl::Inline ; | ||
| 27 | |||
| 28 | # --------------------------------------------------------------------------- | ||
| 29 | # | ||
| 30 | # new - create a new control | ||
| 31 | # | ||
| 32 | |||
| 33 | |||
| 34 | sub new | ||
| 35 | |||
| 36 | { | ||
| 37 | my ($class, $args) = @_ ; | ||
| 38 | |||
| 39 | my $self = Embperl::Form::ControlMultValue -> new($args) ; | ||
| 40 | bless $self, $class ; | ||
| 41 | |||
| 42 | $self -> init ; | ||
| 43 | |||
| 44 | return $self ; | ||
| 45 | } | ||
| 46 | |||
| 47 | # --------------------------------------------------------------------------- | ||
| 48 | # | ||
| 49 | # init - init the new control | ||
| 50 | # | ||
| 51 | |||
| 52 | sub init | ||
| 53 | |||
| 54 | { | ||
| 55 | my ($self) = @_ ; | ||
| 56 | |||
| 57 | my $form = $self -> form ; | ||
| 58 | $self -> {fields} ||= [$self -> {field}] ; | ||
| 59 | $self -> {class} ||= 'ef-control-mult' ; | ||
| 60 | my $options = $form -> {options} ; | ||
| 61 | $form -> new_controls ($self -> {fields}, $options, undef, undef, $options -> {masks}, $options -> {defaults}, 1) ; | ||
| 62 | |||
| 63 | return $self ; | ||
| 64 | } | ||
| 65 | |||
| 66 | # ------------------------------------------------------------------------------------------ | ||
| 67 | # | ||
| 68 | # init_data - daten aufteilen | ||
| 69 | # | ||
| 70 | |||
| 71 | sub init_data | ||
| 72 | { | ||
| 73 | my ($self, $req) = @_ ; | ||
| 74 | |||
| 75 | my $fdat = $req -> {docdata} || \%fdat ; | ||
| 76 | my $name = $self->{name} ; | ||
| 77 | my @entries = ref $fdat->{$name} eq 'ARRAY'?@{$fdat->{$name}}:split("\t",$fdat->{$name}); | ||
| 78 | |||
| 79 | my $field = $self -> {fields}[0] ; | ||
| 80 | my $i = 0 ; | ||
| 81 | my @opt ; | ||
| 82 | my @data ; | ||
| 83 | foreach my $entry (@entries) | ||
| 84 | { | ||
| 85 | $fdat->{"__${name}__$i"} = $entry ; | ||
| 86 | if (1) #$field -> can ('init_data')) | ||
| 87 | { | ||
| 88 | local $field->{name} = "__${name}__$i" ; | ||
| 89 | local $field -> {fullid} = "$self->{fullid}__$i" ; | ||
| 90 | $field -> init_data ($req, $self) if ($field -> can ('init_data')) ; | ||
| 91 | push @data, $fdat->{$field->{name}} ; | ||
| 92 | push @opt, $fdat->{'_opt_' . $field->{name}} // $self -> get_display_text ($req, $entry) ; | ||
| 93 | } | ||
| 94 | |||
| 95 | $i++ ; | ||
| 96 | } | ||
| 97 | $fdat->{"__${name}_max"} = $i?$i:1; | ||
| 98 | $fdat->{$name} //= join ("\t", @data); | ||
| 99 | $fdat->{'_opt_' . $name} //= join (", ", @opt); | ||
| 100 | } | ||
| 101 | |||
| 102 | # ------------------------------------------------------------------------------------------ | ||
| 103 | # | ||
| 104 | # prepare_fdat - daten zusammenfuehren | ||
| 105 | # | ||
| 106 | |||
| 107 | sub prepare_fdat | ||
| 108 | { | ||
| 109 | my ($self, $req) = @_ ; | ||
| 110 | my $fdat = $req -> {form} || \%fdat ; | ||
| 111 | my $name = $self->{name} ; | ||
| 112 | my $max = $fdat->{"__${name}_max"} || 1 ; | ||
| 113 | |||
| 114 | my $field = $self -> {fields}[0] ; | ||
| 115 | my $fieldprep = (ref ($field) =~ /::/) && $field -> can ('prepare_fdat') ; | ||
| 116 | my @rows; | ||
| 117 | my $val ; | ||
| 118 | if ($fieldprep && $fdat -> {$name} eq '') | ||
| 119 | { | ||
| 120 | if (exists ($fdat -> {"_opt_$name"})) | ||
| 121 | { | ||
| 122 | my @vals = split (/\s*,\s*/, $fdat -> {"_opt_$name"}) ; | ||
| 123 | $max = @vals ; | ||
| 124 | for (my $i = 0; $i < $max; $i++) | ||
| 125 | { | ||
| 126 | $val = $fdat->{"_opt___${name}__$i"} = shift @vals ; | ||
| 127 | } | ||
| 128 | } | ||
| 129 | elsif (exists ($fdat -> {"_id_$name"})) | ||
| 130 | { | ||
| 131 | my @vals = split (/\s*,\s*/, $fdat -> {"_id_$name"}) ; | ||
| 132 | $max = @vals ; | ||
| 133 | for (my $i = 0; $i < $max; $i++) | ||
| 134 | { | ||
| 135 | $val = $fdat->{"_id___${name}__$i"} = shift @vals ; | ||
| 136 | } | ||
| 137 | } | ||
| 138 | } | ||
| 139 | |||
| 140 | |||
| 141 | for (my $i = 0; $i < $max; $i++) | ||
| 142 | { | ||
| 143 | if ($fieldprep) | ||
| 144 | { | ||
| 145 | local $field->{name} = "__${name}__$i" ; | ||
| 146 | local $field -> {fullid} = "$self->{fullid}__$i" ; | ||
| 147 | $field -> prepare_fdat ($req) ; | ||
| 148 | } | ||
| 149 | |||
| 150 | $val = $fdat->{"__${name}__$i"} ; | ||
| 151 | push @rows, $val if ($val ne '') ; | ||
| 152 | } | ||
| 153 | $fdat->{$name} = \@rows if (@rows > 1 || defined ($rows[0]) || $fdat->{"__${name}_max"} > 0) ; | ||
| 154 | |||
| 155 | foreach my $key (keys %$fdat) | ||
| 156 | { | ||
| 157 | delete $fdat->{$key} if ($key =~ /^__\Q$name\E__/) ; | ||
| 158 | } | ||
| 159 | } | ||
| 160 | |||
| 161 | # ------------------------------------------------------------------------------------------ | ||
| 162 | # | ||
| 163 | # get_display_text - returns the text that should be displayed | ||
| 164 | # | ||
| 165 | |||
| 166 | sub get_display_text | ||
| 167 | { | ||
| 168 | my ($self, $req, $value) = @_ ; | ||
| 169 | |||
| 170 | my $field = $self -> {fields}[0] ; | ||
| 171 | return if (!$field) ; | ||
| 172 | |||
| 173 | return $field -> get_display_text ($req, $value) ; | ||
| 174 | } | ||
| 175 | |||
| 176 | # --------------------------------------------------------------------------- | ||
| 177 | # | ||
| 178 | # get_option_form_value - returns the option for a given value | ||
| 179 | # | ||
| 180 | # in $value value | ||
| 181 | # ret option | ||
| 182 | # | ||
| 183 | |||
| 184 | sub get_option_from_value | ||
| 185 | |||
| 186 | { | ||
| 187 | my ($self, $value, $req) = @_ ; | ||
| 188 | |||
| 189 | my $field = $self -> {fields}[0] ; | ||
| 190 | return if (!$field) ; | ||
| 191 | |||
| 192 | return $field -> get_option_from_value ($value, $req) ; | ||
| 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, $req) = @_ ; | ||
| 204 | |||
| 205 | my $field = $self -> {fields}[0] ; | ||
| 206 | return if (!$field) ; | ||
| 207 | |||
| 208 | return $field -> get_id_from_value ($value, $req) ; | ||
| 209 | } | ||
| 210 | |||
| 211 | # ------------------------------------------------------------------------------------------ | ||
| 212 | |||
| 213 | sub show | ||
| 214 | { | ||
| 215 | $_[0] -> {fullid} = $_[1] -> {uuid} . '_' . $_[0] -> {id} ; | ||
| 216 | Embperl::Form::Control::show (@_) | ||
| 217 | } | ||
| 218 | |||
| 219 | #sub show_control_readonly { my $self = shift ; $self -> show_control (@_) } | ||
| 220 | |||
| 221 | 1 ; | ||
| 222 | |||
| 223 | __EMBPERL__ | ||
| 224 | |||
| 225 | [# --------------------------------------------------------------------------- | ||
| 226 | # | ||
| 227 | # show - output the whole control including the label | ||
| 228 | #] | ||
| 229 | |||
| 230 | [$sub show ($self, $req) | ||
| 231 | |||
| 232 | $fdat{$self -> {name}} = $self -> {default} if ($fdat{$self -> {name}} eq '' && exists ($self -> {default})) ; | ||
| 233 | my $span = 0 ; | ||
| 234 | |||
| 235 | $]<table class="ef-element ef-element-width-[+ $self -> {width_percent} +] ef-element-[+ $self -> {type} +] [+ ' ' . $self -> {state} +] ef-attach-always" | ||
| 236 | [$ if (!$self -> is_readonly($req) ) $]_ef_attach="ef_mult"[$endif$] > | ||
| 237 | <tr> | ||
| 238 | [- | ||
| 239 | $span += $self -> show_label_cell ($req, $span); | ||
| 240 | $self -> show_control_cell ($req, $span) ; | ||
| 241 | -] | ||
| 242 | </tr> | ||
| 243 | </table>[$ | ||
| 244 | endsub $] | ||
| 245 | |||
| 246 | [# --------------------------------------------------------------------------- | ||
| 247 | # | ||
| 248 | # show_control - output the control | ||
| 249 | #] | ||
| 250 | |||
| 251 | [$ sub show_control ($self, $req) | ||
| 252 | |||
| 253 | my $name = $self -> {name} ; | ||
| 254 | my $max = $fdat{"__${name}_max"} ||= 1 ; | ||
| 255 | |||
| 256 | my $span = ($self->{width_percent}) ; | ||
| 257 | my $nsprefix = $self -> form -> {jsnamespace} ; | ||
| 258 | my $jsname = $name ; | ||
| 259 | $jsname =~ s/[^a-zA-Z0-9]/_/g ; | ||
| 260 | $jsname .= 'Grid' ; | ||
| 261 | $] | ||
| 262 | [$ if ($max == 1 && $self -> is_readonly($req) ) $] | ||
| 263 | [- | ||
| 264 | my $field = $self -> {fields}[0] ; | ||
| 265 | local $field -> {name} = "__${name}__0" ; | ||
| 266 | $field -> show_control_readonly ($req) ; | ||
| 267 | -] | ||
| 268 | [$else$] | ||
| 269 | |||
| 270 | [- | ||
| 271 | $fdat{$name} = $self -> {default} if ($fdat{$name} eq '' && exists ($self -> {default})) ; | ||
| 272 | my $span = 0 ; | ||
| 273 | -] | ||
| 274 | <div [+ do { local $escmode = 0 ; $self -> get_std_control_attr($req) } +]> | ||
| 275 | <input type="hidden" class="ef-control-mult-max" name="__[+ $self -> {name} +]_max" > | ||
| 276 | <table class="ef-control-mult-table" > | ||
| 277 | [- $self -> show_grid_table ($req) ; -] | ||
| 278 | </table> | ||
| 279 | <table class="ef-control-mult-newrow" style="display: none"> | ||
| 280 | [- | ||
| 281 | local $req -> {epf_no_script} = 1 ; | ||
| 282 | $self -> show_grid_table_row ($req, '%row%') ; | ||
| 283 | -] | ||
| 284 | </table> | ||
| 285 | </div> | ||
| 286 | [$endif$] | ||
| 287 | [$endsub$] | ||
| 288 | |||
| 289 | |||
| 290 | |||
| 291 | [# --------------------------------------------------------------------------- | ||
| 292 | # | ||
| 293 | # show_grid_table_row Erzeugt eine Grid-Tabelle-Zeile | ||
| 294 | #] | ||
| 295 | |||
| 296 | [$ sub show_grid_table_row ($self, $req, $i) | ||
| 297 | |||
| 298 | $field = $self -> {fields}[0] ; | ||
| 299 | $id = $self -> {fullid}; | ||
| 300 | $name = $self -> {name} ; | ||
| 301 | my $jsname = $name ; | ||
| 302 | $jsname =~ s/[^a-zA-Z0-9]/_/g ; | ||
| 303 | $jsname .= 'Grid' ; | ||
| 304 | my $ro = $self -> is_readonly ($req) ; | ||
| 305 | $] | ||
| 306 | |||
| 307 | <tr class="cGridRow" id="[+ "${id}_mult-row-$i" +]"> | ||
| 308 | |||
| 309 | <td class="[+ $self -> {class} +]Cell"> | ||
| 310 | [- | ||
| 311 | local $field -> {name} = "__${name}__$i" ; | ||
| 312 | if ($ro) | ||
| 313 | { | ||
| 314 | $field -> show_control_readonly ($req) | ||
| 315 | } | ||
| 316 | else | ||
| 317 | { | ||
| 318 | $field -> show_control ($req) | ||
| 319 | } | ||
| 320 | -] | ||
| 321 | </td> | ||
| 322 | </tr> | ||
| 323 | [$ endsub $] | ||
| 324 | |||
| 325 | |||
| 326 | [$ sub show_label_icon ($self, $req) | ||
| 327 | $name = $self -> {name} ; | ||
| 328 | my $jsname = $name ; | ||
| 329 | $jsname =~ s/[^a-zA-Z0-9]/_/g ; | ||
| 330 | $jsname .= 'Grid' ; | ||
| 331 | |||
| 332 | |||
| 333 | $] | ||
| 334 | [$if (! $self -> is_readonly ($req)) $] | ||
| 335 | <span class="ui-icon ui-icon-circle-plus ef-icon ef-control-mult-add" title="[= ctl:grid_add =]"></span> | ||
| 336 | <span class="ui-icon ui-icon-circle-minus ef-icon ef-control-mult-del" title="[= ctl:grid_del =]"></span> | ||
| 337 | |||
| 338 | [$endif$] | ||
| 339 | [$endsub$] | ||
| 340 | |||
| 341 | [# --------------------------------------------------------------------------- | ||
| 342 | # | ||
| 343 | # show_grid_table Erzeugt eine Grid-Tabelle | ||
| 344 | #] | ||
| 345 | |||
| 346 | [$ sub show_grid_table ($self, $req) | ||
| 347 | my $name = $self->{name} ; | ||
| 348 | my $fields = $self -> {fields} ; | ||
| 349 | my $id = $self -> {fullid}; | ||
| 350 | my $i = 0 ; | ||
| 351 | my $max = $fdat{"__${name}_max"} || 1 ; | ||
| 352 | $] | ||
| 353 | |||
| 354 | [* for ($i = 0; $i < $max ; $i++ ) { *] | ||
| 355 | [- $self -> show_grid_table_row ($req, $i) ; -] | ||
| 356 | [* } *] | ||
| 357 | |||
| 358 | [$endsub$] | ||
| 359 | |||
| 360 | |||
| 361 | |||
| 362 | __END__ |