Skip to content

Commit

Permalink
Fixed FormattedDate cast and some refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
simba77 committed Sep 19, 2021
1 parent 814cc98 commit 4586d0e
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 36 deletions.
7 changes: 5 additions & 2 deletions config/autoload/system.global.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@
'meta_keywords' => 'johncms',
'meta_description' => 'Powered by JohnCMS http://johncms.com',

'email' => 'no-reply@example.com',
'copyright' => 'JohnCMS',
'email' => 'no-reply@example.com',
'copyright' => 'JohnCMS',

'timezone' => 'UTC',
'language' => 'en',

// User model settings
'user_model' => [
Expand Down
4 changes: 2 additions & 2 deletions modules/johncms/language/templates/index.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
</button>
</div>
<div class="modal-body">
<form action="/language/" method="post" name="select_language">
<form action="<?= route('language.index') ?>" method="post" name="select_language">
<?php foreach ($config['lng_list'] as $key => $val): ?>
<div class="custom-control custom-radio mb-2">
<input type="radio" id="lang_<?= $key ?>" name="setlng" class="custom-control-input" value="<?= $key ?>" <?= ($key == $locale ? ' checked="checked"' : '') ?>>
<input type="radio" id="lang_<?= $key ?>" name="setLng" class="custom-control-input" value="<?= $key ?>" <?= ($key == $locale ? ' checked="checked"' : '') ?>>
<label class="custom-control-label" for="lang_<?= $key ?>">
<img class="icon icon-flag" src="<?= $this->asset('images/flags/' . strtolower($key) . '.svg') ?>" alt=".">
<?= $val['name'] ?>
Expand Down
36 changes: 6 additions & 30 deletions system/src/Casts/FormattedDate.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,48 +6,24 @@

use Carbon\Carbon;
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
use Illuminate\Database\Eloquent\Model;
use Johncms\i18n\Translator;
use Johncms\Users\User;
use Johncms\Settings\SiteSettings;

class FormattedDate implements CastsAttributes
{
/**
* Cast the given value.
*
* @param Model $model
* @param string $key
* @param int $value
* @param array $attributes
* @return Carbon|int
*/
public function get($model, $key, $value, $attributes)
public function get($model, string $key, $value, array $attributes)
{
if (! empty($value)) {
/** @var User $user */
$user = di(User::class);
/** @var Translator $translator */
$translator = di(Translator::class);

$settings = di(SiteSettings::class);
return Carbon::createFromTimeString($value)
->addHours($user->config->timeshift)
->locale($translator->getLocale())
->locale($settings->getLanguage())
->timezone($settings->getTimezone())
->isoFormat('lll');
}

return $value;
}

/**
* Prepare the given value for storage.
*
* @param Model $model
* @param string $key
* @param $value
* @param array $attributes
* @return string|null
*/
public function set($model, $key, $value, $attributes): ?string
public function set($model, string $key, $value, array $attributes): ?string
{
if (! empty($value)) {
return Carbon::parse($value)->toDateTimeString();
Expand Down
7 changes: 7 additions & 0 deletions system/src/ConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

namespace Johncms;

use JetBrains\PhpStorm\ArrayShape;
use JetBrains\PhpStorm\Pure;
use Johncms\Console\Commands\ClearCacheCommand;
use Johncms\Console\Commands\MakeMigrationCommand;
use Johncms\Console\Commands\MigrateCommand;
Expand All @@ -27,6 +29,7 @@
use Johncms\Media\MediaEmbed;
use Johncms\Middlewares\CsrfMiddleware;
use Johncms\Middlewares\SessionMiddleware;
use Johncms\Settings\SiteSettings;
use Johncms\Users\AuthProviders\CookiesAuthProvider;
use Johncms\Users\AuthProviders\SessionAuthProvider;
use Johncms\View\AdminRenderEngineFactory;
Expand All @@ -42,6 +45,8 @@

class ConfigProvider
{
#[Pure]
#[ArrayShape(['dependencies' => "\string[][]", 'middleware' => "string[]", 'providers' => "array", 'commands' => "string[]", 'auth_providers' => "string[]"])]
public function __invoke(): array
{
return [
Expand All @@ -53,6 +58,7 @@ public function __invoke(): array
];
}

#[ArrayShape(['aliases' => "string[]", 'factories' => "string[]"])]
private function getDependencies(): array
{
return [
Expand All @@ -78,6 +84,7 @@ private function getDependencies(): array
MediaEmbed::class => MediaEmbed::class,
Session::class => Session::class,
MetaTagManager::class => MetaTagManager::class,
SiteSettings::class => SiteSettings::class,
],
];
}
Expand Down
40 changes: 40 additions & 0 deletions system/src/Settings/SiteSettings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace Johncms\Settings;

use JetBrains\PhpStorm\Pure;
use Johncms\i18n\Translator;
use Johncms\Users\User;
use Psr\Container\ContainerInterface;

class SiteSettings
{
protected ?User $user = null;
protected array $config;
protected Translator $translator;

public function __construct(ContainerInterface $container)
{
$this->config = $container->get('config')['johncms'];
$this->user = $container->get(User::class);
$this->translator = $container->get(Translator::class);
}

public function __invoke(): static
{
return $this;
}

public function getTimezone(): string
{
return $this->user?->settings->timezone ?? $this->config['timezone'];
}

#[Pure]
public function getLanguage(): string
{
return $this->translator->getLocale();
}
}
4 changes: 2 additions & 2 deletions system/src/i18n/TranslatorServiceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ public function __invoke(ContainerInterface $container): Translator
$translator->setLocale(
$this->determineLocale(
$userLng,
$config['johncms']['lng'] ?? 'en',
$config['johncms']['language'] ?? 'en',
$config['johncms']['lng_list'] ?? [],
$request->getPost('setlng')
$request->getPost('setLng')
)
);

Expand Down

0 comments on commit 4586d0e

Please sign in to comment.