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.
This commit is contained in:
Bruno F. Fontes 2018-10-02 23:53:35 -03:00
parent 8157a183c7
commit 954820f46b
Signed by: brunofontes
GPG Key ID: EE3447CE80048495
8 changed files with 193 additions and 16 deletions

View File

@ -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');
}

View File

@ -0,0 +1,91 @@
<?php
namespace App\Http\Controllers;
use App;
use Lang;
use App\User;
use Illuminate\Http\Request;
class LanguageController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
//
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* @param int $locale
* @return \Illuminate\Http\Response
*/
public function update($locale)
{
App::setLocale($locale);
User::setLanguage($locale);
session(['lang' => $locale]);
session()->save();
return back();
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
}

View File

@ -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));
}
}
}

View File

@ -0,0 +1,34 @@
<?php
namespace App\Listeners;
use App\User;
use IlluminateAuthEventsLogin;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Auth\Events\Login;
class SetLanguage
{
/**
* Create the event listener.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Handle the event.
*
* @param IlluminateAuthEventsLogin $event
* @return void
*/
public function handle(Login $event)
{
session(['lang' => User::loggedIn()->language]);
session()->save();
}
}

View File

@ -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,
],
];
/**

View File

@ -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();
}
}
}

View File

@ -0,0 +1,32 @@
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AddLocationToUsers extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->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');
});
}
}

View File

@ -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');