-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrigger.php
More file actions
139 lines (125 loc) · 3.17 KB
/
Trigger.php
File metadata and controls
139 lines (125 loc) · 3.17 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
<?php
namespace App\Task;
use App\Model\AutomationModel;
use App\Model\FlowModel;
use App\Model\StepModel;
use App\Model\TaskModel;
use App\Service\ExecutionContext;
class Trigger extends TaskModel
{
public function __construct()
{
$this->type = 'utility';
$this->name = 'Trigger';
$this->description = 'Trigger something independently from the current flow';
parent::__construct();
}
public function getFields(): array
{
return [
'async' => [
'label' => 'Run async?',
'help' => 'If the automation is using batches then this trigger will always run async.',
'type' => 'checkbox',
'conditionals' => [
'action' => 'automation',
'override_data' => [ 'operator' => 'empty' ],
],
],
'pass_data' => [
'label' => 'Pass current data?',
'type' => 'checkbox',
],
'override_data' => [
'label' => 'Override current data?',
'type' => 'checkbox',
'conditionals' => [
'async' => [ 'operator' => 'empty' ],
],
],
'action' => [
'label' => 'Action',
'type' => 'select',
'choices' => [
'automation' => 'Automation',
'flow' => 'Flow',
'step' => 'Step',
'tasks' => 'Tasks',
],
],
'automation' => [
'label' => 'Automation',
'type' => 'entity',
'entity' => 'automation',
'actions' => [ 'edit', 'create' ],
'conditionals' => [
'action' => 'automation',
],
],
'flow' => [
'label' => 'Flow',
'type' => 'entity',
'entity' => 'flow',
'actions' => [ 'edit', 'create' ],
'conditionals' => [
'action' => 'flow',
],
],
'step' => [
'label' => 'Step',
'type' => 'entity',
'entity' => 'step',
'actions' => [ 'edit', 'create' ],
'conditionals' => [
'action' => 'step',
],
],
'tasks' => [
'label' => 'Tasks',
'type' => 'tasks',
'conditionals' => [
'action' => 'tasks',
],
],
];
}
function execute( array $config, ExecutionContext $context, $data )
{
switch ( $config['action'] ?? '' ) {
case 'automation':
$method = 'execute';
$action = AutomationModel::get( $config['automation'] );
if ( ! empty( $config['async'] ) || $action->getIterator() ) {
$context->getExecuteService()->schedule( $action );
return $data;
}
break;
case 'flow':
$method = 'executeFlow';
$action = FlowModel::get( $config['flow'] );
break;
case 'step':
$method = 'executeStep';
$action = StepModel::get( $config['step'] );
break;
case 'tasks':
$method = 'executeTasks';
$action = $config['tasks'];
break;
default:
$context->addError( 'Invalid action' );
return $data;
}
$service = $context->getExecuteService();
if ( $service && $action ) {
$context->descend();
$request = ( ! empty( $config['pass_data'] ) ) ? $data : [];
$return = $service->$method( $action, $context, $request );
if ( ! empty( $config['override_data'] ) ) {
$data = $return;
}
$context->ascend();
}
return $data;
}
}