| 1 | package WWW::Garden::Design::Export; | ||
| 2 | |||
| 3 | use Moo::Role; | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use utf8; | ||
| 7 | use warnings; | ||
| 8 | |||
| 9 | use boolean; | ||
| 10 | |||
| 11 | use Encode 'encode'; | ||
| 12 | |||
| 13 | use File::Spec; | ||
| 14 | |||
| 15 | use Mojo::Log; | ||
| 16 | |||
| 17 | use Sort::Naturally; | ||
| 18 | |||
| 19 | use SVG::Grid; | ||
| 20 | |||
| 21 | use Text::CSV; | ||
| 22 | use Text::Xslate 'mark_raw'; | ||
| 23 | |||
| 24 | use Types::Standard qw/Any Int HashRef Str/; | ||
| 25 | |||
| 26 | use WWW::Garden::Design::Database; | ||
| 27 | use WWW::Garden::Design::Util::Config; | ||
| 28 | |||
| 29 | has all => | ||
| 30 | ( | ||
| 31 | default => sub{return 'No'}, | ||
| 32 | is => 'rw', | ||
| 33 | isa => Str, | ||
| 34 | required => 0, | ||
| 35 | ); | ||
| 36 | |||
| 37 | has config => | ||
| 38 | ( | ||
| 39 | default => sub{WWW::Garden::Design::Util::Config -> new -> config}, | ||
| 40 | is => 'rw', | ||
| 41 | isa => HashRef, | ||
| 42 | required => 0, | ||
| 43 | ); | ||
| 44 | |||
| 45 | has db => | ||
| 46 | ( | ||
| 47 | is => 'rw', | ||
| 48 | isa => Any, | ||
| 49 | required => 0, | ||
| 50 | ); | ||
| 51 | |||
| 52 | has export_columns => | ||
| 53 | ( | ||
| 54 | default => sub{return {} }, | ||
| 55 | is => 'rw', | ||
| 56 | isa => HashRef, | ||
| 57 | required => 0, | ||
| 58 | ); | ||
| 59 | |||
| 60 | has export_type => | ||
| 61 | ( | ||
| 62 | default => sub{return 0}, | ||
| 63 | is => 'rw', | ||
| 64 | isa => Int, | ||
| 65 | required => 0, | ||
| 66 | ); | ||
| 67 | |||
| 68 | has output_file => | ||
| 69 | ( | ||
| 70 | default => sub{return ''}, | ||
| 71 | is => 'rw', | ||
| 72 | isa => Str, | ||
| 73 | required => 1, | ||
| 74 | ); | ||
| 75 | |||
| 76 | has property_name => | ||
| 77 | ( | ||
| 78 | default => sub{return 'Ron'}, | ||
| 79 | is => 'rw', | ||
| 80 | isa => Str, | ||
| 81 | required => 1, | ||
| 82 | ); | ||
| 83 | |||
| 84 | has standalone_page => | ||
| 85 | ( | ||
| 86 | default => sub{return 0}, | ||
| 87 | is => 'rw', | ||
| 88 | isa => Int, | ||
| 89 | required => 0, | ||
| 90 | ); | ||
| 91 | |||
| 92 | our $VERSION = '0.97'; | ||
| 93 | |||
| 94 | # ----------------------------------------------- | ||
| 95 | |||
| 96 | sub attribute_types2csv | ||
| 97 | { | ||
| 98 | my($self, $csv) = @_; | ||
| 99 | my($attribute_types_table) = $self -> db -> read_table('attribute_types'); | ||
| 100 | my($file_name) = $self -> output_file =~ s/flowers.csv/attribute_types.csv/r; | ||
| 101 | |||
| 102 | $self -> db -> logger -> info("Writing to $file_name"); | ||
| 103 | |||
| 104 | open(my $fh, '>:encoding(utf-8)', $file_name) || die "Can't open(> $file_name): $!"; | ||
| 105 | |||
| 106 | # Column names are in order left-to-right. | ||
| 107 | |||
| 108 | $csv -> combine(qw/name sequence range/); | ||
| 109 | |||
| 110 | print $fh $csv -> string, "\n"; | ||
| 111 | |||
| 112 | for my $attribute_type (@$attribute_types_table) | ||
| 113 | { | ||
| 114 | $csv -> combine | ||
| 115 | ( | ||
| 116 | $$attribute_type{name}, | ||
| 117 | $$attribute_type{sequence}, | ||
| 118 | $$attribute_type{range}, | ||
| 119 | ); | ||
| 120 | |||
| 121 | print $fh $csv -> string, "\n"; | ||
| 122 | } | ||
| 123 | |||
| 124 | close $fh; | ||
| 125 | |||
| 126 | $self -> db -> logger -> info("Wrote $file_name"); | ||
| 127 | |||
| 128 | } # End of attribute_types2csv. | ||
| 129 | |||
| 130 | # ----------------------------------------------- | ||
| 131 | |||
| 132 | sub attributes2csv | ||
| 133 | { | ||
| 134 | my($self, $csv, $flowers) = @_; | ||
| 135 | my($file_name) = $self -> output_file =~ s/flowers.csv/attributes.csv/r; | ||
| 136 | |||
| 137 | $self -> db -> logger -> info("Writing to $file_name"); | ||
| 138 | |||
| 139 | open(my $fh, '>:encoding(utf-8)', $file_name) || die "Can't open(> $file_name): $!"; | ||
| 140 | |||
| 141 | # Column names are in order left-to-right. | ||
| 142 | |||
| 143 | $csv -> combine(qw/common_name attribute_name range/); | ||
| 144 | |||
| 145 | print $fh $csv -> string, "\n"; | ||
| 146 | |||
| 147 | my($common_name); | ||
| 148 | |||
| 149 | for my $flower (sort{uc($$a{common_name}) cmp uc($$b{common_name})} @$flowers) | ||
| 150 | { | ||
| 151 | next if ($$flower{publish} eq 'No'); | ||
| 152 | |||
| 153 | $common_name = $$flower{common_name}; | ||
| 154 | |||
| 155 | for my $attribute (@{$$flower{attributes} }) | ||
| 156 | { | ||
| 157 | $csv -> combine | ||
| 158 | ( | ||
| 159 | $common_name, | ||
| 160 | $$attribute{name}, | ||
| 161 | $$attribute{range}, | ||
| 162 | ); | ||
| 163 | |||
| 164 | print $fh $csv -> string, "\n"; | ||
| 165 | } | ||
| 166 | } | ||
| 167 | |||
| 168 | close $fh; | ||
| 169 | |||
| 170 | $self -> db -> logger -> info("Wrote $file_name"); | ||
| 171 | |||
| 172 | } # End of attributes2csv. | ||
| 173 | |||
| 174 | # ----------------------------------------------- | ||
| 175 | |||
| 176 | sub as_csv | ||
| 177 | { | ||
| 178 | my($self) = @_; | ||
| 179 | |||
| 180 | die "No output file specified\n" if (! $self -> output_file); | ||
| 181 | |||
| 182 | my($csv) = Text::CSV -> new({always_quote => 1, binary => 1}); | ||
| 183 | my($flowers) = $self -> flowers2csv($csv); | ||
| 184 | |||
| 185 | $self -> attribute_types2csv($csv); | ||
| 186 | $self -> attributes2csv($csv, $flowers); | ||
| 187 | $self -> constants2csv($csv); | ||
| 188 | $self -> images2csv($csv, $flowers); | ||
| 189 | $self -> notes2csv($csv, $flowers); | ||
| 190 | $self -> urls2csv($csv, $flowers); | ||
| 191 | |||
| 192 | my($features) = $self -> features2csv($csv); | ||
| 193 | my($property_id2name) = $self -> properties2csv($csv); | ||
| 194 | my($garden_id2name) = $self -> gardens2csv($csv, $property_id2name); | ||
| 195 | |||
| 196 | $self -> flower_locations2csv($csv, $flowers, $property_id2name, $garden_id2name); | ||
| 197 | $self -> feature_locations2csv($csv, $features, $property_id2name, $garden_id2name); | ||
| 198 | $self -> db -> logger -> info('Finished exporting all CSV files'); | ||
| 199 | |||
| 200 | # Return 0 for OK and 1 for error. | ||
| 201 | |||
| 202 | return 0; | ||
| 203 | |||
| 204 | } # End of as_csv. | ||
| 205 | |||
| 206 | # ----------------------------------------------- | ||
| 207 | |||
| 208 | sub as_html | ||
| 209 | { | ||
| 210 | my($self) = @_; | ||
| 211 | my($export_type) = $self -> export_type; | ||
| 212 | my(%columns) = %{$self -> export_columns}; | ||
| 213 | my($count) = 0; | ||
| 214 | my($flowers) = $self -> db -> read_flowers_table; | ||
| 215 | my(@heading) = map{ {td => mark_raw($_)} } sort{$columns{$a}{order} <=> $columns{$b}{order} } keys %columns; | ||
| 216 | my($width) = 40; | ||
| 217 | |||
| 218 | my(@aliases); | ||
| 219 | my($column_name); | ||
| 220 | my(@fields); | ||
| 221 | my(@line); | ||
| 222 | my($native, $name); | ||
| 223 | my($offset); | ||
| 224 | my($thumbnail, @tbody); | ||
| 225 | my($text); | ||
| 226 | |||
| 227 | for my $flower (@$flowers) | ||
| 228 | { | ||
| 229 | next if ($$flower{publish} eq 'No'); | ||
| 230 | |||
| 231 | $$flower{count} = ++$count; | ||
| 232 | @aliases = (); | ||
| 233 | @line = (); | ||
| 234 | |||
| 235 | for (@{$$flower{attributes} }) | ||
| 236 | { | ||
| 237 | $native = $$_{range} if ($$_{name} eq 'Native'); | ||
| 238 | } | ||
| 239 | |||
| 240 | for my $key (sort{$columns{$a}{order} <=> $columns{$b}{order} } keys %columns) | ||
| 241 | { | ||
| 242 | $column_name = $columns{$key}{column_name}; | ||
| 243 | $name = ($column_name eq 'native') ? $native : $$flower{$column_name}; | ||
| 244 | |||
| 245 | if ($key eq 'Aliases') | ||
| 246 | { | ||
| 247 | @fields = split(/\s*,\s*/, $name); | ||
| 248 | $text = ''; | ||
| 249 | |||
| 250 | while (my $field = shift @fields) | ||
| 251 | { | ||
| 252 | if (length($text . $field) > $width) | ||
| 253 | { | ||
| 254 | push @aliases, $text; | ||
| 255 | |||
| 256 | $text = $field; | ||
| 257 | } | ||
| 258 | else | ||
| 259 | { | ||
| 260 | $text .= $text ? ", $field" : $field; | ||
| 261 | } | ||
| 262 | } | ||
| 263 | |||
| 264 | $text = join(',<br>', @aliases, $text); | ||
| 265 | } | ||
| 266 | else | ||
| 267 | { | ||
| 268 | $text = $name; | ||
| 269 | } | ||
| 270 | |||
| 271 | push @line, {td => mark_raw($text)}; | ||
| 272 | } | ||
| 273 | |||
| 274 | $offset = $#line; | ||
| 275 | $line[$offset] = | ||
| 276 | { | ||
| 277 | td => mark_raw | ||
| 278 | ( | ||
| 279 | qq|<a href='$$flower{web_page_url}' target = '_blank'><img src='$$flower{thumbnail_url}' alt = "$$flower{scientific_name}"></a>| | ||
| 280 | ) | ||
| 281 | }; | ||
| 282 | |||
| 283 | push @tbody, [@line]; | ||
| 284 | } | ||
| 285 | |||
| 286 | my(@thead) = [@heading]; | ||
| 287 | my($constants) = $self -> db -> read_constants_table; | ||
| 288 | my($js4datatable) = mark_raw($self -> init_datatable); | ||
| 289 | my($tx) = Text::Xslate -> new | ||
| 290 | ( | ||
| 291 | input_layer => '', | ||
| 292 | path => $$constants{template_path}, | ||
| 293 | ); | ||
| 294 | |||
| 295 | return encode('utf-8', $tx -> render | ||
| 296 | ( | ||
| 297 | $self -> standalone_page ? 'standalone.page.tx' : 'basic.table.tx', | ||
| 298 | { | ||
| 299 | js4datatable => $js4datatable, | ||
| 300 | thead => \@thead, | ||
| 301 | tbody => \@tbody, | ||
| 302 | } | ||
| 303 | ) ); | ||
| 304 | |||
| 305 | } # End of as_html. | ||
| 306 | |||
| 307 | # ----------------------------------------------- | ||
| 308 | |||
| 309 | sub constants2csv | ||
| 310 | { | ||
| 311 | my($self, $csv) = @_; | ||
| 312 | my($constants) = $self -> db -> constants; | ||
| 313 | my($file_name) = $self -> output_file =~ s/flowers.csv/constants.csv/r; | ||
| 314 | |||
| 315 | $self -> db -> logger -> info("Writing to $file_name"); | ||
| 316 | |||
| 317 | open(my $fh, '>:encoding(utf-8)', $file_name) || die "Can't open(> $file_name): $!"; | ||
| 318 | |||
| 319 | # Column names are in order left-to-right. | ||
| 320 | |||
| 321 | $csv -> combine(qw/name value/); | ||
| 322 | |||
| 323 | print $fh $csv -> string, "\n"; | ||
| 324 | |||
| 325 | for my $name (sort keys %$constants) | ||
| 326 | { | ||
| 327 | $csv -> combine | ||
| 328 | ( | ||
| 329 | $name, | ||
| 330 | $$constants{$name}, | ||
| 331 | ); | ||
| 332 | |||
| 333 | print $fh $csv -> string, "\n"; | ||
| 334 | } | ||
| 335 | |||
| 336 | close $fh; | ||
| 337 | |||
| 338 | $self -> db -> logger -> info("Wrote $file_name"); | ||
| 339 | |||
| 340 | } # End of constants2csv. | ||
| 341 | |||
| 342 | # ----------------------------------------------- | ||
| 343 | |||
| 344 | sub export_all_pages | ||
| 345 | { | ||
| 346 | my($self) = @_; | ||
| 347 | my($attribute_types_table) = $self -> db -> read_table('attribute_types'); | ||
| 348 | my($constants) = $self -> db -> constants; | ||
| 349 | my($flowers) = $self -> db -> read_flowers_table; | ||
| 350 | my($target_dir) = File::Spec -> catfile($$constants{homepage_dir}, $$constants{flower_dir}); | ||
| 351 | |||
| 352 | $self -> db -> logger -> info("Export.export_all_pages(). Output directory: $target_dir"); | ||
| 353 | |||
| 354 | # Process each flower looking for others with the same prefix. | ||
| 355 | # The reason for using common name here is it helps when 2 | ||
| 356 | # different plants have the same scientific name, such as | ||
| 357 | # Plectranthus eklonii. | ||
| 358 | |||
| 359 | my($common_name); | ||
| 360 | my(@fields); | ||
| 361 | my($id); | ||
| 362 | my($pig_latin, $prefix, %prefixes); | ||
| 363 | my($scientific_name); | ||
| 364 | |||
| 365 | for my $flower (@$flowers) | ||
| 366 | { | ||
| 367 | next if ($$flower{publish} eq 'No'); | ||
| 368 | |||
| 369 | $common_name = $$flower{common_name}; | ||
| 370 | $id = $$flower{id}; | ||
| 371 | $scientific_name = $$flower{scientific_name}; | ||
| 372 | @fields = split(/\s+/, $scientific_name); | ||
| 373 | $pig_latin = $$flower{pig_latin}; | ||
| 374 | $prefix = $fields[0]; | ||
| 375 | $prefixes{$prefix} = [] if (! $prefixes{$prefix}); | ||
| 376 | |||
| 377 | push @{$prefixes{$prefix} }, [$id, $pig_latin, "$scientific_name => $common_name"]; | ||
| 378 | } | ||
| 379 | |||
| 380 | my($tx) = Text::Xslate -> new | ||
| 381 | ( | ||
| 382 | input_layer => '', | ||
| 383 | path => $$constants{template_path}, | ||
| 384 | ); | ||
| 385 | |||
| 386 | my(@attributes, $aliases); | ||
| 387 | my(@images); | ||
| 388 | my(@links); | ||
| 389 | my(@notes); | ||
| 390 | my($other_id, $other_pig_latin, $other_common_name); | ||
| 391 | my($text); | ||
| 392 | my($url, @urls); | ||
| 393 | my($web_page_name); | ||
| 394 | |||
| 395 | for my $flower (@$flowers) | ||
| 396 | { | ||
| 397 | next if ($$flower{publish} eq 'No'); | ||
| 398 | |||
| 399 | $aliases = $$flower{aliases}; | ||
| 400 | @attributes = (); | ||
| 401 | $common_name = $$flower{common_name}; | ||
| 402 | $id = $$flower{id}; | ||
| 403 | @images = (); | ||
| 404 | @links = (); | ||
| 405 | @notes = (); | ||
| 406 | $scientific_name = $$flower{scientific_name}; | ||
| 407 | $pig_latin = $$flower{pig_latin}; | ||
| 408 | @urls = (); | ||
| 409 | |||
| 410 | # Attributes. | ||
| 411 | |||
| 412 | push @attributes, | ||
| 413 | [ | ||
| 414 | {td => 'Attribute'}, | ||
| 415 | {td => 'Range'}, | ||
| 416 | ]; | ||
| 417 | |||
| 418 | for my $attribute (sort{$$a{sequence} <=> $$b{sequence} } @{$$flower{attributes} }) | ||
| 419 | { | ||
| 420 | push @attributes, | ||
| 421 | [ | ||
| 422 | {td => ucfirst $$attribute{name} }, | ||
| 423 | {td => $$attribute{range} }, | ||
| 424 | ]; | ||
| 425 | } | ||
| 426 | |||
| 427 | # Images. | ||
| 428 | |||
| 429 | push @images, | ||
| 430 | [ | ||
| 431 | {td => 'Descriptions'}, | ||
| 432 | {td => 'Images'}, | ||
| 433 | ]; | ||
| 434 | |||
| 435 | for my $item (@{$$flower{images} }) | ||
| 436 | { | ||
| 437 | push @images, | ||
| 438 | [ | ||
| 439 | {td => mark_raw($$item{description})}, | ||
| 440 | {td => mark_raw("<img src = '$$item{file_name}'>")}, | ||
| 441 | ]; | ||
| 442 | } | ||
| 443 | |||
| 444 | # Links. | ||
| 445 | |||
| 446 | @fields = split(/\s+/, $scientific_name); | ||
| 447 | $prefix = $fields[0]; | ||
| 448 | |||
| 449 | if ($#{$prefixes{$prefix} } > 0) | ||
| 450 | { | ||
| 451 | for my $item (@{$prefixes{$prefix} }) | ||
| 452 | { | ||
| 453 | if ($#links < 0) | ||
| 454 | { | ||
| 455 | push @links, [{td => "Auto-generated links for $scientific_name aka $common_name"}]; | ||
| 456 | } | ||
| 457 | |||
| 458 | ($other_id, $other_pig_latin, $other_common_name) = ($$item[0], $$item[1], $$item[2]); | ||
| 459 | |||
| 460 | if ($other_id != $id) | ||
| 461 | { | ||
| 462 | push @links, [{td => mark_raw("See also <a href = '/Flowers/$other_pig_latin.html'>$other_common_name</a>")}]; | ||
| 463 | } | ||
| 464 | } | ||
| 465 | } | ||
| 466 | |||
| 467 | # Notes. | ||
| 468 | |||
| 469 | for my $note (@{$$flower{notes} }) | ||
| 470 | { | ||
| 471 | $text = $$note{note}; | ||
| 472 | |||
| 473 | if ($text =~ /^http/) | ||
| 474 | { | ||
| 475 | $text = "» <a href = '$text'>$$note{note}</a>"; | ||
| 476 | } | ||
| 477 | else | ||
| 478 | { | ||
| 479 | $text = "» $text"; | ||
| 480 | } | ||
| 481 | |||
| 482 | if ($#notes < 0) | ||
| 483 | { | ||
| 484 | push @notes, | ||
| 485 | [ | ||
| 486 | {td => 'Notes'}, | ||
| 487 | ]; | ||
| 488 | } | ||
| 489 | |||
| 490 | push @notes, | ||
| 491 | [ | ||
| 492 | {td => mark_raw($text)}, | ||
| 493 | ]; | ||
| 494 | } | ||
| 495 | |||
| 496 | # URLs. | ||
| 497 | |||
| 498 | for my $url (@{$$flower{urls} }) | ||
| 499 | { | ||
| 500 | if ($#urls < 0) | ||
| 501 | { | ||
| 502 | push @urls, | ||
| 503 | [ | ||
| 504 | {td => 'URLs'}, | ||
| 505 | ]; | ||
| 506 | } | ||
| 507 | |||
| 508 | push @urls, | ||
| 509 | [ | ||
| 510 | {td => mark_raw("<a href = '$$url{url}'>$$url{url}</a>")}, | ||
| 511 | ]; | ||
| 512 | } | ||
| 513 | |||
| 514 | $web_page_name = File::Spec -> catfile($$constants{homepage_dir}, $$constants{flower_dir}, "$pig_latin.html"); | ||
| 515 | |||
| 516 | $self -> db -> logger -> info("Writing $web_page_name"); | ||
| 517 | |||
| 518 | open(my $fh, '>:encoding(utf-8)', $web_page_name); | ||
| 519 | print $fh $tx -> render | ||
| 520 | ( | ||
| 521 | 'individual.page.tx', | ||
| 522 | { | ||
| 523 | aliases => $aliases eq '' ? '' : "Aliases: $aliases", | ||
| 524 | attributes => \@attributes, | ||
| 525 | common_name => $common_name, | ||
| 526 | height => $$flower{height} || '-', | ||
| 527 | images => \@images, | ||
| 528 | link_count => scalar(@links), # Necessary because Text::Xslate rejects $#$links. | ||
| 529 | links => \@links, | ||
| 530 | note_count => scalar(@notes), | ||
| 531 | notes => \@notes, | ||
| 532 | scientific_name => $scientific_name, | ||
| 533 | title => $scientific_name, | ||
| 534 | url_count => scalar(@urls), | ||
| 535 | urls => \@urls, | ||
| 536 | width => $$flower{width} || '-', | ||
| 537 | } | ||
| 538 | ); | ||
| 539 | close $fh; | ||
| 540 | } | ||
| 541 | |||
| 542 | $self -> db -> logger -> info('Finished exporting all HTML files'); | ||
| 543 | |||
| 544 | # Return 0 for OK and 1 for error. | ||
| 545 | |||
| 546 | return 0; | ||
| 547 | |||
| 548 | } # End of export_all_pages. | ||
| 549 | |||
| 550 | # ----------------------------------------------- | ||
| 551 | |||
| 552 | sub export_features | ||
| 553 | { | ||
| 554 | my($self) = @_; | ||
| 555 | my($constants) = $self -> db -> constants; | ||
| 556 | my($features_table) = $self -> db -> read_features_table; | ||
| 557 | my($target_dir) = File::Spec -> catfile($$constants{homepage_dir}, $$constants{flower_dir}); | ||
| 558 | |||
| 559 | $self -> db -> logger -> info("Export.export_features(). Output directory: $target_dir"); | ||
| 560 | |||
| 561 | my(@file_names); | ||
| 562 | |||
| 563 | for my $feature (sort{$$a{name} cmp $$b{name} } @$features_table) | ||
| 564 | { | ||
| 565 | next if ($$feature{publish} eq 'No'); | ||
| 566 | |||
| 567 | push @file_names, $self -> db -> generate_tile($constants, $feature); | ||
| 568 | } | ||
| 569 | |||
| 570 | my(@heading) = map{ {td => $_} } (qw(Feature Icon) ); | ||
| 571 | my(@row) = [@heading]; | ||
| 572 | my($tx) = Text::Xslate -> new | ||
| 573 | ( | ||
| 574 | input_layer => '', | ||
| 575 | path => $$constants{template_path}, | ||
| 576 | ); | ||
| 577 | |||
| 578 | for my $item (@file_names) | ||
| 579 | { | ||
| 580 | push @row, [{td => $$item{name} }, {td => mark_raw("<object data = '$$constants{homepage_url}$$constants{feature_url}/$$item{file_name}.png'></object>")}]; | ||
| 581 | } | ||
| 582 | |||
| 583 | push @row, [@heading]; | ||
| 584 | |||
| 585 | my($file_name) = File::Spec -> catfile($$constants{homepage_dir}, $$constants{feature_dir}, 'features.html'); | ||
| 586 | |||
| 587 | open(my $fh, '>', $file_name) || die "Can't open: $file_name: $!\n"; | ||
| 588 | print $fh $tx -> render | ||
| 589 | ( | ||
| 590 | ( | ||
| 591 | 'garden.features.tx', | ||
| 592 | { | ||
| 593 | row => \@row, | ||
| 594 | } | ||
| 595 | ) | ||
| 596 | ); | ||
| 597 | |||
| 598 | close $fh; | ||
| 599 | |||
| 600 | $self -> db -> logger -> info('Finished exporting all features'); | ||
| 601 | |||
| 602 | # Return 0 for OK and 1 for error. | ||
| 603 | |||
| 604 | return 0; | ||
| 605 | |||
| 606 | } # End of export_features. | ||
| 607 | |||
| 608 | # ----------------------------------------------- | ||
| 609 | |||
| 610 | sub export_garden_layout | ||
| 611 | { | ||
| 612 | my($self, $gardens_table, $garden_name) = @_; | ||
| 613 | |||
| 614 | $self -> db -> logger -> debug("export_garden_layout(). Processing garden '$garden_name'"); | ||
| 615 | |||
| 616 | my($constants) = $self -> db -> constants; | ||
| 617 | my($flowers) = $self -> db -> read_flowers_table; | ||
| 618 | my($features) = $self -> db -> read_features_table; | ||
| 619 | my($max_x) = 0; | ||
| 620 | my($max_y) = 0; | ||
| 621 | my($property_found) = false; | ||
| 622 | my($property_name) = $self -> property_name; | ||
| 623 | my($x_offset) = $$constants{x_offset}; | ||
| 624 | my($y_offset) = $$constants{y_offset}; | ||
| 625 | |||
| 626 | my(%garden_id2name, %garden_name2id); | ||
| 627 | |||
| 628 | # The calling code checks ($$garden{property_publish} eq 'No') and ($$garden{publish} eq 'No'). | ||
| 629 | |||
| 630 | for my $garden (@$gardens_table) | ||
| 631 | { | ||
| 632 | $garden_name2id{$$garden{name} } = $$garden{id}; | ||
| 633 | $garden_id2name{$$garden{id} } = $$garden{name}; | ||
| 634 | |||
| 635 | if ($property_name eq $$garden{property_name}) | ||
| 636 | { | ||
| 637 | $property_found = true; | ||
| 638 | } | ||
| 639 | } | ||
| 640 | |||
| 641 | die "No such property: $property_name. \n" if (! $property_found); | ||
| 642 | |||
| 643 | my($garden_id) = $garden_name2id{$garden_name}; | ||
| 644 | |||
| 645 | my(%feature_name); | ||
| 646 | |||
| 647 | for my $feature (@$features) | ||
| 648 | { | ||
| 649 | next if ($$feature{publish} eq 'No'); | ||
| 650 | |||
| 651 | $feature_name{$$feature{id} } = $$feature{name}; | ||
| 652 | } | ||
| 653 | |||
| 654 | # 1: Set the parameters. | ||
| 655 | |||
| 656 | my($id); | ||
| 657 | my(%location_xy); | ||
| 658 | my($x); | ||
| 659 | my($y); | ||
| 660 | |||
| 661 | for my $flower (@$flowers) | ||
| 662 | { | ||
| 663 | next if ($$flower{publish} eq 'No'); | ||
| 664 | |||
| 665 | for my $location (@{$$flower{flower_locations} }) | ||
| 666 | { | ||
| 667 | next if ($garden_id2name{$$location{garden_id} } ne $garden_name); | ||
| 668 | |||
| 669 | $id = $$location{id}; | ||
| 670 | $x = $$location{x}; | ||
| 671 | $y = $$location{y}; | ||
| 672 | $max_x = $x if ($x > $max_x); | ||
| 673 | $max_y = $y if ($y > $max_y); | ||
| 674 | $location_xy{$id} = [] if (! $location_xy{$id}); | ||
| 675 | |||
| 676 | push @{$location_xy{$id} }, [$x, $y]; | ||
| 677 | } | ||
| 678 | } | ||
| 679 | |||
| 680 | $self -> db -> logger -> info("Max (x, y) after processing 'flower_locations': ($max_x, $max_y)"); | ||
| 681 | |||
| 682 | for my $feature (@$features) | ||
| 683 | { | ||
| 684 | next if ($$feature{publish} eq 'No'); | ||
| 685 | |||
| 686 | for my $feature (@{$$feature{feature_locations} }) | ||
| 687 | { | ||
| 688 | next if ($garden_id2name{$$feature{garden_id} } ne $garden_name); | ||
| 689 | |||
| 690 | $x = $$feature{x}; | ||
| 691 | $y = $$feature{y}; | ||
| 692 | $max_x = $x if ($x > $max_x); | ||
| 693 | $max_y = $y if ($y > $max_y); | ||
| 694 | } | ||
| 695 | } | ||
| 696 | |||
| 697 | $self -> db -> logger -> info("Max (x, y) after processing 'feature_locations': ($max_x, $max_y)"); | ||
| 698 | |||
| 699 | my($x_cell_count) = $max_x; | ||
| 700 | my($y_cell_count) = $max_y; | ||
| 701 | my($grid) = SVG::Grid -> new | ||
| 702 | ( | ||
| 703 | cell_width => $$constants{cell_width}, | ||
| 704 | cell_height => $$constants{cell_height}, | ||
| 705 | x_cell_count => $x_cell_count, | ||
| 706 | y_cell_count => $y_cell_count, | ||
| 707 | x_offset => $$constants{x_offset}, | ||
| 708 | y_offset => $$constants{y_offset}, | ||
| 709 | ); | ||
| 710 | |||
| 711 | $grid -> grid(stroke => 'blue'); | ||
| 712 | |||
| 713 | # 2: Add the feature locations to the grid. | ||
| 714 | |||
| 715 | my($grid_id); | ||
| 716 | |||
| 717 | for my $item (@$features) | ||
| 718 | { | ||
| 719 | next if ($$item{publish} eq 'No'); | ||
| 720 | |||
| 721 | for my $feature (@{$$item{feature_locations} }) | ||
| 722 | { | ||
| 723 | next if ($garden_id2name{$$feature{garden_id} } ne $garden_name); | ||
| 724 | |||
| 725 | $grid_id = $grid -> svg -> image | ||
| 726 | ( | ||
| 727 | height => $$constants{cell_height}, | ||
| 728 | href => $$item{feature_url}, | ||
| 729 | width => $$constants{cell_width}, | ||
| 730 | x => $x_offset + $$constants{cell_width} * $$feature{x}, # Cell co-ord. | ||
| 731 | y => $y_offset + $$constants{cell_height} * $$feature{y}, # Cell co-ord. | ||
| 732 | ); | ||
| 733 | } | ||
| 734 | } | ||
| 735 | |||
| 736 | # 3: Add the flowers to the grid. | ||
| 737 | |||
| 738 | my($pig_latin); | ||
| 739 | my(%tool_tips); | ||
| 740 | |||
| 741 | $tool_tips{$garden_id} = {}; | ||
| 742 | |||
| 743 | for my $flower (@$flowers) | ||
| 744 | { | ||
| 745 | next if ($$flower{publish} eq 'No'); | ||
| 746 | |||
| 747 | $pig_latin = $$flower{pig_latin}; | ||
| 748 | |||
| 749 | for my $location (@{$$flower{flower_locations} }) | ||
| 750 | { | ||
| 751 | next if ($garden_id2name{$$location{garden_id} } ne $garden_name); | ||
| 752 | |||
| 753 | $grid_id = $grid -> image_link | ||
| 754 | ( | ||
| 755 | href => $$flower{web_page_url}, | ||
| 756 | image => $$flower{thumbnail_url}, | ||
| 757 | show => 'new', # Converted into -show by SVG::Grid. | ||
| 758 | title => "$$flower{scientific_name} / $$flower{common_name}", | ||
| 759 | x => $$location{x}, # Cell co-ord. | ||
| 760 | y => $$location{y}, # Cell co-ord. | ||
| 761 | ); | ||
| 762 | |||
| 763 | $tool_tips{$garden_id}{$grid_id} = "$$flower{scientific_name} / $$flower{common_name}"; | ||
| 764 | } | ||
| 765 | } | ||
| 766 | |||
| 767 | # 4: Add some annotations and write the layout SVG. | ||
| 768 | |||
| 769 | $grid -> text | ||
| 770 | ( | ||
| 771 | 'font-size' => 32, | ||
| 772 | 'font-weight' => '400', | ||
| 773 | text => "'$property_name - $garden_name Garden'", | ||
| 774 | x => $grid -> x_offset + 8, # Pixel co-ord. | ||
| 775 | y => $grid -> y_offset / 2 + 8, # Pixel co-ord. | ||
| 776 | ); | ||
| 777 | $grid -> text | ||
| 778 | ( | ||
| 779 | 'font-size' => 32, | ||
| 780 | 'font-weight' => '400', | ||
| 781 | text => '--> N', | ||
| 782 | x => $grid -> width - 2 * $grid -> cell_width, # Pixel co-ord. | ||
| 783 | y => $grid -> y_offset / 2, # Pixel co-ord. | ||
| 784 | ); | ||
| 785 | $grid -> text | ||
| 786 | ( | ||
| 787 | 'font-size' => 32, | ||
| 788 | 'font-weight' => '400', | ||
| 789 | text => 'Block size: Width: 11.7m. Height: 46m', | ||
| 790 | x => $grid -> width - 20 * $grid -> cell_width, # Pixel co-ord. | ||
| 791 | y => $grid -> height, # Pixel co-ord. | ||
| 792 | ); | ||
| 793 | |||
| 794 | my($file_name) = File::Spec -> catfile($$constants{homepage_dir}, $$constants{flower_dir}, "$garden_name.garden.layout.svg"); | ||
| 795 | |||
| 796 | $self -> db -> logger -> info("Writing to $file_name"); | ||
| 797 | |||
| 798 | $grid -> write(output_file_name => $file_name); | ||
| 799 | |||
| 800 | # 4: Output some HTML. | ||
| 801 | |||
| 802 | my(@garden_index); | ||
| 803 | |||
| 804 | push @garden_index, <<EOS; | ||
| 805 | <html> | ||
| 806 | <head> | ||
| 807 | <title>$property_name - $garden_name Garden Layout</title> | ||
| 808 | <meta http-equiv = 'Content-Type' content = 'text/html; charset=utf-8' /> | ||
| 809 | <link rel = 'stylesheet' type = 'text/css' href = '/assets/css/www/garden/design/homepage.css'> | ||
| 810 | </head> | ||
| 811 | <body> | ||
| 812 | <h1 class = 'centered'><span class = '$$constants{css_class4headings}' id = 'top'>The '$property_name - $garden_name Garden' Layout</span></h1> | ||
| 813 | <br /> | ||
| 814 | <table align = 'center' summary = 'Table for $property_name - $garden_name Garden'> | ||
| 815 | <tr><td>Links</td></tr> | ||
| 816 | EOS | ||
| 817 | |||
| 818 | for my $garden (@$gardens_table) | ||
| 819 | { | ||
| 820 | # Skip other properties and skip the current garden. | ||
| 821 | |||
| 822 | next if ($self -> property_name ne $$garden{property_name}); | ||
| 823 | next if ($garden_name eq $$garden{name}); | ||
| 824 | |||
| 825 | push @garden_index, <<EOS; | ||
| 826 | <tr><td><a href = '$$constants{homepage_url}$$constants{flower_url}/$$garden{name}.garden.layout.html'>The '$property_name - $$garden{name} Garden' Layout</a></td></tr> | ||
| 827 | EOS | ||
| 828 | } | ||
| 829 | |||
| 830 | push @garden_index, <<EOS; | ||
| 831 | <tr><td><a href = '$$constants{homepage_url}/Flowers.html'>The Flower Catalog</a></td></tr> | ||
| 832 | </table> | ||
| 833 | |||
| 834 | <br /> | ||
| 835 | |||
| 836 | <h2 align = 'center'>The '$property_name - $garden_name Garden Layout' (SVG image), with clickable flower thumbnails in situ</h2> | ||
| 837 | |||
| 838 | <table align = 'center' summary = 'Table for $property_name - $garden_name Garden Layout'> | ||
| 839 | <tr><td align = 'center'> | ||
| 840 | <object data = '$$constants{homepage_url}$$constants{flower_url}/$garden_name.garden.layout.svg'></object> | ||
| 841 | </td></tr> | ||
| 842 | </table> | ||
| 843 | |||
| 844 | <br /> | ||
| 845 | |||
| 846 | <table align = 'center' summary = 'First placeholder for link to top'><tr><td align = 'center'><a href = '#top'>Top</a></td></tr></table> | ||
| 847 | </body> | ||
| 848 | EOS | ||
| 849 | |||
| 850 | # Finally, (in several steps) generate the JS which implements ToolTips activated by MouseOver. | ||
| 851 | |||
| 852 | push @garden_index, <<EOS; | ||
| 853 | |||
| 854 | <script type="text/javascript" src="/assets/js/jQuery/jquery-3.1.1.min.js"></script> | ||
| 855 | <script type="text/javascript" src="/assets/js/jQuery/jquery-ui-1.12.1/jquery-ui.min.js"></script> | ||
| 856 | <script type="text/javascript"> | ||
| 857 | |||
| 858 | var tool_tips = []; | ||
| 859 | EOS | ||
| 860 | |||
| 861 | my($index) = -1; | ||
| 862 | |||
| 863 | my(@tips); | ||
| 864 | |||
| 865 | for $grid_id (nsort keys %{$tool_tips{$garden_id} }) | ||
| 866 | { | ||
| 867 | $index++; | ||
| 868 | |||
| 869 | # Must use double-quotes in case the common_name contains a single-quote. | ||
| 870 | # And we use a stack because <<EOS added an extra \n to every output line :-(. | ||
| 871 | |||
| 872 | push @tips, qq|\ttool_tips[$index] = {id: '$grid_id', text: "$tool_tips{$garden_id}{$grid_id}"};|; | ||
| 873 | } | ||
| 874 | |||
| 875 | my($tips) = join("\n", @tips); | ||
| 876 | |||
| 877 | push @garden_index, <<EOS; | ||
| 878 | $tips | ||
| 879 | |||
| 880 | var id; | ||
| 881 | |||
| 882 | for (var i = 0; i < tool_tips.length; i++) | ||
| 883 | { | ||
| 884 | id = document.getElementById(tool_tips[i].id); | ||
| 885 | |||
| 886 | \$(id).tooltip({content: tool_tips[i].text, items: 'span'}); | ||
| 887 | |||
| 888 | \$(id) | ||
| 889 | .mouseenter(function() { | ||
| 890 | \$(id).tooltip('open') | ||
| 891 | }) | ||
| 892 | .mouseleave(function() { | ||
| 893 | \$(id).tooltip('close') | ||
| 894 | }); | ||
| 895 | } | ||
| 896 | |||
| 897 | </script> | ||
| 898 | </html> | ||
| 899 | EOS | ||
| 900 | |||
| 901 | $file_name = File::Spec -> catfile($$constants{homepage_dir}, $$constants{flower_dir}, "$garden_name.garden.layout.html"); | ||
| 902 | |||
| 903 | $self -> db -> logger -> info("Writing to $file_name"); | ||
| 904 | |||
| 905 | open(my $fh, '>:encoding(UTF-8)', $file_name); | ||
| 906 | print $fh map{"$_\n"} @garden_index; | ||
| 907 | close $fh; | ||
| 908 | |||
| 909 | $self -> db -> logger -> info("Finished exporting garden layout for the '$garden_name' garden"); | ||
| 910 | |||
| 911 | } # End of export_garden_layout. | ||
| 912 | |||
| 913 | # ----------------------------------------------- | ||
| 914 | |||
| 915 | sub export_layout_guide | ||
| 916 | { | ||
| 917 | my($self) = @_; | ||
| 918 | my($constants) = $self -> db -> constants; | ||
| 919 | my($gardens_table) = $self -> db -> read_gardens_table; # Warning: Not read_table('gardens'). | ||
| 920 | my($html) = ''; | ||
| 921 | my($property_name) = $self -> property_name; | ||
| 922 | |||
| 923 | # See ~/backup/face.book.txt and face.book.meta.txt. | ||
| 924 | |||
| 925 | $html .= <<EOS; | ||
| 926 | <table align = 'center' summary = 'Facebook like button'> | ||
| 927 | <tr> | ||
| 928 | <td> | ||
| 929 | <div id = 'fb-root'></div> | ||
| 930 | <script> | ||
| 931 | (function(d, s, id) | ||
| 932 | { | ||
| 933 | var js, fjs = d.getElementsByTagName(s)[0]; | ||
| 934 | if (d.getElementById(id)) return; | ||
| 935 | js = d.createElement(s); js.id = id; | ||
| 936 | js.src = '//connect.facebook.net/en_US/sdk.js#xfbml=1'; | ||
| 937 | fjs.parentNode.insertBefore(js, fjs); | ||
| 938 | }(document, 'script', 'facebook-jssdk') ); | ||
| 939 | </script> | ||
| 940 | |||
| 941 | <!-- Your like button code --> | ||
| 942 | <div class = 'fb-like' | ||
| 943 | data-action = 'like' | ||
| 944 | data-href = 'https://savage.net.au/Flowers.html' | ||
| 945 | data-layout = 'standard' | ||
| 946 | data-show-faces ='true'> | ||
| 947 | </div> | ||
| 948 | </td> | ||
| 949 | </tr> | ||
| 950 | </table> | ||
| 951 | <table align = 'center' summary = 'Table for a list of articles'> | ||
| 952 | <tr><td align='center'><br /><span class = '$$constants{css_class4headings}' id = 'articles'>Articles</span></td></tr> | ||
| 953 | <tr><td><a href = '$$constants{homepage_url}$$constants{flower_url}/html/Garden.Design.Software.html'>2016-12-29: Garden Design Software</a></td></tr> | ||
| 954 | <tr><td><a href = '$$constants{homepage_url}$$constants{flower_url}/html/How.To.Net.Dwarf.Apples.html'>2016-01-03: How To Net Dwarf Apples</a></td></tr> | ||
| 955 | <tr><td><a href = '$$constants{homepage_url}$$constants{flower_url}/html/Protecting.Apples.From.Possums.html'>2013-12-08: Protecting Apples From Possums</a></td></tr> | ||
| 956 | </table> | ||
| 957 | <br /> | ||
| 958 | <table align = 'center' summary = 'Table for a list of garden layouts'> | ||
| 959 | <tr><td align = 'center'><span class = '$$constants{css_class4headings}' id = 'garden_layouts'>The Garden Layouts</span></td></tr> | ||
| 960 | EOS | ||
| 961 | |||
| 962 | for my $garden (@$gardens_table) | ||
| 963 | { | ||
| 964 | next if ( ($$garden{property_name} ne $property_name) || ($$garden{property_publish} eq 'No') || ($$garden{publish} eq 'No') ); | ||
| 965 | |||
| 966 | $html .= <<EOS; | ||
| 967 | <tr><td><a href = '$$constants{homepage_url}$$constants{flower_url}/$$garden{name}.garden.layout.html'>The '$property_name - $$garden{name} Garden' Layout, with clickable flower thumbnails in situ</a></td></tr> | ||
| 968 | EOS | ||
| 969 | } | ||
| 970 | |||
| 971 | $html .= <<EOS; | ||
| 972 | </table> | ||
| 973 | <table align = 'center' summary = 'Table for a list of URLs'> | ||
| 974 | <tr><td align = 'center'><br /><span class = '$$constants{css_class4headings}' id = 'various_urls'>Various Links</span></td></tr> | ||
| 975 | <tr><td><a href = '$$constants{homepage_url}$$constants{flower_url}/flowers.schema.svg'>The database schema</a></td></tr> | ||
| 976 | <tr><td><a href = 'https://datatables.net/'>The URL</a> of the free Javascript package which manages the HTML table below</td></tr> | ||
| 977 | <tr><td><a href = 'https://github.com/bgrins/spectrum'>The github repo</a> of the free Javascript package which provides a color spectrum...</td></tr> | ||
| 978 | <tr><td>... and the corresponding <a href = 'https://bgrins.github.io/spectrum/'>on-line docs</a></td></tr> | ||
| 979 | <tr><td><a href = 'http://nurseriesonline.com.au'>Nurseries OnLine - A great Australian website</a></td></tr> | ||
| 980 | <tr><td><a href = 'http://www.flowersforums.com/forums/'>Flower Forums - A great place to ask for help</a></td></tr> | ||
| 981 | <tr><td><a href = 'http://www.LabourofLoveLandscaping.com'>Some identification assistance kindly provided by Kate Kennedy Butler</a></td></tr> | ||
| 982 | <tr><td><a href = 'http://www.theplantlist.org/'>The Plant List - A working list of all plant species</a></td></tr> | ||
| 983 | <tr><td><a href = 'http://www.plantnet.org/'>PlantNet - Identify plants via pix</a></td></tr> | ||
| 984 | </table> | ||
| 985 | EOS | ||
| 986 | |||
| 987 | return $html; | ||
| 988 | |||
| 989 | } # End of export_layout_guide. | ||
| 990 | |||
| 991 | # ----------------------------------------------- | ||
| 992 | |||
| 993 | sub export_layouts | ||
| 994 | { | ||
| 995 | my($self) = @_; | ||
| 996 | my($gardens_table) = $self -> db -> read_gardens_table; # Warning: Not read_table('gardens'). | ||
| 997 | |||
| 998 | for my $garden (@$gardens_table) | ||
| 999 | { | ||
| 1000 | next if ( ($$garden{property_publish} eq 'No') || ($$garden{publish} eq 'No') ); | ||
| 1001 | |||
| 1002 | $self -> export_garden_layout($gardens_table, $$garden{name}) if ($self -> property_name eq $$garden{property_name}); | ||
| 1003 | } | ||
| 1004 | |||
| 1005 | } # End of export_layouts. | ||
| 1006 | |||
| 1007 | # ----------------------------------------------- | ||
| 1008 | |||
| 1009 | sub feature_locations2csv | ||
| 1010 | { | ||
| 1011 | my($self, $csv, $features, $property_id2name, $garden_id2name) = @_; | ||
| 1012 | my($file_name) = $self -> output_file =~ s/flowers.csv/feature_locations.csv/r; | ||
| 1013 | |||
| 1014 | $self -> db -> logger -> info("Writing to $file_name"); | ||
| 1015 | |||
| 1016 | open(my $fh, '>:encoding(utf-8)', $file_name) || die "Can't open(> $file_name): $!"; | ||
| 1017 | |||
| 1018 | # Column names are in order left-to-right. | ||
| 1019 | |||
| 1020 | $csv -> combine(qw/name property_name garden_name xy/); | ||
| 1021 | |||
| 1022 | print $fh $csv -> string, "\n"; | ||
| 1023 | |||
| 1024 | my($feature_name); | ||
| 1025 | my($garden_name); | ||
| 1026 | my(%location); | ||
| 1027 | my($property_name); | ||
| 1028 | |||
| 1029 | for my $feature (@$features) | ||
| 1030 | { | ||
| 1031 | next if ($$feature{publish} eq 'No'); | ||
| 1032 | |||
| 1033 | %location = (); | ||
| 1034 | $feature_name = $$feature{name}; | ||
| 1035 | |||
| 1036 | for my $feature (@{$$feature{feature_locations} }) | ||
| 1037 | { | ||
| 1038 | $garden_name = $$garden_id2name{$$feature{garden_id} }; | ||
| 1039 | $property_name = $$property_id2name{$$feature{property_id} }; | ||
| 1040 | $location{$property_name} = {} if (! $location{$property_name}); | ||
| 1041 | $location{$property_name}{$garden_name} = [] if (! $location{$property_name}{$garden_name}); | ||
| 1042 | |||
| 1043 | push @{$location{$property_name}{$garden_name} }, "$$feature{x},$$feature{y}"; | ||
| 1044 | } | ||
| 1045 | |||
| 1046 | for $property_name (sort keys %location) | ||
| 1047 | { | ||
| 1048 | for $garden_name (sort keys %{$location{$property_name} }) | ||
| 1049 | { | ||
| 1050 | $csv -> combine | ||
| 1051 | ( | ||
| 1052 | $feature_name, | ||
| 1053 | $property_name, | ||
| 1054 | $garden_name, | ||
| 1055 | join(' ', nsort @{$location{$property_name}{$garden_name} }), | ||
| 1056 | ); | ||
| 1057 | |||
| 1058 | print $fh $csv -> string, "\n"; | ||
| 1059 | } | ||
| 1060 | } | ||
| 1061 | } | ||
| 1062 | |||
| 1063 | close $fh; | ||
| 1064 | |||
| 1065 | $self -> db -> logger -> info("Wrote $file_name"); | ||
| 1066 | |||
| 1067 | } # End of feature_locations2csv. | ||
| 1068 | |||
| 1069 | # ----------------------------------------------- | ||
| 1070 | |||
| 1071 | sub features2csv | ||
| 1072 | { | ||
| 1073 | my($self, $csv) = @_; | ||
| 1074 | my($features) = $self -> db -> read_features_table; # Returns a sorted array ref. | ||
| 1075 | my($file_name) = $self -> output_file =~ s/flowers.csv/features.csv/r; | ||
| 1076 | |||
| 1077 | $self -> db -> logger -> info("Writing to $file_name"); | ||
| 1078 | |||
| 1079 | open(my $fh, '>:encoding(utf-8)', $file_name) || die "Can't open(> $file_name): $!"; | ||
| 1080 | |||
| 1081 | # Column names are in order left-to-right. | ||
| 1082 | |||
| 1083 | $csv -> combine(qw/name hex_color publish/); | ||
| 1084 | |||
| 1085 | print $fh $csv -> string, "\n"; | ||
| 1086 | |||
| 1087 | for my $feature (@$features) | ||
| 1088 | { | ||
| 1089 | next if ($$feature{publish} eq 'No'); | ||
| 1090 | |||
| 1091 | $csv -> combine | ||
| 1092 | ( | ||
| 1093 | $$feature{name}, | ||
| 1094 | $$feature{hex_color}, | ||
| 1095 | $$feature{publish}, | ||
| 1096 | ); | ||
| 1097 | |||
| 1098 | print $fh $csv -> string, "\n"; | ||
| 1099 | } | ||
| 1100 | |||
| 1101 | close $fh; | ||
| 1102 | |||
| 1103 | $self -> db -> logger -> info("Wrote $file_name"); | ||
| 1104 | |||
| 1105 | return $features; | ||
| 1106 | |||
| 1107 | } # End of features2csv. | ||
| 1108 | |||
| 1109 | # ----------------------------------------------- | ||
| 1110 | |||
| 1111 | sub flower_locations2csv | ||
| 1112 | { | ||
| 1113 | my($self, $csv, $flowers, $property_id2name, $garden_id2name) = @_; | ||
| 1114 | my($file_name) = $self -> output_file =~ s/flowers.csv/flower_locations.csv/r; | ||
| 1115 | |||
| 1116 | $self -> db -> logger -> info("Writing to $file_name"); | ||
| 1117 | |||
| 1118 | open(my $fh, '>:encoding(utf-8)', $file_name) || die "Can't open(> $file_name): $!"; | ||
| 1119 | |||
| 1120 | # Column names are in order left-to-right. | ||
| 1121 | |||
| 1122 | $csv -> combine(qw/common_name property_name garden_name xy/); | ||
| 1123 | |||
| 1124 | print $fh $csv -> string, "\n"; | ||
| 1125 | |||
| 1126 | my($common_name); | ||
| 1127 | my($garden_name); | ||
| 1128 | my(%location); | ||
| 1129 | my($property_name); | ||
| 1130 | |||
| 1131 | for my $flower (sort{uc($$a{common_name}) cmp uc($$b{common_name})} @$flowers) | ||
| 1132 | { | ||
| 1133 | next if ($$flower{publish} eq 'No'); | ||
| 1134 | |||
| 1135 | $common_name = $$flower{common_name}; | ||
| 1136 | %location = (); | ||
| 1137 | |||
| 1138 | for my $location (@{$$flower{flower_locations} }) | ||
| 1139 | { | ||
| 1140 | $garden_name = $$garden_id2name{$$location{garden_id} }; | ||
| 1141 | $property_name = $$property_id2name{$$location{property_id} }; | ||
| 1142 | $location{$property_name} = {} if (! $location{$property_name}); | ||
| 1143 | $location{$property_name}{$garden_name} = [] if (! $location{$property_name}{$garden_name}); | ||
| 1144 | |||
| 1145 | push @{$location{$property_name}{$garden_name} }, "$$location{x},$$location{y}", | ||
| 1146 | } | ||
| 1147 | |||
| 1148 | for $property_name (sort keys %location) | ||
| 1149 | { | ||
| 1150 | for $garden_name (sort keys %{$location{$property_name} }) | ||
| 1151 | { | ||
| 1152 | $csv -> combine | ||
| 1153 | ( | ||
| 1154 | $common_name, | ||
| 1155 | $property_name, | ||
| 1156 | $garden_name, | ||
| 1157 | join(' ', nsort @{$location{$property_name}{$garden_name} }), | ||
| 1158 | ); | ||
| 1159 | |||
| 1160 | print $fh $csv -> string, "\n"; | ||
| 1161 | } | ||
| 1162 | } | ||
| 1163 | } | ||
| 1164 | |||
| 1165 | close $fh; | ||
| 1166 | |||
| 1167 | $self -> db -> logger -> info("Wrote $file_name"); | ||
| 1168 | |||
| 1169 | } # End of flower_locations2csv. | ||
| 1170 | |||
| 1171 | # ----------------------------------------------- | ||
| 1172 | |||
| 1173 | sub flowers2csv | ||
| 1174 | { | ||
| 1175 | my($self, $csv) = @_; | ||
| 1176 | my($flowers) = $self -> db -> read_flowers_table; | ||
| 1177 | my($output_file) = $self -> output_file; | ||
| 1178 | |||
| 1179 | $self -> db -> logger -> info("Writing to $output_file"); | ||
| 1180 | |||
| 1181 | open(my $fh, '>:encoding(utf-8)', $output_file) || die "Can't open(> $output_file): $!"; | ||
| 1182 | |||
| 1183 | # Column names are in order left-to-right. | ||
| 1184 | |||
| 1185 | $csv -> combine(qw/common_name scientific_name aliases height width publish/); | ||
| 1186 | |||
| 1187 | print $fh $csv -> string, "\n"; | ||
| 1188 | |||
| 1189 | for my $flower (sort{uc($$a{common_name}) cmp uc($$b{common_name})} @$flowers) | ||
| 1190 | { | ||
| 1191 | next if ($$flower{publish} eq 'No'); | ||
| 1192 | |||
| 1193 | $csv -> combine | ||
| 1194 | ( | ||
| 1195 | $$flower{common_name}, | ||
| 1196 | $$flower{scientific_name}, | ||
| 1197 | $$flower{aliases}, | ||
| 1198 | $$flower{height}, | ||
| 1199 | $$flower{width}, | ||
| 1200 | $$flower{publish}, | ||
| 1201 | ); | ||
| 1202 | |||
| 1203 | print $fh $csv -> string, "\n"; | ||
| 1204 | } | ||
| 1205 | |||
| 1206 | close($fh); | ||
| 1207 | |||
| 1208 | $self -> db -> logger -> info("Wrote $output_file"); | ||
| 1209 | |||
| 1210 | return $flowers; | ||
| 1211 | |||
| 1212 | } # End of flowers2csv. | ||
| 1213 | |||
| 1214 | # ----------------------------------------------- | ||
| 1215 | |||
| 1216 | sub gardens2csv | ||
| 1217 | { | ||
| 1218 | my($self, $csv, $property_id2name) = @_; | ||
| 1219 | my($file_name) = $self -> output_file =~ s/flowers.csv/gardens.csv/r; | ||
| 1220 | my($garden_table) = $self -> db -> read_table('gardens'); | ||
| 1221 | |||
| 1222 | $self -> db -> logger -> info("Writing to $file_name"); | ||
| 1223 | |||
| 1224 | open(my $fh, '>:encoding(utf-8)', $file_name) || die "Can't open(> $file_name): $!"; | ||
| 1225 | |||
| 1226 | # Column names are in order left-to-right. | ||
| 1227 | |||
| 1228 | $csv -> combine(qw/property_name garden_name description publish/); | ||
| 1229 | |||
| 1230 | print $fh $csv -> string, "\n"; | ||
| 1231 | |||
| 1232 | my(%garden_id2name); | ||
| 1233 | |||
| 1234 | for my $garden (sort{$$a{name} cmp $$b{name} } @$garden_table) | ||
| 1235 | { | ||
| 1236 | next if ($$garden{publish} eq 'No'); | ||
| 1237 | |||
| 1238 | $garden_id2name{$$garden{id} } = $$garden{name}; | ||
| 1239 | |||
| 1240 | $csv -> combine | ||
| 1241 | ( | ||
| 1242 | $$property_id2name{$$garden{property_id} }, | ||
| 1243 | $$garden{name}, | ||
| 1244 | $$garden{description}, | ||
| 1245 | $$garden{publish}, | ||
| 1246 | ); | ||
| 1247 | |||
| 1248 | print $fh $csv -> string, "\n"; | ||
| 1249 | } | ||
| 1250 | |||
| 1251 | close $fh; | ||
| 1252 | |||
| 1253 | $self -> db -> logger -> info("Wrote $file_name"); | ||
| 1254 | |||
| 1255 | return \%garden_id2name; | ||
| 1256 | |||
| 1257 | } # End of gardens2csv. | ||
| 1258 | |||
| 1259 | # ----------------------------------------------- | ||
| 1260 | |||
| 1261 | sub images2csv | ||
| 1262 | { | ||
| 1263 | my($self, $csv, $flowers) = @_; | ||
| 1264 | my($file_name) = $self -> output_file =~ s/flowers.csv/images.csv/r; | ||
| 1265 | |||
| 1266 | $self -> db -> logger -> info("Writing to $file_name"); | ||
| 1267 | |||
| 1268 | open(my $fh, '>:encoding(utf-8)', $file_name) || die "Can't open(> $file_name): $!"; | ||
| 1269 | |||
| 1270 | # Column names are in order left-to-right. | ||
| 1271 | |||
| 1272 | $csv -> combine(qw/common_name description file_name/); | ||
| 1273 | |||
| 1274 | print $fh $csv -> string, "\n"; | ||
| 1275 | |||
| 1276 | my($common_name); | ||
| 1277 | |||
| 1278 | for my $flower (sort{uc($$a{common_name}) cmp uc($$b{common_name})} @$flowers) | ||
| 1279 | { | ||
| 1280 | next if ($$flower{publish} eq 'No'); | ||
| 1281 | |||
| 1282 | $common_name = $$flower{common_name}; | ||
| 1283 | |||
| 1284 | for my $item (sort{$$a{flower_id} cmp $$b{flower_id} } @{$$flower{images} }) | ||
| 1285 | { | ||
| 1286 | $csv -> combine | ||
| 1287 | ( | ||
| 1288 | $common_name, | ||
| 1289 | $$item{description}, | ||
| 1290 | $$item{raw_name}, # We don't want the whole domain/url! | ||
| 1291 | ); | ||
| 1292 | |||
| 1293 | print $fh $csv -> string, "\n"; | ||
| 1294 | } | ||
| 1295 | } | ||
| 1296 | |||
| 1297 | close $fh; | ||
| 1298 | |||
| 1299 | $self -> db -> logger -> info("Wrote $file_name"); | ||
| 1300 | |||
| 1301 | } # End of images2csv. | ||
| 1302 | |||
| 1303 | # ----------------------------------------------- | ||
| 1304 | |||
| 1305 | sub init_export | ||
| 1306 | { | ||
| 1307 | my($self) = @_; | ||
| 1308 | my($export_type) = $self -> export_type; | ||
| 1309 | my($standalone_page) = $self -> standalone_page; | ||
| 1310 | my($all) = $self -> all; | ||
| 1311 | |||
| 1312 | if ($all !~ /^(?:No|Yes)$/i) | ||
| 1313 | { | ||
| 1314 | die "The 'all' flag must be Yes or No'\n"; | ||
| 1315 | } | ||
| 1316 | |||
| 1317 | # Warning: The line in web.site.xml which runs this script must not use command line options. | ||
| 1318 | # That means, that whatever options that code needs must be the defaults. | ||
| 1319 | |||
| 1320 | $self -> export_columns | ||
| 1321 | ({ | ||
| 1322 | 'Native' => | ||
| 1323 | { | ||
| 1324 | column_name => 'native', | ||
| 1325 | order => 2, # The value 1 is not used. | ||
| 1326 | }, | ||
| 1327 | 'Scientific name' => | ||
| 1328 | { | ||
| 1329 | column_name => 'scientific_name', | ||
| 1330 | order => 3, | ||
| 1331 | }, | ||
| 1332 | 'Common name' => | ||
| 1333 | { | ||
| 1334 | column_name => 'common_name', | ||
| 1335 | order => 4, | ||
| 1336 | }, | ||
| 1337 | 'Aliases' => | ||
| 1338 | { | ||
| 1339 | column_name => 'aliases', | ||
| 1340 | order => 5, | ||
| 1341 | }, | ||
| 1342 | 'Thumbnail <span class = "index">(clickable)</span>' => | ||
| 1343 | { | ||
| 1344 | column_name => 'thumbnail_file_name', | ||
| 1345 | order => 6, | ||
| 1346 | }, | ||
| 1347 | }); | ||
| 1348 | |||
| 1349 | if ($export_type < 0) | ||
| 1350 | { | ||
| 1351 | $self -> export_type(0); | ||
| 1352 | } | ||
| 1353 | elsif ($export_type > 1) | ||
| 1354 | { | ||
| 1355 | $self -> export_type(1); | ||
| 1356 | } | ||
| 1357 | |||
| 1358 | if ($standalone_page < 0) | ||
| 1359 | { | ||
| 1360 | $self -> standalone_page(0); | ||
| 1361 | } | ||
| 1362 | elsif ($standalone_page > 1) | ||
| 1363 | { | ||
| 1364 | $self -> standalone_page(1); | ||
| 1365 | } | ||
| 1366 | |||
| 1367 | |||
| 1368 | } # End of init_export. | ||
| 1369 | |||
| 1370 | # ----------------------------------------------- | ||
| 1371 | |||
| 1372 | sub notes2csv | ||
| 1373 | { | ||
| 1374 | my($self, $csv, $flowers) = @_; | ||
| 1375 | my($file_name) = $self -> output_file =~ s/flowers.csv/notes.csv/r; | ||
| 1376 | |||
| 1377 | $self -> db -> logger -> info("Writing to $file_name"); | ||
| 1378 | |||
| 1379 | open(my $fh, '>:encoding(utf-8)', $file_name) || die "Can't open(> $file_name): $!"; | ||
| 1380 | |||
| 1381 | # Column names are in order left-to-right. | ||
| 1382 | |||
| 1383 | $csv -> combine(qw/common_name note/); | ||
| 1384 | |||
| 1385 | print $fh $csv -> string, "\n"; | ||
| 1386 | |||
| 1387 | my($common_name); | ||
| 1388 | |||
| 1389 | for my $flower (sort{uc($$a{common_name}) cmp uc($$b{common_name})} @$flowers) | ||
| 1390 | { | ||
| 1391 | next if ($$flower{publish} eq 'No'); | ||
| 1392 | |||
| 1393 | $common_name = $$flower{common_name}; | ||
| 1394 | |||
| 1395 | for my $note (sort{$$a{flower_id} cmp $$b{flower_id} } @{$$flower{notes} }) | ||
| 1396 | { | ||
| 1397 | $csv -> combine | ||
| 1398 | ( | ||
| 1399 | $common_name, | ||
| 1400 | $$note{note}, | ||
| 1401 | ); | ||
| 1402 | |||
| 1403 | print $fh $csv -> string, "\n"; | ||
| 1404 | } | ||
| 1405 | } | ||
| 1406 | |||
| 1407 | close $fh; | ||
| 1408 | |||
| 1409 | $self -> db -> logger -> info("Wrote $file_name"); | ||
| 1410 | |||
| 1411 | } # End of notes2csv. | ||
| 1412 | |||
| 1413 | # ----------------------------------------------- | ||
| 1414 | |||
| 1415 | sub properties2csv | ||
| 1416 | { | ||
| 1417 | my($self, $csv) = @_; | ||
| 1418 | my($property_table) = $self -> db -> read_table('properties'); | ||
| 1419 | my($file_name) = $self -> output_file =~ s/flowers.csv/properties.csv/r; | ||
| 1420 | |||
| 1421 | $self -> db -> logger -> info("Writing to $file_name"); | ||
| 1422 | |||
| 1423 | open(my $fh, '>:encoding(utf-8)', $file_name) || die "Can't open(> $file_name): $!"; | ||
| 1424 | |||
| 1425 | # Column names are in order left-to-right. | ||
| 1426 | |||
| 1427 | $csv -> combine(qw/name description publish/); | ||
| 1428 | |||
| 1429 | print $fh $csv -> string, "\n"; | ||
| 1430 | |||
| 1431 | my(%property_id2name); | ||
| 1432 | |||
| 1433 | for my $row (sort{uc($$a{name}) cmp uc($$b{name})} @$property_table) | ||
| 1434 | { | ||
| 1435 | next if ($$row{publish} eq 'No'); | ||
| 1436 | |||
| 1437 | $property_id2name{$$row{id} } = $$row{name}; | ||
| 1438 | |||
| 1439 | $csv -> combine | ||
| 1440 | ( | ||
| 1441 | $$row{name}, | ||
| 1442 | $$row{description}, | ||
| 1443 | $$row{publish} | ||
| 1444 | ); | ||
| 1445 | |||
| 1446 | print $fh $csv -> string, "\n"; | ||
| 1447 | } | ||
| 1448 | |||
| 1449 | close $fh; | ||
| 1450 | |||
| 1451 | $self -> db -> logger -> info("Wrote $file_name"); | ||
| 1452 | |||
| 1453 | return \%property_id2name; | ||
| 1454 | |||
| 1455 | } # End of properties2csv. | ||
| 1456 | |||
| 1457 | # ----------------------------------------------- | ||
| 1458 | |||
| 1459 | sub urls2csv | ||
| 1460 | { | ||
| 1461 | my($self, $csv, $flowers) = @_; | ||
| 1462 | my($file_name) = $self -> output_file =~ s/flowers.csv/urls.csv/r; | ||
| 1463 | |||
| 1464 | $self -> db -> logger -> info("Writing to $file_name"); | ||
| 1465 | |||
| 1466 | open(my $fh, '>:encoding(utf-8)', $file_name) || die "Can't open(> $file_name): $!"; | ||
| 1467 | |||
| 1468 | # Column names are in order left-to-right. | ||
| 1469 | |||
| 1470 | $csv -> combine(qw/common_name url/); | ||
| 1471 | |||
| 1472 | print $fh $csv -> string, "\n"; | ||
| 1473 | |||
| 1474 | my($common_name); | ||
| 1475 | |||
| 1476 | for my $flower (sort{uc($$a{common_name}) cmp uc($$b{common_name})} @$flowers) | ||
| 1477 | { | ||
| 1478 | next if ($$flower{publish} eq 'No'); | ||
| 1479 | |||
| 1480 | $common_name = $$flower{common_name}; | ||
| 1481 | |||
| 1482 | for my $url (sort{$$a{flower_id} cmp $$b{flower_id} } @{$$flower{urls} }) | ||
| 1483 | { | ||
| 1484 | $csv -> combine | ||
| 1485 | ( | ||
| 1486 | $common_name, | ||
| 1487 | $$url{url}, | ||
| 1488 | ); | ||
| 1489 | |||
| 1490 | print $fh $csv -> string, "\n"; | ||
| 1491 | } | ||
| 1492 | } | ||
| 1493 | |||
| 1494 | close $fh; | ||
| 1495 | |||
| 1496 | $self -> db -> logger -> info("Wrote $file_name"); | ||
| 1497 | |||
| 1498 | } # End of urls2csv. | ||
| 1499 | |||
| 1500 | # ----------------------------------------------- | ||
| 1501 | |||
| 1502 | 1; | ||
| 1503 | |||
| 1504 | =pod | ||
| 1505 | |||
| 1506 | =head1 NAME | ||
| 1507 | |||
| 1508 | WWW::Garden::Design::Export - Manage the 'flowers' database | ||
| 1509 | |||
| 1510 | =head1 Machine-Readable Change Log | ||
| 1511 | |||
| 1512 | The file Changes was converted into Changelog.ini by L<Module::Metadata::Changes>. | ||
| 1513 | |||
| 1514 | =head1 Version Numbers | ||
| 1515 | |||
| 1516 | Version numbers < 1.00 represent development versions. From 1.00 up, they are production versions. | ||
| 1517 | |||
| 1518 | =head1 Repository | ||
| 1519 | |||
| 1520 | L<https://github.com/ronsavage/WWW-Garden-Design> | ||
| 1521 | |||
| 1522 | =head1 Support | ||
| 1523 | |||
| 1524 | Email the author, or log a bug on RT: | ||
| 1525 | |||
| 1526 | L<https://rt.cpan.org/Public/Dist/Display.html?Name=WWW::Garden::Design>. | ||
| 1527 | |||
| 1528 | =head1 Author | ||
| 1529 | |||
| 1530 | L<WWW::Garden::Design> was written by Ron Savage I<E<lt>ron@savage.net.auE<gt>> in 2014. | ||
| 1531 | |||
| 1532 | My homepage: L<https://savage.net.au/>. | ||
| 1533 | |||
| 1534 | =head1 Copyright | ||
| 1535 | |||
| 1536 | Australian copyright (c) 2018, Ron Savage. | ||
| 1537 | |||
| 1538 | All Programs of mine are 'OSI Certified Open Source Software'; | ||
| 1539 | you can redistribute them and/or modify them under the terms of | ||
| 1540 | The Perl License, a copy of which is available at: | ||
| 1541 | http://dev.perl.org/licenses/ | ||
| 1542 | |||
| 1543 | =cut |