1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\app\Library\CrudPanel\Traits; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Arr; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Key-value store for operations. |
9
|
|
|
*/ |
10
|
|
|
trait Settings |
11
|
|
|
{ |
12
|
|
|
protected $settings = []; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Getter for the settings key-value store. |
16
|
|
|
* |
17
|
|
|
* @param string $key Usually operation.name (ex: list.exportButtons) |
18
|
|
|
* @return mixed [description] |
19
|
|
|
*/ |
20
|
|
|
public function get(string $key) |
21
|
|
|
{ |
22
|
|
|
return $this->settings[$key] ?? null; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Setter for the settings key-value store. |
27
|
|
|
* |
28
|
|
|
* @param string $key Usually operation.name (ex: reorder.max_level) |
29
|
|
|
* @param mixed $value The value you want to store. |
30
|
|
|
* @return mixed Setting value. |
31
|
|
|
*/ |
32
|
|
|
public function set(string $key, $value) |
33
|
|
|
{ |
34
|
|
|
return $this->settings[$key] = $value; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Check if the settings key is used (has a value). |
39
|
|
|
* |
40
|
|
|
* @param string $key Usually operation.name (ex: reorder.max_level) |
41
|
|
|
* @return bool |
42
|
|
|
*/ |
43
|
|
|
public function has(string $key) |
44
|
|
|
{ |
45
|
|
|
if (isset($this->settings[$key])) { |
46
|
|
|
return true; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return false; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Get all operation settings, ordered by key. |
54
|
|
|
* |
55
|
|
|
* @return array |
56
|
|
|
*/ |
57
|
|
|
public function settings() |
58
|
|
|
{ |
59
|
|
|
return Arr::sort($this->settings, function ($value, $key) { |
60
|
|
|
return $key; |
61
|
|
|
}); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Getter and setter for the settings key-value store. |
66
|
|
|
* |
67
|
|
|
* @param string $key Usually operation.name (ex: list.exportButtons) |
68
|
|
|
* @param mixed $value The value you want to store. |
69
|
|
|
* @return mixed Setting value for setter. True/false for getter. |
70
|
|
|
*/ |
71
|
|
|
public function setting(string $key, $value = null) |
72
|
|
|
{ |
73
|
|
|
if ($value === null) { |
74
|
|
|
return $this->get($key); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $this->set($key, $value); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Convenience method for getting or setting a key on the current operation. |
82
|
|
|
* |
83
|
|
|
* @param string $key Has no operation prepended. (ex: exportButtons) |
84
|
|
|
* @param mixed $value The value you want to store. |
85
|
|
|
* @return mixed Setting value for setter. True/false for getter. |
86
|
|
|
*/ |
87
|
|
|
public function operationSetting(string $key, $value = null, $operation = null) |
88
|
|
|
{ |
89
|
|
|
$operation = $operation ?? $this->getCurrentOperation(); |
|
|
|
|
90
|
|
|
|
91
|
|
|
return $this->setting($operation.'.'.$key, $value); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Getter for the settings key-value store on a certain operation. |
96
|
|
|
* Defaults to the current operation. |
97
|
|
|
* |
98
|
|
|
* @param string $key Has no operation prepended. (ex: exportButtons) |
99
|
|
|
* @return mixed [description] |
100
|
|
|
*/ |
101
|
|
|
public function getOperationSetting(string $key, $operation = null) |
102
|
|
|
{ |
103
|
|
|
$operation = $operation ?? $this->getCurrentOperation(); |
104
|
|
|
|
105
|
|
|
return $this->get($operation.'.'.$key) ?? config('backpack.crud.operations.'.$this->getCurrentOperation().'.'.$key) ?? null; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Check if the settings key is used (has a value). |
110
|
|
|
* Defaults to the current operation. |
111
|
|
|
* |
112
|
|
|
* @param string $key Has no operation prepended. (ex: exportButtons) |
113
|
|
|
* @return mixed [description] |
114
|
|
|
*/ |
115
|
|
|
public function hasOperationSetting(string $key, $operation = null) |
116
|
|
|
{ |
117
|
|
|
$operation = $operation ?? $this->getCurrentOperation(); |
118
|
|
|
|
119
|
|
|
return $this->has($operation.'.'.$key); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Setter for the settings key-value store for a certain operation. |
124
|
|
|
* Defaults to the current operation. |
125
|
|
|
* |
126
|
|
|
* @param string $key Has no operation prepended. (ex: max_level) |
127
|
|
|
* @param bool $value True/false depending on success. |
128
|
|
|
*/ |
129
|
|
|
public function setOperationSetting(string $key, $value, $operation = null) |
130
|
|
|
{ |
131
|
|
|
$operation = $operation ?? $this->getCurrentOperation(); |
132
|
|
|
|
133
|
|
|
return $this->set($operation.'.'.$key, $value); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Automatically set values in config file (config/backpack/crud) |
138
|
|
|
* as settings values for that operation. |
139
|
|
|
* |
140
|
|
|
* @param string $configPath Config string that leads to where the configs are stored. |
141
|
|
|
*/ |
142
|
|
|
public function loadDefaultOperationSettingsFromConfig($configPath = null) |
143
|
|
|
{ |
144
|
|
|
$operation = $this->getCurrentOperation(); |
145
|
|
|
$configPath = $configPath ?? 'backpack.operations.'.$operation; |
146
|
|
|
$configSettings = config($configPath); |
147
|
|
|
|
148
|
|
|
if (is_array($configSettings) && count($configSettings)) { |
149
|
|
|
foreach ($configSettings as $key => $value) { |
150
|
|
|
$this->setOperationSetting($key, $value); |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Get the current CRUD operations loaded. |
157
|
|
|
* |
158
|
|
|
* @return array operaiton names |
159
|
|
|
*/ |
160
|
|
|
public function getAvailableOperationsList(): array |
161
|
|
|
{ |
162
|
|
|
return collect($this->settings) |
|
|
|
|
163
|
|
|
->keys() |
164
|
|
|
->filter(fn(string $key): bool => str_contains($key, '.access')) |
165
|
|
|
->map(fn(string $key): string => str_replace('.access', '', $key)) |
166
|
|
|
->values() |
167
|
|
|
->toArray(); |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|