Skip to content

Commit

Permalink
User settings improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
simba77 committed Feb 18, 2023
1 parent d112309 commit 0e571c3
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 32 deletions.
2 changes: 1 addition & 1 deletion config/autoload/system.global.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

'meta_title' => 'JohnCMS',
'meta_keywords' => 'johncms',
'meta_description' => 'Powered by JohnCMS http://johncms.com',
'meta_description' => 'Powered by JohnCMS https://johncms.com',

'email' => 'no-reply@example.com',
'copyright' => 'JohnCMS',
Expand Down
14 changes: 9 additions & 5 deletions modules/johncms/personal/src/Forms/SettingsForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@
use Johncms\Forms\Inputs\InputText;
use Johncms\Forms\Inputs\Select;
use Johncms\i18n\Languages;
use Johncms\Settings\SiteSettings;
use Johncms\Users\User;
use Johncms\Utility\DateTime;
use Johncms\View\Themes;

class SettingsForm extends AbstractForm
{
public function __construct(
protected ?User $userData = null
protected SiteSettings $siteSettings,
protected ?User $userData = null,
) {
parent::__construct();
}
Expand All @@ -31,13 +33,13 @@ protected function prepareFormFields(): array
->setOptions($this->getLanguages())
->setLabel(__('Language'))
->setNameAndId('lang')
->setValue($this->getValue('lang'));
->setValue($this->getValue('lang', $this->siteSettings->getLanguage()));

$fields['timezone'] = (new Select())
->setOptions($this->getTimezones())
->setLabel(__('Timezone'))
->setNameAndId('timezone')
->setValue($this->getValue('timezone'));
->setValue(parent::getValue('timezone', $this->siteSettings->getTimezone()));

$fields['directUrl'] = (new Checkbox())
->setLabel(__('Direct links'))
Expand All @@ -50,7 +52,7 @@ protected function prepareFormFields(): array
->setLabel(__('Elements per page'))
->setPlaceholder(__('Elements per page'))
->setNameAndId('perPage')
->setValue($this->getValue('perPage'));
->setValue(parent::getValue('perPage', $this->siteSettings->getPerPage()));

$fields['theme'] = (new Select())
->setOptions($this->getThemesList())
Expand All @@ -65,7 +67,9 @@ public function getValue(string $fieldName, mixed $default = null): mixed
{
if ($this->userData) {
// Base fields
return parent::getValue($fieldName, $this->userData->settings?->$fieldName);
if ($this->userData->settings?->$fieldName !== null) {
return parent::getValue($fieldName, $this->userData->settings?->$fieldName);
}
}
return parent::getValue($fieldName, $default);
}
Expand Down
12 changes: 7 additions & 5 deletions system/src/ConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
use Psr\Http\Message\ServerRequestInterface;
use Psr\Log\LoggerInterface;
use Psr\SimpleCache\CacheInterface;
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Serializer\SerializerInterface;

class ConfigProvider
Expand Down Expand Up @@ -81,10 +82,11 @@ private function getDependencies(): array
{
return [
'aliases' => [
Request::class => ServerRequestInterface::class,
RequestFactory::class => ServerRequestInterface::class,
Factory::class => Render::class,
'view' => Render::class,
Request::class => ServerRequestInterface::class,
RequestFactory::class => ServerRequestInterface::class,
Factory::class => Render::class,
'view' => Render::class,
SerializerInterface::class => Serializer::class,
],

'factories' => [
Expand All @@ -107,7 +109,7 @@ private function getDependencies(): array
ExceptionHandlers::class => ExceptionHandlers::class,
RouterFactory::class => RouterFactory::class,
Dispatcher::class => DispatcherFactory::class,
SerializerInterface::class => SerializerFactory::class,
Serializer::class => SerializerFactory::class,
],
];
}
Expand Down
12 changes: 9 additions & 3 deletions system/src/Users/Casts/UserSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@

use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
use Johncms\Users\UserConfig;
use JsonException;
use Symfony\Component\Serializer\Serializer;

class UserSettings implements CastsAttributes
{
Expand All @@ -24,14 +25,18 @@ class UserSettings implements CastsAttributes
*
* @param Model $model
* @param mixed $value
* @throws JsonException
*/
public function get($model, string $key, $value, array $attributes): UserConfig
{
$settings = [];
if (! empty($value)) {
$settings = json_decode($value, true, 512, JSON_THROW_ON_ERROR);
}
return new UserConfig($settings);

$serializer = di(Serializer::class);

return $serializer->denormalize($settings, UserConfig::class);
}

/**
Expand All @@ -42,6 +47,7 @@ public function get($model, string $key, $value, array $attributes): UserConfig
*/
public function set($model, string $key, $value, array $attributes): string
{
return json_encode((new Collection($value))->toArray(), JSON_THROW_ON_ERROR);
$serializer = di(Serializer::class);
return $serializer->serialize($value, 'json');
}
}
23 changes: 5 additions & 18 deletions system/src/Users/UserConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,16 @@ class UserConfig
/** @var bool Allow direct external links */
public bool $directUrl = false;

public int $perPage = 10;
public ?int $perPage = null;

public string $lang = 'en';
public ?string $lang = null;

public string $timezone = 'UTC';
public ?string $timezone = null;

public ?string $theme = null;

public function __construct(array $settings = [])
public function __set($name, $value)
{
foreach ($settings as $key => $value) {
$this->$key = $this->castValue(gettype($this->$key), $value);
}
}

private function castValue(string $type, mixed $value): float|bool|int|string
{
return match ($type) {
'int', 'integer' => (int) $value,
'float' => (float) $value,
'string' => (string) $value,
'bool', 'boolean' => (bool) $value,
default => $value,
};
$this->$name = $value;
}
}

0 comments on commit 0e571c3

Please sign in to comment.