forked from ProcessMaker/processmaker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScreenVersion.php
More file actions
81 lines (71 loc) · 2.84 KB
/
ScreenVersion.php
File metadata and controls
81 lines (71 loc) · 2.84 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
<?php
namespace ProcessMaker\Http\Resources;
use Illuminate\Support\Arr;
use ProcessMaker\Models\Screen;
use ProcessMaker\Models\ScreenVersion as ScreenVersionModel;
use ProcessMaker\ProcessTranslations\ScreenTranslation;
class ScreenVersion extends ApiResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request
* @return array
*/
public function toArray($request)
{
$screenVersion = parent::toArray($request);
$include = explode(',', $request->input('include', ''));
$task = null;
if (in_array('nested', $include)) {
$this->setDefaultScreenForNestedScreens($screenVersion);
$task = $request->route('task');
$processRequest = null;
if ($task) {
$processRequest = $task->processRequest;
}
$nested = [];
foreach ($this->parent->nestedScreenIds($processRequest) as $id) {
$nestedScreen = Screen::find($id);
if ($nestedScreen) {
$nested[] = $nestedScreen->versionFor($processRequest)->toArray();
}
}
$screenVersion['nested'] = $nested;
}
// If web entry, apply translations
if (!$task) {
// Apply translations to screen
$screenTranslation = new ScreenTranslation();
$screenVersion['config'] = $screenTranslation->applyTranslations(new ScreenVersionModel($screenVersion));
// Apply translations to nested screens
if (!array_key_exists('nested', $screenVersion)) {
return $screenVersion;
}
foreach ($screenVersion['nested'] as &$nestedScreen) {
$nestedScreen['config'] = $screenTranslation->applyTranslations(new ScreenVersionModel($nestedScreen));
}
}
return $screenVersion;
}
/**
* Set the default screen for nested screens when no screen has been selected.
*/
private function setDefaultScreenForNestedScreens(array &$screenVersion): void
{
$configArray = $screenVersion['config'];
foreach ($configArray as $key => $config) {
foreach ($config['items'] as $itemKey => $item) {
if (isset($item['component']) && $item['component'] === 'FormNestedScreen') {
$configScreen = $item['config']['screen'] ?? null;
if (Screen::where('id', $configScreen)->doesntExist()) {
$defaultScreenId = Screen::where('key', 'default-form-screen')->value('id');
$path = "{$key}.items.{$itemKey}.config.screen";
Arr::set($configArray, $path, $defaultScreenId);
}
}
}
}
$screenVersion['config'] = $configArray;
}
}