Skip to content

Commit 76d8ba5

Browse files
committed
Implement conditional events
1 parent 57691f4 commit 76d8ba5

11 files changed

Lines changed: 114 additions & 16 deletions

ProcessMaker/Bpmn/Process.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace ProcessMaker\Bpmn;
4+
5+
use Illuminate\Support\Facades\Cache;
6+
use ProcessMaker\Nayra\Bpmn\Models\Process as ModelsProcess;
7+
8+
class Process extends ModelsProcess
9+
{
10+
11+
/**
12+
* Get a property.
13+
*
14+
* @param string $name
15+
* @param mixed $default
16+
*
17+
* @return mixed
18+
*/
19+
public function getProperty($name, $default = null)
20+
{
21+
if ($name === 'conditionals') {
22+
$key = '_process_' . $this->getOwnerDocument()->getModel()->getKey();
23+
$properties = Cache::store('global_variables')->get($key, []);
24+
return $properties[$name] ?? $default;
25+
} else {
26+
return parent::getProperty($name, $default);
27+
}
28+
}
29+
30+
/**
31+
* Set a property.
32+
*
33+
* @param string $name
34+
* @param mixed $value
35+
*
36+
* @return $this
37+
*/
38+
public function setProperty($name, $value)
39+
{
40+
if ($name === 'conditionals') {
41+
$key = '_process_' . $this->getOwnerDocument()->getModel()->getKey();
42+
$properties = Cache::store('global_variables')->get($key, []);
43+
$properties[$name] = $value;
44+
\Log::info(['global_variables', $key, $properties]);
45+
Cache::store('global_variables')->forever($key, $properties);
46+
return $this;
47+
} else {
48+
return parent::setProperty($name, $value);
49+
}
50+
}
51+
}

ProcessMaker/BpmnEngine.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace ProcessMaker;
44

5+
use ProcessMaker\Models\GlobalDataStore;
56
use ProcessMaker\Models\ProcessRequest;
67
use ProcessMaker\Nayra\Contracts\Bpmn\ProcessInterface;
78
use ProcessMaker\Nayra\Contracts\Bpmn\StartEventInterface;
@@ -48,6 +49,7 @@ public function __construct(RepositoryInterface $repository, $dispatcher)
4849
$this->uid = uniqid();
4950
$this->repository = $repository;
5051
$this->dispatcher = $dispatcher;
52+
$this->setDataStore(new GlobalDataStore());
5153
}
5254

5355
/**

ProcessMaker/Jobs/BpmnAction.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,9 @@ public function handle()
5353
$request = !$this->instance && $this instanceof StartEvent ? $response : $this->instance;
5454
if ($request) {
5555
$request->logError($exception, $element);
56+
} else {
57+
throw $exception;
5658
}
57-
throw $exception;
5859
} finally {
5960
if (isset($this->instanceId)) {
6061
$this->unlockInstance($this->instanceId);

ProcessMaker/Models/Process.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
use ProcessMaker\Exception\TaskDoesNotHaveUsersException;
1818
use ProcessMaker\Exception\UserOrGroupAssignmentEmptyException;
1919
use ProcessMaker\Nayra\Contracts\Bpmn\ActivityInterface;
20-
use ProcessMaker\Nayra\Contracts\Bpmn\ProcessInterface;
2120
use ProcessMaker\Nayra\Contracts\Bpmn\ScriptTaskInterface;
2221
use ProcessMaker\Nayra\Contracts\Bpmn\ServiceTaskInterface;
2322
use ProcessMaker\Nayra\Contracts\Storage\BpmnDocumentInterface;
@@ -45,7 +44,7 @@
4544
* @property string $description
4645
* @property string $name
4746
* @property string $status
48-
* @property string start_events
47+
* @property array start_events
4948
* @property \Carbon\Carbon $updated_at
5049
* @property \Carbon\Carbon $created_at
5150
*
@@ -65,7 +64,7 @@
6564
* @OA\Property(property="warnings", type="string"),
6665
* @OA\Property(property="self_service_tasks", type="array", @OA\Items(type="object")),
6766
* @OA\Property(property="signal_events", type="array", @OA\Items(type="object")),
68-
67+
6968
* ),
7069
* @OA\Schema(
7170
* schema="Process",
@@ -214,6 +213,7 @@ class Process extends Model implements HasMedia, ProcessModelInterface
214213
'warnings' => 'array',
215214
'self_service_tasks' => 'array',
216215
'signal_events' => 'array',
216+
'conditional_events' => 'array',
217217
];
218218

219219
/**
@@ -1119,4 +1119,18 @@ public function getUpdatedStartEventsSignalEvents(): array
11191119

11201120
return $signalReferences->toArray();
11211121
}
1122+
1123+
/**
1124+
* Get the unique Conditional Start Events.
1125+
*
1126+
* @return array
1127+
*/
1128+
public function getUpdatedConditionalStartEvents()
1129+
{
1130+
return collect($this->start_events)->filter(function ($startEvent) {
1131+
return collect($startEvent['eventDefinitions'])->search(function ($event) {
1132+
return $event['$type'] === 'conditionalEventDefinition';
1133+
}) !== false;
1134+
})->pluck('id');
1135+
}
11221136
}

ProcessMaker/Models/ProcessRequest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,7 @@ public function logError(Throwable $exception, FlowElementInterface $element = n
474474
$errors[] = $error;
475475
$this->errors = $errors;
476476
$this->status = 'ERROR';
477+
\Log::error($exception);
477478
$this->save();
478479
}
479480

@@ -663,7 +664,7 @@ public function updateCatchEvents()
663664
{
664665
$signalEvents = [];
665666
foreach ($this->tokens as $token) {
666-
$element = $token->getDefinition(true);
667+
$element = $token->getDefinition(true, $this->tokens->toArray());
667668
if ($element instanceof IntermediateCatchEventInterface) {
668669
foreach ($element->getEventDefinitions() as $eventDefinition) {
669670
if ($eventDefinition instanceof SignalEventDefinitionInterface) {

ProcessMaker/Models/ProcessRequestToken.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ class ProcessRequestToken extends Model implements TokenInterface
139139
protected $casts = [
140140
'data' => 'array',
141141
'self_service_groups' => 'array',
142+
'token_properties' => 'array',
142143
];
143144

144145
/**
@@ -273,9 +274,10 @@ public function assignableUsers()
273274
*
274275
* @return array
275276
*/
276-
public function getDefinition($asObject = false)
277+
public function getDefinition($asObject = false, $par = null)
277278
{
278-
$process = $this->processRequest->processVersion ?: $this->processRequest->process;
279+
$request = $this->processRequest ?: $this->getInstance();
280+
$process = $request->processVersion ?: $request->process;
279281
$definitions = $process->getDefinitions();
280282
$element = $definitions->findElementById($this->element_id);
281283
if (!$element) {
@@ -636,4 +638,16 @@ public function logError(Throwable $error, FlowElementInterface $bpmnElement)
636638
{
637639
$this->getInstance()->logError($error, $bpmnElement);
638640
}
641+
642+
public function updateTokenProperties()
643+
{
644+
$allowed = ['conditionals'];
645+
$this->token_properties = array_filter(
646+
$this->getProperties(),
647+
function ($key) use ($allowed) {
648+
return in_array($key, $allowed);
649+
},
650+
ARRAY_FILTER_USE_KEY
651+
);
652+
}
639653
}

ProcessMaker/Models/ProcessVersion.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ class ProcessVersion extends Model implements ProcessModelInterface
4747
'warnings' => 'array',
4848
'self_service_tasks' => 'array',
4949
'signal_events' => 'array',
50+
'conditional_events' => 'array',
5051
];
5152

5253
/**

ProcessMaker/Observers/ProcessObserver.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,6 @@ public function saving(Process $process)
3434
$process->start_events = $process->getUpdatedStartEvents();
3535
$process->self_service_tasks = $process->getUpdatedSelfServiceTasks();
3636
$process->signal_events = $process->getUpdatedStartEventsSignalEvents();
37+
$process->conditional_events = $process->getUpdatedConditionalStartEvents();
3738
}
3839
}

ProcessMaker/Repositories/DefinitionsRepository.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22
namespace ProcessMaker\Repositories;
33

4+
use ProcessMaker\Bpmn\Process;
45
use ProcessMaker\Models\CallActivity;
56
use ProcessMaker\Models\DataStore;
67
use ProcessMaker\Models\FormalExpression;
@@ -9,16 +10,13 @@
910
use ProcessMaker\Models\TimerExpression;
1011
use ProcessMaker\Nayra\Contracts\RepositoryInterface;
1112
use ProcessMaker\Nayra\RepositoryTrait;
12-
use ProcessMaker\Repositories\ExecutionInstanceRepository;
13-
use ProcessMaker\Repositories\TokenRepository;
1413

1514
/**
1615
* Definitions Repository
1716
*
1817
*/
1918
class DefinitionsRepository implements RepositoryInterface
2019
{
21-
2220
use RepositoryTrait;
2321

2422
private $tokenRepository = null;
@@ -56,7 +54,8 @@ public function getTokenRepository()
5654
return $this->tokenRepository;
5755
}
5856

59-
public function createDataStore() {
57+
public function createDataStore()
58+
{
6059
return new DataStore();
6160
}
6261

@@ -69,4 +68,16 @@ public function createMessage()
6968
{
7069
return new Message();
7170
}
71+
72+
/**
73+
* Create instance of Process.
74+
*
75+
* @return \ProcessMaker\Nayra\Contracts\Bpmn\ProcessInterface
76+
*/
77+
public function createProcess()
78+
{
79+
$process = new Process();
80+
$process->setRepository($this);
81+
return $process;
82+
}
7283
}

ProcessMaker/Repositories/ExecutionInstanceRepository.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,10 @@ public function loadExecutionInstanceByUid($instanceId, StorageInterface $storag
6868
'index' => $token->element_index,
6969
'element_ref' => $token->element_id,
7070
];
71-
$token->setProperties($tokenInfo);
71+
$token->setProperties(array_merge($token->token_properties ?: [], $tokenInfo));
7272
$element = $storage->getElementInstanceById($tokenInfo['element_ref']);
7373
$element->addToken($instance, $token);
7474
}
75-
$mytokens2 = [];
76-
foreach ($instance->getTokens() as $tt) {
77-
$mytokens2[] = $tt->id;
78-
}
7975
return $instance;
8076
}
8177

0 commit comments

Comments
 (0)