From 4f980a78a410fc4014cbc00a366d8f7981a9a478 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9ia=20Bohner?= Date: Sat, 7 May 2022 22:53:32 -0300 Subject: [PATCH] Add table column and filter --- README.md | 50 +++++++++++++- src/Concerns/CanFormatTimezone.php | 38 +++++++++++ src/Concerns/HasTimezoneOptions.php | 37 ++++++++++ src/Concerns/HasTimezoneType.php | 29 ++++++++ src/Forms/Components/TimezoneSelect.php | 76 ++------------------- src/Tables/Columns/TimezoneColumn.php | 34 +++++++++ src/Tables/Filters/TimezoneSelectFilter.php | 15 ++++ 7 files changed, 207 insertions(+), 72 deletions(-) create mode 100644 src/Concerns/CanFormatTimezone.php create mode 100644 src/Concerns/HasTimezoneOptions.php create mode 100644 src/Concerns/HasTimezoneType.php create mode 100644 src/Tables/Columns/TimezoneColumn.php create mode 100644 src/Tables/Filters/TimezoneSelectFilter.php diff --git a/README.md b/README.md index 04107c9..040f85c 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,8 @@ composer require tapp/filament-timezone-field ## Usage +### Form Field + Add to your Filament resource: ```php @@ -26,11 +28,11 @@ public static function form(Form $form): Form } ``` -### Appareance +#### Appareance ![Filament Timezone Field](https://raw.githubusercontent.com/TappNetwork/filament-timezone-field/main/docs/filament-timezone-field.png) -### Options +#### Options To use GMT instead of UTC (default is UTC), add the `->timezoneType('GMT')` method: @@ -49,7 +51,7 @@ public static function form(Form $form): Form } ``` -All Filament select field methods are available to use: +All [Filament select field](https://filamentphp.com/docs/2.x/forms/fields#select) methods are available to use: ```php use Tapp\FilamentTimezoneField\Forms\Components\TimezoneSelect; @@ -66,3 +68,45 @@ public static function form(Form $form): Form ]); } ``` + +### Table Column + +```php +use Tapp\FilamentTimezoneField\Tables\Columns\TimezoneColumn; + +public static function table(Table $table): Table +{ + return $table + ->columns([ + //... + TimezoneColumn::make('timezone') + ->timezoneType('GMT') + ->formattedOffsetAndTimezone(), + ]) + // ... +} +``` + +#### Options + +| Method | Description | +| --- | --- | +| ->formattedTimezone() | Show formatted timezone name | +| ->formattedOffsetAndTimezone() | Show formatted offset and timezone name | +| ->timezoneType('GMT') | Use GMT instead of UTC | + +### Table Filter + +```php +use Tapp\FilamentTimezoneField\Tables\Filters\TimezoneSelectFilter; + +public static function table(Table $table): Table +{ + return $table + //... + ->filters([ + TimezoneSelectFilter::make('timezone'), + // ... + ]) +} +``` diff --git a/src/Concerns/CanFormatTimezone.php b/src/Concerns/CanFormatTimezone.php new file mode 100644 index 0000000..1d4346a --- /dev/null +++ b/src/Concerns/CanFormatTimezone.php @@ -0,0 +1,38 @@ +getTimezoneType().($offset ? sprintf('%+03d:%02d', $hours, $minutes) : ''); + } + + protected function getFormattedTimezoneName(string $name): string + { + return str_replace( + ['/', '_', 'St '], + [', ', ' ', 'St. '], + $name + ); + } + + protected function getFormattedOffsetAndTimezone(string $offset, string $timezone): string + { + return sprintf('(%s) %s', $this->getFormattedOffset($offset), $this->getFormattedTimezoneName($timezone)); + } + + public function getOffset(string $timezone): string + { + $now = new DateTime('now', new DateTimeZone($this->getTimezoneType())); + + return $now->setTimezone(new DateTimeZone($timezone))->getOffset(); + } +} diff --git a/src/Concerns/HasTimezoneOptions.php b/src/Concerns/HasTimezoneOptions.php new file mode 100644 index 0000000..cfe43ef --- /dev/null +++ b/src/Concerns/HasTimezoneOptions.php @@ -0,0 +1,37 @@ +getTimezones(); + + $this->options = $options; + + return $this->options; + } + + public function getTimezones(): array + { + $timezones = DateTimeZone::listIdentifiers(DateTimeZone::ALL); + + $data = []; + + $now = new DateTime('now', new DateTimeZone($this->getTimezoneType())); + + foreach ($timezones as $timezone) { + $offsets[] = $offset = $now->setTimezone(new DateTimeZone($timezone))->getOffset(); + + $data[$timezone] = $this->getFormattedOffsetAndTimezone($offset, $timezone); + } + + array_multisort($offsets, $data); + + return $data; + } +} diff --git a/src/Concerns/HasTimezoneType.php b/src/Concerns/HasTimezoneType.php new file mode 100644 index 0000000..bf14dfb --- /dev/null +++ b/src/Concerns/HasTimezoneType.php @@ -0,0 +1,29 @@ +timezoneType = strtoupper($type); + + return $this; + } + + public function getTimezoneType(): string + { + if ($this->timezoneType === null && !in_array($this->timezoneType, $this->allowedTimezoneTypes)) { + $this->timezoneType = 'UTC'; + } + + return $this->timezoneType; + } +} diff --git a/src/Forms/Components/TimezoneSelect.php b/src/Forms/Components/TimezoneSelect.php index 86d0710..713bd42 100644 --- a/src/Forms/Components/TimezoneSelect.php +++ b/src/Forms/Components/TimezoneSelect.php @@ -2,78 +2,16 @@ namespace Tapp\FilamentTimezoneField\Forms\Components; -use DateTimeZone; use Filament\Forms\Components\Select; +use Tapp\FilamentTimezoneField\Concerns\CanFormatTimezone; +use Tapp\FilamentTimezoneField\Concerns\HasTimezoneOptions; +use Tapp\FilamentTimezoneField\Concerns\HasTimezoneType; class TimezoneSelect extends Select { - protected string $view = 'forms::components.select'; - - protected string | null $timezoneType = null; - - protected array $allowedTimezoneTypes = [ - 'UTC', - 'GMT', - ]; - - public function getOptions(): array - { - $options = $this->getTimezones(); - - $this->options = $options; - - return $this->options; - } - - public function timezoneType(string | null $type): static - { - $this->timezoneType = strtoupper($type); - - return $this; - } - - public function getTimezoneType(): string - { - if ($this->timezoneType === null && !in_array($this->timezoneType, $this->allowedTimezoneTypes)) { - $this->timezoneType = 'UTC'; - } - - return $this->timezoneType; - } + use CanFormatTimezone; + use HasTimezoneOptions; + use HasTimezoneType; - public function getTimezones(): array - { - $timezones = DateTimeZone::listIdentifiers(DateTimeZone::ALL); - - $data = []; - - $now = new \DateTime('now', new DateTimeZone($this->getTimezoneType())); - - foreach ($timezones as $timezone) { - $offsets[] = $offset = $now->setTimezone(new DateTimeZone($timezone))->getOffset(); - - $data[$timezone] = sprintf('(%s) %s', $this->getFormattedOffset($offset), $this->getFormattedTimezoneName($timezone)); - } - - array_multisort($offsets, $data); - - return $data; - } - - protected function getFormattedOffset(string $offset): string - { - $hours = intval($offset / 3600); - $minutes = abs(intval($offset % 3600 / 60)); - - return $this->getTimezoneType().($offset ? sprintf('%+03d:%02d', $hours, $minutes) : ''); - } - - protected function getFormattedTimezoneName(string $name): string - { - return str_replace( - ['/', '_', 'St '], - [', ', ' ', 'St. '], - $name - ); - } + protected string $view = 'forms::components.select'; } diff --git a/src/Tables/Columns/TimezoneColumn.php b/src/Tables/Columns/TimezoneColumn.php new file mode 100644 index 0000000..6c1f158 --- /dev/null +++ b/src/Tables/Columns/TimezoneColumn.php @@ -0,0 +1,34 @@ +defaultState = $this->formatStateUsing(static function (Column $column, $state): ?string { + return $column->getFormattedTimezoneName($state); + }); + + return $this; + } + + public function formattedOffsetAndTimezone(): static + { + $this->defaultState = $this->formatStateUsing(static function (Column $column, $state): ?string { + $offset = $column->getOffset($state); + + return $column->getFormattedOffsetAndTimezone($offset, $state); + }); + + return $this; + } +} diff --git a/src/Tables/Filters/TimezoneSelectFilter.php b/src/Tables/Filters/TimezoneSelectFilter.php new file mode 100644 index 0000000..bf7fa7d --- /dev/null +++ b/src/Tables/Filters/TimezoneSelectFilter.php @@ -0,0 +1,15 @@ +