forked from ProcessMaker/processmaker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScriptsInScreen.php
More file actions
99 lines (91 loc) · 2.86 KB
/
ScriptsInScreen.php
File metadata and controls
99 lines (91 loc) · 2.86 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
<?php
namespace ProcessMaker\Assets;
use ProcessMaker\Managers\ExportManager;
use ProcessMaker\Models\Screen;
use ProcessMaker\Models\Script;
class ScriptsInScreen
{
public $type = Script::class;
public $owner = Screen::class;
/**
* Get the scripts (ex. watchers) used in a screen
*
* @param Screen $screen
* @param array $scripts
*
* @return array
*/
public function referencesToExport(Screen $screen, array $scripts = [])
{
$config = $screen->watchers;
if (is_array($config)) {
$this->findInArray($config, function ($item) use (&$scripts) {
if (is_array($item) && !empty($item['script_id'])) {
$scripts[] = [Script::class, $item['script_id']];
}
});
}
return $scripts;
}
/**
* Update references used in an imported screen
*
* @param Screen $process
* @param array $references
*
* @return void
*/
public function updateReferences(Screen $screen, array $references, ExportManager $exportManager)
{
$watches = $screen->watchers;
if (is_array($watches)) {
foreach ($watches as &$watcher) {
$refParts = explode('-', $watcher['script_id']);
if ($refParts[0] === 'data_source') {
continue;
}
$oldRef = $refParts[1];
if (isset($references[Script::class][$oldRef])) {
$newRef = $references[Script::class][$oldRef]->getKey();
} else {
$newRef = null;
$exportManager->addLogMessage(
'ScriptsInScreen:references',
__(
'Imported file does not contain the script #:script assigned to a watcher',
['script' => $oldRef]
),
false,
__("Missing watcher's script")
);
}
$watcher['script_id'] = $newRef;
$watcher['script']['id'] = "script-$newRef";
if ($newRef) {
$watcher['script']['title'] = $references[Script::class][$oldRef]->title;
}
}
}
$screen->watchers = $watches;
$screen->save();
}
/**
* Find recursively in an array
*
* @param array $array
* @param callable $callback
*
* @return void
*/
private function findInArray(array $array, callable $callback)
{
call_user_func($callback, $array);
foreach ($array as $item) {
if (is_array($item)) {
$this->findInArray($item, $callback);
} else {
call_user_func($callback, $item);
}
}
}
}