From bc3c7852838324c633ebaff20d2bc3bc9196e404 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=2E=20Nagy=20Gerg=C5=91?= Date: Wed, 27 Mar 2024 19:11:38 +0100 Subject: [PATCH 1/6] fix date handling --- src/Fields/Date.php | 57 +++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 53 insertions(+), 4 deletions(-) diff --git a/src/Fields/Date.php b/src/Fields/Date.php index 52c7f0d2..5f87eadf 100644 --- a/src/Fields/Date.php +++ b/src/Fields/Date.php @@ -4,8 +4,10 @@ use Closure; use DateTimeInterface; +use DateTimeZone; use Illuminate\Database\Eloquent\Model; use Illuminate\Http\Request; +use Illuminate\Support\Facades\Config; use Illuminate\Support\Facades\Date as DateFactory; class Date extends Field @@ -18,13 +20,18 @@ class Date extends Field /** * The timezone. */ - protected string $timezone = 'UTC'; + protected string $timezone; /** * Indicates if the field should include time. */ protected bool $withTime = false; + /** + * The default timezone. + */ + protected static ?string $defaultTimezone = null; + /** * Create a new field instance. */ @@ -34,6 +41,15 @@ public function __construct(string $label, Closure|string|null $modelAttribute = $this->type('date'); $this->step(1); + $this->timezone(static::$defaultTimezone ?: Config::get('app.timezone')); + } + + /** + * Set the default timezone. + */ + public static function defaultTimezone(string|DateTimeZone $value): void + { + static::$defaultTimezone = $value instanceof DateTimeZone ? $value->getName() : $value; } /** @@ -77,13 +93,46 @@ public function withTime(bool $value = true): static /** * Set the timezone. */ - public function timezone(string $value): static + public function timezone(string|DateTimeZone $value): static { - $this->timezone = $value; + $this->timezone = $value instanceof DateTimeZone ? $value->getName() : $value; + + $this->suffix($this->timezone); return $this; } + /** + * {@inheritdoc} + */ + public function getValueForHydrate(Request $request): ?string + { + $value = parent::getValueForHydrate($request); + + if (! is_null($value)) { + $value = DateFactory::parse($value, $this->timezone) + ->setTimezone(Config::get('app.timezone')) + ->toISOString(); + } + + return $value; + } + + /** + * {@inheritdoc} + */ + public function getValue(Model $model): mixed + { + $value = parent::getValue($model); + + if (! is_null($value)) { + $value = DateFactory::parse($value, Config::get('app.timezone')) + ->setTimezone($this->timezone); + } + + return $value; + } + /** * {@inheritdoc} */ @@ -91,7 +140,7 @@ public function resolveFormat(Request $request, Model $model): ?string { if (is_null($this->formatResolver)) { $this->formatResolver = function (Request $request, Model $model, mixed $value): ?string { - return is_null($value) ? $value : DateFactory::parse($value)->setTimezone($this->timezone)->format($this->format); + return is_null($value) ? $value : $value->format($this->format); }; } From 6e8aba3b314b9fb0f756f3fd7af566d46c3c31b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=2E=20Nagy=20Gerg=C5=91?= Date: Thu, 28 Mar 2024 14:26:01 +0100 Subject: [PATCH 2/6] version --- src/Root.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Root.php b/src/Root.php index a94d2d78..734c247c 100644 --- a/src/Root.php +++ b/src/Root.php @@ -22,7 +22,7 @@ class Root * * @var string */ - public const VERSION = '2.2.8'; + public const VERSION = '2.2.9'; /** * The registered booting callbacks. From 945f667c3eae06c384ccee604165699069976901 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=2E=20Nagy=20Gerg=C5=91?= Date: Fri, 29 Mar 2024 01:23:25 +0100 Subject: [PATCH 3/6] wip --- src/Fields/Date.php | 16 ++-------------- src/Models/Notification.php | 3 ++- src/Root.php | 20 ++++++++++++++++++++ 3 files changed, 24 insertions(+), 15 deletions(-) diff --git a/src/Fields/Date.php b/src/Fields/Date.php index 5f87eadf..ced795d9 100644 --- a/src/Fields/Date.php +++ b/src/Fields/Date.php @@ -3,6 +3,7 @@ namespace Cone\Root\Fields; use Closure; +use Cone\Root\Root; use DateTimeInterface; use DateTimeZone; use Illuminate\Database\Eloquent\Model; @@ -27,11 +28,6 @@ class Date extends Field */ protected bool $withTime = false; - /** - * The default timezone. - */ - protected static ?string $defaultTimezone = null; - /** * Create a new field instance. */ @@ -41,15 +37,7 @@ public function __construct(string $label, Closure|string|null $modelAttribute = $this->type('date'); $this->step(1); - $this->timezone(static::$defaultTimezone ?: Config::get('app.timezone')); - } - - /** - * Set the default timezone. - */ - public static function defaultTimezone(string|DateTimeZone $value): void - { - static::$defaultTimezone = $value instanceof DateTimeZone ? $value->getName() : $value; + $this->timezone(Root::instance()->getTimezone()); } /** diff --git a/src/Models/Notification.php b/src/Models/Notification.php index 6e445194..0701f2fd 100644 --- a/src/Models/Notification.php +++ b/src/Models/Notification.php @@ -4,6 +4,7 @@ use Cone\Root\Database\Factories\NotificationFactory; use Cone\Root\Interfaces\Models\Notification as Contract; +use Cone\Root\Root; use Cone\Root\Traits\InteractsWithProxy; use Illuminate\Database\Eloquent\Casts\Attribute; use Illuminate\Database\Eloquent\Concerns\HasUuids; @@ -68,7 +69,7 @@ protected function formattedCreatedAt(): Attribute { return new Attribute( get: function (): ?string { - return $this->created_at?->isoFormat('YYYY. MMMM DD. HH:mm'); + return $this->created_at?->setTimezone(Root::instance()->getTimezone())?->isoFormat('YYYY. MMMM DD. HH:mm'); } ); } diff --git a/src/Root.php b/src/Root.php index 734c247c..1512bb68 100644 --- a/src/Root.php +++ b/src/Root.php @@ -8,6 +8,7 @@ use Cone\Root\Navigation\Registry as Navigation; use Cone\Root\Resources\Resources; use Cone\Root\Widgets\Widgets; +use DateTimeZone; use Illuminate\Contracts\Foundation\Application; use Illuminate\Http\Request; use Illuminate\Support\Facades\App; @@ -59,6 +60,8 @@ class Root */ protected ?Closure $authResolver = null; + protected string $timezone; + /** * Create a new Root instance. */ @@ -69,6 +72,7 @@ public function __construct(Application $app) $this->widgets = new Widgets(); $this->navigation = new Navigation(); $this->breadcrumbs = new Breadcrumbs(); + $this->timezone = $app['config']->get('app.timezone'); } /** @@ -171,4 +175,20 @@ public function authorize(Closure $callback): void { $this->authResolver = $callback; } + + /** + * Set the Root timezone. + */ + public function setTimezone(string|DateTimeZone $value): void + { + $this->timezone = $value instanceof DateTimeZone ? $value->getName() : $value; + } + + /** + * Get the Root timezone. + */ + public function getTimezone(): string + { + return $this->timezone; + } } From 5c80eabb72a0b59f6871b6b1b6d9f7133b5c98af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=2E=20Nagy=20Gerg=C5=91?= Date: Sat, 30 Mar 2024 08:22:35 +0100 Subject: [PATCH 4/6] wip --- src/Http/Controllers/Auth/LoginController.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/Http/Controllers/Auth/LoginController.php b/src/Http/Controllers/Auth/LoginController.php index 2d465c15..bc64a806 100644 --- a/src/Http/Controllers/Auth/LoginController.php +++ b/src/Http/Controllers/Auth/LoginController.php @@ -49,9 +49,7 @@ public function login(Request $request): RedirectResponse } if (! Auth::guard()->user()->hasVerifiedEmail()) { - $this->logout($request); - - throw ValidationException::withMessages([ + return $this->logout($request)->withErrors([ 'email' => [__('auth.unverified')], ]); } From 5c3783ca3d102cad094228b92f8eb6a21d7fc98b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=2E=20Nagy=20Gerg=C5=91?= Date: Tue, 2 Apr 2024 14:33:35 +0200 Subject: [PATCH 5/6] wip --- resources/views/fields/input.blade.php | 4 ++-- resources/views/fields/morph-to.blade.php | 4 ++-- resources/views/fields/select.blade.php | 4 ++-- resources/views/fields/slug.blade.php | 4 ++-- src/Root.php | 5 ++++- 5 files changed, 12 insertions(+), 9 deletions(-) diff --git a/resources/views/fields/input.blade.php b/resources/views/fields/input.blade.php index fafaa854..b2217e2b 100644 --- a/resources/views/fields/input.blade.php +++ b/resources/views/fields/input.blade.php @@ -7,11 +7,11 @@
@if($prefix) -
{!! $prefix !!}
+
{!! $prefix !!}
@endif @if($suffix) -
{!! $suffix !!}
+
{!! $suffix !!}
@endif
@if($invalid) diff --git a/resources/views/fields/morph-to.blade.php b/resources/views/fields/morph-to.blade.php index 51b6236d..aa88a144 100644 --- a/resources/views/fields/morph-to.blade.php +++ b/resources/views/fields/morph-to.blade.php @@ -21,7 +21,7 @@ class="form-control"
@if($prefix) -
{!! $prefix !!}
+
{!! $prefix !!}
@endif @if($suffix) -
{!! $suffix !!}
+
{!! $suffix !!}
@endif
@if($invalid) diff --git a/resources/views/fields/select.blade.php b/resources/views/fields/select.blade.php index 7f0cf9c0..76551afb 100644 --- a/resources/views/fields/select.blade.php +++ b/resources/views/fields/select.blade.php @@ -8,7 +8,7 @@ @if(! empty($options))
@if($prefix) -
{!! $prefix !!}
+
{!! $prefix !!}
@endif @if($suffix) -
{!! $suffix !!}
+
{!! $suffix !!}
@endif
@if($invalid) diff --git a/resources/views/fields/slug.blade.php b/resources/views/fields/slug.blade.php index 3994c9a1..4e0b0cba 100644 --- a/resources/views/fields/slug.blade.php +++ b/resources/views/fields/slug.blade.php @@ -7,11 +7,11 @@
@if($prefix) -
{!! $prefix !!}
+
{!! $prefix !!}
@endif @if($suffix) -
{!! $suffix !!}
+
{!! $suffix !!}
@endif