Skip to content

Commit

Permalink
[TASK] Add support for basic action in YAML scenarios
Browse files Browse the repository at this point in the history
Related: #155
  • Loading branch information
ohader committed Apr 6, 2020
1 parent 7e6ad7d commit 4e14e56
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ class DataHandlerFactory
*/
private $dataMapPerWorkspace = [];

/**
* @var array
*/
private $commandMapPerWorkspace = [];

/**
* @var bool[]
*/
Expand Down Expand Up @@ -79,6 +84,14 @@ public function getDataMapPerWorkspace(): array
return $this->dataMapPerWorkspace;
}

/**
* @return array
*/
public function getCommandMapPerWorkspace(): array
{
return $this->commandMapPerWorkspace;
}

/**
* @return string[]
*/
Expand Down Expand Up @@ -143,6 +156,9 @@ private function processEntityItem(
$tableName = $entityConfiguration->getTableName();
$newId = $this->getUniqueIdForNewRecords();
$this->setInDataMap($tableName, $newId, $values, (int)$workspaceId);
if (isset($itemSettings['actions'])) {
$this->setInCommandMap($tableName, $newId, $nodeId, $itemSettings['actions'], (int)$workspaceId);
}

foreach ($itemSettings['versionVariants'] ?? [] as $versionVariantSettings) {
$this->processVersionVariantItem(
Expand Down Expand Up @@ -208,6 +224,9 @@ private function processLanguageVariantItem(
$newId = $this->getUniqueIdForNewRecords();
$workspaceId = $itemSettings['version']['workspace'] ?? 0;
$this->setInDataMap($tableName, $newId, $values, (int)$workspaceId);
if (isset($itemSettings['actions'])) {
$this->setInCommandMap($tableName, $newId, $nodeId, $itemSettings['actions'], (int)$workspaceId);
}

foreach ($itemSettings['languageVariants'] ?? [] as $variantItemSettings) {
$this->processLanguageVariantItem(
Expand Down Expand Up @@ -257,6 +276,9 @@ private function processVersionVariantItem(

$tableName = $entityConfiguration->getTableName();
$this->setInDataMap($tableName, $ancestorId, $values, (int)$values['workspace']);
if (isset($itemSettings['actions'])) {
$this->setInCommandMap($tableName, $ancestorId, $nodeId, $itemSettings['actions'], (int)$values['workspace']);
}
}

/**
Expand Down Expand Up @@ -487,6 +509,37 @@ private function setInDataMap(
$this->dataMapPerWorkspace[$workspaceId][$tableName][$identifier] = $values;
}

private function setInCommandMap(
string $tableName,
string $identifier,
?string $nodeId,
array $actionItems,
int $workspaceId = 0
): void {
if (empty($actionItems)) {
return;
}
// @todo implement `immediate` actions -> needs to split dataMap & commandMap in logical sections
foreach ($actionItems as $actionItem) {
$action = $actionItem['action'] ?? null;
$type = $actionItem['type'] ?? null;
$target = $actionItem['target'] ?? null;
if ($action === 'move') {
if ($type === 'toPage' && $target !== null) {
$this->commandMapPerWorkspace[$workspaceId][$tableName][$identifier]['move'] = $target;
} elseif ($type === 'toTop' && $nodeId !== null) {
$this->commandMapPerWorkspace[$workspaceId][$tableName][$identifier]['move'] = $nodeId;
} elseif ($type === 'afterRecord' && $target !== null) {
$this->commandMapPerWorkspace[$workspaceId][$tableName][$identifier]['move'] = '-' . $target;
}
} elseif ($action === 'delete') {
$this->commandMapPerWorkspace[$workspaceId][$tableName][$identifier]['delete'] = true;
} elseif ($action === 'discard' && $workspaceId > 0) {
$this->commandMapPerWorkspace[$workspaceId][$tableName][$identifier]['clearWSID'] = true;
}
}
}

/**
* @param int $workspaceId
* @param string $tableName
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,17 @@ public function invokeFactory(DataHandlerFactory $factory)
$this->dataHandler->errorLog
);
}
foreach ($factory->getCommandMapPerWorkspace() as $workspaceId => $commandMap) {
$commandMap = $this->updateCommandMap($commandMap);
$backendUser = clone $this->backendUser;
$backendUser->workspace = $workspaceId;
$this->dataHandler->start([], $commandMap, $backendUser);
$this->dataHandler->process_cmdmap();
$this->errors = array_merge(
$this->errors,
$this->dataHandler->errorLog
);
}
}

/**
Expand Down Expand Up @@ -123,4 +134,35 @@ function ($value) {
}
return $updatedTableDataMap;
}

/**
* @param array $commandMap
* @return array
*/
private function updateCommandMap(array $commandMap): array
{
$updatedTableCommandMap = [];
foreach ($commandMap as $tableName => $tableDataMap) {
foreach ($tableDataMap as $key => $values) {
$key = $this->dataHandler->substNEWwithIDs[$key] ?? $key;
$values = array_map(
function ($value) {
if (!is_string($value)) {
return $value;
}
if (strpos($value, 'NEW') === 0) {
return $this->dataHandler->substNEWwithIDs[$value] ?? $value;
}
if (strpos($value, '-NEW') === 0) {
return $this->dataHandler->substNEWwithIDs[substr($value, 1)] ?? $value;
}
return $value;
},
$values
);
$updatedTableCommandMap[$tableName][$key] = $values;
}
}
return $updatedTableCommandMap;
}
}

0 comments on commit 4e14e56

Please sign in to comment.