-
Notifications
You must be signed in to change notification settings - Fork 247
Expand file tree
/
Copy pathBundleAsset.php
More file actions
192 lines (156 loc) · 5.34 KB
/
Copy pathBundleAsset.php
File metadata and controls
192 lines (156 loc) · 5.34 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
<?php
namespace ProcessMaker\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Support\Str;
use ProcessMaker\Enums\ExporterMap;
class BundleAsset extends ProcessMakerModel
{
use HasFactory;
public const INTEGRITY_VALID = 'valid';
public const INTEGRITY_MISSING = 'missing';
public const INTEGRITY_TYPE_UNAVAILABLE = 'type_unavailable';
protected $guarded = ['id'];
protected $appends = ['name', 'url', 'type', 'owner_name', 'categories', 'integrity_status'];
const DATA_SOURCE_CLASS = 'ProcessMaker\Packages\Connectors\DataSources\Models\DataSource';
const COLLECTION_CLASS = 'ProcessMaker\Plugins\Collections\Models\Collection';
const DECISION_TABLE_CLASS = 'ProcessMaker\Package\PackageDecisionEngine\Models\DecisionTable';
const FLOW_GENIE_CLASS = 'ProcessMaker\Package\PackageAi\Models\FlowGenie';
const PM_BLOCK_CLASS = 'ProcessMaker\Package\PackagePmBlocks\Models\PmBlock';
public static function canExport(?ProcessMakerModel $asset)
{
return $asset !== null
&& method_exists($asset, 'export')
&& ExporterMap::getExporterClassForModel($asset);
}
public function bundle()
{
return $this->belongsTo(Bundle::class);
}
public function asset()
{
return $this->morphTo();
}
public function getKeyAttribute()
{
return $this->asset_type . '-' . $this->asset_id;
}
public static function makeKey(ProcessMakerModel $asset)
{
return $asset::class . '-' . $asset->id;
}
public function getNameAttribute()
{
$asset = $this->resolvedAsset();
if ($asset === null) {
return __('Missing :type #:id', [
'type' => $this->typeLabel(),
'id' => $this->asset_id,
]);
}
if (
$this->asset_type === Screen::class ||
$this->asset_type === Script::class
) {
return $asset->title;
}
return $asset->name;
}
public function getUrlAttribute()
{
if ($this->integrity_status !== self::INTEGRITY_VALID) {
return null;
}
switch($this->asset_type) {
case Screen::class:
return "/designer/screen-builder/{$this->asset_id}/edit";
case Script::class:
return "/designer/scripts/{$this->asset_id}/builder";
case Process::class:
return "/modeler/{$this->asset_id}";
case self::DATA_SOURCE_CLASS:
return "/designer/data-sources/{$this->asset_id}/edit";
case self::COLLECTION_CLASS:
return "/collections/{$this->asset_id}/edit";
case self::DECISION_TABLE_CLASS:
return "/designer/decision-tables/table-builder/{$this->asset_id}/edit";
case self::FLOW_GENIE_CLASS:
return "/designer/flow-genies/{$this->asset_id}/edit";
case self::PM_BLOCK_CLASS:
return "/designer/pm-blocks/{$this->asset_id}/edit";
default:
return null;
}
}
public function getTypeAttribute()
{
switch($this->asset_type) {
case Screen::class:
return 'Screen';
case Script::class:
return 'Script';
case Process::class:
return 'Process';
case self::DATA_SOURCE_CLASS:
return 'data_source';
case self::COLLECTION_CLASS:
return 'collection';
case self::DECISION_TABLE_CLASS:
return 'decision_table';
case self::FLOW_GENIE_CLASS:
return 'flow_genie';
case self::PM_BLOCK_CLASS:
return 'pm_block';
default:
return null;
}
}
public function getOwnerNameAttribute()
{
$asset = $this->resolvedAsset();
if ($asset && method_exists($asset, 'user') && $asset->user) {
return $asset->user->firstname . ' ' . $asset->user->lastname;
}
return null;
}
public function getCategoriesAttribute()
{
if ($this->asset_type === self::COLLECTION_CLASS) {
return [];
}
$asset = $this->resolvedAsset();
if ($asset && method_exists($asset, 'categories')) {
return $asset->categories->pluck('name')->toArray();
}
return [];
}
public function getIntegrityStatusAttribute(): string
{
if (!class_exists($this->asset_type)) {
return self::INTEGRITY_TYPE_UNAVAILABLE;
}
return $this->resolvedAsset() === null
? self::INTEGRITY_MISSING
: self::INTEGRITY_VALID;
}
public function integrityDetails(): array
{
return [
'bundle_asset_id' => $this->id,
'asset_type' => $this->asset_type,
'asset_id' => $this->asset_id,
'integrity_status' => $this->integrity_status,
];
}
private function resolvedAsset(): ?ProcessMakerModel
{
if (!class_exists($this->asset_type)) {
return null;
}
return $this->asset;
}
private function typeLabel(): string
{
$type = $this->type ?? class_basename($this->asset_type);
return Str::headline($type);
}
}