| 1 | package HTML::TurboForm::Element; | ||
| 2 | |||
| 3 | use warnings; | ||
| 4 | use strict; | ||
| 5 | use Scalar::MoreUtils qw(empty); | ||
| 6 | use base qw/ Class::Accessor /; | ||
| 7 | __PACKAGE__->mk_accessors( qw/ params submit wrapper errorclass pure default dbsearchfield dbdata optionstext dbop dbid dblabel ignore_dbix type id name label text value request options optionsnum class left_class limit right_class row_class attributes table submit columns / ); | ||
| 8 | |||
| 9 | sub new{ | ||
| 10 | my ($class, $request) = @_; | ||
| 11 | |||
| 12 | my $self = $class->SUPER::new( $request ); | ||
| 13 | $self->{view} =''; | ||
| 14 | $self->{submitted} = 0; | ||
| 15 | $self->{submitted} = 1 if ($request->{ $self->name }); | ||
| 16 | |||
| 17 | if ($self->dbdata and $self->dbid and $self->dblabel){ | ||
| 18 | my @t = @{ $self->dbdata }; | ||
| 19 | foreach (@t){ | ||
| 20 | my $label_method = $self->dblabel; | ||
| 21 | my $value_method = $self->dbid; | ||
| 22 | my $l=$_->$label_method; | ||
| 23 | my $v=$_->$value_method; | ||
| 24 | $self->options->{$l}=$v; | ||
| 25 | } | ||
| 26 | } | ||
| 27 | |||
| 28 | if ($self->submit){ | ||
| 29 | @{$self->{modules}} = ('jquery/jquery'); | ||
| 30 | $self->{js} = ' $("#'.$self->name.'").'.$self->submit.'(function(){$("form")[0].submit(); }); '; | ||
| 31 | } | ||
| 32 | |||
| 33 | if ($self->dbdata and $self->dbid and not $self->dblabel){ | ||
| 34 | my @t = @{ $self->dbdata }; | ||
| 35 | my @tmp; | ||
| 36 | foreach (@t){ | ||
| 37 | my $value_method = $self->dbid; | ||
| 38 | my $v=$_->$value_method; | ||
| 39 | push(@tmp,$v); | ||
| 40 | } | ||
| 41 | @{$self->{options}} = @tmp; | ||
| 42 | } | ||
| 43 | |||
| 44 | $self->init(); | ||
| 45 | return $self; | ||
| 46 | } | ||
| 47 | |||
| 48 | sub init{ | ||
| 49 | my ($self) = @_; | ||
| 50 | } | ||
| 51 | |||
| 52 | sub add_options{ | ||
| 53 | my ($self, $opt) = @_; | ||
| 54 | $self->{options} = $opt; | ||
| 55 | } | ||
| 56 | |||
| 57 | sub reset_options{ | ||
| 58 | my ($self, $opt) = @_; | ||
| 59 | $self->{dbdata}=[]; | ||
| 60 | $self->{options}=[]; | ||
| 61 | $self->{options} = $opt; | ||
| 62 | } | ||
| 63 | |||
| 64 | sub freeze{ | ||
| 65 | my($self) =@_; | ||
| 66 | } | ||
| 67 | |||
| 68 | sub populate{ | ||
| 69 | my($self) =@_; | ||
| 70 | } | ||
| 71 | |||
| 72 | sub get_attr{ | ||
| 73 | my ($self) =@_; | ||
| 74 | my $result=""; | ||
| 75 | |||
| 76 | while ( my( $key,$value) = each %{$self->{attributes}}){ | ||
| 77 | if ($value) { | ||
| 78 | $result.=' '.$key.'="'.$value.'"'; | ||
| 79 | } else { | ||
| 80 | $result.=' '.$key; | ||
| 81 | } | ||
| 82 | } | ||
| 83 | |||
| 84 | return $result.' '; | ||
| 85 | } | ||
| 86 | |||
| 87 | sub check_param{ | ||
| 88 | my ($self, $name)=@_; | ||
| 89 | my $result=0; | ||
| 90 | if ( exists($self->{params}->{ $name })) { | ||
| 91 | $result=1; | ||
| 92 | } | ||
| 93 | return $result; | ||
| 94 | } | ||
| 95 | |||
| 96 | sub get_dbix{ | ||
| 97 | my ($self)=@_; | ||
| 98 | |||
| 99 | if (!$self->ignore_dbix) { | ||
| 100 | my $dbname=$self->name if ($self->name); | ||
| 101 | $dbname =$self->dbsearchfield if ($self->dbsearchfield); | ||
| 102 | |||
| 103 | if ($self->type eq 'Select'){ return 0 if ($self->get_value() eq '-1'); } | ||
| 104 | |||
| 105 | if($self->get_value() ne '') { | ||
| 106 | return { $dbname => $self->get_value()}; | ||
| 107 | } else { | ||
| 108 | return 0; | ||
| 109 | } | ||
| 110 | } else {return 0;} | ||
| 111 | } | ||
| 112 | |||
| 113 | sub vor{ | ||
| 114 | my ($self,$options)=@_; | ||
| 115 | |||
| 116 | return "" if ( $self->pure ); | ||
| 117 | my $error=''; | ||
| 118 | |||
| 119 | $error=$options->{error_message} if $options->{error_message}; | ||
| 120 | my $result=''; | ||
| 121 | my $table=''; | ||
| 122 | |||
| 123 | my $rwc=''; | ||
| 124 | my $rtc=''; | ||
| 125 | my $ltc=''; | ||
| 126 | my $class='class="form_row"'; | ||
| 127 | my $errorclass=" ".$self->errorclass if ($self->errorclass); | ||
| 128 | |||
| 129 | if ($self->{class}) { $class='class="'.$self->{class}.'"'; } | ||
| 130 | if ($self->{row_class}) { $rwc = " class='".$self->{row_class}."' "; } | ||
| 131 | if ($self->{right_class}) { $rtc = " class='".$self->{right_class}."' "; } | ||
| 132 | if ($self->{left_class}) { $ltc = " class='".$self->{left_class}."' "; } | ||
| 133 | |||
| 134 | if ($self->{view} eq '') { | ||
| 135 | $error="<div class='form_error'>$error</div>" if ($error ne ''); | ||
| 136 | |||
| 137 | $self->label('') if (!$self->label); | ||
| 138 | $errorclass='' if (!$errorclass); | ||
| 139 | $result=$table."<div ".$class.$rwc.">".$error. | ||
| 140 | "<div class='form_left'".$ltc.">".$self->label."</div>". | ||
| 141 | "<div class='form_right".$errorclass."'".$rtc.">"; | ||
| 142 | |||
| 143 | #$result=$table."<div ".$class.$rwc.">".$error. | ||
| 144 | # "<div class='form_left'".$ltc.">".$self->label."</div>". | ||
| 145 | # "<div class='form_right'".$rtc.">"; | ||
| 146 | |||
| 147 | $result=$table."<div ".$class.$rwc.">" if ($self->type eq "Html"); | ||
| 148 | } | ||
| 149 | |||
| 150 | if ($self->{view} eq 'table') { | ||
| 151 | $error='<tr><td colspan="2">'.$error.'</td></tr>' if ($error ne ''); | ||
| 152 | |||
| 153 | $table='' if (!$table); | ||
| 154 | $error='' if (!$error); | ||
| 155 | $class=''; | ||
| 156 | $rwc='' if (!$rwc); | ||
| 157 | $rtc='' if (!$rtc); | ||
| 158 | $self->label('') if (!$self->label); | ||
| 159 | |||
| 160 | $result = $table. $error. "<tr ". $class. $rwc.">". | ||
| 161 | "<td ".$ltc.">".$self->label."</td>". | ||
| 162 | "<td ".$rtc.">"; | ||
| 163 | |||
| 164 | $result=$table.'<tr><td colspan="2" '.$class.$rwc.'>' if ($self->type eq "Html"); | ||
| 165 | } | ||
| 166 | |||
| 167 | if ($self->{view} eq 'column') { | ||
| 168 | $self->label('') if (!$self->label); | ||
| 169 | $result='<td>'.$self->label.'</td><td>'; | ||
| 170 | $result.=$error.'<br />' if ($error ne ''); | ||
| 171 | } | ||
| 172 | |||
| 173 | if ($self->wrapper){ | ||
| 174 | my $wrap=$self->wrapper; | ||
| 175 | my $s=''; | ||
| 176 | $s=$self->label if (!$s); | ||
| 177 | $wrap=~s/<label>/$s/g; | ||
| 178 | $wrap=~s/<error>//g if (!$error); | ||
| 179 | $wrap=~s/<error>/$error/g if ($error ne ''); | ||
| 180 | my $pos=index($wrap,'<element>'); | ||
| 181 | $result=substr($wrap,0,$pos); | ||
| 182 | $self->{after_wrap}=substr($wrap,$pos+9); | ||
| 183 | } | ||
| 184 | |||
| 185 | return $result; | ||
| 186 | } | ||
| 187 | |||
| 188 | sub nach{ | ||
| 189 | my ($self)=@_; | ||
| 190 | |||
| 191 | return "" if ($self->pure ); | ||
| 192 | my $result= "</div></div>"; | ||
| 193 | my $table=''; | ||
| 194 | $result='' if ($self->wrapper); | ||
| 195 | |||
| 196 | $result="</div>" if ($self->type eq "Html"); | ||
| 197 | $result="</td></tr>" if ($self->{view} eq 'table'); | ||
| 198 | $result="</td>" if ($self->{view} eq 'column'); | ||
| 199 | |||
| 200 | if ($self->wrapper){ | ||
| 201 | $result=$self->{after_wrap} if ($self->{after_wrap}); | ||
| 202 | } | ||
| 203 | $result.="\n"; | ||
| 204 | return $result; | ||
| 205 | } | ||
| 206 | |||
| 207 | sub get_label{ | ||
| 208 | my ($self) = @_; | ||
| 209 | my $result=''; | ||
| 210 | $result=$self->label if $self->label; | ||
| 211 | return $result; | ||
| 212 | } | ||
| 213 | |||
| 214 | sub get_value{ | ||
| 215 | my ($self) = @_; | ||
| 216 | my $result=''; | ||
| 217 | $result=$self->{request}->{$self->name} if (!empty($self->{request}->{$self->name})); | ||
| 218 | return $result; | ||
| 219 | } | ||
| 220 | |||
| 221 | 1; | ||
| 222 | |||
| 223 | |||
| 224 | __END__ |