-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from TappNetwork/add_table_column_and_filter
Add table column and filter
- Loading branch information
Showing
7 changed files
with
206 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
namespace Tapp\FilamentTimezoneField\Concerns; | ||
|
||
use DateTime; | ||
use DateTimeZone; | ||
|
||
trait CanFormatTimezone | ||
{ | ||
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 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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
namespace Tapp\FilamentTimezoneField\Concerns; | ||
|
||
use DateTime; | ||
use DateTimeZone; | ||
|
||
trait HasTimezoneOptions | ||
{ | ||
public function getOptions(): array | ||
{ | ||
$options = $this->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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
namespace Tapp\FilamentTimezoneField\Concerns; | ||
|
||
trait HasTimezoneType | ||
{ | ||
protected string | null $timezoneType = null; | ||
|
||
protected array $allowedTimezoneTypes = [ | ||
'UTC', | ||
'GMT', | ||
]; | ||
|
||
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
namespace Tapp\FilamentTimezoneField\Tables\Columns; | ||
|
||
use Filament\Tables\Columns\Column; | ||
use Filament\Tables\Columns\TextColumn; | ||
use Tapp\FilamentTimezoneField\Concerns\CanFormatTimezone; | ||
use Tapp\FilamentTimezoneField\Concerns\HasTimezoneType; | ||
|
||
class TimezoneColumn extends TextColumn | ||
{ | ||
use CanFormatTimezone; | ||
use HasTimezoneType; | ||
|
||
public function formattedTimezone(): static | ||
{ | ||
$this->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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
namespace Tapp\FilamentTimezoneField\Tables\Filters; | ||
|
||
use Filament\Tables\Filters\SelectFilter; | ||
use Tapp\FilamentTimezoneField\Concerns\CanFormatTimezone; | ||
use Tapp\FilamentTimezoneField\Concerns\HasTimezoneOptions; | ||
use Tapp\FilamentTimezoneField\Concerns\HasTimezoneType; | ||
|
||
class TimezoneSelectFilter extends SelectFilter | ||
{ | ||
use CanFormatTimezone; | ||
use HasTimezoneOptions; | ||
use HasTimezoneType; | ||
} |