Skip to content

Commit

Permalink
refactor: reduce nesting in Export.pm (#7180)
Browse files Browse the repository at this point in the history
* refactor: reduce nesting in Export.pm

Taking the most deeply nested part of Export.pm and putting it into a
seperate function
  • Loading branch information
dipietroR committed Aug 16, 2022
1 parent 8f9caf9 commit 1c17eb0
Showing 1 changed file with 37 additions and 31 deletions.
68 changes: 37 additions & 31 deletions lib/ProductOpener/Export.pm
Original file line number Diff line number Diff line change
Expand Up @@ -279,37 +279,7 @@ sub export_csv($args_ref) {
elsif ($group_id eq "images") {
if ($args_ref->{include_images_paths}) {
if (defined $product_ref->{images}) {

# First list the selected images
my %selected_images = ();
foreach my $imageid (sort keys %{$product_ref->{images}}) {

if ($imageid =~ /^(front|ingredients|nutrition|packaging|other)_(\w\w)$/) {

$selected_images{$product_ref->{images}{$imageid}{imgid}} = 1;
$populated_fields{"image_" . $imageid . "_file"} = sprintf("%08d", 10 * 1000 ) . "_" . $imageid;
# Also export the crop coordinates
foreach my $coord (qw(x1 x2 y1 y2 angle normalize white_magic coordinates_image_size)) {
if ((defined $product_ref->{images}{$imageid}{$coord})
and (($coord !~ /^(x|y)/) or ($product_ref->{images}{$imageid}{$coord} != -1)) # -1 is passed when the image is not cropped
) {
$populated_fields{"image_" . $imageid . "_" . $coord} = sprintf("%08d", 10 * 1000 ) . "_" . $imageid . "_" . $coord;
}
}
}
}

# Then list unselected images as other
my $other = 0;
foreach my $imageid (sort keys %{$product_ref->{images}}) {

if (($imageid =~ /^(\d+)$/) and (not defined $selected_images{$imageid})) {
$other++;
$populated_fields{"image_" . "other_" . $other . "_file"} = sprintf("%08d", 10 * 1000 ) . "_" . "other_" . $other;
# Keep the imgid for second loop on products
$other_images{$product_ref->{code} . "." . "other_" . $other} = { imgid => $imageid};
}
}
include_image_paths($product_ref, \%populated_fields, \%other_images);
}
}
}
Expand Down Expand Up @@ -622,5 +592,41 @@ sub export_csv($args_ref) {
}



sub include_image_paths($product_ref, $populated_fields_ref, $other_images_ref) {

# First list the selected images
my %selected_images = ();
foreach my $imageid (sort keys %{$product_ref->{images}}) {

if ($imageid =~ /^(front|ingredients|nutrition|packaging|other)_(\w\w)$/) {

$selected_images{$product_ref->{images}{$imageid}{imgid}} = 1;
$populated_fields_ref->{"image_" . $imageid . "_file"} = sprintf("%08d", 10 * 1000 ) . "_" . $imageid;
# Also export the crop coordinates
foreach my $coord (qw(x1 x2 y1 y2 angle normalize white_magic coordinates_image_size)) {
if ((defined $product_ref->{images}{$imageid}{$coord})
and (($coord !~ /^(x|y)/) or ($product_ref->{images}{$imageid}{$coord} != -1)) # -1 is passed when the image is not cropped
) {
$populated_fields_ref->{"image_" . $imageid . "_" . $coord} = sprintf("%08d", 10 * 1000 ) . "_" . $imageid . "_" . $coord;
}
}
}
}

# Then list unselected images as other
my $other = 0;
foreach my $imageid (sort keys %{$product_ref->{images}}) {

if (($imageid =~ /^(\d+)$/) and (not defined $selected_images{$imageid})) {
$other++;
$populated_fields_ref->{"image_" . "other_" . $other . "_file"} = sprintf("%08d", 10 * 1000 ) . "_" . "other_" . $other;
# Keep the imgid for second loop on products
$other_images_ref->{$product_ref->{code} . "." . "other_" . $other} = { imgid => $imageid};
}
}
return;
}

1;

0 comments on commit 1c17eb0

Please sign in to comment.