1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Uccello\Core\Support\Traits; |
4
|
|
|
|
5
|
|
|
use Uccello\Core\Models\Entity; |
6
|
|
|
use Uccello\Core\Models\Module; |
7
|
|
|
use Uccello\Core\Models\Filter; |
8
|
|
|
use Illuminate\Support\Facades\Cache; |
9
|
|
|
use Uccello\Core\Support\Scopes; |
10
|
|
|
use Illuminate\Support\Facades\Auth; |
11
|
|
|
use Illuminate\Support\Facades\Schema; |
12
|
|
|
use Illuminate\Support\Str; |
13
|
|
|
use Uccello\Core\Models\Domain; |
14
|
|
|
|
15
|
|
|
trait UccelloModule |
16
|
|
|
{ |
17
|
|
|
use RelatedlistTrait; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* The "booting" method of the trait. |
21
|
|
|
* |
22
|
|
|
* @return void |
23
|
|
|
*/ |
24
|
|
|
protected static function bootUccelloModule() |
25
|
|
|
{ |
26
|
|
|
if (static::isFilteredByUser()) { |
27
|
|
|
static::addGlobalScope(new Scopes\AssignedUser); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
// Create uuid after save |
31
|
|
|
static::created(function ($model) { |
32
|
|
|
$module = Module::where('model_class', get_class($model))->first(); |
33
|
|
|
if ($module) { |
34
|
|
|
Entity::create([ |
35
|
|
|
'id' => (string) Str::uuid(), |
36
|
|
|
'module_id' => $module->id, |
37
|
|
|
'record_id' => $model->getKey(), |
38
|
|
|
'creator_id' => auth()->id(), |
39
|
|
|
]); |
40
|
|
|
} |
41
|
|
|
}); |
42
|
|
|
|
43
|
|
|
// Delete uuid after forced delete |
44
|
|
|
static::deleted(function ($model) { |
45
|
|
|
if (!empty($model->uuid) && (!method_exists($model, 'isForceDeleting') || $model->isForceDeleting() === true)) { |
46
|
|
|
$entity = Entity::find($model->uuid); |
47
|
|
|
if ($entity) { |
48
|
|
|
$entity->delete(); |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
}); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function initializeUccelloModule() |
55
|
|
|
{ |
56
|
|
|
$this->appends = array_merge($this->appends, ['recordLabel','uuid']); |
|
|
|
|
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
protected static function isFilteredByUser() |
60
|
|
|
{ |
61
|
|
|
$isFilteredByUser = false; |
62
|
|
|
|
63
|
|
|
$user = Auth::user(); |
64
|
|
|
|
65
|
|
|
if ($user && !$user->is_admin) { |
|
|
|
|
66
|
|
|
$module = static::getModuleFromClass(static::class); |
67
|
|
|
|
68
|
|
|
if ($module && $module->data && property_exists($module->data, 'private') && $module->data->private) { |
69
|
|
|
$isFilteredByUser = true; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return $isFilteredByUser; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function getTableAttribute() |
77
|
|
|
{ |
78
|
|
|
return $this->table; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function getModuleAttribute() |
82
|
|
|
{ |
83
|
|
|
return static::getModuleFromClass(get_class($this)); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
protected static function getModuleFromClass($className) |
87
|
|
|
{ |
88
|
|
|
$modules = Cache::rememberForever('modules_by_model_class', function () { |
89
|
|
|
$modulesGroupedByModelClass = collect(); |
90
|
|
|
Module::all()->map(function ($item) use ($modulesGroupedByModelClass) { |
91
|
|
|
$modulesGroupedByModelClass[$item->model_class] = $item; |
92
|
|
|
return $modulesGroupedByModelClass; |
93
|
|
|
}); |
94
|
|
|
return $modulesGroupedByModelClass; |
95
|
|
|
}); |
96
|
|
|
return $modules[(string) $className] ?? null; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function getUuidAttribute() |
100
|
|
|
{ |
101
|
|
|
$uuid = null; |
102
|
|
|
|
103
|
|
|
$module = $this->module; |
104
|
|
|
|
105
|
|
|
if ($module) { |
106
|
|
|
return Cache::rememberForever('uuid_'.$module->getKey().'_'.$this->getKey(), function () use ($module) { |
|
|
|
|
107
|
|
|
$entity = Entity::where('module_id', $module->getKey()) |
108
|
|
|
->where('record_id', $this->getKey()) |
109
|
|
|
->first(); |
110
|
|
|
|
111
|
|
|
$uuid = null; |
112
|
|
|
if ($entity) { |
113
|
|
|
$uuid = $entity->getKey(); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return $uuid; |
117
|
|
|
}); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return $uuid; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Returns Assigned User |
125
|
|
|
* |
126
|
|
|
* @return string|null |
127
|
|
|
*/ |
128
|
|
|
public function getAssignedUserAttribute(): ?string |
129
|
|
|
{ |
130
|
|
|
return $this->assigned_user_id; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Returns user who created the entity, if defined. |
135
|
|
|
* |
136
|
|
|
* @return \Uccello\Core\Models\User|null |
137
|
|
|
*/ |
138
|
|
|
public function getCreatorAttribute() |
139
|
|
|
{ |
140
|
|
|
$creator = null; |
141
|
|
|
|
142
|
|
|
$module = $this->module; |
143
|
|
|
|
144
|
|
|
if ($module) { |
145
|
|
|
$entity = Entity::where('module_id', $module->getKey()) |
146
|
|
|
->where('record_id', $this->getKey()) |
147
|
|
|
->first(); |
148
|
|
|
|
149
|
|
|
if ($entity) { |
150
|
|
|
$creator = $entity->creator; |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
return $creator; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
public function scopeInDomain($query, ?Domain $domain, $withDescendants = false) |
158
|
|
|
{ |
159
|
|
|
if (!empty($domain) && Schema::hasColumn($this->table, 'domain_id')) { |
160
|
|
|
// Activate descendant view if the user is allowed |
161
|
|
|
if (Auth::user() && Auth::user()->canSeeDescendantsRecords($domain) && $withDescendants) { |
|
|
|
|
162
|
|
|
$query->whereIn('domain_id', function ($query) use ($domain) { |
163
|
|
|
$query->select('id') |
164
|
|
|
->from((new Domain)->getTable()) |
165
|
|
|
->where('path', 'like', $domain->id.'/%') |
166
|
|
|
->orWhere('path', 'like', '%/'.$domain->id.'/%') |
167
|
|
|
->get(); |
168
|
|
|
}); |
169
|
|
|
} else { |
170
|
|
|
$query->where('domain_id', $domain->id); |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
return $query; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Add filter conditions to a query. |
179
|
|
|
* @param QueryBuilder $query |
|
|
|
|
180
|
|
|
* @param Filter|int|array $filter |
181
|
|
|
* @return QueryBuilder|null |
182
|
|
|
*/ |
183
|
|
|
public function scopeFilterBy($query, $filter) |
184
|
|
|
{ |
185
|
|
|
$filterModel = null; |
186
|
|
|
|
187
|
|
|
if (!empty($filter)) { |
188
|
|
|
// $filter: int id |
189
|
|
|
if (is_numeric($filter)) { |
190
|
|
|
// TODO: Check permissions ?? (domain, user) |
191
|
|
|
$filterModel = Filter::where('id', $filter) |
192
|
|
|
->where('module_id', $this->module->getKey()) |
193
|
|
|
->first(); |
194
|
|
|
} |
195
|
|
|
// $filter: array data |
196
|
|
|
elseif (is_array($filter)) { |
197
|
|
|
$filterModel = Filter::newFromData($filter); |
198
|
|
|
} |
199
|
|
|
// $filter: Filter model |
200
|
|
|
elseif (substr(strrchr(get_class($filter), "\\"), 1) == 'Filter') { |
201
|
|
|
$filterModel = $filter; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
if ($filterModel) { |
205
|
|
|
// Conditions |
206
|
|
|
if (!empty($filterModel->conditions)) { |
207
|
|
|
// Search |
208
|
|
|
if (!empty($filterModel->conditions->search)) { |
|
|
|
|
209
|
|
|
foreach ($filterModel->conditions->search as $fieldName => $searchValue) { |
210
|
|
|
// Get field by name and search by field column |
211
|
|
|
$field = $this->module->getField($fieldName); |
212
|
|
|
if (isset($searchValue) && !is_null($field)) { |
213
|
|
|
$uitype = uitype($field->uitype_id); |
214
|
|
|
$query = $uitype->addConditionToSearchQuery($query, $field, $searchValue); |
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
// Order results |
221
|
|
|
if (!empty($filterModel->order)) { |
222
|
|
|
foreach ($filterModel->order as $fieldName => $value) { |
|
|
|
|
223
|
|
|
// Get field by name and search by field column |
224
|
|
|
$field = $this->module->getField($fieldName); |
225
|
|
|
if (!is_null($field)) { |
226
|
|
|
$query = $query->orderBy($field->column, $value); |
227
|
|
|
} |
228
|
|
|
} |
229
|
|
|
} |
230
|
|
|
} |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
return $query; |
234
|
|
|
} |
235
|
|
|
} |
236
|
|
|
|