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