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
|
|
|
]); |
39
|
|
|
} |
40
|
|
|
}); |
41
|
|
|
|
42
|
|
|
// Delete uuid after forced delete |
43
|
|
|
static::deleted(function ($model) { |
44
|
|
|
if (!empty($model->uuid) && (!method_exists($model, 'isForceDeleting') || $model->isForceDeleting() === true)) { |
45
|
|
|
$entity = Entity::find($model->uuid); |
46
|
|
|
if ($entity) { |
47
|
|
|
$entity->delete(); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
}); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function initializeUccelloModule() |
54
|
|
|
{ |
55
|
|
|
$this->appends = array_merge($this->appends, ['recordLabel','uuid']); |
|
|
|
|
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
protected static function isFilteredByUser() |
59
|
|
|
{ |
60
|
|
|
$isFilteredByUser = false; |
61
|
|
|
|
62
|
|
|
$user = Auth::user(); |
63
|
|
|
|
64
|
|
|
if ($user && !$user->is_admin) { |
|
|
|
|
65
|
|
|
$module = static::getModuleFromClass(static::class); |
66
|
|
|
|
67
|
|
|
if ($module && $module->data && property_exists($module->data, 'private') && $module->data->private) { |
68
|
|
|
$isFilteredByUser = true; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return $isFilteredByUser; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function getTableAttribute() |
76
|
|
|
{ |
77
|
|
|
return $this->table; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function getModuleAttribute() |
81
|
|
|
{ |
82
|
|
|
return static::getModuleFromClass(get_class($this)); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
protected static function getModuleFromClass($className) |
86
|
|
|
{ |
87
|
|
|
$modules = Cache::rememberForever('modules_by_model_class', function () { |
88
|
|
|
$modulesGroupedByModelClass = collect(); |
89
|
|
|
Module::all()->map(function ($item) use ($modulesGroupedByModelClass) { |
90
|
|
|
$modulesGroupedByModelClass[$item->model_class] = $item; |
91
|
|
|
return $modulesGroupedByModelClass; |
92
|
|
|
}); |
93
|
|
|
return $modulesGroupedByModelClass; |
94
|
|
|
}); |
95
|
|
|
return $modules[(string) $className] ?? null; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function getUuidAttribute() |
99
|
|
|
{ |
100
|
|
|
$uuid = null; |
101
|
|
|
|
102
|
|
|
$entity = Entity::where('module_id', $this->module->getKey()) |
103
|
|
|
->where('record_id', $this->getKey()) |
|
|
|
|
104
|
|
|
->first(); |
105
|
|
|
|
106
|
|
|
if ($entity) { |
107
|
|
|
$uuid = $entity->getKey(); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return $uuid; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Returns Assigned User |
115
|
|
|
* |
116
|
|
|
* @return string|null |
117
|
|
|
*/ |
118
|
|
|
public function getAssignedUserAttribute(): ?string |
119
|
|
|
{ |
120
|
|
|
return $this->assigned_user_id; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function scopeInDomain($query, ?Domain $domain, $withDescendants = false) |
124
|
|
|
{ |
125
|
|
|
if (!empty($domain) && Schema::hasColumn($this->table, 'domain_id')) { |
126
|
|
|
// Activate descendant view if the user is allowed |
127
|
|
|
if (Auth::user()->canSeeDescendantsRecords($domain) && $withDescendants) { |
|
|
|
|
128
|
|
|
$domainsIds = $domain->findDescendants()->pluck('id'); |
129
|
|
|
$query = $query->whereIn('domain_id', $domainsIds); |
130
|
|
|
} else { |
131
|
|
|
$query = $query->where('domain_id', $domain->id); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
return $query; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Returns Assigned User |
140
|
|
|
* @param QueryBuilder $query |
|
|
|
|
141
|
|
|
* @param Field|int|array $filter |
|
|
|
|
142
|
|
|
* @return QueryBuilder|null |
143
|
|
|
*/ |
144
|
|
|
public function scopeFilterBy($query, $filter) |
145
|
|
|
{ |
146
|
|
|
$filterModel = null; |
147
|
|
|
|
148
|
|
|
if (!empty($filter)) { |
149
|
|
|
// $filter: int id |
150
|
|
|
if (is_numeric($filter)) { |
151
|
|
|
// TODO: Check permissions ?? (domain, user) |
152
|
|
|
$filterModel = Filter::where('id', $filter) |
153
|
|
|
->where('module_id', $this->module->getKey()) |
154
|
|
|
->first(); |
155
|
|
|
} |
156
|
|
|
// $filter: array data |
157
|
|
|
elseif (is_array($filter)) { |
158
|
|
|
$filterModel = Filter::newFromData($filter); |
159
|
|
|
} |
160
|
|
|
// $filter: Filter model |
161
|
|
|
elseif (substr(strrchr(get_class($filter), "\\"), 1) == 'Filter') { |
162
|
|
|
$filterModel = $filter; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
if ($filterModel) { |
166
|
|
|
// Conditions |
167
|
|
|
if (!empty($filterModel->conditions)) { |
168
|
|
|
// Search |
169
|
|
|
if (!empty($filterModel->conditions->search)) { |
|
|
|
|
170
|
|
|
foreach ($filterModel->conditions->search as $fieldName => $searchValue) { |
171
|
|
|
// Get field by name and search by field column |
172
|
|
|
$field = $this->module->getField($fieldName); |
173
|
|
|
if (isset($searchValue) && !is_null($field)) { |
174
|
|
|
$uitype = uitype($field->uitype_id); |
175
|
|
|
$query = $uitype->addConditionToSearchQuery($query, $field, $searchValue); |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
// Order results |
182
|
|
|
if (!empty($filterModel->order)) { |
183
|
|
|
foreach ($filterModel->order as $fieldName => $value) { |
|
|
|
|
184
|
|
|
// Get field by name and search by field column |
185
|
|
|
$field = $this->module->getField($fieldName); |
186
|
|
|
if (!is_null($field)) { |
187
|
|
|
$query = $query->orderBy($field->column, $value); |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
return $query; |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
|