-
Notifications
You must be signed in to change notification settings - Fork 243
Expand file tree
/
Copy pathProjectAssetTrait.php
More file actions
211 lines (178 loc) · 6.39 KB
/
ProjectAssetTrait.php
File metadata and controls
211 lines (178 loc) · 6.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
<?php
namespace ProcessMaker\Traits;
use Exception;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use ProcessMaker\Exception\ProjectAssetSyncException;
use ProcessMaker\Models\Group;
use ProcessMaker\Models\User;
trait ProjectAssetTrait
{
const PROJECT_MODEL_CLASS = 'ProcessMaker\Package\Projects\Models\Project';
const PROJECT_ASSET_MODEL_CLASS = 'ProcessMaker\Package\Projects\Models\ProjectAsset';
public function syncProjectAsset($requestOrInteger, $assetModelClass)
{
if (!class_exists(self::PROJECT_ASSET_MODEL_CLASS)) {
return;
}
$projectIds = $this->extractProjectIds($requestOrInteger);
try {
// Sync the project assets with the prepared project IDs
$this->projectAssets()->syncWithPivotValues($projectIds, ['asset_type' => $assetModelClass]);
$this->updateProjectUpdatedAt($projectIds);
} catch (Exception $e) {
throw new ProjectAssetSyncException('Error syncing project assets: ' . $e->getMessage());
}
}
/**
* Extract project IDs from the provided input.
*
* @param mixed $input
* @return array
*/
private function extractProjectIds($input)
{
if ($input instanceof Request && $input->has('projects')) {
$projectIds = $input->input('projects', '');
} elseif (is_int($input)) {
$projectIds = $input;
} elseif (is_string($input)) {
$projectIds = $input;
} else {
return [];
}
if (is_string($projectIds)) {
$projectIds = $this->convertStringToArray($projectIds);
}
return $projectIds;
}
/**
* Convert a string to an array, handling JSON decoding.
*
* @param string $input
* @return array
*/
private function convertStringToArray($input)
{
$decodedProjects = json_decode($input, true);
if (is_array($decodedProjects)) {
return array_column($decodedProjects, 'id');
}
return array_map('intval', array_filter(explode(',', trim($input, ','))));
}
public function getProjectsAttribute()
{
if (class_exists(self::PROJECT_ASSET_MODEL_CLASS)) {
$projectAssets = self::PROJECT_ASSET_MODEL_CLASS::where('asset_id', $this->id)
->where('asset_type', get_class($this))
->get();
return json_encode($projectAssets->map(function ($projectAsset) {
return $projectAsset->project;
}));
}
return json_encode([]);
}
/**
* Update the 'updated_at' field of projects.
*
* @param array|int $projectIds
* @return void
*/
public function updateProjectUpdatedAt($projectIds)
{
if (!class_exists(self::PROJECT_MODEL_CLASS)) {
return;
}
foreach ((array) $projectIds as $projectId) {
$project = self::PROJECT_MODEL_CLASS::find($projectId);
if ($project) {
$project->touch();
}
}
}
private static function clearAndRebuildUserProjectAssetsCache()
{
if (!hasPackage('package-projects')) {
return;
}
if (Auth::user()) {
$userId = Auth::user()->id;
Cache::forget("user_{$userId}_project_assets");
Cache::remember("user_{$userId}_project_assets", 86400, function () use ($userId) {
return self::getUserProjectsAssets($userId);
});
}
}
private static function getProjectAssetsForUser($userId)
{
if (!hasPackage('package-projects')) {
return [];
}
return Cache::remember("user_{$userId}_project_assets", 86400, function () use ($userId) {
return self::getUserProjectsAssets($userId);
});
}
private static function getUserProjectsAssets($userId)
{
$userProjectsWithAssets = self::fetchUserProjectAssets($userId);
return self::formatProjectAssetsArray($userProjectsWithAssets);
}
/**
* Fetch projects with assets for the given user.
*/
private static function fetchUserProjectAssets($userId)
{
// Fetch projects where the user is the owner
$ownerProjects = DB::table('projects')
->where('user_id', $userId)
->pluck('id')
->toArray();
// Fetch projects where the user is a member (User::class) or belongs to a group (Group::class)
$memberProjects = DB::table('project_members')
->where(function ($query) use ($userId) {
$query->where('member_id', $userId)
->where('member_type', User::class);
})
->orWhere(function ($query) use ($userId) {
$query->where('member_type', Group::class)
->whereIn('member_id', function ($subQuery) use ($userId) {
$subQuery->select('group_id')
->from('group_members')
->where('member_id', $userId);
});
})
->pluck('project_id')
->toArray();
// Combine both sets of project IDs and remove duplicates
$projectIds = array_unique(array_merge($ownerProjects, $memberProjects));
// Fetch project assets for the combined project IDs
return DB::table('projects')
->join('project_assets', 'projects.id', '=', 'project_assets.project_id')
->whereIn('projects.id', $projectIds)
->select('project_assets.asset_id as asset_id', 'project_assets.asset_type')
->get()
->unique()
->toArray();
}
/**
* Format projects with assets into the desired structure.
*/
private static function formatProjectAssetsArray($projectsWithAssets)
{
$formattedArray = [];
foreach ($projectsWithAssets as $project) {
$assetType = $project->asset_type;
$assetId = $project->asset_id;
if (!isset($formattedArray[$assetType])) {
$formattedArray[$assetType] = [];
}
if (!isset($formattedArray[$assetType])) {
$formattedArray[$assetType] = [];
}
$formattedArray[$assetType][] = $assetId;
}
return $formattedArray;
}
}