From 675200303a384533dc29861e553fb6d3d476cf85 Mon Sep 17 00:00:00 2001 From: Bruno Fontes Date: Tue, 2 Oct 2018 23:53:35 -0300 Subject: [PATCH] Added: language to User profile (DB) The language were set only on session. But now it is stored with user profile, on DB. It is important as now I can send alert e-mails to each user on their own languages and not the activer user language. Also, wherever the user logs out and logs in again, it will see the same site locale. --- app/Http/Controllers/AlertController.php | 11 ++- app/Http/Controllers/LanguageController.php | 91 +++++++++++++++++++ app/Listeners/AlertReturnedItem.php | 6 +- app/Listeners/SetLanguage.php | 34 +++++++ app/Providers/EventServiceProvider.php | 9 +- app/User.php | 20 +++- ...018_10_02_202925_add_location_to_users.php | 32 +++++++ routes/web.php | 6 +- 8 files changed, 193 insertions(+), 16 deletions(-) create mode 100644 app/Http/Controllers/LanguageController.php create mode 100644 app/Listeners/SetLanguage.php create mode 100644 database/migrations/2018_10_02_202925_add_location_to_users.php diff --git a/app/Http/Controllers/AlertController.php b/app/Http/Controllers/AlertController.php index 60dd7a7..e46aea8 100644 --- a/app/Http/Controllers/AlertController.php +++ b/app/Http/Controllers/AlertController.php @@ -2,6 +2,7 @@ namespace App\Http\Controllers; +use Auth; use \App\User; use \App\Mail\UserWaiting; use Illuminate\Http\Request; @@ -11,15 +12,15 @@ class AlertController extends Controller public function store(Request $request) { $item = User::loggedIn()->items()->find(request('item')); - $item->waiting_user_id = \Auth::id(); + $item->waiting_user_id = Auth::id(); $item->timestamps = false; $item->save(); - $loggedUser = \Auth::user()->name; + $loggedUser = Auth::user()->name; $userWithItem = User::find($item->used_by); - \Mail::to($userWithItem)->send( - new UserWaiting($loggedUser, $userWithItem->name, $item) - ); + \Mail::to($userWithItem) + ->locale($userWithItem->language) + ->send(new UserWaiting($loggedUser, $userWithItem->name, $item)); return redirect('home'); } diff --git a/app/Http/Controllers/LanguageController.php b/app/Http/Controllers/LanguageController.php new file mode 100644 index 0000000..41d8809 --- /dev/null +++ b/app/Http/Controllers/LanguageController.php @@ -0,0 +1,91 @@ + $locale]); + session()->save(); + return back(); + } + + /** + * Remove the specified resource from storage. + * + * @param int $id + * @return \Illuminate\Http\Response + */ + public function destroy($id) + { + // + } +} diff --git a/app/Listeners/AlertReturnedItem.php b/app/Listeners/AlertReturnedItem.php index f3630dc..1db13fe 100644 --- a/app/Listeners/AlertReturnedItem.php +++ b/app/Listeners/AlertReturnedItem.php @@ -32,9 +32,9 @@ class AlertReturnedItem { if ($event->item->waiting_user_id) { $user = User::find($event->item->waiting_user_id); - Mail::to($user)->send( - new ItemAvailable($user->name, $event->item) - ); + Mail::to($user) + ->locale($user->language) + ->send(new ItemAvailable($user->name, $event->item)); } } } diff --git a/app/Listeners/SetLanguage.php b/app/Listeners/SetLanguage.php new file mode 100644 index 0000000..0509f4b --- /dev/null +++ b/app/Listeners/SetLanguage.php @@ -0,0 +1,34 @@ + User::loggedIn()->language]); + session()->save(); + } +} diff --git a/app/Providers/EventServiceProvider.php b/app/Providers/EventServiceProvider.php index 3d41953..07d6954 100644 --- a/app/Providers/EventServiceProvider.php +++ b/app/Providers/EventServiceProvider.php @@ -2,12 +2,13 @@ namespace App\Providers; +use App\Events\ReturnItem; +use App\Listeners\SetLanguage; +use App\Listeners\AlertReturnedItem; use Illuminate\Support\Facades\Event; use Illuminate\Auth\Events\Registered; use Illuminate\Auth\Listeners\SendEmailVerificationNotification; use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider; -use App\Events\ReturnItem; -use App\Listeners\AlertReturnedItem; class EventServiceProvider extends ServiceProvider { @@ -24,6 +25,10 @@ class EventServiceProvider extends ServiceProvider ReturnItem::class => [ AlertReturnedItem::class, ], + + 'Illuminate\Auth\Events\Login' => [ + SetLanguage::class, + ], ]; /** diff --git a/app/User.php b/app/User.php index 6b353dd..a05942c 100644 --- a/app/User.php +++ b/app/User.php @@ -2,6 +2,7 @@ namespace App; +use Auth; use Illuminate\Notifications\Notifiable; use Illuminate\Contracts\Auth\MustVerifyEmail; use Illuminate\Foundation\Auth\User as Authenticatable; @@ -46,6 +47,23 @@ class User extends Authenticatable implements MustVerifyEmail */ public static function loggedIn() { - return (new static)->findOrFail(\Auth::id()); + return (new static)->findOrFail(Auth::id()); + } + + /** + * Set the default website language + * for the acual user + * + * @param string $language The language code + * + * @return void + */ + public static function setLanguage(string $language) + { + if (Auth::check()) { + $user = self::loggedIn(); + $user->language = $language; + $user->save(); + } } } diff --git a/database/migrations/2018_10_02_202925_add_location_to_users.php b/database/migrations/2018_10_02_202925_add_location_to_users.php new file mode 100644 index 0000000..2808d57 --- /dev/null +++ b/database/migrations/2018_10_02_202925_add_location_to_users.php @@ -0,0 +1,32 @@ +string('language')->after('email_verified_at')->default('en'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('users', function (Blueprint $table) { + $table->dropColumn('language'); + }); + } +} diff --git a/routes/web.php b/routes/web.php index 7c582d2..c111a63 100644 --- a/routes/web.php +++ b/routes/web.php @@ -15,11 +15,7 @@ Route::get('/', function () { return view('welcome'); }); -Route::get('/lang/{locale}', function ($locale) { - session(['lang' => $locale]); - session()->save(); - return back(); -}); +Route::get('/lang/{locale}', 'LanguageController@update')->name('language'); Route::get('/product', 'ProductController@index')->middleware('verified'); Route::get('/product/{product}', 'ProductController@show')->middleware('verified');