|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace TopviewDigital\LangSwitcher\Model; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
6
|
|
|
|
|
7
|
|
|
class LangSwitcher extends Model |
|
8
|
|
|
{ |
|
9
|
|
|
protected $table = '_lang_switcher'; |
|
10
|
|
|
public $timestamps = false; |
|
11
|
|
|
/** |
|
12
|
|
|
* The attributes that are mass assignable. |
|
13
|
|
|
* |
|
14
|
|
|
* @var array |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $fillable = [ |
|
17
|
|
|
'class', 'method', 'middleware', 'enable', |
|
18
|
|
|
]; |
|
19
|
|
|
|
|
20
|
|
|
public static function registerGuard(array $settings) |
|
21
|
|
|
{ |
|
22
|
|
|
$keys = ['class', 'method', 'middleware', 'enable']; |
|
23
|
|
|
$settings = array_intersect_key($settings, array_flip($keys)); |
|
24
|
|
|
if (!empty(array_intersect(array_key($settings), $keys))) { |
|
|
|
|
|
|
25
|
|
|
$enable = $settings['enable'] ?? true; |
|
26
|
|
|
unset($settings['enable']); |
|
27
|
|
|
$settings = self::firstOrCreate($settings); |
|
28
|
|
|
$settings->refresh(); |
|
29
|
|
|
$settings->enable = $enable ? 1 : 0; |
|
30
|
|
|
$settings->save(); |
|
31
|
|
|
} |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public static function switchLocale($back = false) |
|
35
|
|
|
{ |
|
36
|
|
|
$switch_to = request()->input(config('lang-switcher.request.input')); |
|
37
|
|
|
self::setLocale($switch_to); |
|
38
|
|
|
if ($back) { |
|
39
|
|
|
return back(); |
|
40
|
|
|
} |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public static function setLocale($switch_to = null) |
|
44
|
|
|
{ |
|
45
|
|
|
$field = config('lang-switcher.field'); |
|
46
|
|
|
if (config('lang-switcher.via_model')) { |
|
47
|
|
|
foreach (self::where('enable', 1)->get() as $guard) { |
|
48
|
|
|
$class = $guard->class; |
|
|
|
|
|
|
49
|
|
|
$method = $guard->method; |
|
|
|
|
|
|
50
|
|
|
$model = $class::$method(); |
|
51
|
|
|
if ($model && $model->$field) { |
|
52
|
|
|
if ($switch_to) { |
|
53
|
|
|
$model->$field = $switch_to; |
|
54
|
|
|
$model->save(); |
|
55
|
|
|
} |
|
56
|
|
|
App::setLocale($model->$field); |
|
|
|
|
|
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
if (!request()->session()->has($field) || request()->session()->get($field) != app()->getLocale()) { |
|
|
|
|
|
|
61
|
|
|
request()->session()->put($field, app()->getLocale()); |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|