| 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::number ; | ||
| 20 | |||
| 21 | use strict ; | ||
| 22 | use base 'Embperl::Form::Control::input' ; | ||
| 23 | |||
| 24 | use Embperl::Inline ; | ||
| 25 | |||
| 26 | use vars qw{%fdat} ; | ||
| 27 | |||
| 28 | # ------------------------------------------------------------------------------------------ | ||
| 29 | |||
| 30 | sub get_std_control_attr | ||
| 31 | { | ||
| 32 | my ($self, $req, $id, $type, $addclass) = @_ ; | ||
| 33 | |||
| 34 | return $self -> SUPER::get_std_control_attr ($req, $id, $type, $type eq 'readonly'?'ef-control-number-readonly':$addclass) ; | ||
| 35 | } | ||
| 36 | |||
| 37 | # ------------------------------------------------------------------------------------------ | ||
| 38 | # | ||
| 39 | # get_display_text - returns the text that should be displayed | ||
| 40 | # | ||
| 41 | |||
| 42 | sub get_display_text | ||
| 43 | { | ||
| 44 | my ($self, $req, $value, $compact) = @_ ; | ||
| 45 | |||
| 46 | $value = $self -> get_value ($req) if (!defined ($value)) ; | ||
| 47 | |||
| 48 | return if ($value eq '') ; | ||
| 49 | if ($compact) | ||
| 50 | { | ||
| 51 | return $value + 0 if (!$self -> {allow_unit}) ; | ||
| 52 | return $value ; | ||
| 53 | } | ||
| 54 | |||
| 55 | my $unit = $self->{unit} ; | ||
| 56 | my $unittext = !$unit?'':$self -> form -> convert_text ($self, ($unit =~ /:/)?$unit:'unit:' . lc($unit), $unit, $req) ; | ||
| 57 | $unittext =~ s/^unit:// ; | ||
| 58 | |||
| 59 | return $value . ' ' . $unittext ; | ||
| 60 | } | ||
| 61 | |||
| 62 | # ------------------------------------------------------------------------------------------ | ||
| 63 | # | ||
| 64 | # get_sort_value - returns the value that should be used to sort | ||
| 65 | # | ||
| 66 | |||
| 67 | sub get_sort_value | ||
| 68 | { | ||
| 69 | my ($self, $req, $value) = @_ ; | ||
| 70 | |||
| 71 | $value = $self -> get_value ($req) if (!defined ($value)) ; | ||
| 72 | return $value + 0 ; | ||
| 73 | } | ||
| 74 | |||
| 75 | |||
| 76 | # --------------------------------------------------------------------------- | ||
| 77 | # | ||
| 78 | # show_control_readonly - output the control as readonly | ||
| 79 | # | ||
| 80 | |||
| 81 | sub xshow_control_readonly | ||
| 82 | { | ||
| 83 | my ($self, $req, $value) = @_ ; | ||
| 84 | |||
| 85 | my $unit = $self->{unit} ; | ||
| 86 | my $unittext = !$unit?'':$self -> form -> convert_text ($self, ($unit =~ /:/)?$unit:'unit:' . lc($unit), $unit, $req) ; | ||
| 87 | $unittext =~ s/^unit:// ; | ||
| 88 | $value = $self -> get_value ($req) if (!defined ($value)) ; | ||
| 89 | |||
| 90 | $self -> SUPER::show_control_readonly ($req, $value, $unit && $value ne ''?$unittext:'') ; | ||
| 91 | } | ||
| 92 | |||
| 93 | # ------------------------------------------------------------------------------------------ | ||
| 94 | # | ||
| 95 | # init_data - daten aufteilen | ||
| 96 | # | ||
| 97 | |||
| 98 | sub init_data | ||
| 99 | { | ||
| 100 | my ($self, $req, $parentctrl, $force) = @_ ; | ||
| 101 | |||
| 102 | my $fdat = $req -> {docdata} || \%fdat ; | ||
| 103 | my $name = $self->{name} ; | ||
| 104 | my $val = $fdat->{$name} ; | ||
| 105 | return if ($val eq '' || (!$force && $req -> {"ef_number_init_done_$name"})) ; | ||
| 106 | |||
| 107 | my $num = $self -> get_display_text ($req, $val, 1) ; | ||
| 108 | |||
| 109 | $fdat->{$name} = $num ; | ||
| 110 | $req -> {"ef_number_init_done_$name"} = 1 ; | ||
| 111 | } | ||
| 112 | |||
| 113 | # --------------------------------------------------------------------------- | ||
| 114 | # | ||
| 115 | # init_markup - add any dynamic markup to the form data | ||
| 116 | # | ||
| 117 | |||
| 118 | sub init_markup | ||
| 119 | |||
| 120 | { | ||
| 121 | my ($self, $req, $parentctl, $method) = @_ ; | ||
| 122 | |||
| 123 | return if (!$self -> is_readonly($req) && (! $parentctl || ! $parentctl -> is_readonly($req))) ; | ||
| 124 | |||
| 125 | my $fdat = $req -> {docdata} || \%fdat ; | ||
| 126 | my $name = $self->{name} ; | ||
| 127 | my $val = $fdat->{$name} ; | ||
| 128 | return if ($val eq '' || ($req -> {"ef_number_init_done_$name"})) ; | ||
| 129 | |||
| 130 | my $num = $self -> get_display_text ($req, $val) ; | ||
| 131 | |||
| 132 | $fdat->{$name} = $num ; | ||
| 133 | $req -> {"ef_number_init_done_$name"} = 1 ; | ||
| 134 | } | ||
| 135 | |||
| 136 | # --------------------------------------------------------------------------- | ||
| 137 | # | ||
| 138 | # get_validate_auto_rules - get rules for validation, in case user did | ||
| 139 | # not specify any | ||
| 140 | # | ||
| 141 | |||
| 142 | sub get_validate_auto_rules | ||
| 143 | { | ||
| 144 | my ($self, $req) = @_ ; | ||
| 145 | |||
| 146 | return [ $self -> {required}?(required => 1):(emptyok => 1), -type => 'PosInteger' ] ; | ||
| 147 | } | ||
| 148 | |||
| 149 | 1 ; | ||
| 150 | |||
| 151 | __EMBPERL__ | ||
| 152 | |||
| 153 | [# --------------------------------------------------------------------------- | ||
| 154 | # | ||
| 155 | # show_control - output the control | ||
| 156 | #] | ||
| 157 | |||
| 158 | [$ sub show_control ($self, $req) | ||
| 159 | |||
| 160 | $self->{size} ||= 10 ; | ||
| 161 | |||
| 162 | my $unit = $self->{unit} ; | ||
| 163 | my $unittext = !$unit?'':$self -> form -> convert_text ($self, ($unit =~ /:/)?$unit:'unit:' . lc($unit), $unit, $req) ; | ||
| 164 | $unittext =~ s/^unit:// ; | ||
| 165 | $] | ||
| 166 | [- $self -> SUPER::show_control ; -] | ||
| 167 | [$if ($unit) $][+ $unittext +][$endif$] | ||
| 168 | [$endsub$] | ||
| 169 | |||
| 170 | __END__ |