View source
<?php
namespace Drupal\maestro_webform\Plugin\EngineTasks;
use Drupal\node\Entity\NodeType;
use Drupal\webform\Controller\WebformSubmissionViewController;
use Drupal\Core\Url;
use Drupal\webform\Entity\WebformSubmission;
use Drupal\Core\Plugin\PluginBase;
use Drupal\maestro\MaestroEngineTaskInterface;
use Drupal\maestro\MaestroTaskTrait;
use Drupal\Core\Form\FormStateInterface;
use Drupal\maestro\Engine\MaestroEngine;
use Drupal\maestro\Form\MaestroExecuteInteractive;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Drupal\webform\Entity\Webform;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\ReplaceCommand;
use Drupal\Core\Routing\TrustedRedirectResponse;
class MaestroWebformTask extends PluginBase implements MaestroEngineTaskInterface {
use MaestroTaskTrait;
public function __construct(array $configuration = NULL) {
if (is_array($configuration)) {
$this->processID = $configuration[0];
$this->queueID = $configuration[1];
}
}
public function isInteractive() {
return TRUE;
}
public function shortDescription() {
return $this
->t('Webfom Task');
}
public function description() {
return $this
->t('Maestro Interactive Webform Task.');
}
public function getPluginId() {
return 'MaestroWebform';
}
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 TRUE;
}
public function getExecutableForm($modal, MaestroExecuteInteractive $parent) {
$form['#title'] = $this
->t('Submission Review');
$templateTask = MaestroEngine::getTemplateTaskByQueueID($this->queueID);
$taskUniqueSubmissionId = $templateTask['data']['unique_id'];
$webformMachineName = $templateTask['data']['webform_machine_name'];
$queueEntry = MaestroEngine::getQueueEntryById($this->queueID);
if ($queueEntry) {
$started_date = intval($queueEntry->started_date
->getString());
$created_date = intval($queueEntry->created
->getString());
if ($started_date - $created_date < 5) {
$queueEntry
->set('started_date', time());
$queueEntry
->save();
}
}
$webformNode = FALSE;
if (isset($templateTask['data']['use_nodes_attached']) && $templateTask['data']['use_nodes_attached'] == 1) {
if (isset($templateTask['data']['webform_nodes_attached_variable']) && $templateTask['data']['webform_nodes_attached_variable'] != 'none') {
$node_id = intval(MaestroEngine::getProcessVariable($templateTask['data']['webform_nodes_attached_variable'], $this->processID));
$webformNode = 'node/' . $node_id;
}
elseif (isset($templateTask['data']['webform_nodes_attached_to']) && $templateTask['data']['webform_nodes_attached_to'] != 'none') {
$webformNode = $templateTask['data']['webform_nodes_attached_to'];
}
}
$sid = MaestroEngine::getEntityIdentiferByUniqueID($this->processID, $taskUniqueSubmissionId);
$webform_submission = NULL;
if ($sid) {
$webform_submission = WebformSubmission::load($sid);
}
if ($webform_submission) {
if (isset($templateTask['data']['show_edit_form']) && $templateTask['data']['show_edit_form'] == 1) {
$url = Url::fromUserInput('/admin/structure/webform/manage/contact/submission/' . $sid . '/edit', [
'query' => [
'maestro' => 1,
'queueid' => $this->queueID,
],
]);
$response = new RedirectResponse($url
->toString());
return $response;
}
else {
$container = \Drupal::getContainer();
$webformSubmissionViewController = WebformSubmissionViewController::create($container);
$webform_build_view = $webformSubmissionViewController
->view($webform_submission, 'table', $langcode = NULL);
$form['submission_information'] = $webform_build_view['information'];
$form['submission_information']['#open'] = FALSE;
$form['submission_data'] = $webform_build_view['submission'];
}
}
else {
if ($sid !== FALSE && $sid !== NULL) {
\Drupal::messenger()
->addError(t('The submission attached to this workflow was unable to be fetched.'));
$form['status_messages'] = [
'#type' => 'status_messages',
'#weight' => -15,
];
}
else {
if ($webformNode && $webformNode !== FALSE) {
$url = Url::fromUserInput('/' . $webformNode, [
'query' => [
'maestro' => 1,
'queueid' => $this->queueID,
],
]);
}
else {
$url = Url::fromUserInput('/webform/' . $webformMachineName, [
'query' => [
'maestro' => 1,
'queueid' => $this->queueID,
],
]);
}
$response = new RedirectResponse($url
->toString());
return $response;
}
}
$form['queueID'] = [
'#type' => 'hidden',
'#title' => $this
->t('The queue ID of this task'),
'#default_value' => $this->queueID,
'#description' => $this
->t('queueID'),
];
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Accept'),
];
$form['actions']['reject'] = [
'#type' => 'submit',
'#value' => $this
->t('Reject'),
];
$form['#attached']['library'][] = 'maestro_webform/maestro-webform-css';
\Drupal::moduleHandler()
->invokeAll('maestro_webform_submission_form', [
$this->queueID,
&$form,
&$this,
]);
return $form;
}
public function handleExecuteSubmit(array &$form, FormStateInterface $form_state) {
$completeTask = TRUE;
$queueID = intval($form_state
->getValue('maestro_queue_id'));
$triggeringElement = $form_state
->getTriggeringElement();
$templateTask = MaestroEngine::getTemplateTaskByQueueID($queueID);
if (strstr($triggeringElement['#id'], 'edit-submit') !== FALSE && $queueID > 0) {
MaestroEngine::completeTask($queueID, \Drupal::currentUser()
->id());
}
else {
\Drupal::moduleHandler()
->invokeAll('maestro_webform_submission_set_cancel_completion_status', [
$queueID,
&$form,
&$form_state,
$triggeringElement,
&$completeTask,
]);
if ($completeTask) {
MaestroEngine::completeTask($queueID, \Drupal::currentUser()
->id());
MaestroEngine::setTaskStatus($queueID, TASK_STATUS_CANCEL);
}
}
if (isset($templateTask['data']['redirect_to']) && $templateTask['data']['redirect_to'] != '') {
$response = new TrustedRedirectResponse('/' . $templateTask['data']['redirect_to']);
$form_state
->setResponse($response);
}
else {
$response = new TrustedRedirectResponse('/taskconsole');
$form_state
->setResponse($response);
}
\Drupal::moduleHandler()
->invokeAll('maestro_webform_submission_form_submit', [
$queueID,
&$form,
&$form_state,
$triggeringElement,
]);
}
public function getTaskEditForm(array $task, $templateMachineName) {
$webforms = Webform::loadMultiple();
$webform_options = [
'' => $this
->t('Please Choose'),
];
foreach ($webforms as $key => $webform) {
$webform_options[$key] = $webform
->get('title');
}
$form['webform_machine_name'] = [
'#type' => 'select',
'#options' => $webform_options,
'#title' => $this
->t('Webform'),
'#description' => $this
->t('The Webform you wish to use when no previous submissions tagged with the Unique Identifier (next field) exist in the workflow.
If a submission exists in the workflow, this field is used to show the webform\'s output.'),
'#default_value' => isset($task['data']['webform_machine_name']) ? $task['data']['webform_machine_name'] : '',
'#required' => TRUE,
'#ajax' => [
'callback' => [
$this,
'fetchNodesAttached',
],
'event' => 'change',
'wrapper' => 'attached-nodes',
'progress' => [
'type' => 'throbber',
'message' => t('Fetching Nodes Attached...'),
],
],
];
$form['unique_id'] = [
'#type' => 'textfield',
'#title' => $this
->t('Unique Identifier'),
'#description' => $this
->t('The name of the key used when tracking the webform content for the maestro process. By default the identifier is "submission".'),
'#default_value' => isset($task['data']['unique_id']) ? $task['data']['unique_id'] : '',
'#required' => TRUE,
];
$form['show_edit_form'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Show the edit form if submission already exists?'),
'#description' => $this
->t('If a webform submission already exists, checking this option will send you to the edit form for the submission rather than the summary.'),
'#default_value' => isset($task['data']['show_edit_form']) ? $task['data']['show_edit_form'] : '0',
];
$form['use_nodes_attached'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Webform attached to a node?'),
'#description' => $this
->t('Is your webform attached to a node?
When checked, signals Maestro that your entry/edit of a webform depends on the node it is attached to.'),
'#default_value' => isset($task['data']['use_nodes_attached']) ? $task['data']['use_nodes_attached'] : '0',
];
$form['nodes_attached'] = [
'#type' => 'fieldset',
'#title' => $this
->t('Webform Attached to Node Settings'),
'#states' => [
'visible' => [
':input[name="use_nodes_attached"]' => [
'checked' => TRUE,
],
],
],
];
$variables = MaestroEngine::getTemplateVariables($templateMachineName);
$options = [];
$options['none'] = $this
->t('Do not use process variable');
foreach ($variables as $variableName => $arr) {
$options[$variableName] = $variableName;
}
$form['nodes_attached']['webform_nodes_attached_variable'] = [
'#type' => 'select',
'#options' => $options,
'#title' => $this
->t('Specify a variable to use that holds a Node ID.'),
'#description' => $this
->t('The variable selected must be populated by a single Node ID which the chosen webform is attached to.'),
'#default_value' => isset($task['data']['webform_nodes_attached_variable']) ? $task['data']['webform_nodes_attached_variable'] : '',
'#required' => FALSE,
'#states' => [
'visible' => [
':input[name="use_nodes_attached"]' => [
'checked' => TRUE,
],
],
],
];
$options = $this
->_getAttachedNodeOptions(isset($task['data']['webform_machine_name']) ? $task['data']['webform_machine_name'] : '');
$form['nodes_attached']['webform_nodes_attached_to'] = [
'#type' => 'select',
'#title' => $this
->t('Select a Node Attached To Specified Webform'),
'#description' => $this
->t('Enabled when you choose NOT to use a process variable. Listed are the nodes that use the specified webform.'),
'#states' => [
'enabled' => [
':input[name="webform_nodes_attached_variable"]' => [
'value' => 'none',
],
],
],
'#options' => $options,
'#default_value' => isset($task['data']['webform_nodes_attached_to']) ? $task['data']['webform_nodes_attached_to'] : 'none',
'#required' => FALSE,
'#prefix' => '<div id="attached-nodes">',
'#suffix' => '</div>',
];
$form['skip_webform_handlers'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Skip Any Maestro Webform Submission Handlers'),
'#description' => $this
->t('Maestro allows you to spawn a workflow via a Webform submission handler. Check to bypass any Maestro webform submission handlers attached to the webform chosen.'),
'#default_value' => isset($task['data']['skip_webform_handlers']) ? $task['data']['skip_webform_handlers'] : '0',
];
$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,
];
return $form;
}
public function fetchNodesAttached(array &$form, FormStateInterface $form_state) {
$options = $this
->_getAttachedNodeOptions($form_state
->getValue('webform_machine_name'));
$elem = [
'#type' => 'select',
'#options' => $options,
'#title' => $this
->t('Nodes Attached To selected webform'),
'#description' => $this
->t('Enabled when you choose NOT to use a process variable. Listed are the nodes that use the specified webform.'),
'#description_display' => 'after',
'#states' => [
'enabled' => [
':input[name="webform_nodes_attached_variable"]' => [
'value' => 'none',
],
],
],
'#required' => FALSE,
'#prefix' => '<div id="attached-nodes">',
'#suffix' => '</div>',
];
$renderer = \Drupal::service('renderer');
$response = new AjaxResponse();
$response
->addCommand(new ReplaceCommand('#attached-nodes', $renderer
->render($elem)));
return $response;
}
public function validateTaskEditForm(array &$form, FormStateInterface $form_state) {
}
public function prepareTaskForSave(array &$form, FormStateInterface $form_state, array &$task) {
$task['data']['unique_id'] = $form_state
->getValue('unique_id');
$task['data']['webform_machine_name'] = $form_state
->getValue('webform_machine_name');
$task['data']['modal'] = 'notmodal';
$task['data']['skip_webform_handlers'] = $form_state
->getValue('skip_webform_handlers');
$task['data']['webform_nodes_attached_to'] = $form_state
->getValue('webform_nodes_attached_to');
$task['data']['use_nodes_attached'] = $form_state
->getValue('use_nodes_attached');
$task['data']['webform_nodes_attached_variable'] = $form_state
->getValue('webform_nodes_attached_variable');
$task['data']['redirect_to'] = $form_state
->getValue('redirect_to');
$task['data']['show_edit_form'] = $form_state
->getValue('show_edit_form');
}
public function performValidityCheck(array &$validation_failure_tasks, array &$validation_information_tasks, array $task) {
}
public function getTemplateBuilderCapabilities() {
return [
'edit',
'drawlineto',
'removelines',
'remove',
];
}
protected function _getAttachedNodeOptions($webform_machine_name) {
$options = [];
if ($webform_machine_name != '') {
$field_configs = \Drupal::entityTypeManager()
->getStorage('field_config')
->loadByProperties([
'entity_type' => 'node',
]);
$node_types = NodeType::loadMultiple();
$ntypes = [];
$fnames = [];
foreach ($field_configs as $field_config) {
if ($field_config
->get('field_type') === 'webform') {
$bundle = $field_config
->get('bundle');
$ntypes[$bundle] = $node_types[$bundle];
$field_name = $field_config
->get('field_name');
$fnames[$field_name] = $field_name;
}
}
if (count($fnames) > 0) {
$query = \Drupal::entityTypeManager()
->getStorage('node')
->getQuery();
$or = $query
->orConditionGroup();
foreach ($fnames as $field_name) {
$or
->condition($field_name . '.target_id', $webform_machine_name);
}
$query
->condition($or);
$result = $query
->execute();
$entities = \Drupal::entityTypeManager()
->getStorage('node')
->loadMultiple($result);
foreach ($entities as $id => $node) {
$options['node/' . $id] = $node
->label();
}
}
}
if (count($options)) {
$options = array_merge([
'none' => $this
->t('Not Selected'),
], $options);
}
else {
$options['none'] = $this
->t('No nodes attach this webform');
}
return $options;
}
}