| 1 | ############################################################################## | ||
| 2 | ## | ||
| 3 | ## Web::Reactor application machinery | ||
| 4 | ## 2014-2021 (c) Vladi Belperchinov-Shabanski "Cade" | ||
| 5 | ## <cade@noxrun.com> <cade@bis.bg> <cade@cpan.org> | ||
| 6 | ## | ||
| 7 | ## LICENSE: GPLv2 | ||
| 8 | ## | ||
| 9 | ############################################################################## | ||
| 10 | package Web::Reactor::HTML::Form; | ||
| 11 | use strict; | ||
| 12 | use Exporter; | ||
| 13 | use Data::Tools 1.31; | ||
| 14 | use Exception::Sink; | ||
| 15 | |||
| 16 | use Web::Reactor::HTML::Utils; | ||
| 17 | |||
| 18 | # FIXME: TODO: use common func to add html elements common tags: ID,DISABLED,etc. | ||
| 19 | # FIXME: TODO: ...including abstract ones as GEO(metry) | ||
| 20 | # FIXME: TODO: change VALUE to be html value (currently it is DATA), and DISPLAY to be visible text (currently it is VALUE) | ||
| 21 | |||
| 22 | use parent 'Web::Reactor::Base'; | ||
| 23 | |||
| 24 | ############################################################################## | ||
| 25 | |||
| 26 | sub new | ||
| 27 | { | ||
| 28 | my $class = shift; | ||
| 29 | my %env = @_; | ||
| 30 | |||
| 31 | $class = ref( $class ) || $class; | ||
| 32 | my $self = { | ||
| 33 | 'ENV' => \%env, | ||
| 34 | }; | ||
| 35 | |||
| 36 | bless $self, $class; | ||
| 37 | |||
| 38 | # FIXME: move as argument, not env option | ||
| 39 | $self->__set_reo( $env{ 'REO_REACTOR' } ); | ||
| 40 | |||
| 41 | return $self; | ||
| 42 | } | ||
| 43 | |||
| 44 | ############################################################################## | ||
| 45 | |||
| 46 | sub create_uniq_id | ||
| 47 | { | ||
| 48 | my $self = shift; | ||
| 49 | |||
| 50 | return $self->get_reo()->create_uniq_id(); | ||
| 51 | } | ||
| 52 | |||
| 53 | sub __check_name | ||
| 54 | { | ||
| 55 | my $name = shift; | ||
| 56 | $name =~ /^[A-Z_0-9_:.]+$/ or boom "invalid or empty NAME attribute [$name]"; | ||
| 57 | return 1; | ||
| 58 | } | ||
| 59 | |||
| 60 | ############################################################################## | ||
| 61 | |||
| 62 | sub begin | ||
| 63 | { | ||
| 64 | my $self = shift; | ||
| 65 | |||
| 66 | my %opt = @_; | ||
| 67 | |||
| 68 | my $form_name = uc $opt{ 'NAME' }; | ||
| 69 | my $form_id = $opt{ 'ID' }; | ||
| 70 | my $method = uc $opt{ 'METHOD' } || 'POST'; | ||
| 71 | my $action = $opt{ 'ACTION' } || '?'; | ||
| 72 | my $default_button = $opt{ 'DEFAULT_BUTTON' }; | ||
| 73 | |||
| 74 | $self->{ 'CLASS_MAP' } = $opt{ 'CLASS_MAP' } || {}; | ||
| 75 | |||
| 76 | $method =~ /^(POST|GET)$/ or boom "METHOD can either POST or GET"; | ||
| 77 | |||
| 78 | my $reo = $self->get_reo(); | ||
| 79 | my $psid = $reo->get_page_session_id(); | ||
| 80 | |||
| 81 | |||
| 82 | $form_name ||= | ||
| 83 | __check_name( $form_name ); | ||
| 84 | |||
| 85 | $form_id ||= $form_name; | ||
| 86 | $form_id .= "_$psid"; | ||
| 87 | |||
| 88 | $self->{ 'FORM_NAME' } = $form_name; | ||
| 89 | $self->{ 'FORM_ID' } = $form_id = $form_id || $self->create_uniq_id(); | ||
| 90 | $self->{ 'RADIO' } = {}; | ||
| 91 | $self->{ 'RET_MAP' } = {}; # return data mapping (combo, checkbox, etc.) | ||
| 92 | $self->{ 'FORM_STATE' } = {}; | ||
| 93 | |||
| 94 | my $options; | ||
| 95 | |||
| 96 | $options .= " autocomplete='off'" if $opt{ 'NO_AUTOCOMPLETE' }; | ||
| 97 | |||
| 98 | my $text; | ||
| 99 | |||
| 100 | # FIXME: TODO: debug info inside html text, begin formname end etc. | ||
| 101 | |||
| 102 | $self->state( FORM_NAME => $form_name ); | ||
| 103 | |||
| 104 | my $reo = $self->get_reo(); | ||
| 105 | |||
| 106 | my $page_session = $reo->get_page_session(); | ||
| 107 | $page_session->{ ':FORM_DEF' }{ $form_name } = {}; | ||
| 108 | |||
| 109 | $text .= "<form name='$form_name' id='$form_id' action='$action' method='$method' enctype='multipart/form-data' $options></form>"; | ||
| 110 | ### $text .= "<input style='display: none;' name='__avoidiebug__' form='$form_id'>"; # stupid IE bugs | ||
| 111 | if( $default_button ) | ||
| 112 | { | ||
| 113 | $text .= "<input style='display: none;' type='image' name='BUTTON:$default_button' src='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVQI12NgYGBgAAAABQABXvMqOgAAAABJRU5ErkJggg==' border=0 height=0 width=0 onDblClick='return false;' form='$form_id'>" | ||
| 114 | } | ||
| 115 | return $text; | ||
| 116 | } | ||
| 117 | |||
| 118 | sub state | ||
| 119 | { | ||
| 120 | my $self = shift; | ||
| 121 | |||
| 122 | $self->{ 'FORM_STATE' } = { %{ $self->{ 'FORM_STATE' } }, @_ }; | ||
| 123 | |||
| 124 | return undef; | ||
| 125 | } | ||
| 126 | |||
| 127 | sub end | ||
| 128 | { | ||
| 129 | my $self = shift; | ||
| 130 | |||
| 131 | my $text; | ||
| 132 | |||
| 133 | $text .= $self->end_radios(); | ||
| 134 | # $text .= "</form>"; | ||
| 135 | |||
| 136 | # FIXME: TODO: debug info inside html text, begin formname end etc. | ||
| 137 | |||
| 138 | my $reo = $self->get_reo(); | ||
| 139 | my $page_session = $reo->get_page_session(); | ||
| 140 | |||
| 141 | my $form_name = $self->{ 'FORM_NAME' }; | ||
| 142 | my $form_id = $self->{ 'FORM_ID' }; | ||
| 143 | $page_session->{ ':FORM_DEF' }{ $form_name }{ 'RET_MAP' } = $self->{ 'RET_MAP' }; | ||
| 144 | |||
| 145 | my $state_keeper = $reo->args_here( %{ $self->{ 'FORM_STATE' } } ); | ||
| 146 | $text .= "<input type=hidden name='_' value='$state_keeper' form='$form_id'>"; | ||
| 147 | |||
| 148 | $text .= "\n"; | ||
| 149 | return $text; | ||
| 150 | } | ||
| 151 | |||
| 152 | sub __ret_map_set | ||
| 153 | { | ||
| 154 | my $self = shift; | ||
| 155 | my $name = shift; # entry input name | ||
| 156 | |||
| 157 | $self->{ 'RET_MAP' }{ $name } ||= {}; | ||
| 158 | |||
| 159 | if( @_ > 0 ) | ||
| 160 | { | ||
| 161 | boom "expected even number of arguments" unless @_ % 2 == 0; | ||
| 162 | %{ $self->{ 'RET_MAP' }{ $name } } = ( %{ $self->{ 'RET_MAP' }{ $name } }, @_ ); | ||
| 163 | } | ||
| 164 | |||
| 165 | return $self->{ 'RET_MAP' }{ $name }; | ||
| 166 | } | ||
| 167 | |||
| 168 | ############################################################################## | ||
| 169 | # classic html input checkbox | ||
| 170 | |||
| 171 | sub checkbox | ||
| 172 | { | ||
| 173 | my $self = shift; | ||
| 174 | |||
| 175 | my %opt = @_; | ||
| 176 | |||
| 177 | my $name = uc $opt{ 'NAME' }; | ||
| 178 | my $class = $opt{ 'CLASS' } || $self->{ 'CLASS_MAP' }{ 'CHECKBOX' } || 'checkbox'; | ||
| 179 | my $value = $opt{ 'VALUE' } ? 1 : 0; | ||
| 180 | my $args = $opt{ 'ARGS' }; | ||
| 181 | |||
| 182 | __check_name( $name ); | ||
| 183 | |||
| 184 | my $options; | ||
| 185 | $options .= $value ? " checked " : undef; | ||
| 186 | |||
| 187 | my $text; | ||
| 188 | |||
| 189 | my $ch_id = $self->create_uniq_id(); # checkbox data holder | ||
| 190 | |||
| 191 | my $form_id = $self->{ 'FORM_ID' }; | ||
| 192 | #print STDERR "ccccccccccccccccccccc CHECKBOX [$name] [$value]\n"; | ||
| 193 | #$text .= "<input type='checkbox' name='$name' value='1' $options>"; | ||
| 194 | $text .= "\n"; | ||
| 195 | $text .= "<input type='hidden' name='$name' id='$ch_id' value='$value' form='$form_id' $args>"; | ||
| 196 | # --> $text .= html_element( "input", undef, type => 'hidden', name => $name, id => $ch_id, value => $value, form => $form_id, extra => $args ); | ||
| 197 | # $text .= qq[ <input type='checkbox' $options checkbox_data_input_id="$ch_id" onclick='document.getElementById( "$ch_id" ).value = this.checked ? 1 : 0'> ]; | ||
| 198 | $text .= qq[ <input type='checkbox' $options data-checkbox-input-id="$ch_id" form='$form_id' onclick='reactor_form_checkbox_toggle(this)' class='$class'> ]; | ||
| 199 | # --> $text .= html_element( "input", undef, type => 'checkbox', 'data-checkbox-input-id' => $ch_id, form => $form_id, onclick='reactor_form_checkbox_toggle(this)', class => $class, extra => $options ); | ||
| 200 | $text .= "\n"; | ||
| 201 | |||
| 202 | return $text; | ||
| 203 | } | ||
| 204 | |||
| 205 | ############################################################################## | ||
| 206 | # multi-stages css-styled checkbox | ||
| 207 | |||
| 208 | sub checkbox_multi | ||
| 209 | { | ||
| 210 | my $self = shift; | ||
| 211 | |||
| 212 | my %opt = @_; | ||
| 213 | |||
| 214 | my $name = uc $opt{ 'NAME' }; | ||
| 215 | my $class = $opt{ 'CLASS' } || $self->{ 'CLASS_MAP' }{ 'CHECKBOX' } || 'checkbox'; | ||
| 216 | my $value = $opt{ 'VALUE' }; | ||
| 217 | my $args = $opt{ 'ARGS' }; | ||
| 218 | my $stages = $opt{ 'STAGES' } || 2; | ||
| 219 | my $labels = $opt{ 'LABELS' } || [ 'x', '√' ]; | ||
| 220 | my $hint = $opt{ 'HINT' }; | ||
| 221 | |||
| 222 | __check_name( $name ); | ||
| 223 | |||
| 224 | $value = abs( int( $value ) ); | ||
| 225 | $value = 0 if $value >= $stages; | ||
| 226 | |||
| 227 | my $text; | ||
| 228 | |||
| 229 | my $labels_spans; | ||
| 230 | for my $s ( 0 .. $stages - 1 ) | ||
| 231 | { | ||
| 232 | $labels_spans .= html_element( 'span', $labels->[$s], style => "display: none" ); | ||
| 233 | } | ||
| 234 | |||
| 235 | my $reo = $self->get_reo(); | ||
| 236 | my $hint_handler = $hint ? html_hover_layer( $reo, VALUE => $hint ) : undef; | ||
| 237 | |||
| 238 | my $cb_id = $self->create_uniq_id(); # checkbox id | ||
| 239 | my $el_id = $self->create_uniq_id(); # checkbox label element id | ||
| 240 | |||
| 241 | my $form_id = $self->{ 'FORM_ID' }; | ||
| 242 | #print STDERR "ccccccccccccccccccccc CHECKBOX [$name] [$value]\n"; | ||
| 243 | #$text .= "<input type='checkbox' name='$name' value='1' $options>"; | ||
| 244 | $text .= "\n"; | ||
| 245 | ### $text .= qq[< input type='hidden' name='$name' id='$cb_id' value='$value' form='$form_id' $args>]; | ||
| 246 | $text .= html_element( "input", undef, type => 'hidden', name => $name, id => $cb_id, value => $value, form => $form_id, extra => $args ); | ||
| 247 | #$text .= qq[<span class='$current_class' id='$el_id' data-stages='$stages' data-checkbox-input-id='$cb_id' onclick='reactor_form_multi_checkbox_toggle(this)' $hint_handler $options>$label</span>]; | ||
| 248 | $text .= html_element( "span", $labels_spans, id => $el_id, 'data-stages' => $stages, 'data-checkbox-input-id' => $cb_id, onclick => 'reactor_form_multi_checkbox_toggle(this)', extra => $hint_handler ); | ||
| 249 | ### $text .= qq[<script>reactor_form_multi_checkbox_setup_id( '$el_id' )</script>]; | ||
| 250 | $text .= html_element( "script", "reactor_form_multi_checkbox_setup_id( '$el_id' )" ); | ||
| 251 | $text .= "\n"; | ||
| 252 | |||
| 253 | return $text; | ||
| 254 | } | ||
| 255 | |||
| 256 | ############################################################################## | ||
| 257 | |||
| 258 | sub checkbox_3state | ||
| 259 | { | ||
| 260 | my $self = shift; | ||
| 261 | |||
| 262 | my %args = @_; # to fix uneven args | ||
| 263 | return $self->checkbox_multi( %args, STAGES => 3, LABELS => [ '?', '√', 'x' ] ); | ||
| 264 | } | ||
| 265 | |||
| 266 | ############################################################################## | ||
| 267 | |||
| 268 | sub radio | ||
| 269 | { | ||
| 270 | my $self = shift; | ||
| 271 | |||
| 272 | my %opt = @_; | ||
| 273 | |||
| 274 | my $name = uc $opt{ 'NAME' }; # FIXME:escape or check? | ||
| 275 | my $class = $opt{ 'CLASS' } || $self->{ 'CLASS_MAP' }{ 'RADIO' } || 'radio'; | ||
| 276 | my $on = $opt{ 'ON' }; # active? | ||
| 277 | my $ret = $opt{ 'RET' }; # map return value! | ||
| 278 | my $extra = $opt{ 'EXTRA' }; | ||
| 279 | |||
| 280 | __check_name( $name ); | ||
| 281 | |||
| 282 | my $text; | ||
| 283 | |||
| 284 | my $val = $self->create_uniq_id(); | ||
| 285 | |||
| 286 | my $form_id = $self->{ 'FORM_ID' }; | ||
| 287 | my $checked = $on ? 'checked' : undef; | ||
| 288 | $text .= "<input type='radio' $checked name='$name' value='$val' form='$form_id' $extra>"; | ||
| 289 | |||
| 290 | $self->__ret_map_set( $name, $val => $ret ) if defined $ret; | ||
| 291 | |||
| 292 | $text .= "\n"; | ||
| 293 | return $text; | ||
| 294 | } | ||
| 295 | |||
| 296 | sub end_radios | ||
| 297 | { | ||
| 298 | my $self = shift; | ||
| 299 | |||
| 300 | my $text; | ||
| 301 | |||
| 302 | # nothing for now | ||
| 303 | |||
| 304 | return $text; | ||
| 305 | } | ||
| 306 | |||
| 307 | ############################################################################## | ||
| 308 | =pod | ||
| 309 | |||
| 310 | $form->select( DATA => $data ); | ||
| 311 | |||
| 312 | $data = [ | ||
| 313 | { | ||
| 314 | KEY => string | ||
| 315 | VALUE => string | ||
| 316 | ORDER => \d+ | ||
| 317 | }, | ||
| 318 | ]; | ||
| 319 | |||
| 320 | $data = { | ||
| 321 | KEY => { | ||
| 322 | KEY => string | ||
| 323 | VALUE => string | ||
| 324 | ORDER => \d+ | ||
| 325 | }, | ||
| 326 | KEY => value..., | ||
| 327 | }; | ||
| 328 | |||
| 329 | =cut | ||
| 330 | |||
| 331 | sub select | ||
| 332 | { | ||
| 333 | my $self = shift; | ||
| 334 | |||
| 335 | my %opt = @_; | ||
| 336 | |||
| 337 | my $name = uc $opt{ 'NAME' }; | ||
| 338 | my $id = $opt{ 'ID' }; | ||
| 339 | my $class = $opt{ 'CLASS' } || $self->{ 'CLASS_MAP' }{ 'SELECT' } || 'select'; | ||
| 340 | my $rows = $opt{ 'SIZE' } || $opt{ 'ROWS' } || 1; | ||
| 341 | my $args = $opt{ 'ARGS' }; | ||
| 342 | my $disabled = $opt{ 'DISABLED' }; | ||
| 343 | |||
| 344 | __check_name( $name ); | ||
| 345 | |||
| 346 | my $data = $opt{ 'DATA' }; # array reference or hash reference, inside hashesh are the same | ||
| 347 | my $selected = $opt{ 'SELECTED' }; # hashref with selected keys (values are true's) | ||
| 348 | my $sel_data; | ||
| 349 | |||
| 350 | my $options; | ||
| 351 | |||
| 352 | $options .= "disabled='disabled' " if $disabled; | ||
| 353 | |||
| 354 | if( ref($data) eq 'HASH' ) | ||
| 355 | { | ||
| 356 | $sel_data = []; | ||
| 357 | my %res; | ||
| 358 | while( my ( $k, $v ) = each %$data ) | ||
| 359 | { | ||
| 360 | my %e = ( 'KEY' => $k ); | ||
| 361 | if( ref($v) eq 'HASH' ) | ||
| 362 | { | ||
| 363 | %e = ( %e, %$v ); | ||
| 364 | } | ||
| 365 | else | ||
| 366 | { | ||
| 367 | $e{ 'VALUE' } = $v; | ||
| 368 | } | ||
| 369 | push @$sel_data, \%e; | ||
| 370 | } | ||
| 371 | # FIXME: @$sel_data = sort { ... } @$sel_data; | ||
| 372 | } | ||
| 373 | elsif( ref($data) eq 'ARRAY' ) | ||
| 374 | { | ||
| 375 | $sel_data = $data; | ||
| 376 | } | ||
| 377 | else | ||
| 378 | { | ||
| 379 | boom "DATA must be either ARRAY or HASH reference"; | ||
| 380 | } | ||
| 381 | hash_uc_ipl( $_ ) for @$sel_data; | ||
| 382 | |||
| 383 | my $extra = $opt{ 'EXTRA' }; | ||
| 384 | |||
| 385 | my $text; | ||
| 386 | my $form_id = $self->{ 'FORM_ID' }; | ||
| 387 | |||
| 388 | $extra .= qq[ onchange='this.form.submit()'] if $opt{ 'RESUBMIT_ON_CHANGE' }; | ||
| 389 | if( $opt{ 'RADIO' } ) | ||
| 390 | { | ||
| 391 | for my $hr ( @$sel_data ) | ||
| 392 | { | ||
| 393 | my $sel = $hr->{ 'SELECTED' } ? 'selected' : ''; # is selected? | ||
| 394 | my $key = $hr->{ 'KEY' }; | ||
| 395 | my $value = $hr->{ 'VALUE' }; | ||
| 396 | |||
| 397 | $sel = 'selected' if ( ref( $selected ) and $selected->{ $key } ) or ( $selected eq $key ); | ||
| 398 | #print STDERR "sssssssssssssssssssssssss RADIO [$name] [$value] [$key] $sel -- {$extra}\n"; | ||
| 399 | $text .= $self->radio( NAME => $name, RET => $key, ON => $sel, EXTRA => $extra, DISABLED => $disabled ) . " $value"; | ||
| 400 | $text .= "<br>" if $opt{ 'RADIO' } != 2; | ||
| 401 | } | ||
| 402 | # FIXME: kakvo stava ako nqma dadeno selected pri submit na formata? | ||
| 403 | } | ||
| 404 | else | ||
| 405 | { | ||
| 406 | my $multiple = 'multiple' if $opt{ 'MULTIPLE' }; | ||
| 407 | $text .= "<select class='$class' id='$id' name='$name' size='$rows' $multiple form='$form_id' $args $extra $options>"; | ||
| 408 | |||
| 409 | my $pad = ' ' x 3; | ||
| 410 | for my $hr ( @$sel_data ) | ||
| 411 | { | ||
| 412 | my $sel = $hr->{ 'SELECTED' } ? 'selected' : ''; # is selected? | ||
| 413 | my $key = $hr->{ 'KEY' }; | ||
| 414 | my $value = $hr->{ 'VALUE' }; | ||
| 415 | my $id = $self->create_uniq_id(); | ||
| 416 | $self->__ret_map_set( $name, $id => $key ); | ||
| 417 | |||
| 418 | $sel = 'selected' if ( ref( $selected ) and $selected->{ $key } ) or ( $selected eq $key ); | ||
| 419 | #print STDERR "sssssssssssssssssssssssss COMBO [$name] [$value] [$key] $sel\n"; | ||
| 420 | $text .= "<option value='$id' $sel>$value$pad</option>\n"; | ||
| 421 | } | ||
| 422 | |||
| 423 | $text .= "</select>"; | ||
| 424 | } | ||
| 425 | # print STDERR "FOOOOOOOOOOOOOOOOOOOOOORM[$text](@$sel){@$order}"; | ||
| 426 | |||
| 427 | $text .= "\n"; | ||
| 428 | return $text; | ||
| 429 | } | ||
| 430 | |||
| 431 | sub combo | ||
| 432 | { | ||
| 433 | my $self = shift; | ||
| 434 | |||
| 435 | my %opt = @_; | ||
| 436 | $opt{ 'ROWS' } ||= 1; | ||
| 437 | return $self->select( %opt ); | ||
| 438 | } | ||
| 439 | |||
| 440 | ############################################################################## | ||
| 441 | |||
| 442 | sub textarea | ||
| 443 | { | ||
| 444 | my $self = shift; | ||
| 445 | |||
| 446 | my %opt = @_; | ||
| 447 | |||
| 448 | my $name = uc $opt{ 'NAME' }; | ||
| 449 | my $class = $opt{ 'CLASS' } || $self->{ 'CLASS_MAP' }{ 'TEXTAREA' } || 'textarea'; | ||
| 450 | my $id = $opt{ 'ID' }; | ||
| 451 | my $data = $opt{ 'VALUE' }; | ||
| 452 | my $rows = $opt{ 'ROWS' } || 10; | ||
| 453 | my $cols = $opt{ 'COLS' } || 5; | ||
| 454 | my $maxl = $opt{ 'MAXLEN' } || $opt{ 'MAX' }; | ||
| 455 | my $geo = $opt{ 'GEOMETRY' } || $opt{ 'GEO' }; | ||
| 456 | my $args = $opt{ 'ARGS' }; | ||
| 457 | |||
| 458 | __check_name( $name ); | ||
| 459 | |||
| 460 | ( $cols, $rows ) = ( $1, $2 ) if $geo =~ /(\d+)[\*\/\\](\d+)/i; | ||
| 461 | |||
| 462 | |||
| 463 | my $options; | ||
| 464 | |||
| 465 | $options .= "disabled='disabled' " if $opt{ 'DISABLED' }; | ||
| 466 | $options .= "maxlength='$maxl' " if $maxl > 0; | ||
| 467 | $options .= "id='$id' " if $id ne ''; | ||
| 468 | $options .= "readonly='readonly' " if $opt{ 'READONLY' } || $opt{ 'RO' }; | ||
| 469 | $options .= "required='required' " if $opt{ 'REQUIRED' } || $opt{ 'REQ' }; | ||
| 470 | $options .= "onFocus=\"this.value=''\" " if $opt{ 'FOCUS_AUTO_CLEAR' }; | ||
| 471 | |||
| 472 | my $extra = $opt{ 'EXTRA' }; | ||
| 473 | $options .= " $extra "; | ||
| 474 | |||
| 475 | $data = str_html_escape( $data ); | ||
| 476 | |||
| 477 | my $text; | ||
| 478 | my $form_id = $self->{ 'FORM_ID' }; | ||
| 479 | |||
| 480 | $text .= "<textarea class='$class' name='$name' rows='$rows' cols='$cols' $options form='$form_id' $args>$data</textarea>"; | ||
| 481 | |||
| 482 | $text .= "\n"; | ||
| 483 | return $text; | ||
| 484 | } | ||
| 485 | |||
| 486 | ############################################################################## | ||
| 487 | |||
| 488 | sub input | ||
| 489 | { | ||
| 490 | my $self = shift; | ||
| 491 | |||
| 492 | my %opt = @_; | ||
| 493 | |||
| 494 | my $name = uc $opt{ 'NAME' }; | ||
| 495 | my $class = $opt{ 'CLASS' } || $self->{ 'CLASS_MAP' }{ 'INPUT' } || 'line'; | ||
| 496 | my $value = $opt{ 'VALUE' }; | ||
| 497 | my $key = $opt{ 'KEY' }; | ||
| 498 | my $id = $opt{ 'ID' }; | ||
| 499 | # FIXME: default data? | ||
| 500 | my $size = $opt{ 'SIZE' } || $opt{ 'LEN' } || $opt{ 'WIDTH' }; | ||
| 501 | my $maxl = $opt{ 'MAXLEN' } || $opt{ 'MAX' }; | ||
| 502 | |||
| 503 | my $len = $opt{ 'LEN' }; | ||
| 504 | my $args = $opt{ 'ARGS' }; | ||
| 505 | my $hid = $opt{ 'HIDDEN' }; | ||
| 506 | my $ret = $opt{ 'RET' } || $opt{ 'RETURN' }; # if return value should be mapped, works only with HIDDEN | ||
| 507 | |||
| 508 | my $clear = $opt{ 'DISABLED' } ? undef : $opt{ 'CLEAR' }; | ||
| 509 | |||
| 510 | my $datalist = $opt{ 'DATALIST' }; # array ref with 'key' & 'value' hash | ||
| 511 | |||
| 512 | $size = $maxl = $len if $len > 0; | ||
| 513 | |||
| 514 | my $options; | ||
| 515 | |||
| 516 | $options .= "disabled='disabled' " if $opt{ 'DISABLED' }; | ||
| 517 | $options .= "size='$size' " if $size > 0; | ||
| 518 | $options .= "maxlength='$maxl' " if $maxl > 0; | ||
| 519 | $options .= "id='$id' " if $id ne ''; | ||
| 520 | $options .= "type='password' " if $opt{ 'PASS' } || $opt{ 'PASSWORD' }; | ||
| 521 | $options .= "type='hidden' " if $hid; # FIXME: handle TYPE better | ||
| 522 | $options .= "readonly='readonly' " if $opt{ 'READONLY' } || $opt{ 'RO' }; | ||
| 523 | $options .= "required='required' " if $opt{ 'REQUIRED' } || $opt{ 'REQ' }; | ||
| 524 | $options .= "onFocus=\"this.value=''\" " if $opt{ 'FOCUS_AUTO_CLEAR' }; | ||
| 525 | $options .= "autocomplete='off' " if $opt{ 'NO_AUTOCOMPLETE' }; | ||
| 526 | |||
| 527 | |||
| 528 | my $extra = $opt{ 'EXTRA' }; | ||
| 529 | $options .= " $extra "; | ||
| 530 | |||
| 531 | $value = str_html_escape( $value ); | ||
| 532 | |||
| 533 | __check_name( $name ); | ||
| 534 | |||
| 535 | if( $hid and defined $ret ) | ||
| 536 | { | ||
| 537 | # if input is hidden and return value mapping requested, VALUE is not used! | ||
| 538 | $value = $self->create_uniq_id(); | ||
| 539 | $self->__ret_map_set( $name, $value => $ret ); | ||
| 540 | } | ||
| 541 | |||
| 542 | my $clear_tag; | ||
| 543 | if( $clear ) | ||
| 544 | { | ||
| 545 | my $reo = $self->get_reo(); | ||
| 546 | my $clear_hint_handler = html_hover_layer( $reo, VALUE => 'Clear field' ); | ||
| 547 | |||
| 548 | if( $clear =~ /^[a-z_\-0-9\/]+\.(png|jpg|jpeg|gif|svg)$/ ) | ||
| 549 | { | ||
| 550 | $clear_tag = qq[ <img class='icon-clear' src='$clear' border='0' onClick='return set_value("$id", "")' $clear_hint_handler > ]; | ||
| 551 | } | ||
| 552 | else | ||
| 553 | { | ||
| 554 | my $s = $clear eq 1 ? '⊗' : $clear; | ||
| 555 | $clear_tag = qq[ <span class='icon-clear' border='0' onClick='return set_value("$id", "")' $clear_hint_handler >$s</span> ]; | ||
| 556 | } | ||
| 557 | } | ||
| 558 | |||
| 559 | my $text; | ||
| 560 | |||
| 561 | my $form_id = $self->{ 'FORM_ID' }; | ||
| 562 | |||
| 563 | if( $datalist ) | ||
| 564 | { | ||
| 565 | my $resub = $opt{ 'RESUBMIT_ON_CHANGE' } ? 1 : 0; | ||
| 566 | |||
| 567 | my $empty_key = str_html_escape( $opt{ 'EMPTY_KEY' } ); | ||
| 568 | my $input_id = $self->create_uniq_id(); | ||
| 569 | my $datalist_id = $self->create_uniq_id(); | ||
| 570 | $class .= " search_list"; | ||
| 571 | $text .= "\n\n\n\n\n<input id=$input_id type=hidden name='$name' value='$key' form='$form_id' >"; | ||
| 572 | $text .= "\n<input class='$class' value='$value' list=$datalist_id $options form='$form_id' $args data-input-id=$input_id data-empty-key='$empty_key' onchange='return reactor_datalist_change( this, $resub )'>$clear_tag"; | ||
| 573 | $text .= "\n<datalist id=$datalist_id>"; | ||
| 574 | for my $e ( @$datalist ) | ||
| 575 | { | ||
| 576 | my $k = $e->{ 'KEY' }; | ||
| 577 | my $v = $e->{ 'VALUE' }; | ||
| 578 | $text .= html_element( 'option', undef, name => $v, value => $v, 'data-key' => $k ); | ||
| 579 | } | ||
| 580 | $text .= "\n</datalist>\n\n\n\n"; | ||
| 581 | } | ||
| 582 | else | ||
| 583 | { | ||
| 584 | $text .= "<input class='$class' name='$name' value='$value' $options form='$form_id' $args>$clear_tag"; | ||
| 585 | } | ||
| 586 | |||
| 587 | $text .= "\n"; | ||
| 588 | return $text; | ||
| 589 | } | ||
| 590 | |||
| 591 | ############################################################################## | ||
| 592 | |||
| 593 | sub file_upload | ||
| 594 | { | ||
| 595 | my $self = shift; | ||
| 596 | |||
| 597 | my %opt = @_; | ||
| 598 | |||
| 599 | my $name = uc $opt{ 'NAME' }; | ||
| 600 | my $class = $opt{ 'CLASS' } || $self->{ 'CLASS_MAP' }{ 'FILE_UPLOAD' } || 'file_upload'; | ||
| 601 | my $id = $opt{ 'ID' }; | ||
| 602 | my $args = $opt{ 'ARGS' }; | ||
| 603 | |||
| 604 | my $options; | ||
| 605 | |||
| 606 | $options .= "multiple " if $opt{ 'MULTI' }; | ||
| 607 | $options .= "id='$id' " if $id ne ''; | ||
| 608 | |||
| 609 | my $text; | ||
| 610 | |||
| 611 | my $form_id = $self->{ 'FORM_ID' }; | ||
| 612 | |||
| 613 | $text .= "<input class='$class' name='$name' type=file $options form='$form_id' $args>"; | ||
| 614 | |||
| 615 | $text .= "\n"; | ||
| 616 | return $text; | ||
| 617 | } | ||
| 618 | |||
| 619 | sub file_upload_multi | ||
| 620 | { | ||
| 621 | my $self = shift; | ||
| 622 | return $self->file_upload( @_, MULTI => 1 ); | ||
| 623 | } | ||
| 624 | |||
| 625 | |||
| 626 | ############################################################################## | ||
| 627 | |||
| 628 | sub button | ||
| 629 | { | ||
| 630 | my $self = shift; | ||
| 631 | |||
| 632 | my %opt = @_; | ||
| 633 | |||
| 634 | my $name = uc $opt{ 'NAME' }; | ||
| 635 | my $id = $opt{ 'ID' }; | ||
| 636 | my $class = $opt{ 'CLASS' } || 'button'; | ||
| 637 | my $value = $opt{ 'VALUE' }; | ||
| 638 | my $args = $opt{ 'ARGS' }; | ||
| 639 | |||
| 640 | __check_name( $name ); | ||
| 641 | |||
| 642 | my $options; | ||
| 643 | |||
| 644 | if( $opt{ 'DISABLED' } ) | ||
| 645 | { | ||
| 646 | $options .= "disabled='disabled' " ; | ||
| 647 | $class .= " disabled-button"; | ||
| 648 | } | ||
| 649 | |||
| 650 | my $text; | ||
| 651 | |||
| 652 | $name =~ s/^button://i; | ||
| 653 | |||
| 654 | my $form_id = $self->{ 'FORM_ID' }; | ||
| 655 | # $text .= "<input class='$class' id='$id' type='submit' name='button:$name' value='$value' onDblClick='return false;' form='$form_id' $options $args>"; | ||
| 656 | #$text .= "<button class='$class' id='$id' type='submit' name='button:$name' value='1' onDblClick='return false;' form='$form_id' $options $args>$value</button>"; | ||
| 657 | |||
| 658 | $text .= html_element( 'button', $value, form => $form_id, class => $class, id => $id, name => "button:$name", onDblClick => 'return false;', extra => "$options $args" ); | ||
| 659 | |||
| 660 | $text .= "\n"; | ||
| 661 | return $text; | ||
| 662 | } | ||
| 663 | |||
| 664 | sub image_button | ||
| 665 | { | ||
| 666 | my $self = shift; | ||
| 667 | |||
| 668 | my %opt = @_; | ||
| 669 | |||
| 670 | my $name = uc $opt{ 'NAME' }; | ||
| 671 | my $id = $opt{ 'ID' }; | ||
| 672 | my $class = $opt{ 'CLASS' } || 'image_button'; | ||
| 673 | my $src = $opt{ 'SRC' } || $opt{ 'IMG' }; | ||
| 674 | my $args = $opt{ 'ARGS' }; | ||
| 675 | my $extra = $opt{ 'EXTRA' }; | ||
| 676 | |||
| 677 | my $options; | ||
| 678 | |||
| 679 | # FIXME: make this for all entries! common func? | ||
| 680 | for my $o ( qw( HEIGHT WIDTH ONMOUSEOVER ) ) | ||
| 681 | { | ||
| 682 | my $e = $opt{ $o }; | ||
| 683 | # FIXME: escape? $e | ||
| 684 | $options .= "$o='$e' " if $e ne ''; | ||
| 685 | } | ||
| 686 | |||
| 687 | __check_name( $name ); | ||
| 688 | |||
| 689 | my $text; | ||
| 690 | |||
| 691 | my $form_id = $self->{ 'FORM_ID' }; | ||
| 692 | $name =~ s/^button://i; | ||
| 693 | $text .= "<input class='$class' id='$id' type='image' name='button:$name' src='$src' border=0 $options onDblClick='return false;' $args form='$form_id' $extra>"; | ||
| 694 | |||
| 695 | $text .= "\n"; | ||
| 696 | return $text; | ||
| 697 | } | ||
| 698 | |||
| 699 | sub image_button_default | ||
| 700 | { | ||
| 701 | my $self = shift; | ||
| 702 | |||
| 703 | my %opt = @_; | ||
| 704 | |||
| 705 | my $user_agent = $self->get_reo()->get_user_session_agent(); | ||
| 706 | |||
| 707 | my $default_class = 'hidden'; | ||
| 708 | $default_class = 'hidden2' if $user_agent =~ /MSIE|Safari/; | ||
| 709 | |||
| 710 | $opt{ 'HEIGHT' } = 0; | ||
| 711 | $opt{ 'WIDTH' } = 0; | ||
| 712 | $opt{ 'CLASS' } = $opt{ 'CLASS' } || $default_class; | ||
| 713 | |||
| 714 | return $self->image_button( %opt ); | ||
| 715 | } | ||
| 716 | |||
| 717 | sub get_id | ||
| 718 | { | ||
| 719 | my $self = shift; | ||
| 720 | |||
| 721 | return $self->{ 'FORM_ID' }; | ||
| 722 | } | ||
| 723 | |||
| 724 | =pod | ||
| 725 | |||
| 726 | my $form = new Review::HTML::Form; | ||
| 727 | |||
| 728 | $text .= $form->image_submit_default( NAME => 'def_but', SRC => 'img/empty.png' ); | ||
| 729 | |||
| 730 | $text .= $form->line( NAME => 'line_one', DATA => ( $I{ 'LINE_ONE' } || 'ne se chete' ) ); | ||
| 731 | |||
| 732 | $text .= $form->begin( NAME => 'try1' ); | ||
| 733 | $text .= 'cb1' . $form->cb( NAME => 'cb1', VAL => 1 ); | ||
| 734 | $text .= 'cb2' . $form->cb( NAME => 'cb2', VAL => 0 ); | ||
| 735 | $text .= 'cb3' . $form->cb( NAME => 'cb3', VAL => 0, MAX => 3, RET => [ 'qwe', 'asd', '[-]' ] ); | ||
| 736 | $text .= "<p>"; | ||
| 737 | |||
| 738 | $text .= "<hr noshade>"; | ||
| 739 | $text .= 'r1' . $form->radio( NAME => 'r1' ); | ||
| 740 | $text .= 'r2' . $form->radio( NAME => 'r1' ); | ||
| 741 | $text .= 'r3' . $form->radio( NAME => 'r1', ON => 1 ); | ||
| 742 | $text .= 'r4' . $form->radio( NAME => 'r1' ); | ||
| 743 | $text .= 'r5' . $form->radio( NAME => 'r1' ); | ||
| 744 | $text .= "<hr noshade>"; | ||
| 745 | $text .= 'r1' . $form->radio( NAME => 'r2', ON => 1 ); | ||
| 746 | $text .= 'r2' . $form->radio( NAME => 'r2', RET => 'asd' ); | ||
| 747 | $text .= 'r3' . $form->radio( NAME => 'r2', RET => 'qwe' ); | ||
| 748 | $text .= 'r4' . $form->radio( NAME => 'r2', RET => 'zxc' ); | ||
| 749 | $text .= 'r5' . $form->radio( NAME => 'r2', RET => '[-]' ); | ||
| 750 | $text .= "<hr noshade>"; | ||
| 751 | |||
| 752 | my $data = { | ||
| 753 | 'one' => 'This is test one', | ||
| 754 | 'opa' => 'Opa test ooooooe', | ||
| 755 | 'two' => 'Test two tralala', | ||
| 756 | 'tra' => 'Tralala again+++', | ||
| 757 | }; | ||
| 758 | |||
| 759 | $text .= $form->select( NAME => 'sel2', DATA => $data, SELECTED => [ 'opa' ], ROWS => 4 ); | ||
| 760 | |||
| 761 | $text .= "<p>"; | ||
| 762 | $text .= $form->button( NAME => 'bbb', VALUE => '"%!@#$&^' ); | ||
| 763 | $text .= $form->end(); | ||
| 764 | |||
| 765 | =cut | ||
| 766 | |||
| 767 | ############################################################################## | ||
| 768 | 1; | ||
| 769 | ############################################################################## |