Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

task: create CollageLayoutEnum #1049

Merged
merged 1 commit into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 9 additions & 13 deletions api/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

require_once '../lib/boot.php';

use Photobooth\Enum\CollageLayoutEnum;
use Photobooth\Enum\FolderEnum;
use Photobooth\Environment;
use Photobooth\Service\ConfigurationService;
Expand Down Expand Up @@ -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'];
}
Expand All @@ -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
Expand Down
18 changes: 2 additions & 16 deletions lib/configsetup.inc.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use Photobooth\Enum\CollageLayoutEnum;
use Photobooth\Enum\ImageFilterEnum;
use Photobooth\Enum\MailSecurityTypeEnum;
use Photobooth\Enum\TimezoneEnum;
Expand Down Expand Up @@ -770,22 +771,7 @@
'type' => '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' => [
Expand Down
17 changes: 11 additions & 6 deletions src/Configuration/PhotoboothConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Photobooth\Configuration;

use Photobooth\Enum\CollageLayoutEnum;
use Photobooth\Enum\ImageFilterEnum;
use Photobooth\Enum\MailSecurityTypeEnum;
use Photobooth\Enum\TimezoneEnum;
Expand Down Expand Up @@ -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'])
Expand Down
95 changes: 95 additions & 0 deletions src/Enum/CollageLayoutEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php

declare(strict_types=1);

namespace Photobooth\Enum;

use Photobooth\Enum\Interface\LabelInterface;
use Photobooth\Utility\PathUtility;

enum CollageLayoutEnum: string implements LabelInterface
{
case TWO_PLUS_TWO_1 = '2+2-1';
case TWO_PLUS_TWO_2 = '2+2-2';
case ONE_PLUS_THREE_1 = '1+3-1';
case ONE_PLUS_THREE_2 = '1+3-2';
case THREE_PLUS_ONE = '3+1';
case ONE_PLUS_TWO = '1+2';
case TWO_PLUS_ONE = '2+1';
case TWO_X_FOUR_1 = '2x4-1';
case TWO_X_FOUR_2 = '2x4-2';
case TWO_X_FOUR_3 = '2x4-3';
case TWO_X_FOUR_4 = '2x4-4';
case TWO_X_THREE_1 = '2x3-1';
case TWO_X_THREE_2 = '2x3-2';
case COLLAGE_JSON = 'collage.json';

public function label(): string
{
return match($this) {
self::TWO_PLUS_TWO_1 => '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;
}
}
5 changes: 4 additions & 1 deletion src/Factory/CollageConfigFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@
namespace Photobooth\Factory;

use Photobooth\Dto\CollageConfig;
use Photobooth\Enum\CollageLayoutEnum;
use Photobooth\Utility\PathUtility;

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'];
Expand Down
Loading