From bb8d3006298a893c67a208dd7ba28cf2f5001f83 Mon Sep 17 00:00:00 2001 From: Andreas Skomski Date: Mon, 13 Jan 2025 20:36:05 +0100 Subject: [PATCH] task: create CollageLayoutEnum Change-Id: I0bf66cd515afa045c8d3cf2baecb6b1c43e56909 --- api/admin.php | 22 ++--- lib/configsetup.inc.php | 18 +--- src/Configuration/PhotoboothConfiguration.php | 17 ++-- src/Enum/CollageLayoutEnum.php | 95 +++++++++++++++++++ src/Factory/CollageConfigFactory.php | 5 +- 5 files changed, 121 insertions(+), 36 deletions(-) create mode 100644 src/Enum/CollageLayoutEnum.php diff --git a/api/admin.php b/api/admin.php index 5ad384c4d..423ef4c67 100644 --- a/api/admin.php +++ b/api/admin.php @@ -5,6 +5,7 @@ require_once '../lib/boot.php'; +use Photobooth\Enum\CollageLayoutEnum; use Photobooth\Enum\FolderEnum; use Photobooth\Environment; use Photobooth\Service\ConfigurationService; @@ -217,18 +218,17 @@ } } - $collageLayout = $newConfig['collage']['layout']; + $newConfig['collage']['limit'] = CollageLayoutEnum::getLimitByValue($newConfig['collage']['layout']); + if ($newConfig['collage']['limit'] === 0) { + $logger->debug('Collage limit = 0. Falling back to defaults.'); + $newConfig['collage']['layout'] = CollageLayoutEnum::TWO_PLUS_TWO_2->value; + $newConfig['collage']['limit'] = CollageLayoutEnum::TWO_PLUS_TWO_2->limit(); + } + $collageConfigFilePath = PathUtility::getAbsolutePath('private/collage.json'); - if ($collageLayout === '1+2' || $collageLayout === '2+1' || strpos($collageLayout, '2x3') === 0) { - $newConfig['collage']['limit'] = 3; - } elseif ($collageLayout == 'collage.json' && file_exists($collageConfigFilePath)) { + if ($newConfig['collage']['layout'] == 'collage.json' && file_exists($collageConfigFilePath)) { $collageConfig = json_decode((string)file_get_contents($collageConfigFilePath), true); if (is_array($collageConfig)) { - if (array_key_exists('layout', $collageConfig)) { - $newConfig['collage']['limit'] = count($collageConfig['layout']); - } else { - $newConfig['collage']['limit'] = count($collageConfig); - } if (array_key_exists('placeholder', $collageConfig)) { $newConfig['collage']['placeholder'] = $collageConfig['placeholder']; } @@ -238,11 +238,7 @@ if (array_key_exists('placeholderpath', $collageConfig)) { $newConfig['collage']['placeholderpath'] = $collageConfig['placeholderpath']; } - } else { - $newConfig['collage']['limit'] = 4; } - } else { - $newConfig['collage']['limit'] = 4; } // If there is a collage placeholder whithin the correct range (0 < placeholderposition <= collage limit), we need to decrease the collage limit by 1 diff --git a/lib/configsetup.inc.php b/lib/configsetup.inc.php index 9db17a96a..1ed1df26d 100644 --- a/lib/configsetup.inc.php +++ b/lib/configsetup.inc.php @@ -1,5 +1,6 @@ 'select', 'name' => 'collage[layout]', 'placeholder' => $defaultConfig['collage']['layout'], - 'options' => [ - '2+2-1' => '2+2', - '2+2-2' => '2+2 (2)', - '1+3-1' => '1+3', - '1+3-2' => '1+3 (2)', - '3+1' => '3+1', - '1+2' => '1+2', - '2+1' => '2+1', - '2x4-1' => '2x4', - '2x4-2' => '2x4 (2)', - '2x4-3' => '2x4 (3)', - '2x4-4' => '2x4 (4)', - '2x3-1' => '2x3', - '2x3-2' => '2x3 (2)', - 'collage.json' => 'private/collage.json', - ], + 'options' => CollageLayoutEnum::cases(), 'value' => $config['collage']['layout'], ], 'layout_generator' => [ diff --git a/src/Configuration/PhotoboothConfiguration.php b/src/Configuration/PhotoboothConfiguration.php index 701ba3ac8..8537c59b0 100644 --- a/src/Configuration/PhotoboothConfiguration.php +++ b/src/Configuration/PhotoboothConfiguration.php @@ -2,6 +2,7 @@ namespace Photobooth\Configuration; +use Photobooth\Enum\CollageLayoutEnum; use Photobooth\Enum\ImageFilterEnum; use Photobooth\Enum\MailSecurityTypeEnum; use Photobooth\Enum\TimezoneEnum; @@ -1489,12 +1490,16 @@ protected function addCollage(): NodeDefinition ->end() ->end() ->enumNode('layout') - ->values([ - '2+2-1', '2+2-2', '1+3-1', '1+3-2', '3+1', '1+2', '2+1', - '2x4-1', '2x4-2', '2x4-3', '2x4-4', '2x3-1', '2x3-2', - 'collage.json', - ]) - ->defaultValue('2+2-2') + ->values(CollageLayoutEnum::cases()) + ->defaultValue(CollageLayoutEnum::TWO_PLUS_TWO_2) + ->beforeNormalization() + ->always(function ($value) { + if (is_string($value)) { + $value = CollageLayoutEnum::from($value); + } + return $value; + }) + ->end() ->end() ->enumNode('resolution') ->values(['150dpi', '300dpi', '400dpi', '600dpi']) diff --git a/src/Enum/CollageLayoutEnum.php b/src/Enum/CollageLayoutEnum.php new file mode 100644 index 000000000..489a3a15b --- /dev/null +++ b/src/Enum/CollageLayoutEnum.php @@ -0,0 +1,95 @@ + '2+2 Layout (Option 1)', + self::TWO_PLUS_TWO_2 => '2+2 Layout (Option 2)', + self::ONE_PLUS_THREE_1 => '1+3 Layout (Option 1)', + self::ONE_PLUS_THREE_2 => '1+3 Layout (Option 2)', + self::THREE_PLUS_ONE => '3+1 Layout', + self::ONE_PLUS_TWO => '1+2 Layout', + self::TWO_PLUS_ONE => '2+1 Layout', + self::TWO_X_FOUR_1 => '2x4 Layout (Option 1)', + self::TWO_X_FOUR_2 => '2x4 Layout (Option 2)', + self::TWO_X_FOUR_3 => '2x4 Layout (Option 3)', + self::TWO_X_FOUR_4 => '2x4 Layout (Option 4)', + self::TWO_X_THREE_1 => '2x3 Layout (Option 1)', + self::TWO_X_THREE_2 => '2x3 Layout (Option 2)', + self::COLLAGE_JSON => 'Collage JSON Configuration', + }; + } + + public function limit(): int + { + return match($this) { + self::TWO_PLUS_TWO_1, + self::TWO_PLUS_TWO_2, + self::ONE_PLUS_THREE_1, + self::ONE_PLUS_THREE_2, + self::THREE_PLUS_ONE, + self::TWO_X_FOUR_1, + self::TWO_X_FOUR_2, + self::TWO_X_FOUR_3, + self::TWO_X_FOUR_4 => 4, + + self::ONE_PLUS_TWO, + self::TWO_PLUS_ONE, + self::TWO_X_THREE_1, + self::TWO_X_THREE_2 => 3, + + self::COLLAGE_JSON => self::getCollageJsonLimit(), + }; + } + + private static function getCollageJsonLimit(): int + { + $collageConfigFilePath = PathUtility::getAbsolutePath('private/collage.json'); + $fallbackLimit = 0; + + if (!file_exists($collageConfigFilePath)) { + return $fallbackLimit; + } + + $collageConfig = json_decode((string)file_get_contents($collageConfigFilePath), true); + + if (is_array($collageConfig)) { + return array_key_exists('layout', $collageConfig) + ? count($collageConfig['layout']) + : count($collageConfig); + } + + return $fallbackLimit; + } + + public static function getLimitByValue(string $value): int + { + $case = self::tryFrom($value); + + return $case?->limit() ?? 0; + } +} diff --git a/src/Factory/CollageConfigFactory.php b/src/Factory/CollageConfigFactory.php index 9c05cdf82..28ee3263a 100644 --- a/src/Factory/CollageConfigFactory.php +++ b/src/Factory/CollageConfigFactory.php @@ -3,6 +3,7 @@ namespace Photobooth\Factory; use Photobooth\Dto\CollageConfig; +use Photobooth\Enum\CollageLayoutEnum; use Photobooth\Utility\PathUtility; class CollageConfigFactory @@ -10,7 +11,9 @@ class CollageConfigFactory public static function fromConfig(array $config): CollageConfig { $collageConfig = new CollageConfig(); - $collageConfig->collageLayout = $config['collage']['layout']; + $collageConfig->collageLayout = $config['collage']['layout'] instanceof CollageLayoutEnum + ? $config['collage']['layout']->value + : (string) $config['collage']['layout']; $collageConfig->collageResolution = (int) substr($config['collage']['resolution'], 0, -3); $collageConfig->collageBackgroundColor = $config['collage']['background_color']; $collageConfig->collageFrame = $config['collage']['frame'];