forked from ProcessMaker/processmaker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScreen.php
More file actions
378 lines (332 loc) · 10.5 KB
/
Screen.php
File metadata and controls
378 lines (332 loc) · 10.5 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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
<?php
namespace ProcessMaker\Models;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\Validation\Rule;
use ProcessMaker\Assets\ScreensInScreen;
use ProcessMaker\Contracts\PrometheusMetricInterface;
use ProcessMaker\Contracts\ScreenInterface;
use ProcessMaker\Events\TranslationChanged;
use ProcessMaker\Traits\Exportable;
use ProcessMaker\Traits\ExtendedPMQL;
use ProcessMaker\Traits\HasCategories;
use ProcessMaker\Traits\HasScreenFields;
use ProcessMaker\Traits\HasVersioning;
use ProcessMaker\Traits\HideSystemResources;
use ProcessMaker\Traits\ProjectAssetTrait;
use ProcessMaker\Traits\SerializeToIso8601;
use ProcessMaker\Validation\CategoryRule;
/**
* Class Screen
*
*
* @property string id
* @property string title
* @property string description
* @property array content
* @property array config
* @property array computed
* @property array custom_css
* @property array watchers
* @property string label
* @property Carbon type
* @property Carbon $updated_at
* @property Carbon $created_at
*
* @OA\Schema(
* schema="screensEditable",
* @OA\Property(property="title", type="string"),
* @OA\Property(property="type", type="string"),
* @OA\Property(property="description", type="string"),
* @OA\Property(property="config", type="array", @OA\Items(type="object")),
* @OA\Property(property="computed", type="array", @OA\Items(type="object")),
* @OA\Property(property="watchers", type="array", @OA\Items(type="object")),
* @OA\Property(property="custom_css", type="string"),
* @OA\Property(property="screen_category_id", type="string"),
* ),
* @OA\Schema(
* schema="screens",
* allOf={
* @OA\Schema(ref="#/components/schemas/screensEditable"),
* @OA\Schema(
* @OA\Property(property="id", type="string", format="id"),
* @OA\Property(property="created_at", type="string", format="date-time"),
* @OA\Property(property="updated_at", type="string", format="date-time"),
* ),
* },
* )
*
* @OA\Schema(
* schema="screenExported",
* @OA\Property(property="url", type="string"),
* )
*/
class Screen extends ProcessMakerModel implements ScreenInterface, PrometheusMetricInterface
{
use SerializeToIso8601;
use HideSystemResources;
use HasCategories;
use HasScreenFields;
use HasVersioning;
use ExtendedPMQL;
use Exportable;
use ProjectAssetTrait;
const categoryClass = ScreenCategory::class;
protected $connection = 'processmaker';
/**
* The table name attribute
* @var string
*/
protected $table = 'screens';
protected $casts = [
'config' => 'array',
'computed' => 'array',
'watchers' => 'array',
'translations' => 'array',
];
protected $appends = [
'projects',
];
/**
* The attributes that aren't mass assignable.
*
* @var array
*/
protected $guarded = [
'id',
'created_at',
'updated_at',
];
public static function boot()
{
parent::boot();
$clearCacheCallback = function () {
self::clearAndRebuildUserProjectAssetsCache();
};
static::updating($clearCacheCallback);
static::deleting($clearCacheCallback);
}
/**
* Validation rules
*
* @param $existing
*
* @return array
*/
public static function rules($existing = null)
{
$unique = Rule::unique('screens')->ignore($existing);
return [
'title' => ['required', $unique, 'alpha_spaces'],
'description' => 'required',
'type' => 'required',
'screen_category_id' => [new CategoryRule($existing)],
];
}
/**
* Get the associated versions
*/
public function versions()
{
return $this->hasMany(ScreenVersion::class);
}
/**
* Get the associated category
*/
public function category()
{
return $this->belongsTo(ScreenCategory::class, 'screen_category_id');
}
/**
* Get the associated projects
*/
public function projects()
{
return $this->belongsToMany('ProcessMaker\Package\Projects\Models\Project',
'project_assets',
'asset_id',
'project_id',
'id',
'id'
)->wherePivot('asset_type', static::class);
}
// Define the relationship with the ProjectAsset model
public function projectAssets()
{
return $this->belongsToMany('ProcessMaker\Package\Projects\Models\ProjectAsset',
'project_assets', 'asset_id', 'project_id')
->withPivot('asset_type')
->wherePivot('asset_type', static::class)->withTimestamps();
}
/**
* Set multiple|single categories to the screen
*
* @param string $value
*/
public function setScreenCategoryIdAttribute($value)
{
return $this->setMultipleCategories($value, 'screen_category_id');
}
/**
* Get multiple|single categories of the screen
*
* @param string $value
*/
public function getScreenCategoryIdAttribute($value)
{
return implode(',', $this->categories()->pluck('category_id')->toArray()) ?: $value;
}
public function builderComponent()
{
if (isset($this->config['builderComponent'])) {
return $this->config['builderComponent'];
}
return 'ScreenBuilder';
}
public function renderComponent()
{
if (isset($this->config['renderComponent'])) {
return $this->config['renderComponent'];
}
return 'task-screen';
}
/**
* PMQL field (id = screens.id)
*
* @return string
*/
public function fieldAliasId()
{
return 'screens.id';
}
/**
* Get a recursive list of nested screens IDs in this screen
*
* @return int[] nested screen IDs
*/
public function nestedScreenIds(ProcessRequest $processRequest = null)
{
$screenIds = [];
$screenFinder = new ScreensInScreen();
$screenFinder->setProcessRequest($processRequest);
foreach ($screenFinder->referencesToExport($this) as $screen) {
$screenIds[] = $screen[1];
}
return $screenIds;
}
/**
* PMQL value alias for fulltext field
*
* @param string $value
*
* @return callable
*/
public function valueAliasFullText($value, $expression)
{
return function ($query) use ($value) {
$this->scopeFilter($query, $value);
};
}
/**
* Filter settings with a string
*
* @param $query
*
* @param $filter string
*/
public function scopeFilter($query, $filterStr)
{
$filter = '%' . mb_strtolower($filterStr) . '%';
$query->where(function ($query) use ($filter, $filterStr) {
$query->where('screens.title', 'like', $filter)
->orWhere('screens.description', 'like', $filter)
->orWhere('screens.status', '=', $filterStr)
->orWhereIn('screens.id', function ($qry) use ($filter) {
$qry->select('assignable_id')
->from('category_assignments')
->leftJoin('screen_categories', function ($join) {
$join->on('screen_categories.id', '=', 'category_assignments.category_id');
$join->where('category_assignments.category_type', '=', ScreenCategory::class);
$join->where('category_assignments.assignable_type', '=', self::class);
})
->where('screen_categories.name', 'like', $filter);
});
});
return $query;
}
/**
* Return the label to be used in grafana reports
*
* @return string
*/
public function getPrometheusMetricLabel(): string
{
return 'screen.' . $this->id;
}
public static function getScreenByKey(string $key) : ?self
{
$screen = self::firstWhere('key', $key);
if (!$screen) {
$screen = self::createScreenByKey($key);
}
return $screen;
}
public static function getScreenByKeyPerDefault(string $key) : ?self
{
$screen = self::firstWhere('key', $key);
if (!$screen) {
$screen = self::createScreenByKey($key, false, null, 1);
} else {
$screen = self::createScreenByKey($key, false, null, 1, 'key');
}
return $screen;
}
private static function createScreenByKey(string $key, bool $isSystem = true, string $path = null, $isDefault = 0, $columnKey = null): self
{
// If no path is provided, use the default path
if (!$path) {
$path = database_path('processes/screens/' . "{$key}.json");
}
// Check if file exists
if (!file_exists($path)) {
throw new \RuntimeException("Screen configuration file not found: {$path}");
}
// Read and decode JSON with validation
$jsonContent = file_get_contents($path);
$json = json_decode($jsonContent, true);
if (json_last_error() !== JSON_ERROR_NONE) {
throw new \RuntimeException('Invalid JSON in screen configuration: ' . json_last_error_msg());
}
if (!isset($json['screens']) || !is_array($json['screens'])) {
throw new \RuntimeException("Invalid screen configuration format: 'screens' array is required");
}
$screen = collect($json['screens'])->first();
if (!$screen) {
throw new \RuntimeException('No screen configuration found in JSON');
}
// Look for existing screen by title
$newScreen = is_null($columnKey) ? self::firstWhere('title', $screen['title']) : self::firstWhere($columnKey, $screen[$columnKey]);
// Create new screen
unset($screen['categories']);
$screen['screen_category_id'] = null;
$screen['is_default'] = $isDefault;
if ($newScreen) {
$newScreen->fill($screen);
$newScreen->save();
} else {
$newScreen = self::updateOrCreate(
['key' => $screen['key']],
$screen
);
}
// Assign system category if needed
if ($isSystem && $newScreen) {
$screenCategory = ScreenCategory::firstOrCreate([
'name' => 'System',
'is_system' => 1,
]);
$newScreen->categories()->attach([$screenCategory->id]);
}
return $newScreen;
}
}