View source
<?php
namespace Drupal\maestro\Plugin\EngineTasks;
use Drupal\Core\Plugin\PluginBase;
use Drupal\maestro\MaestroEngineTaskInterface;
use Drupal\maestro\MaestroTaskTrait;
use Drupal\maestro\Engine\MaestroEngine;
use Drupal\Core\Form\FormStateInterface;
use Drupal\maestro\Form\MaestroExecuteInteractive;
use Drupal\Core\Routing\TrustedRedirectResponse;
class MaestroInteractiveTask 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 TRUE;
}
public function shortDescription() {
return t('Interactive Task');
}
public function description() {
return $this
->t('Interactive Task.');
}
public function getPluginId() {
return 'MaestroInteractive';
}
public function getTaskColours() {
return '#0000ff';
}
public function execute() {
$queueRecord = \Drupal::entityTypeManager()
->getStorage('maestro_queue')
->load($this->queueID);
$queueRecord
->set('run_once', 1);
$queueRecord
->save();
return FALSE;
}
public function getExecutableForm($modal, MaestroExecuteInteractive $parent) {
$form['queueID'] = [
'#type' => 'hidden',
'#title' => $this
->t('The queue ID of this task'),
'#default_value' => $this->queueID,
'#description' => $this
->t('queueID'),
];
$form['information_text'] = [
'#plain_text' => $this
->t('Default Maestro Interactive Task.'),
'#suffix' => '<br><br>',
];
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Complete'),
];
$form['actions']['reject'] = [
'#type' => 'submit',
'#value' => $this
->t('Reject'),
];
if ($modal == 'modal') {
$form['actions']['submit']['#ajax'] = [
'callback' => [
$parent,
'completeForm',
],
'wrapper' => '',
];
$form['actions']['reject']['#ajax'] = [
'callback' => [
$parent,
'completeForm',
],
'wrapper' => '',
];
}
return $form;
}
public function handleExecuteSubmit(array &$form, FormStateInterface $form_state) {
$queueID = intval($form_state
->getValue('maestro_queue_id'));
$triggeringElement = $form_state
->getTriggeringElement();
if (strstr($triggeringElement['#id'], 'edit-submit') !== FALSE && $queueID > 0) {
MaestroEngine::completeTask($queueID, \Drupal::currentUser()
->id());
}
else {
MaestroEngine::completeTask($queueID, \Drupal::currentUser()
->id());
MaestroEngine::setTaskStatus($queueID, TASK_STATUS_CANCEL);
}
$task = MaestroEngine::getTemplateTaskByQueueID($queueID);
if (isset($task['data']['redirect_to'])) {
$response = new TrustedRedirectResponse($task['data']['redirect_to']);
$form_state
->setResponse($response);
}
}
public function getTaskEditForm(array $task, $templateMachineName) {
$form = [
'#markup' => t('Interactive Task Edit'),
];
$handlers = \Drupal::moduleHandler()
->invokeAll('maestro_interactive_handlers', []);
$handler_desc = $this
->t('The function that contains the form definition for this instance of the interactive task.');
if (isset($task['handler']) && isset($handlers[$task['handler']])) {
$handler_desc = $handlers[$task['handler']];
}
$form['handler'] = [
'#type' => 'textfield',
'#title' => $this
->t('Handler'),
'#default_value' => isset($task['handler']) ? $task['handler'] : '',
'#required' => FALSE,
'#autocomplete_route_name' => 'maestro.autocomplete.interactive_handlers',
'#ajax' => [
'callback' => [
$this,
'interactiveHandlerCallback',
],
'event' => 'autocompleteclose',
'wrapper' => 'handler-ajax-refresh-wrapper',
'progress' => [
'type' => 'throbber',
'message' => NULL,
],
],
];
$form['handler_help_text'] = [
'#type' => 'html_tag',
'#tag' => 'div',
'#value' => $handler_desc,
'#readonly' => TRUE,
'#attributes' => [
'class' => [
'handler-help-message',
],
'id' => [
'handler-ajax-refresh-wrapper',
],
],
];
$form['redirect_to'] = [
'#type' => 'textfield',
'#title' => $this
->t('Return Path'),
'#description' => $this
->t('You can specify where your return path should go upon task completion.'),
'#default_value' => isset($task['data']['redirect_to']) ? $task['data']['redirect_to'] : 'taskconsole',
'#required' => TRUE,
];
$form['modal'] = [
'#type' => 'select',
'#title' => $this
->t('Task presentation'),
'#description' => $this
->t('Should this task be shown as a modal or full screen task.'),
'#default_value' => isset($task['data']['modal']) ? $task['data']['modal'] : 'modal',
'#options' => [
'modal' => $this
->t('Modal'),
'notmodal' => $this
->t('Full Page'),
],
'#required' => TRUE,
];
return $form;
}
public function interactiveHandlerCallback(array &$form, FormStateInterface $form_state) {
$selected_handler = $new_objective_id = $form_state
->getValue('handler');
$handlers = \Drupal::moduleHandler()
->invokeAll('maestro_interactive_handlers', []);
if ($selected_handler != '' && !function_exists($selected_handler)) {
$handler_desc = \Drupal::translation()
->translate('This handler form function does not exist.');
}
elseif (isset($handlers[$selected_handler])) {
$handler_desc = $handlers[$selected_handler];
}
else {
$handler_desc = \Drupal::translation()
->translate('The function that contains the form definition for this instance of the interactive task.');
}
$form['handler_help_text'] = [
'#type' => 'html_tag',
'#tag' => 'div',
'#value' => $handler_desc,
'#readonly' => TRUE,
'#attributes' => [
'class' => [
'handler-help-message',
],
'id' => [
'handler-ajax-refresh-wrapper',
],
],
];
return $form['handler_help_text'];
}
public function validateTaskEditForm(array &$form, FormStateInterface $form_state) {
$handler = $form_state
->getValue('handler');
if (strpos($handler, '[') > 0) {
$string_parts = explode('[', $handler);
$handler = $string_parts[0];
}
if ($handler != '' && !function_exists($handler)) {
$form_state
->setErrorByName('handler', $this
->t('This handler form function does not exist.'));
}
}
public function prepareTaskForSave(array &$form, FormStateInterface $form_state, array &$task) {
$task['handler'] = $form_state
->getValue('handler');
if (strpos($task['handler'], '[') > 0) {
$string_parts = explode('[', $task['handler']);
$task['handler'] = $string_parts[0];
}
$task['data']['modal'] = $form_state
->getValue('modal');
$redirect = $form_state
->getValue('redirect_to');
if (isset($redirect)) {
$task['data']['redirect_to'] = $redirect;
}
else {
$task['data']['redirect_to'] = '';
}
}
public function performValidityCheck(array &$validation_failure_tasks, array &$validation_information_tasks, array $task) {
if (array_key_exists('handler', $task) && $task['handler'] == '' || !array_key_exists('handler', $task)) {
$validation_information_tasks[] = [
'taskID' => $task['id'],
'taskLabel' => $task['label'],
'reason' => t('The Interactive Task handler is missing and thus the engine will assign the default handler to this task.'),
];
}
if (array_key_exists('modal', $task['data']) && $task['data']['modal'] == '' || !array_key_exists('modal', $task['data'])) {
$validation_failure_tasks[] = [
'taskID' => $task['id'],
'taskLabel' => $task['label'],
'reason' => t('The Interactive Task has not been set up properly. The "Task Presentation" option is missing and thus the engine will be unable to execute this task.'),
];
}
if (array_key_exists('assigned', $task) && $task['assigned'] == '' || !array_key_exists('assigned', $task)) {
$validation_failure_tasks[] = [
'taskID' => $task['id'],
'taskLabel' => $task['label'],
'reason' => t('The Interactive Task has not been set up properly. The Interactive Task requires assignments to actors, roles or other assignment options.'),
];
}
}
public function getTemplateBuilderCapabilities() {
return [
'edit',
'drawlineto',
'removelines',
'remove',
];
}
}