forked from ProcessMaker/processmaker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScreensInScreen.php
More file actions
131 lines (117 loc) · 4.37 KB
/
ScreensInScreen.php
File metadata and controls
131 lines (117 loc) · 4.37 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
<?php
namespace ProcessMaker\Assets;
use DOMXPath;
use Illuminate\Support\Arr;
use ProcessMaker\Contracts\ScreenInterface;
use ProcessMaker\Exception\MaximumRecursionException;
use ProcessMaker\Managers\ExportManager;
use ProcessMaker\Models\Process;
use ProcessMaker\Models\ProcessRequest;
use ProcessMaker\Models\Screen;
use ProcessMaker\Providers\WorkflowServiceProvider;
class ScreensInScreen
{
public $type = Screen::class;
public $owner = Screen::class;
private $processRequest = null;
private $recursion = 0;
/**
* Get the screens (nested) used in a screen
*
* @param Screen $screen
* @param array $screens
*
* @return array
*/
public function referencesToExport(ScreenInterface $screen, array $screens = [], ExportManager $manager = null, bool $recursive = true)
{
if ($this->recursion > 10) {
throw new MaximumRecursionException(
'Max screen recursion depth of 10 exceeded. Is a child screen referencing its parent?'
);
}
$config = $screen->versionFor($this->processRequest)->config;
if (is_array($config)) {
$this->findInArray($config, function ($item) use (&$screens, $manager, $recursive) {
if (is_array($item) && isset($item['component']) && $item['component'] === 'FormNestedScreen' && !empty($item['config']['screen'])) {
$screens[] = [Screen::class, $item['config']['screen']];
if ($recursive) {
$screen = app(Screen::class)->find($item['config']['screen']);
$this->recursion++;
if ($screen) {
$screens = $this->referencesToExport($screen, $screens, $manager, $recursive);
}
$this->recursion--;
}
}
});
}
return $screens;
}
/**
* Update references used in an imported screen
*
* @param Screen $process
* @param array $references
* @param ExportManager $exportManager
*
* @return void
*/
public function updateReferences(Screen $screen, array $references, ExportManager $exportManager)
{
$config = $screen->config;
if (is_array($config)) {
$this->findInArray($config, function ($item, $key) use ($references, &$config, $exportManager) {
if (is_array($item) && isset($item['component']) && $item['component'] === 'FormNestedScreen' && !empty($item['config']['screen'])) {
$oldRef = $item['config']['screen'];
if ((array_key_exists($oldRef, $references[Screen::class]))) {
$newRef = $references[Screen::class][$oldRef]->getKey();
} else {
$newRef = null;
$exportManager->addLogMessage(
'ScreensInScreen:references',
__(
'Imported file does not contain the screen #:screen assigned to a nested screen',
['screen' => $oldRef]
),
false,
__("Missing Nested Screen's screen")
);
}
Arr::set($config, "$key.config.screen", $newRef);
}
});
$screen->config = $config;
$screen->save();
}
}
/**
* Find recursively in an array
*
* @param array $array
* @param callable $callback
*
* @return void
*/
private function findInArray(array $array, callable $callback, array $path = [])
{
call_user_func($callback, $array, implode('.', $path));
foreach ($array as $key => $item) {
if (is_array($item)) {
$this->findInArray($item, $callback, array_merge($path, [$key]));
} else {
call_user_func($callback, $item, implode('.', array_merge($path, [$key])));
}
}
}
/**
* Set the process requests for version context
*
* @param ProcessRequest $processRequest
* @return void
*/
public function setProcessRequest(ProcessRequest $processRequest = null)
{
$this->processRequest = $processRequest;
}
}