forked from ProcessMaker/processmaker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProcessSeeder.php
More file actions
259 lines (241 loc) · 9.65 KB
/
ProcessSeeder.php
File metadata and controls
259 lines (241 loc) · 9.65 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
<?php
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Hash;
use ProcessMaker\Models\EnvironmentVariable;
use ProcessMaker\Models\Group;
use ProcessMaker\Models\GroupMember;
use ProcessMaker\Models\Screen;
use ProcessMaker\Models\Process;
use ProcessMaker\Models\ProcessTaskAssignment;
use ProcessMaker\Models\ProcessNotificationSetting;
use ProcessMaker\Models\Script;
use ProcessMaker\Models\User;
use ProcessMaker\Providers\WorkflowServiceProvider;
use ProcessMaker\Nayra\Contracts\Bpmn\ActivityInterface;
use ProcessMaker\Nayra\Contracts\Bpmn\ScriptTaskInterface;
class ProcessSeeder extends Seeder
{
/**
* Array of [language => mime-type]
*/
const mimeTypes = [
'javascript' => 'application/javascript',
'lua' => 'application/x-lua',
'php' => 'application/x-php',
];
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
if (Process::count() !== 0) {
return;
}
//load user admin
$admin = User::where('username', 'admin')->firstOrFail();
foreach (glob(database_path('processes') . '/*.bpmn') as $filename) {
$process = factory(Process::class)->make([
'bpmn' => file_get_contents($filename),
'user_id' => $admin->getKey(),
'status' => 'ACTIVE',
]);
//Load the process title from the the main process of the BPMN definition
$processes = $process->getDefinitions()->getElementsByTagName('process');
if ($processes->item(0)) {
$processDefinition = $processes->item(0)->getBpmnElementInstance();
if (!empty($processDefinition->getName())) {
$process->name = $processDefinition->getName();
}
}
//Or load the process title from the collaboration of the BPMN definition
$collaborations = $process->getDefinitions()->getElementsByTagName('collaboration');
if ($collaborations->item(0)) {
$collaborationDefinition = $collaborations->item(0)->getBpmnElementInstance();
if (!empty($collaborationDefinition->getName())) {
$process->name = $collaborationDefinition->getName();
}
}
$process->save();
$definitions = $process->getDefinitions();
//Create scripts from the BPMN process definition
$scriptTasks = $definitions->getElementsByTagName('scriptTask');
foreach ($scriptTasks as $scriptTaskNode) {
$scriptTask = $scriptTaskNode->getBpmnElementInstance();
//Create a row in the Scripts table
$script = factory(Script::class)->create([
'title' => $scriptTask->getName('name') . ' Script',
'code' => $scriptTaskNode->getElementsByTagName('script')->item(0)->nodeValue,
'language' => $this->languageOfMimeType($scriptTask->getScriptFormat()),
]);
$scriptTaskNode->setAttributeNS(
WorkflowServiceProvider::PROCESS_MAKER_NS, 'scriptRef', $script->id
);
$scriptTaskNode->setAttributeNS(
WorkflowServiceProvider::PROCESS_MAKER_NS, 'config', '{}'
);
}
//Create/Assign Users to tasks
$lanes = $definitions->getElementsByTagName('lane');
foreach($lanes as $nodeLane) {
$lane = $nodeLane->getBpmnElementInstance();
$user = $this->getUserOrCreate($lane->getName());
foreach($lane->getFlowNodes() as $node) {
if ($node instanceof ActivityInterface && !($node instanceof ScriptTaskInterface)) {
factory(ProcessTaskAssignment::class)->create([
'process_id' => $process->getKey(),
'process_task_id' => $node->getId(),
'assignment_id' => $user->getKey(),
'assignment_type' => User::class,
]);
}
}
}
//Add notifications to request events
$notificationTypes = ['started', 'canceled', 'completed'];
foreach ($notificationTypes as $notificationType) {
factory(ProcessNotificationSetting::class)->create([
'process_id' => $process->getKey(),
'notifiable_type' => 'requester',
'notification_type' => $notificationType,
]);
}
//Add screens to the process
$admin = User::where('username', 'admin')->firstOrFail();
$humanTasks = ['task', 'userTask'];
foreach($humanTasks as $humanTask) {
$tasks = $definitions->getElementsByTagName($humanTask);
foreach($tasks as $task) {
$screenRef = $task->getAttributeNS(WorkflowServiceProvider::PROCESS_MAKER_NS, 'screenRef');
$id = $task->getAttribute('id');
if ($screenRef) {
$screen = $this->createScreen($id, $screenRef, $process);
$task->setAttributeNS(WorkflowServiceProvider::PROCESS_MAKER_NS, 'screenRef', $screen->getKey());
}
//Assign "admin" to the task if it does not have user assigned
$assignments = ProcessTaskAssignment::where('process_id', $process->getKey())
->where('process_task_id', $id)
->count();
if (!$assignments) {
factory(ProcessTaskAssignment::class)->create([
'process_id' => $process->getKey(),
'process_task_id' => $id,
'assignment_id' => $admin->getKey(),
'assignment_type' => User::class,
]);
}
//Add notifications to task events
factory(ProcessNotificationSetting::class)->create([
'process_id' => $process->getKey(),
'element_id' => $id,
'notifiable_type' => 'assignee',
'notification_type' => 'assigned',
]);
factory(ProcessNotificationSetting::class)->create([
'process_id' => $process->getKey(),
'element_id' => $id,
'notifiable_type' => 'requester',
'notification_type' => 'completed',
]);
}
}
//Update the screen and script references in the BPMN of the process
$process->bpmn = $definitions->saveXML();
$process->save();
}
}
/**
* Load the JSON of a screen.
*
* @param string $id
* @param string $screenRef
* @param string $process
*
* @return Screen
*/
private function createScreen($id, $screenRef, $process) {
if (file_exists(database_path('processes/screens/' . $screenRef . '.json'))) {
$json = json_decode(file_get_contents(database_path('processes/screens/' . $screenRef . '.json')));
return factory(Screen::class)->create([
'title' => $json[0]->name,
'config' => $json
]);
} elseif (file_exists(database_path('processes/screens/' . $id . '.json'))) {
$json = json_decode(file_get_contents(database_path('processes/screens/' . $id . '.json')));
return factory(Screen::class)->create([
'title' => $json[0]->name,
'config' => $json,
]);
}
}
/**
* Get the language that corresponds to an specific mime-type.
*
* @param string $mime
*
* @return string
*/
private function languageOfMimeType($mime)
{
return in_array($mime, self::mimeTypes) ? array_search($mime, self::mimeTypes) : '';
}
/**
* Format name without spaces and to lowercase
*
* @param $name
*
* @return string
*/
private function formatName($name)
{
return strtolower(str_replace(' ', '.', $name));
}
/**
* Get or create a user by full name.
*
* @param string $userFullName
*
* @return User
*/
private function getUserOrCreate($userFullName)
{
$name = $this->formatName($userFullName);
$user = User::where('username', $name)
->first();
if (!$user) {
$user = factory(User::class)->create([
'username' => $name,
'password' => Hash::make('admin'),
'status' => 'ACTIVE',
'is_administrator' => true
]);
}
return $user;
}
/**
* Get or create a group by name
*
* @param $name
*
* @return mixed
*/
private function getGroupOrCreate($name)
{
$group = Group::where('name', $name)->first();
if (!$group) {
$group = factory(Group::class)->create([
'name' => $name,
'status' => 'ACTIVE'
]);
}
factory(GroupMember::class)->create( [
'member_id' => function () use ($name) {
return $this->getUserOrCreate($name)->getKey();
},
'member_type' => User::class,
'group_id' => $group->getKey()
]);
return $group;
}
}