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 Uccello\Core\Models\Domain; |
13
|
|
|
|
14
|
|
|
trait UccelloModule |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* The "booting" method of the model. |
18
|
|
|
* |
19
|
|
|
* @return void |
20
|
|
|
*/ |
21
|
|
|
protected static function boot() |
22
|
|
|
{ |
23
|
|
|
parent::boot(); |
24
|
|
|
|
25
|
|
|
if (static::isFilteredByUser()) { |
26
|
|
|
static::addGlobalScope(new Scopes\AssignedUser); |
27
|
|
|
} |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
protected static function isFilteredByUser() |
31
|
|
|
{ |
32
|
|
|
$isFilteredByUser = false; |
33
|
|
|
|
34
|
|
|
$user = Auth::user(); |
35
|
|
|
|
36
|
|
|
if ($user && !$user->is_admin) { |
|
|
|
|
37
|
|
|
$module = static::getModuleFromClass(static::class); |
38
|
|
|
|
39
|
|
|
if ($module && $module->data && property_exists($module->data, 'private') && $module->data->private) { |
40
|
|
|
$isFilteredByUser = true; |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
return $isFilteredByUser; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function getTableAttribute() |
48
|
|
|
{ |
49
|
|
|
return $this->table; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function getModuleAttribute() |
53
|
|
|
{ |
54
|
|
|
return static::getModuleFromClass(get_class($this)); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
protected static function getModuleFromClass($className) |
58
|
|
|
{ |
59
|
|
|
$modules = Cache::rememberForever('modules_by_model_class', function () { |
60
|
|
|
$modulesGroupedByModelClass = collect(); |
61
|
|
|
Module::all()->map(function ($item) use ($modulesGroupedByModelClass) { |
62
|
|
|
$modulesGroupedByModelClass[$item->model_class] = $item; |
63
|
|
|
return $modulesGroupedByModelClass; |
64
|
|
|
}); |
65
|
|
|
return $modulesGroupedByModelClass; |
66
|
|
|
}); |
67
|
|
|
return $modules[(string) $className] ?? null; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function getUuidAttribute() |
71
|
|
|
{ |
72
|
|
|
$uuid = null; |
73
|
|
|
|
74
|
|
|
$entity = Entity::where('module_id', $this->module->getKey()) |
75
|
|
|
->where('record_id', $this->getKey()) |
|
|
|
|
76
|
|
|
->first(); |
77
|
|
|
|
78
|
|
|
if ($entity) { |
79
|
|
|
$uuid = $entity->getKey(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $uuid; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Returns Assigned User |
87
|
|
|
* |
88
|
|
|
* @return string|null |
89
|
|
|
*/ |
90
|
|
|
public function getAssignedUserAttribute(): ?string |
91
|
|
|
{ |
92
|
|
|
return $this->assigned_user_id; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function scopeInDomain($query, ?Domain $domain, $withDescendants = false) |
96
|
|
|
{ |
97
|
|
|
if (!empty($domain) && Schema::hasColumn($this->table, 'domain_id')) { |
98
|
|
|
// Activate descendant view if the user is allowed |
99
|
|
|
if (Auth::user()->canSeeDescendantsRecords($domain) && $withDescendants) { |
|
|
|
|
100
|
|
|
$domainsIds = $domain->findDescendants()->pluck('id'); |
101
|
|
|
$query = $query->whereIn('domain_id', $domainsIds); |
102
|
|
|
} else { |
103
|
|
|
$query = $query->where('domain_id', $domain->id); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return $query; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Returns Assigned User |
112
|
|
|
* @param QueryBuilder $query |
|
|
|
|
113
|
|
|
* @param Field|int|array $filter |
|
|
|
|
114
|
|
|
* @return QueryBuilder|null |
115
|
|
|
*/ |
116
|
|
|
public function scopeFilterBy($query, $filter) |
117
|
|
|
{ |
118
|
|
|
$filterModel = null; |
119
|
|
|
|
120
|
|
|
if (!empty($filter)) { |
121
|
|
|
// $filter: int id |
122
|
|
|
if (is_numeric($filter)) { |
123
|
|
|
// TODO: Check permissions ?? (domain, user) |
124
|
|
|
$filterModel = Filter::where('id', $filter) |
125
|
|
|
->where('module_id', $this->module->getKey()) |
126
|
|
|
->first(); |
127
|
|
|
} |
128
|
|
|
// $filter: array data |
129
|
|
|
elseif (is_array($filter)) { |
130
|
|
|
$filterModel = Filter::newFromData($filter); |
131
|
|
|
} |
132
|
|
|
// $filter: Filter model |
133
|
|
|
elseif (substr(strrchr(get_class($filter), "\\"), 1) == 'Filter') { |
134
|
|
|
$filterModel = $filter; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
if ($filterModel) { |
138
|
|
|
// Conditions |
139
|
|
|
if (!empty($filterModel->conditions)) { |
140
|
|
|
// Search |
141
|
|
|
if (!empty($filterModel->conditions->search)) { |
|
|
|
|
142
|
|
|
foreach ($filterModel->conditions->search as $fieldName => $searchValue) { |
143
|
|
|
// Get field by name and search by field column |
144
|
|
|
$field = $this->module->getField($fieldName); |
145
|
|
|
if (isset($searchValue) && !is_null($field)) { |
146
|
|
|
$uitype = uitype($field->uitype_id); |
147
|
|
|
$query = $uitype->addConditionToSearchQuery($query, $field, $searchValue); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
// Order results |
154
|
|
|
if (!empty($filterModel->order)) { |
155
|
|
|
foreach ($filterModel->order as $fieldName => $value) { |
|
|
|
|
156
|
|
|
// Get field by name and search by field column |
157
|
|
|
$field = $this->module->getField($fieldName); |
158
|
|
|
if (!is_null($field)) { |
159
|
|
|
$query = $query->orderBy($field->column, $value); |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
return $query; |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|