|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Epesi\Base\CommonData\Models; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Collection; |
|
6
|
|
|
use atk4\data\Model; |
|
7
|
|
|
use Epesi\Core\Data\HasEpesiConnection; |
|
|
|
|
|
|
8
|
|
|
|
|
9
|
|
|
class CommonDataNotFound extends \Exception {} |
|
10
|
|
|
|
|
11
|
|
|
class CommonData extends Model { |
|
12
|
|
|
use HasEpesiConnection; |
|
13
|
|
|
|
|
14
|
|
|
public $table = 'commondata'; |
|
15
|
|
|
|
|
16
|
|
|
public $caption = 'Common Data'; |
|
17
|
|
|
|
|
18
|
|
|
public $title_field = 'value'; |
|
19
|
|
|
|
|
20
|
|
|
protected static $cache = [ |
|
21
|
|
|
'id' => [], |
|
22
|
|
|
'value' => [], |
|
23
|
|
|
'array' => [] |
|
24
|
|
|
]; |
|
25
|
|
|
|
|
26
|
|
|
function init() { |
|
27
|
|
|
parent::init(); |
|
28
|
|
|
|
|
29
|
|
|
$this->addFields([ |
|
30
|
|
|
'key' => ['caption' => __('Key')], |
|
31
|
|
|
'value' => ['caption' => __('Value')], |
|
32
|
|
|
'position' => ['type' => 'integer', 'caption' => __('Position')], |
|
33
|
|
|
'readonly' => ['type' => 'boolean'] |
|
34
|
|
|
]); |
|
35
|
|
|
|
|
36
|
|
|
$this->hasOne('parent', [self::class, 'our_field' => 'parent']); |
|
37
|
|
|
|
|
38
|
|
|
$this->getAction('edit')->ui['execButton'] = ['Button', __('Save'), 'class' => ['primary']]; |
|
39
|
|
|
|
|
40
|
|
|
$this->addHook('beforeInsert', function($node, & $data) { |
|
41
|
|
|
$data['position'] = $node->action('fx', ['max', 'position'])->getOne() + 1; |
|
42
|
|
|
|
|
43
|
|
|
$data['readonly'] = $data['readonly'] ?? 0; |
|
44
|
|
|
}); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public static function getId($path, $clearCache = false) |
|
48
|
|
|
{ |
|
49
|
|
|
$id = null; |
|
50
|
|
|
foreach(explode('/', trim($path,'/')) as $nodeKey) { |
|
51
|
|
|
$parentId = $id; |
|
52
|
|
|
|
|
53
|
|
|
if ($nodeKey === '') continue; //ignore empty paths |
|
54
|
|
|
|
|
55
|
|
|
if ($clearCache || empty(self::$cache['id'][$parentId][$nodeKey])) { |
|
56
|
|
|
$node = self::siblings($parentId)->addCondition('key', $nodeKey)->tryLoadAny(); |
|
57
|
|
|
|
|
58
|
|
|
if (! $node->loaded()) return false; |
|
59
|
|
|
|
|
60
|
|
|
self::$cache['id'][$parentId][$nodeKey] = $node->id; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
$id = self::$cache['id'][$parentId][$nodeKey]; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
return $id; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public static function newId($path, $readonly = false) |
|
70
|
|
|
{ |
|
71
|
|
|
if (! $path = trim($path,'/')) return false; |
|
72
|
|
|
|
|
73
|
|
|
$id = null; |
|
74
|
|
|
foreach(explode('/', $path) as $nodeKey) { |
|
75
|
|
|
if ($nodeKey === '') continue; |
|
76
|
|
|
|
|
77
|
|
|
$parentId = $id; |
|
78
|
|
|
|
|
79
|
|
|
$node = self::siblings($parentId)->addCondition('key', $nodeKey)->tryLoadAny(); |
|
80
|
|
|
|
|
81
|
|
|
if ($node->loaded()) { |
|
82
|
|
|
$id = $node->id; |
|
83
|
|
|
} |
|
84
|
|
|
else { |
|
85
|
|
|
$id = $node->insert([ |
|
86
|
|
|
'parent' => $parentId, |
|
87
|
|
|
'key' => $nodeKey, |
|
88
|
|
|
'readonly' => $readonly |
|
89
|
|
|
]); |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
return $id; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
public static function setValue($path, $value, $overwrite = true, $readonly = false) |
|
97
|
|
|
{ |
|
98
|
|
|
if (! $id = self::getId($path)) { |
|
99
|
|
|
if (! $id = self::newId($path, $readonly)) return false; |
|
100
|
|
|
} else { |
|
101
|
|
|
if (! $overwrite) return false; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
self::create()->tryLoad($id)->save(compact('value', 'readonly')); |
|
105
|
|
|
|
|
106
|
|
|
self::clearCache(); |
|
107
|
|
|
|
|
108
|
|
|
return true; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
public static function clearCache() |
|
112
|
|
|
{ |
|
113
|
|
|
self::$cache = array_fill_keys(array_keys(self::$cache), []); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
public static function getValue($path, $translate = false) |
|
117
|
|
|
{ |
|
118
|
|
|
$key = md5(serialize($path)); |
|
119
|
|
|
|
|
120
|
|
|
if (! isset(self::$cache['value'][$key])) { |
|
121
|
|
|
if(! $id = self::getId($path)) return false; |
|
122
|
|
|
|
|
123
|
|
|
self::$cache['value'][$key] = self::create()->tryLoad($id)->get('value'); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
return $translate? __(self::$cache['value'][$key]): self::$cache['value'][$key]; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Creates new array for common use. |
|
131
|
|
|
* |
|
132
|
|
|
* @param $path string |
|
133
|
|
|
* @param $array array initialization value |
|
134
|
|
|
* @param $overwrite bool whether method should overwrite if array already exists, otherwise the data will be appended |
|
135
|
|
|
* @param $readonly bool do not allow user to change this array from GUI |
|
136
|
|
|
*/ |
|
137
|
|
|
public static function newArray($path, $array, $overwrite = false, $readonly = false) |
|
138
|
|
|
{ |
|
139
|
|
|
self::validateArrayKeys($array); |
|
140
|
|
|
|
|
141
|
|
|
$path = trim($path, '/'); |
|
142
|
|
|
|
|
143
|
|
|
if ($id = self::getId($path)) { |
|
144
|
|
|
if (! $overwrite) { |
|
145
|
|
|
self::extendArray($path, $array); |
|
146
|
|
|
return true; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
self::create()->delete($id); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
if(! $id = self::newId($path, $readonly)) return false; |
|
153
|
|
|
|
|
154
|
|
|
if ($overwrite) { |
|
155
|
|
|
self::create()->tryLoad($id)->save(compact('readonly')); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
foreach ($array as $key => $value) { |
|
159
|
|
|
self::setValue($path . '/' . $key, $value, true, $readonly); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
return true; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* Extends common data array. |
|
167
|
|
|
* |
|
168
|
|
|
* @param $path string |
|
169
|
|
|
* @param $array array values to insert |
|
170
|
|
|
* @param $overwrite bool whether method should overwrite data if array key already exists, otherwise the data will be preserved |
|
171
|
|
|
*/ |
|
172
|
|
|
public static function extendArray($path, $array, $overwrite=false, $readonly=false) |
|
173
|
|
|
{ |
|
174
|
|
|
self::validateArrayKeys($array); |
|
175
|
|
|
|
|
176
|
|
|
$path = trim($path, '/'); |
|
177
|
|
|
|
|
178
|
|
|
if (! self::getId($path)){ |
|
179
|
|
|
return self::newArray($path, $array, $overwrite, $readonly); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
foreach ($array as $key => $value) { |
|
183
|
|
|
self::setValue($path . '/' . $key, $value, $overwrite, $readonly); |
|
184
|
|
|
} |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* Returns common data array. |
|
189
|
|
|
* |
|
190
|
|
|
* @param string array name |
|
191
|
|
|
* @return mixed returns an array if such array exists, false otherwise |
|
192
|
|
|
*/ |
|
193
|
|
|
public static function getArray($path, $sortColumn = 'position', $silent = false) |
|
194
|
|
|
{ |
|
195
|
|
|
return self::getCollection($path, $silent)->sortBy($sortColumn)->pluck('value', 'key')->all(); |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* Removes common data array or entry. |
|
200
|
|
|
* |
|
201
|
|
|
* @param $path string |
|
202
|
|
|
* @return true on success, false otherwise |
|
203
|
|
|
*/ |
|
204
|
|
|
public static function deleteArray($path){ |
|
205
|
|
|
if (! $id = self::getId($path, true)) return false; |
|
|
|
|
|
|
206
|
|
|
|
|
207
|
|
|
self::create()->delete($id); |
|
208
|
|
|
|
|
209
|
|
|
self::clearCache(); |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
public static function siblings($parent = null) |
|
213
|
|
|
{ |
|
214
|
|
|
$parentId = is_numeric($parent)? $parent: self::getId($parent); |
|
215
|
|
|
|
|
216
|
|
|
$model = self::create(); |
|
217
|
|
|
|
|
218
|
|
|
//@TODO: remove below when adding null condition fixed |
|
219
|
|
|
return $parentId? $model->addCondition('parent', $parentId): $model->addCondition($model->expr('parent is NULL')); |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
public static function ancestors($node) |
|
223
|
|
|
{ |
|
224
|
|
|
$node = is_numeric($node)? self::create()->tryLoad($node): $node; |
|
225
|
|
|
|
|
226
|
|
|
if (! $node['parent']) return []; |
|
227
|
|
|
|
|
228
|
|
|
return array_filter(array_merge([$node['parent']], self::ancestors($node['parent']))); |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
public static function ancestorsAndSelf($node) |
|
232
|
|
|
{ |
|
233
|
|
|
$node = is_numeric($node)? self::create()->tryLoad($node): $node; |
|
234
|
|
|
|
|
235
|
|
|
return array_filter(array_merge([$node['id']], self::ancestors($node))); |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
/** |
|
239
|
|
|
* Returns common data collection. |
|
240
|
|
|
* |
|
241
|
|
|
* @param $path string |
|
242
|
|
|
* @return Collection |
|
243
|
|
|
*/ |
|
244
|
|
|
public static function getCollection($path, $silent = false) |
|
245
|
|
|
{ |
|
246
|
|
|
if(isset(self::$cache['array'][$path])) { |
|
247
|
|
|
return self::$cache['array'][$path]; |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
if (! $id = self::getId($path)) { |
|
251
|
|
|
if ($silent) return collect(); |
|
252
|
|
|
|
|
253
|
|
|
throw new CommonDataNotFound('Invalid CommonData::getArray() request: ' . $path); |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
return self::$cache['array'][$path] = collect(self::siblings($id)->export()); |
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
|
|
protected static function validateArrayKeys($array) |
|
260
|
|
|
{ |
|
261
|
|
|
foreach($array as $key => $value) { |
|
262
|
|
|
if (strpos($key, '/') === false) continue; |
|
263
|
|
|
|
|
264
|
|
|
\Exception('Invalid common data key: '. $key); |
|
|
|
|
|
|
265
|
|
|
} |
|
266
|
|
|
} |
|
267
|
|
|
} |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths