diff --git a/CHANGELOG.md b/CHANGELOG.md index 45a196b..54ee57c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,22 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). +## [1.13.0] - 2023-02-17 + +### Removed +- Removed requirement for `treeware/plant` package. + +## [1.12.0] - 2023-02-15 + +### Added +- Support for Laravel 10 (Thank you [laravelshift](https://github.com/laravel-shift) ([PR#87](https://github.com/jamesmills/laravel-timezone/pull/87)) + +## [1.11.0] - 2022-02-16 +### Added +- Added Laravel 9 support. (Thank you [grantholle](https://github.com/grantholle)) ([PR#80](https://github.com/jamesmills/laravel-timezone/pull/80)) +- Make the message shown to users when timezone is set configurable. (Thank you [samtlewis](https://github.com/samtlewis)) ([PR#66](https://github.com/jamesmills/laravel-timezone/pull/66)) +- Added option to enable Carbon's translatedFormat. (Thank you [aldesrahim](https://github.com/aldesrahim)) ([PR#69](https://github.com/jamesmills/laravel-timezone/pull/69)) + ## [1.8.0] - 2020-01-22 ### Added - Added the ability to turn off flash messages and use others packages. (Thank you [amandiobm](https://github.com/amandiobm)) ([PR#15](https://github.com/jamesmills/laravel-timezone/pull/15)) diff --git a/README.md b/README.md index 77c62b7..e2910b9 100644 --- a/README.md +++ b/README.md @@ -121,6 +121,8 @@ When the timezone has been set, we display a flash message, By default, is confi [mckenziearts/laravel-notify](https://github.com/mckenziearts/laravel-notify) - `'flash' => 'mckenziearts'` +[usernotnull/tall-toasts](https://github.com/usernotnull/tall-toasts) - `'flash' => 'tall-toasts'` + To override this configuration, you just need to change the `flash` property inside the configuration file `config/timezone.php` for the desired package. You can disable flash messages by setting `'flash' => 'off'`. ### Overwrite existing timezones in the database @@ -135,6 +137,10 @@ By default, the date format will be `jS F Y g:i:a`. To override this configurati This lookup array configuration makes it possible to find the remote address of the user in any attribute inside the Laravel `request` helper, by any key. Having in mind when the key is found inside the attribute, that key will be used. By default, we use the `server` attribute with the key `REMOTE_ADDR`. To override this configuration, you just need to change the `lookup` property inside the configuration file `config/timezone.php` for the desired lookup. +### User Message + +You may configure the message shown to the user when the timezone is set by changing the `message` property inside the configuration file `config/timezone.php` + ### Underlying GeoIp Package If you wish to customise the underlying `torann/geoip` package you can publish the config file by using the command below. diff --git a/composer.json b/composer.json index fe5e326..3bf0964 100644 --- a/composer.json +++ b/composer.json @@ -9,20 +9,19 @@ "authors": [ { "name": "James Mills", - "email": "james@clicksco.com" + "email": "james@jamesmills.co.uk" } ], "require": { "php": ">=7.4", - "laravel/framework": "5.6.* || 5.7.* || 5.8.* || ^6 || ^7 || ^8", + "laravel/framework": "5.6.*|5.7.*|5.8.*|^6|^7|^8|^9|^10.0", "torann/geoip": "^3.0", - "nesbot/carbon": "^1.0 || ^2.0" + "nesbot/carbon": "^1.0|^2.0" }, "autoload": { "psr-4": { "JamesMills\\LaravelTimezone\\": "src/", "JamesMills\\LaravelTimezone\\Database\\Seeds\\": "database/seeds/" - } }, "extra": { @@ -33,7 +32,7 @@ } }, "require-dev": { - "friendsofphp/php-cs-fixer": "^2.12" + "friendsofphp/php-cs-fixer": "^2.12|^3.14" }, "scripts": { "pre-package-install": [ diff --git a/src/LaravelTimezoneServiceProvider.php b/src/LaravelTimezoneServiceProvider.php index e20f239..4ba0c55 100644 --- a/src/LaravelTimezoneServiceProvider.php +++ b/src/LaravelTimezoneServiceProvider.php @@ -55,6 +55,8 @@ function ($expression) { return ""; } elseif (count($options) == 3) { return ""; + } elseif (count($options) == 4) { + return ""; } else { return 'error'; } diff --git a/src/Listeners/Auth/UpdateUsersTimezone.php b/src/Listeners/Auth/UpdateUsersTimezone.php index 7e3eac1..f403c6b 100644 --- a/src/Listeners/Auth/UpdateUsersTimezone.php +++ b/src/Listeners/Auth/UpdateUsersTimezone.php @@ -9,7 +9,6 @@ class UpdateUsersTimezone { - /** * Handle the event. * @@ -52,7 +51,7 @@ public function handle($event) if ($user->timezone != $geoip_info['timezone']) { if (config('timezone.overwrite') == true || $user->timezone == null) { - $user->timezone = $geoip_info['timezone']; + $user->timezone = $geoip_info['timezone'] ?? $geoip_info->time_zone['name']; $user->save(); $this->notify($geoip_info); @@ -65,11 +64,11 @@ public function handle($event) */ private function notify(Location $geoip_info) { - if (config('timezone.flash') == 'off') { + if (request()->hasSession() && config('timezone.flash') == 'off') { return; } - $message = 'We have set your timezone to ' . $geoip_info['timezone']; + $message = sprintf(config('timezone.message', 'We have set your timezone to %s'), $geoip_info['timezone']); if (config('timezone.flash') == 'laravel') { request()->session()->flash('success', $message); @@ -100,11 +99,17 @@ private function notify(Location $geoip_info) return; } + + if (config('timezone.flash') == 'tall-toasts') { + toast()->success($message)->pushOnNextPage(); + + return; + } } /** - * @return mixed - */ + * @return mixed + */ private function getFromLookup() { $result = null; @@ -134,7 +139,7 @@ private function lookup($type, $keys) $value = null; foreach ($keys as $key) { - if (!request()->$type->has($key)) { + if (! request()->$type->has($key)) { continue; } $value = request()->$type->get($key); diff --git a/src/Timezone.php b/src/Timezone.php index bc2b6d1..6d9d6b4 100644 --- a/src/Timezone.php +++ b/src/Timezone.php @@ -12,21 +12,23 @@ class Timezone * @param bool $format_timezone * @return string */ - public function convertToLocal(?Carbon $date, $format = null, $format_timezone = false) : string + public function convertToLocal(?Carbon $date, $format = null, $format_timezone = false, $enableTranslation = null) : string { if (is_null($date)) { - return 'Empty'; + return __('Empty'); } $timezone = (auth()->user()->timezone) ?? config('app.timezone'); - + + $enableTranslation = $enableTranslation !== null ? $enableTranslation : config('timezone.enableTranslation'); + $date->setTimezone($timezone); if (is_null($format)) { - return $date->format(config('timezone.format')); + return $enableTranslation ? $date->translatedFormat(config('timezone.format')) : $date->format(config('timezone.format')); } - $formatted_date_time = $date->format($format); + $formatted_date_time = $enableTranslation ? $date->translatedFormat($format) : $date->format($format); if ($format_timezone) { return $formatted_date_time . ' ' . $this->formatTimezone($date); diff --git a/src/config/timezone.php b/src/config/timezone.php index 7b992b0..3bede06 100644 --- a/src/config/timezone.php +++ b/src/config/timezone.php @@ -9,7 +9,7 @@ | | Here you may configure if to use the laracasts/flash package for flash | notifications when a users timezone is set. - | options [off, laravel, laracasts, mercuryseries, spatie, mckenziearts] + | options [off, laravel, laracasts, mercuryseries, spatie, mckenziearts, tall-toasts] | */ @@ -39,6 +39,17 @@ */ 'format' => 'jS F Y g:i:a', + + /* + |-------------------------------------------------------------------------- + | Enable translated output + |-------------------------------------------------------------------------- + | + | Here you may configure if you would like to use translated output. + | + */ + + 'enableTranslation' => false, /* |-------------------------------------------------------------------------- @@ -59,4 +70,16 @@ ], ], + /* + |-------------------------------------------------------------------------- + | User Message + |-------------------------------------------------------------------------- + | + | Here you may configure the message shown to the user when the timezone is set. + | Be sure to include the %s which will be replaced by the detected timezone. + | e.g. We have set your timezone to America/New_York + | + */ + + 'message' => 'We have set your timezone to %s', ];