Skip to content

Commit

Permalink
ChannelForm: Render default values as element values
Browse files Browse the repository at this point in the history
Instead of rendering default values as placeholders, render them as form element values.
  • Loading branch information
raviks789 committed May 24, 2024
1 parent e044065 commit 31de5e1
Showing 1 changed file with 62 additions and 36 deletions.
98 changes: 62 additions & 36 deletions application/forms/ChannelForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,20 @@
use ipl\Validator\EmailAddressValidator;
use ipl\Web\Common\CsrfCounterMeasure;
use ipl\Web\Compat\CompatForm;
use stdClass;

/**
* @phpstan-type ChannelOptionConfig array{
* name: string,
* type: string,
* label: array<string, string>,
* help?: array<string, string>,
* required?: bool,
* options?: array<string, string>,
* default?: string|bool|int|float,
* min?: float|int,
* max?: float|int
* }
*/
class ChannelForm extends CompatForm
{
use CsrfCounterMeasure;
Expand All @@ -28,6 +40,9 @@ class ChannelForm extends CompatForm
/** @var ?int Channel ID */
private $channelId;

/** @var array<string, mixed> */
private $defaultChannelOptions = [];

public function __construct(Connection $db, ?int $channelId = null)
{
$this->db = $db;
Expand Down Expand Up @@ -161,7 +176,21 @@ protected function onSuccess()
}

$channel = $this->getValues();
$channel['config'] = json_encode($channel['config']);
$config = array_filter(
$channel['config'],
function ($configItem, $key) {
return (
$configItem !== null
&& (
! isset($this->defaultChannelOptions[$key])
|| $this->defaultChannelOptions[$key] !== $configItem
)
);
},
ARRAY_FILTER_USE_BOTH
);

$channel['config'] = json_encode($config);
if ($this->channelId === null) {
$this->db->insert('channel', $channel);
} else {
Expand All @@ -177,8 +206,8 @@ protected function onSuccess()
*/
protected function createConfigElements(string $type, string $config): void
{
/** @var array<int, stdClass> $elementsConfig */
$elementsConfig = json_decode($config, false);
/** @var array<int, ChannelOptionConfig> $elementsConfig */
$elementsConfig = json_decode($config, true);

if (empty($elementsConfig)) {
return;
Expand All @@ -190,8 +219,8 @@ protected function createConfigElements(string $type, string $config): void
foreach ($elementsConfig as $elementConfig) {
/** @var BaseFormElement $elem */
$elem = $this->createElement(
$this->getElementType($elementConfig),
$elementConfig->name,
$this->getElementType($elementConfig['type']),
$elementConfig['name'],
$this->getElementOptions($elementConfig)
);

Expand All @@ -204,15 +233,15 @@ protected function createConfigElements(string $type, string $config): void
}

/**
* Get the element type from given element config
* Get the element type for the given option type
*
* @param stdClass $elementConfig The config object of an element
* @param string $configType The option type
*
* @return string
*/
protected function getElementType(stdClass $elementConfig): string
protected function getElementType(string $configType): string
{
switch ($elementConfig->type) {
switch ($configType) {
case 'string':
$elementType = 'text';
break;
Expand Down Expand Up @@ -242,48 +271,45 @@ protected function getElementType(stdClass $elementConfig): string
/**
* Get the element options from the given element config
*
* @param stdClass $elementConfig
* @param ChannelOptionConfig $elementConfig
*
* @return string[]
* @return array<string, mixed>
*/
protected function getElementOptions(stdClass $elementConfig): array
protected function getElementOptions(array $elementConfig): array
{
$options = [
'label' => $this->fromCurrentLocale($elementConfig->label)
'label' => $this->fromCurrentLocale($elementConfig['label'])
];

if (isset($elementConfig->help)) {
$options['description'] = $this->fromCurrentLocale($elementConfig->help);
if (isset($elementConfig['help'])) {
$options['description'] = $this->fromCurrentLocale($elementConfig['help']);
}

if (isset($elementConfig->required)) {
$options['required'] = $elementConfig->required;
if (isset($elementConfig['required'])) {
$options['required'] = $elementConfig['required'];
}

$isSelectElement = isset($elementConfig->options)
&& ($elementConfig->type === 'option' || $elementConfig->type === 'options');
$isSelectElement = isset($elementConfig['options'])
&& ($elementConfig['type'] === 'option' || $elementConfig['type'] === 'options');
if ($isSelectElement) {
$options['options'] = (array) $elementConfig->options;
if ($elementConfig->type === 'options') {
$options['options'] = $elementConfig['options'];
if ($elementConfig['type'] === 'options') {
$options['multiple'] = true;
}
}

if (isset($elementConfig->default)) {
if ($isSelectElement || $elementConfig->type === 'bool') {
$options['value'] = $elementConfig->default;
} else {
$options['placeholder'] = $elementConfig->default;
}
if (isset($elementConfig['default'])) {
$this->defaultChannelOptions[$elementConfig['name']] = $elementConfig['default'];
$options['value'] = $elementConfig['default'];
}

if ($elementConfig->type === "number") {
if (isset($elementConfig->min)) {
$options['min'] = $elementConfig->min;
if ($elementConfig['type'] === "number") {
if (isset($elementConfig['min'])) {
$options['min'] = $elementConfig['min'];
}

if (isset($elementConfig->max)) {
$options['max'] = $elementConfig->max;
if (isset($elementConfig['max'])) {
$options['max'] = $elementConfig['max'];
}
}

Expand All @@ -295,17 +321,17 @@ protected function getElementOptions(stdClass $elementConfig): array
*
* Fallback to locale `en_US` if the current locale isn't provided
*
* @param stdClass $localeMap
* @param array<string, string> $localeMap
*
* @return ?string Only returns null if the fallback locale is also not specified
*/
protected function fromCurrentLocale(stdClass $localeMap): ?string
protected function fromCurrentLocale(array $localeMap): ?string
{
/** @var GettextTranslator $translator */
$translator = StaticTranslator::$instance;
$default = $translator->getDefaultLocale();
$locale = $translator->getLocale();

return $localeMap->$locale ?? $localeMap->$default ?? null;
return $localeMap[$locale] ?? $localeMap[$default] ?? null;
}
}

0 comments on commit 31de5e1

Please sign in to comment.