61 lines
1.4 KiB
PHP
61 lines
1.4 KiB
PHP
<?php
|
|
|
|
namespace App\Repos\System\Nova;
|
|
|
|
use App\Models\System\Location\Province;
|
|
use Closure;
|
|
use Illuminate\Http\Request;
|
|
use Laravel\Nova\Events\ServingNova;
|
|
|
|
class NovaRepo
|
|
{
|
|
/**
|
|
* Serving nova application
|
|
*/
|
|
public static function serving(ServingNova $event): void
|
|
{
|
|
static::setLocale($event);
|
|
}
|
|
|
|
/**
|
|
* Set locales
|
|
*/
|
|
public static function setLocale($event): void
|
|
{
|
|
$user = $event->request->user();
|
|
|
|
if (array_key_exists($user?->locale, config('app.locales'))) {
|
|
app()->setLocale($user->locale);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Locale Switcher Save
|
|
*/
|
|
public static function localeSwitcherSave(): Closure
|
|
{
|
|
return function (Request $request) {
|
|
$locale = $request->post('locale');
|
|
|
|
if (array_key_exists($locale, config('app.locales'))) {
|
|
$request->user()->update(['locale' => $locale]);
|
|
}
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Depends on region
|
|
*/
|
|
public static function dependsOnRegion(string $attribute = 'region', string $model = Province::class): Closure
|
|
{
|
|
return function ($field, $request, $formData) use ($attribute, $model) {
|
|
info($formData->{$attribute});
|
|
$field->options(
|
|
$formData->{$attribute}
|
|
? $model::where('region', $formData->{$attribute})->pluck('name', 'id')
|
|
: []
|
|
);
|
|
};
|
|
}
|
|
}
|