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 Apr 12, 2024
1 parent 110764e commit 4ae51ff
Showing 1 changed file with 70 additions and 36 deletions.
106 changes: 70 additions & 36 deletions application/forms/ChannelForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use ipl\Validator\EmailAddressValidator;
use ipl\Web\Common\CsrfCounterMeasure;
use ipl\Web\Compat\CompatForm;
use stdClass;

class ChannelForm extends CompatForm
{
Expand All @@ -28,6 +27,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 +163,20 @@ protected function onSuccess()
}

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

foreach ($this->defaultChannelOptions as $key => $value) {
if (array_key_exists($key, $config) && $config[$key] === $value) {
unset($config[$key]);
}
}

$channel['config'] = json_encode($config);
if ($this->channelId === null) {
$this->db->insert('channel', $channel);
} else {
Expand All @@ -177,8 +192,20 @@ protected function onSuccess()
*/
protected function createConfigElements(string $type, string $config): void
{
/** @var array<int, stdClass> $elementsConfig */
$elementsConfig = json_decode($config, false);
/**
* @var array<int, array{
* 'name': string,
* 'type': string,
* 'label': array<string, string>,
* 'help'?: array<string, string>,
* 'required'?: bool,
* 'options'?: array<string, string>,
* 'default'?: string|bool,
* 'min'?: float|int,
* 'max'?: float|int
* }> $elementsConfig
*/
$elementsConfig = json_decode($config, true);

if (empty($elementsConfig)) {
return;
Expand All @@ -190,8 +217,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 +231,15 @@ protected function createConfigElements(string $type, string $config): void
}

/**
* Get the element type from given element config
* Get the element type config type of the object
*
* @param stdClass $elementConfig The config object of an element
* @param string $configType The config type of the object
*
* @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 +269,55 @@ protected function getElementType(stdClass $elementConfig): string
/**
* Get the element options from the given element config
*
* @param stdClass $elementConfig
* @param array{
* 'name': string,
* 'type': string,
* 'label': array<string, string>,
* 'help'?: array<string, string>,
* 'required'?: bool,
* 'options'?: array<string, string>,
* 'default'?: string|bool,
* 'min'?: float|int,
* 'max'?: float|int
* } $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 +329,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 4ae51ff

Please sign in to comment.