View source
<?php
namespace Drupal\maestro\Plugin\EngineTasks;
use Drupal\Core\Plugin\PluginBase;
use Drupal\maestro\MaestroEngineTaskInterface;
use Drupal\maestro\Engine\MaestroEngine;
use Drupal\maestro\MaestroTaskTrait;
use Drupal\Core\Form\FormStateInterface;
use Drupal\maestro\Form\MaestroExecuteInteractive;
class MaestroIfTask extends PluginBase implements MaestroEngineTaskInterface {
use MaestroTaskTrait;
public function __construct($configuration = NULL) {
if (is_array($configuration)) {
$this->processID = $configuration[0];
$this->queueID = $configuration[1];
}
}
public function isInteractive() {
return FALSE;
}
public function shortDescription() {
return t('If Task');
}
public function description() {
return $this
->t('Performs logic on task completion or variables.');
}
public function getPluginId() {
return 'MaestroIf';
}
public function getTaskColours() {
return '#daa520';
}
public function execute() {
$templateMachineName = MaestroEngine::getTemplateIdFromProcessId($this->processID);
$taskMachineName = MaestroEngine::getTaskIdFromQueueId($this->queueID);
$task = MaestroEngine::getTemplateTaskByID($templateMachineName, $taskMachineName);
$statusFlag = NULL;
$ifData = $task['data']['if'];
$variable = $ifData['variable'];
$variableValue = $ifData['variable_value'];
$method = $ifData['method'];
$status = $ifData['status'];
$operator = $ifData['operator'];
switch ($method) {
case 'byvariable':
$processVariableValue = MaestroEngine::getProcessVariable($variable, $this->processID);
$statusFlag = TRUE;
switch ($operator) {
case '=':
if (strcmp($variableValue, $processVariableValue) != 0) {
$statusFlag = FALSE;
}
break;
case '!=':
if (strcmp($variableValue, $processVariableValue) == 0) {
$statusFlag = FALSE;
}
break;
case '<':
if (floatval($processVariableValue) > floatval($variableValue)) {
$statusFlag = FALSE;
}
break;
case '>':
if (floatval($processVariableValue) < floatval($variableValue)) {
$statusFlag = FALSE;
}
break;
}
break;
case 'bylasttaskstatus':
$pointers = MaestroEngine::getTaskPointersFromTemplate($templateMachineName, $taskMachineName);
$query = \Drupal::entityQuery('maestro_queue');
$andMainConditions = $query
->andConditionGroup()
->condition('process_id', $this->processID)
->condition('status', '0', '<>')
->condition('archived', '1');
$orConditionGroup = $query
->orConditionGroup();
foreach ($pointers as $taskID) {
$orConditionGroup
->condition('task_id', $taskID);
}
$andMainConditions
->condition($orConditionGroup);
$query
->condition($andMainConditions);
$entity_ids = $query
->execute();
foreach ($entity_ids as $entityID) {
$queueRecord = \Drupal::entityTypeManager()
->getStorage('maestro_queue')
->load($entityID);
if (strcmp($status, $queueRecord->status
->getString()) != 0) {
$statusFlag = FALSE;
}
}
if ($statusFlag === NULL) {
$statusFlag = TRUE;
}
break;
}
if ($statusFlag !== NULL) {
if ($statusFlag == TRUE) {
$this->executionStatus = TASK_STATUS_SUCCESS;
$this->completionStatus = MAESTRO_TASK_COMPLETION_NORMAL;
}
else {
$this->executionStatus = TASK_STATUS_SUCCESS;
$this->completionStatus = MAESTRO_TASK_COMPLETION_USE_FALSE_BRANCH;
}
return TRUE;
}
\Drupal::logger('maestro')
->error('If task does not have a statusFlag set and is unable to complete');
return FALSE;
}
public function getExecutableForm($modal, MaestroExecuteInteractive $parent) {
}
public function handleExecuteSubmit(array &$form, FormStateInterface $form_state) {
}
public function getTaskEditForm(array $task, $templateMachineName) {
$ifParms = isset($task['data']['if']) ? $task['data']['if'] : [];
$form = [
'#markup' => $this
->t('Edit the logic for this IF task'),
];
$form['method'] = [
'#type' => 'radios',
'#title' => $this
->t('Execute the IF:'),
'#options' => [
'byvariable' => $this
->t('By Variable'),
'bylasttaskstatus' => $this
->t('By Last Task Status'),
],
'#default_value' => isset($ifParms['method']) ? $ifParms['method'] : '',
'#required' => TRUE,
'#attributes' => [
'onclick' => 'maestro_if_task_toggle(this);',
],
'#attached' => [
'library' => [
'maestro/maestro-engine-task-edit',
],
],
];
$variables = MaestroEngine::getTemplateVariables($templateMachineName);
$options = [];
foreach ($variables as $variableName => $arr) {
$options[$variableName] = $variableName;
}
$form['byvariable'] = [
'#id' => 'byvar',
'#tree' => TRUE,
'#type' => 'details',
'#title' => $this
->t('By Variable Options'),
'#open' => FALSE,
];
$form['byvariable']['variable'] = [
'#type' => 'select',
'#title' => $this
->t('Argument variable'),
'#required' => FALSE,
'#default_value' => isset($ifParms['variable']) ? $ifParms['variable'] : '',
'#options' => $options,
];
$form['byvariable']['operator'] = [
'#type' => 'select',
'#title' => $this
->t('Operator'),
'#required' => FALSE,
'#default_value' => isset($ifParms['operator']) ? $ifParms['operator'] : '',
'#options' => [
'=' => '=',
'>' => '>',
'<' => '<',
'!=' => '!=',
],
];
$form['byvariable']['variable_value'] = [
'#type' => 'textfield',
'#title' => $this
->t('Variable value'),
'#description' => $this
->t('The IF will check against this value during execution'),
'#default_value' => isset($ifParms['variable_value']) ? $ifParms['variable_value'] : '',
'#required' => FALSE,
];
$form['bystatus'] = [
'#id' => 'bystatus',
'#tree' => TRUE,
'#type' => 'details',
'#title' => $this
->t('By Last Task Status'),
'#open' => FALSE,
'#markup' => $this
->t('This method is only useful if ONLY ONE task points to this IF.
If more than one task points to this IF task, a FALSE will be returned if ANY of those
tasks do not have a status of the status chosen in the status selector.'),
];
$form['bystatus']['status'] = [
'#type' => 'select',
'#title' => $this
->t('Status'),
'#required' => FALSE,
'#default_value' => isset($ifParms['status']) ? $ifParms['status'] : '',
'#options' => [
TASK_STATUS_SUCCESS => $this
->t('Last Task Status is Success'),
TASK_STATUS_CANCEL => $this
->t('Last Task Status is Cancel'),
TASK_STATUS_HOLD => $this
->t('Last Task Status is Hold'),
TASK_STATUS_ABORTED => $this
->t('Last Task Status is Aborted'),
],
];
if (isset($ifParms['method']) && $ifParms['method'] == 'byvariable') {
$form['byvariable']['#open'] = TRUE;
$form['bystatus']['#open'] = FALSE;
}
else {
$form['byvariable']['#open'] = FALSE;
$form['bystatus']['#open'] = TRUE;
}
return $form;
}
public function validateTaskEditForm(array &$form, FormStateInterface $form_state) {
$method = $form_state
->getValue('method');
switch ($method) {
case 'byvariable':
$byvars = $form_state
->getValue('byvariable');
if (empty($byvars['variable'])) {
$form_state
->setErrorByName('byvariable][variable', $this
->t('When doing an IF by variable, you must provide a variable to IF on.'));
}
if (empty($byvars['operator'])) {
$form_state
->setErrorByName('byvariable][operator', $this
->t('When doing an IF by variable, you must provide a operator.'));
}
if (empty($byvars['variable_value'])) {
$form_state
->setErrorByName('byvariable][variable_value', $this
->t('When doing an IF by variable, you must provide a variable value.'));
}
$form['byvariable']['#open'] = TRUE;
$form['bystatus']['#open'] = FALSE;
break;
case 'bystatus':
$byvars = $form_state
->getValue('bystatus');
if (empty($byvars['status'])) {
$form_state
->setErrorByName('bystatus][status', $this
->t('When doing an IF by statys, you must provide a status value.'));
}
$form['byvariable']['#open'] = FALSE;
$form['bystatus']['#open'] = TRUE;
break;
}
}
public function prepareTaskForSave(array &$form, FormStateInterface $form_state, array &$task) {
$method = $form_state
->getValue('method');
$byvariable = $form_state
->getValue('byvariable');
$bystatus = $form_state
->getValue('bystatus');
$task['data']['if'] = [
'method' => $method,
'variable' => $byvariable['variable'],
'operator' => $byvariable['operator'],
'variable_value' => $byvariable['variable_value'],
'status' => $bystatus['status'],
];
}
public function performValidityCheck(array &$validation_failure_tasks, array &$validation_information_tasks, array $task) {
$data = $task['data']['if'];
if (array_key_exists('method', $data) && $data['method'] == '' || !array_key_exists('method', $data)) {
$validation_failure_tasks[] = [
'taskID' => $task['id'],
'taskLabel' => $task['label'],
'reason' => t('The IF task has not been set up properly. The method of "By Variable" or "By Status" is missing and thus the engine will be unable to execute this task.'),
];
}
else {
if (array_key_exists('operator', $data) && $data['operator'] == '' || !array_key_exists('operator', $data)) {
$validation_failure_tasks[] = [
'taskID' => $task['id'],
'taskLabel' => $task['label'],
'reason' => t('The IF task has not been set up properly. The operator has not been set. The engine will be unable to execute this task.'),
];
}
switch ($data['method']) {
case 'byvariable':
if (array_key_exists('variable', $data) && $data['variable'] == '' || !array_key_exists('variable', $data)) {
$validation_failure_tasks[] = [
'taskID' => $task['id'],
'taskLabel' => $task['label'],
'reason' => t('The IF task has not been set up properly. The variable has not been set. The engine will be unable to execute this task.'),
];
}
if (!array_key_exists('variable_value', $data)) {
$validation_failure_tasks[] = [
'taskID' => $task['id'],
'taskLabel' => $task['label'],
'reason' => t('The IF task has not been set up properly. The variable value has not been set. The engine will be unable to execute this task.'),
];
}
break;
case 'bystatus':
if (array_key_exists('status', $data) && $data['status'] == '' || !array_key_exists('status', $data)) {
$validation_failure_tasks[] = [
'taskID' => $task['id'],
'taskLabel' => $task['label'],
'reason' => t('The IF task has not been set up properly. The status has not been set. The engine will be unable to execute this task.'),
];
}
break;
}
}
}
public function getTemplateBuilderCapabilities() {
return [
'edit',
'drawlineto',
'drawfalselineto',
'removelines',
'remove',
];
}
}