| 1 | #============================================================================= | ||
| 2 | # | ||
| 3 | # Copyright (c) 2010 Ars Aperta, Itaapy, Pierlis, Talend. | ||
| 4 | # Copyright (c) 2012 Jean-Marie Gouarné. | ||
| 5 | # Author: Jean-Marie Gouarné <jean.marie.gouarne@online.fr> | ||
| 6 | # | ||
| 7 | #============================================================================= | ||
| 8 | use 5.010_001; | ||
| 9 | use strict; | ||
| 10 | #============================================================================= | ||
| 11 | # Variable fields | ||
| 12 | #============================================================================= | ||
| 13 | package ODF::lpOD::Field; | ||
| 14 | use base 'ODF::lpOD::Element'; | ||
| 15 | our $VERSION = '1.003'; | ||
| 16 | use constant PACKAGE_DATE => '2012-03-28T09:14:15'; | ||
| 17 | use ODF::lpOD::Common; | ||
| 18 | #----------------------------------------------------------------------------- | ||
| 19 | |||
| 20 | sub _create { ODF::lpOD::Field->create(@_) } | ||
| 21 | |||
| 22 | #----------------------------------------------------------------------------- | ||
| 23 | |||
| 24 | sub create | ||
| 25 | { | ||
| 26 | my $caller = shift; | ||
| 27 | my $tag = shift; | ||
| 28 | unless ($tag) | ||
| 29 | { | ||
| 30 | alert "Missing element tag"; | ||
| 31 | return FALSE; | ||
| 32 | } | ||
| 33 | my %opt = process_options | ||
| 34 | ( | ||
| 35 | type => 'string', | ||
| 36 | value => undef, | ||
| 37 | text => undef, | ||
| 38 | @_ | ||
| 39 | ); | ||
| 40 | my $field = ODF::lpOD::Element->create($tag); | ||
| 41 | unless ($field) | ||
| 42 | { | ||
| 43 | alert "Field $tag creation failure"; | ||
| 44 | return FALSE; | ||
| 45 | } | ||
| 46 | bless $field, __PACKAGE__; | ||
| 47 | $field->set_name($opt{name}); | ||
| 48 | if (defined $opt{text}) | ||
| 49 | { | ||
| 50 | $field->set_text($opt{text}); | ||
| 51 | delete $opt{text}; | ||
| 52 | } | ||
| 53 | unless ($field->set_type($opt{type})) | ||
| 54 | { | ||
| 55 | alert "Type setting failure"; | ||
| 56 | $field->delete; return FALSE; | ||
| 57 | } | ||
| 58 | if (defined $opt{value}) | ||
| 59 | { | ||
| 60 | unless ($field->set_value($opt{value})) | ||
| 61 | { | ||
| 62 | alert "Value setting failure"; | ||
| 63 | $field->delete; return FALSE; | ||
| 64 | } | ||
| 65 | } | ||
| 66 | |||
| 67 | return $field; | ||
| 68 | } | ||
| 69 | |||
| 70 | #----------------------------------------------------------------------------- | ||
| 71 | |||
| 72 | sub get_type | ||
| 73 | { | ||
| 74 | my $self = shift; | ||
| 75 | return $self->att('office:value-type') // 'string'; | ||
| 76 | } | ||
| 77 | |||
| 78 | sub set_type | ||
| 79 | { | ||
| 80 | my $self = shift; | ||
| 81 | my $type = shift; | ||
| 82 | if (!defined $type) | ||
| 83 | { | ||
| 84 | $self->del_attribute('office:value-type'); | ||
| 85 | $self->del_attribute('office:currency'); | ||
| 86 | } | ||
| 87 | elsif (grep $type eq $_, @ODF::lpOD::Common::DATA_TYPES) | ||
| 88 | { | ||
| 89 | $self->set_att('office:value-type', $type); | ||
| 90 | $self->del_attribute('office:currency') | ||
| 91 | unless $type eq 'currency'; | ||
| 92 | } | ||
| 93 | else | ||
| 94 | { | ||
| 95 | alert "Wrong data type"; $type = FALSE; | ||
| 96 | } | ||
| 97 | return $type; | ||
| 98 | } | ||
| 99 | |||
| 100 | sub get_currency | ||
| 101 | { | ||
| 102 | my $self = shift; | ||
| 103 | return $self->att('office:currency'); | ||
| 104 | } | ||
| 105 | |||
| 106 | sub set_currency | ||
| 107 | { | ||
| 108 | my $self = shift; | ||
| 109 | my $currency = shift; | ||
| 110 | $self->set_type('currency') if $currency; | ||
| 111 | return $self->set_att('office:currency', $currency); | ||
| 112 | } | ||
| 113 | |||
| 114 | sub get_value | ||
| 115 | { | ||
| 116 | my $self = shift; | ||
| 117 | my $type = $self->get_type(); | ||
| 118 | my $value; | ||
| 119 | if ($type eq 'string') | ||
| 120 | { | ||
| 121 | $value = $self->get_attribute('office:string-value'); | ||
| 122 | } | ||
| 123 | elsif ($type eq 'date' || $type eq 'time') | ||
| 124 | { | ||
| 125 | my $attr = 'office:' . $type . '-value'; | ||
| 126 | $value = $self->att($attr); | ||
| 127 | } | ||
| 128 | elsif ($type eq 'float' || $type eq 'currency' || $type eq 'percentage') | ||
| 129 | { | ||
| 130 | $value = $self->att('office:value'); | ||
| 131 | } | ||
| 132 | elsif ($type eq 'boolean') | ||
| 133 | { | ||
| 134 | my $v = $self->att('office:boolean-value'); | ||
| 135 | $value = defined $v ? is_true($v) : undef; | ||
| 136 | } | ||
| 137 | return wantarray ? ($value, $type) : $value; | ||
| 138 | } | ||
| 139 | |||
| 140 | sub set_value | ||
| 141 | { | ||
| 142 | my $self = shift; | ||
| 143 | my $value = shift; | ||
| 144 | return undef unless defined $value; | ||
| 145 | my $type = $self->get_type(); | ||
| 146 | |||
| 147 | my $v = check_odf_value($value, $type); | ||
| 148 | if ($type eq 'string') | ||
| 149 | { | ||
| 150 | $self->set_attribute('office:string-value' => $v); | ||
| 151 | } | ||
| 152 | elsif ($type eq 'date') | ||
| 153 | { | ||
| 154 | if (is_numeric($v)) | ||
| 155 | { | ||
| 156 | $v = iso_date($v); | ||
| 157 | } | ||
| 158 | $self->set_att('office:date-value', $v); | ||
| 159 | } | ||
| 160 | elsif ($type eq 'time') | ||
| 161 | { | ||
| 162 | $self->set_att('office:time-value', $v); | ||
| 163 | } | ||
| 164 | elsif ($type eq 'float' || $type eq 'currency' || $type eq 'percentage') | ||
| 165 | { | ||
| 166 | $self->set_att('office:value', $v); | ||
| 167 | } | ||
| 168 | elsif ($type eq 'boolean') | ||
| 169 | { | ||
| 170 | $self->set_att('office:boolean-value', $v); | ||
| 171 | } | ||
| 172 | return $self->get_value; | ||
| 173 | } | ||
| 174 | |||
| 175 | #============================================================================= | ||
| 176 | package ODF::lpOD::Variable; | ||
| 177 | use base 'ODF::lpOD::Field'; | ||
| 178 | our $VERSION = '1.000'; | ||
| 179 | use constant PACKAGE_DATE => '2010-12-24T13:58:47'; | ||
| 180 | use ODF::lpOD::Common; | ||
| 181 | #----------------------------------------------------------------------------- | ||
| 182 | |||
| 183 | sub set_text | ||
| 184 | { | ||
| 185 | my $self = shift; | ||
| 186 | my $type = $self->get_type; | ||
| 187 | unless ($type eq 'text') | ||
| 188 | { | ||
| 189 | alert "Text not allowed for $type variables"; | ||
| 190 | return FALSE; | ||
| 191 | } | ||
| 192 | return $self->set_attribute('office:string-value' => shift); | ||
| 193 | } | ||
| 194 | |||
| 195 | sub get_text | ||
| 196 | { | ||
| 197 | my $self = shift; | ||
| 198 | return $self->get_attribute('office:string-value'); | ||
| 199 | } | ||
| 200 | |||
| 201 | #============================================================================= | ||
| 202 | package ODF::lpOD::UserVariable; | ||
| 203 | use base 'ODF::lpOD::Variable'; | ||
| 204 | our $VERSION = '1.002'; | ||
| 205 | use constant PACKAGE_DATE => '2012-03-14T15:26:44'; | ||
| 206 | use ODF::lpOD::Common; | ||
| 207 | #----------------------------------------------------------------------------- | ||
| 208 | |||
| 209 | sub context_tag { 'text:user-field-decls' } | ||
| 210 | sub context_path { CONTENT, ('//' . context_tag) } | ||
| 211 | |||
| 212 | sub _create { ODF::lpOD::UserVariable->create(@_) } | ||
| 213 | |||
| 214 | #----------------------------------------------------------------------------- | ||
| 215 | |||
| 216 | sub create | ||
| 217 | { | ||
| 218 | my $caller = shift; | ||
| 219 | return bless | ||
| 220 | ODF::lpOD::Field->create('text:user-field-decl', @_), | ||
| 221 | __PACKAGE__; | ||
| 222 | } | ||
| 223 | |||
| 224 | #============================================================================= | ||
| 225 | package ODF::lpOD::SimpleVariable; | ||
| 226 | use base 'ODF::lpOD::Variable'; | ||
| 227 | our $VERSION = '1.002'; | ||
| 228 | use constant PACKAGE_DATE => '2012-03-14T15:28:42'; | ||
| 229 | use ODF::lpOD::Common; | ||
| 230 | #----------------------------------------------------------------------------- | ||
| 231 | |||
| 232 | sub context_tag { 'text:variable-decls'} | ||
| 233 | sub context_path { CONTENT, ('//' . context_tag) } | ||
| 234 | |||
| 235 | #----------------------------------------------------------------------------- | ||
| 236 | |||
| 237 | sub _create { ODF::lpOD::SimpleVariable->create(@_) } | ||
| 238 | |||
| 239 | #----------------------------------------------------------------------------- | ||
| 240 | |||
| 241 | sub create | ||
| 242 | { | ||
| 243 | my $caller = shift; | ||
| 244 | return bless | ||
| 245 | ODF::lpOD::Field->create('text:variable-decl', @_), | ||
| 246 | __PACKAGE__; | ||
| 247 | } | ||
| 248 | |||
| 249 | #----------------------------------------------------------------------------- | ||
| 250 | |||
| 251 | sub set_value {} | ||
| 252 | sub get_value {} | ||
| 253 | |||
| 254 | #============================================================================= | ||
| 255 | package ODF::lpOD::TextField; | ||
| 256 | use base 'ODF::lpOD::Field'; | ||
| 257 | our $VERSION = '1.000'; | ||
| 258 | use constant PACKAGE_DATE => '2010-12-24T13:59:24'; | ||
| 259 | use ODF::lpOD::Common; | ||
| 260 | #----------------------------------------------------------------------------- | ||
| 261 | |||
| 262 | our @TYPES = | ||
| 263 | ( | ||
| 264 | 'date', 'time', 'page number', 'page continuation', 'page count', | ||
| 265 | 'sender firstname', 'sender lastname', 'sender initials', | ||
| 266 | 'sender title', 'sender position', 'sender email', | ||
| 267 | 'sender phone private', 'sender fax', 'sender company', | ||
| 268 | 'sender phone work', 'sender street', 'sender postal code', | ||
| 269 | 'sender city', 'sender country', 'sender state or province', | ||
| 270 | 'creator', 'author name', 'author initials', 'chapter', 'file name', | ||
| 271 | 'template name', 'sheet name', 'title', 'subject', | ||
| 272 | 'user field get', 'variable' | ||
| 273 | ); | ||
| 274 | |||
| 275 | sub check_type | ||
| 276 | { | ||
| 277 | my $type = shift; | ||
| 278 | return (grep $type eq $_, @TYPES) ? $type : FALSE; | ||
| 279 | } | ||
| 280 | |||
| 281 | sub types { @TYPES } | ||
| 282 | |||
| 283 | sub classify | ||
| 284 | { | ||
| 285 | my $arg = shift; | ||
| 286 | my ($tag, $elt, $class); | ||
| 287 | if (ref $arg) | ||
| 288 | { | ||
| 289 | $elt = $arg; | ||
| 290 | $tag = $elt->get_tag; | ||
| 291 | } | ||
| 292 | else | ||
| 293 | { | ||
| 294 | $tag = $arg; | ||
| 295 | } | ||
| 296 | return undef unless $tag =~ /^text:/; | ||
| 297 | $tag =~ s/^.*://; $tag =~ s/-/ /g; | ||
| 298 | if (grep $tag eq $_, @TYPES) | ||
| 299 | { | ||
| 300 | $class = __PACKAGE__; | ||
| 301 | return $elt ? bless $elt, $class : $class; | ||
| 302 | } | ||
| 303 | return undef; | ||
| 304 | } | ||
| 305 | |||
| 306 | sub set_class | ||
| 307 | { | ||
| 308 | my $self = shift; | ||
| 309 | return classify($self); | ||
| 310 | } | ||
| 311 | |||
| 312 | sub set_style | ||
| 313 | { | ||
| 314 | my $self = shift; | ||
| 315 | return $self->set_attribute('style:data-style-name' => shift); | ||
| 316 | } | ||
| 317 | |||
| 318 | sub get_style | ||
| 319 | { | ||
| 320 | my $self = shift; | ||
| 321 | return $self->get_attribute('style:data-style-name'); | ||
| 322 | } | ||
| 323 | |||
| 324 | #----------------------------------------------------------------------------- | ||
| 325 | |||
| 326 | sub get_value | ||
| 327 | { | ||
| 328 | my $self = shift; | ||
| 329 | my $att = $self->get_tag() . '-value'; | ||
| 330 | return $self->get_attribute($att) // $self->get_text; | ||
| 331 | } | ||
| 332 | |||
| 333 | sub set_value { my $self = shift; $self->not_allowed } | ||
| 334 | sub set_text { my $self = shift; $self->not_allowed } | ||
| 335 | |||
| 336 | #============================================================================= | ||
| 337 | 1; |