|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Epesi\Core\System\User\Settings\Database\Models; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
6
|
|
|
use Illuminate\Database\Eloquent\Collection; |
|
7
|
|
|
use Illuminate\Support\Facades\Auth; |
|
8
|
|
|
|
|
9
|
|
|
class UserSetting extends Model |
|
10
|
|
|
{ |
|
11
|
|
|
public $timestamps = false; |
|
12
|
|
|
protected $fillable = ['user_id', 'group', 'name', 'value']; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @var Collection |
|
16
|
|
|
*/ |
|
17
|
|
|
private static $userVariables; |
|
18
|
|
|
private static $adminVariables; |
|
19
|
|
|
|
|
20
|
|
|
private static function cache() { |
|
21
|
|
|
self::cacheUserVariables(); |
|
22
|
|
|
self::cacheAdminVariables(); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
private static function cacheUserVariables() { |
|
26
|
|
|
if(isset(self::$userVariables)) return; |
|
27
|
|
|
|
|
28
|
|
|
$userId = Auth::id(); |
|
29
|
|
|
|
|
30
|
|
|
foreach (self::where('user_id', $userId)->get() as $row) { |
|
31
|
|
|
self::$userVariables[$userId][$row['group']][$row['name']] = $row['value']; |
|
32
|
|
|
} |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
private static function cacheAdminVariables() { |
|
36
|
|
|
if(isset(self::$adminVariables)) return; |
|
37
|
|
|
|
|
38
|
|
|
foreach (self::where('user_id', 0) as $row) { |
|
39
|
|
|
self::$adminVariables[$row['group']][$row['name']] = $row['value']; |
|
40
|
|
|
} |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public static function get($group, $name, $user = null) { |
|
44
|
|
|
$user = $user?: Auth::id(); |
|
45
|
|
|
|
|
46
|
|
|
if (!$user || !is_numeric($user)) return; |
|
47
|
|
|
|
|
48
|
|
|
self::cache(); |
|
49
|
|
|
|
|
50
|
|
|
return self::$userVariables[$user][$group][$name]?? self::getAdmin($group, $name); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public static function getGroup($group, $user = null) { |
|
54
|
|
|
$user = $user?: Auth::id(); |
|
55
|
|
|
|
|
56
|
|
|
if (!$user || !is_numeric($user)) return; |
|
57
|
|
|
|
|
58
|
|
|
self::cache(); |
|
59
|
|
|
|
|
60
|
|
|
return self::$userVariables[$user][$group]?? []; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public static function getAdmin($group, $name) { |
|
64
|
|
|
self::cache(); |
|
65
|
|
|
|
|
66
|
|
|
return self::$adminVariables[$group][$name]?? null; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public static function getAdminGroup($group) { |
|
70
|
|
|
self::cache(); |
|
71
|
|
|
|
|
72
|
|
|
return self::$adminVariables[$group]?? []; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public static function put($group, $name, $value, $user = null) { |
|
76
|
|
|
$user_id = $user?: Auth::id(); |
|
77
|
|
|
|
|
78
|
|
|
if (!$user_id || !is_numeric($user_id)) return; |
|
79
|
|
|
|
|
80
|
|
|
self::cache(); |
|
81
|
|
|
|
|
82
|
|
|
self::$userVariables[$user_id][$group][$name] = $value; |
|
83
|
|
|
|
|
84
|
|
|
return self::updateOrCreate(compact('user_id', 'group', 'name'), compact('value')); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
public static function putGroup($group, $values, $user = null) { |
|
88
|
|
|
foreach ($values as $name => $value) { |
|
89
|
|
|
self::put($group, $name, $value, $user); |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
public static function putAdmin($group, $name, $value, $user = null) { |
|
|
|
|
|
|
94
|
|
|
self::cache(); |
|
95
|
|
|
|
|
96
|
|
|
self::$adminVariables[$group][$name] = $value; |
|
97
|
|
|
|
|
98
|
|
|
$user_id = 0; |
|
99
|
|
|
|
|
100
|
|
|
return self::updateOrCreate(compact('user_id', 'group', 'name'), compact('value')); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
public static function forget($group, $name, $user = null) { |
|
104
|
|
|
$user_id = $user?: Auth::id(); |
|
105
|
|
|
|
|
106
|
|
|
if (!$user_id || !is_numeric($user_id)) return; |
|
107
|
|
|
|
|
108
|
|
|
self::cache(); |
|
109
|
|
|
|
|
110
|
|
|
unset(self::$userVariables[$user_id][$group][$name]); |
|
111
|
|
|
|
|
112
|
|
|
return self::where(compact('user_id', 'group', 'name'))->delete(); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
public static function forgetAdmin($group, $name) { |
|
116
|
|
|
self::cache(); |
|
117
|
|
|
|
|
118
|
|
|
unset(self::$adminVariables[$group][$name]); |
|
119
|
|
|
|
|
120
|
|
|
$user_id = 0; |
|
121
|
|
|
|
|
122
|
|
|
return self::where(compact('user_id', 'group', 'name'))->delete(); |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.