-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add ability to set timezone of an employee (#706)
- Loading branch information
Showing
15 changed files
with
355 additions
and
61 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,109 @@ | ||
<?php | ||
|
||
namespace App\Helpers; | ||
|
||
use DateTimeZone; | ||
use Carbon\Carbon; | ||
|
||
class TimezoneHelper | ||
{ | ||
/** | ||
* Get the list of timezones. | ||
* | ||
* @return array | ||
*/ | ||
public static function getListOfTimezones(): array | ||
{ | ||
$timezones = DateTimeZone::listIdentifiers(); | ||
|
||
$timezoneCollection = collect(); | ||
foreach ($timezones as $timezone) { | ||
$formatedTimezone = self::formatTimezone($timezone); | ||
$timezoneCollection->push([ | ||
'value' => $formatedTimezone['name'], | ||
'label' => $formatedTimezone['friendly_name'], | ||
'sort_key' => $formatedTimezone['offset'], | ||
]); | ||
} | ||
|
||
// we need to remove the keys we don't need as the view needs only 2 keys | ||
$timezoneCollection = $timezoneCollection->sortBy('sort_key'); | ||
$timezoneCollection = $timezoneCollection->map(function ($item) { | ||
return array_only($item, ['value', 'label']); | ||
}); | ||
|
||
// we need to reset the keys before converting it to an array | ||
// otherwise, Vue doesn't like it | ||
return $timezoneCollection->values()->toArray(); | ||
} | ||
|
||
/** | ||
* Format a timezone to be displayed (english locale only). | ||
* | ||
* @param string $timezone | ||
* @return array | ||
*/ | ||
private static function formatTimezone(string $timezone): array | ||
{ | ||
$offset = self::getOffset($timezone); | ||
|
||
if ($timezone == 'UTC') { | ||
$timezoneName = 'UTC'; | ||
} else { | ||
$timezoneName = Carbon::now($timezone)->tzName; | ||
} | ||
|
||
$friendlyName = self::getTimezoneFriendlyName($timezoneName, $offset); | ||
|
||
return [ | ||
'name' => $timezoneName, | ||
'friendly_name' => $friendlyName, | ||
'offset' => $offset, | ||
]; | ||
} | ||
|
||
private static function getTimezoneFriendlyName(string $timezoneName, string $offset): string | ||
{ | ||
$dtimezone = new DateTimeZone($timezoneName); | ||
$location = $dtimezone->getLocation(); | ||
|
||
if ($timezoneName == 'UTC') { | ||
$friendlyName = '(UTC) Universal Time Coordinated'; | ||
} else { | ||
if (empty($location['comments'])) { | ||
$friendlyName = '(UTC '.$offset.') '.$timezoneName; | ||
} else { | ||
$friendlyName = '(UTC '.$offset.') '.$location['comments'].' ('.$timezoneName.')'; | ||
} | ||
} | ||
|
||
return $friendlyName; | ||
} | ||
|
||
private static function getOffset(string $timezoneName): string | ||
{ | ||
return Carbon::now($timezoneName)->format('P'); | ||
} | ||
|
||
/** | ||
* Get the value/label combo of the timezone in an array based on its codename. | ||
* | ||
* @param string $timezoneName | ||
* @return array | ||
*/ | ||
public static function getTimezoneKeyValue(string $timezoneName): array | ||
{ | ||
$timezones = DateTimeZone::listIdentifiers(); | ||
|
||
foreach ($timezones as $timezone) { | ||
if ($timezoneName == Carbon::now($timezone)->tzName) { | ||
return [ | ||
'value' => $timezoneName, | ||
'label' => self::getTimezoneFriendlyName($timezoneName, self::getOffset($timezoneName)), | ||
]; | ||
} | ||
} | ||
|
||
return []; | ||
} | ||
} |
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,42 @@ | ||
<?php | ||
|
||
namespace App\Http\ViewHelpers\Employee; | ||
|
||
use Carbon\Carbon; | ||
use App\Helpers\TimezoneHelper; | ||
use App\Models\Company\Employee; | ||
|
||
class EmployeeEditViewHelper | ||
{ | ||
/** | ||
* Get information about the employee being edited. | ||
* | ||
* @param Employee $employee | ||
* @return array | ||
*/ | ||
public static function show(Employee $employee): array | ||
{ | ||
return [ | ||
'id' => $employee->id, | ||
'first_name' => $employee->first_name, | ||
'last_name' => $employee->last_name, | ||
'name' => $employee->name, | ||
'email' => $employee->email, | ||
'phone' => $employee->phone_number, | ||
'birthdate' => (! $employee->birthdate) ? null : [ | ||
'year' => $employee->birthdate->year, | ||
'month' => $employee->birthdate->month, | ||
'day' => $employee->birthdate->day, | ||
], | ||
'hired_at' => (! $employee->hired_at) ? null : [ | ||
'year' => $employee->hired_at->year, | ||
'month' => $employee->hired_at->month, | ||
'day' => $employee->hired_at->day, | ||
], | ||
'twitter_handle' => $employee->twitter_handle, | ||
'slack_handle' => $employee->slack_handle, | ||
'max_year' => Carbon::now()->year, | ||
'timezone' => TimezoneHelper::getTimezoneKeyValue($employee->timezone), | ||
]; | ||
} | ||
} |
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
21 changes: 21 additions & 0 deletions
21
database/migrations/2021_04_03_182709_add_timezones_to_employees.php
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,21 @@ | ||
<?php | ||
|
||
use Illuminate\Support\Facades\Schema; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Database\Migrations\Migration; | ||
|
||
class AddTimezonesToEmployees extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
*/ | ||
public function up() | ||
{ | ||
// necessary for SQLlite | ||
Schema::enableForeignKeyConstraints(); | ||
|
||
Schema::table('employees', function (Blueprint $table) { | ||
$table->string('timezone')->after('pronoun_id')->default('UTC'); | ||
}); | ||
} | ||
} |
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
Oops, something went wrong.