View source
<?php
namespace Drupal\maestro_template_builder\Form;
use Drupal\Core\Url;
use Drupal\Component\Serialization\Json;
use Drupal\maestro\Engine\MaestroEngine;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\maestro_template_builder\Ajax\FireJavascriptCommand;
class MaestroTemplateBuilderForm extends FormBase {
public function getFormId() {
return 'template_builder';
}
public function validateForm(array &$form, FormStateInterface $form_state) {
}
public function submitForm(array &$form, FormStateInterface $form_state) {
}
public function editTask(array &$form, FormStateInterface $form_state) {
$_SESSION['maestro_template_builder']['maestro_editing_task'] = $form_state
->getValue('task_clicked');
$response = new AjaxResponse();
$response
->addCommand(new FireJavascriptCommand('maestroCloseTaskMenu', []));
$response
->addCommand(new FireJavascriptCommand('maestroEditTask', []));
return $response;
}
public function moveTaskComplete(array &$form, FormStateInterface $form_state) {
$taskMoved = $form_state
->getValue('task_clicked');
$top = $form_state
->getValue('task_top');
$left = $form_state
->getValue('task_left');
$templateMachineName = $form_state
->getValue('template_machine_name');
$template = MaestroEngine::getTemplate($templateMachineName);
$template->tasks[$taskMoved]['top'] = $top;
$template->tasks[$taskMoved]['left'] = $left;
$template
->save();
$response = new AjaxResponse();
$response
->addCommand(new FireJavascriptCommand('maestroNoOp', []));
return $response;
}
public function drawLineComplete(array &$form, FormStateInterface $form_state) {
$taskFrom = $form_state
->getValue('task_line_from');
$taskTo = $form_state
->getValue('task_line_to');
$templateMachineName = $form_state
->getValue('template_machine_name');
$template = MaestroEngine::getTemplate($templateMachineName);
$templateTaskFrom = MaestroEngine::getTemplateTaskByID($templateMachineName, $taskFrom);
$templateTaskTo = MaestroEngine::getTemplateTaskByID($templateMachineName, $taskTo);
$nextsteps = explode(',', $templateTaskFrom['nextstep']);
if (!array_search($taskTo, $nextsteps)) {
if ($templateTaskFrom['nextstep'] != '') {
$templateTaskFrom['nextstep'] .= ',';
}
$templateTaskFrom['nextstep'] .= $taskTo;
$template->tasks[$taskFrom]['nextstep'] = $templateTaskFrom['nextstep'];
$template->validated = FALSE;
$template
->save();
}
$response = new AjaxResponse();
$response
->addCommand(new FireJavascriptCommand('signalValidationRequired', []));
$response
->addCommand(new FireJavascriptCommand('maestroCloseTaskMenu', []));
return $response;
}
public function drawLineTo(array &$form, FormStateInterface $form_state) {
$taskFrom = $form_state
->getValue('task_clicked');
$templateMachineName = $form_state
->getValue('template_machine_name');
$template = MaestroEngine::getTemplate($templateMachineName);
$task = MaestroEngine::getTemplateTaskByID($templateMachineName, $taskFrom);
if ($task['tasktype'] == 'MaestroEnd') {
$response = new AjaxResponse();
$response
->addCommand(new FireJavascriptCommand('maestroSignalError', [
'message' => t('You are not able to draw a line FROM an end task!'),
]));
return $response;
}
$response = new AjaxResponse();
$response
->addCommand(new FireJavascriptCommand('maestroDrawLineTo', [
'taskid' => $taskFrom,
]));
$response
->addCommand(new FireJavascriptCommand('maestroCloseTaskMenu', []));
return $response;
}
public function drawFalseLineTo(array &$form, FormStateInterface $form_state) {
$taskFrom = $form_state
->getValue('task_clicked');
$templateMachineName = $form_state
->getValue('template_machine_name');
$template = MaestroEngine::getTemplate($templateMachineName);
$task = MaestroEngine::getTemplateTaskByID($templateMachineName, $taskFrom);
if ($task['tasktype'] == 'MaestroEnd') {
$response = new AjaxResponse();
$response
->addCommand(new FireJavascriptCommand('maestroSignalError', [
'message' => t('You are not able to draw a line FROM an end task!'),
]));
return $response;
}
$response = new AjaxResponse();
$response
->addCommand(new FireJavascriptCommand('maestroDrawFalseLineTo', [
'taskid' => $taskFrom,
]));
$response
->addCommand(new FireJavascriptCommand('maestroCloseTaskMenu', []));
return $response;
}
public function drawFalseLineComplete(array &$form, FormStateInterface $form_state) {
$taskFrom = $form_state
->getValue('task_line_from');
$taskTo = $form_state
->getValue('task_line_to');
$templateMachineName = $form_state
->getValue('template_machine_name');
$template = MaestroEngine::getTemplate($templateMachineName);
$templateTaskFrom = MaestroEngine::getTemplateTaskByID($templateMachineName, $taskFrom);
$templateTaskTo = MaestroEngine::getTemplateTaskByID($templateMachineName, $taskTo);
$nextsteps = explode(',', $templateTaskFrom['nextfalsestep']);
if (!array_search($taskTo, $nextsteps)) {
if ($templateTaskFrom['nextfalsestep'] != '') {
$templateTaskFrom['nextfalsestep'] .= ',';
}
$templateTaskFrom['nextfalsestep'] .= $taskTo;
$template->tasks[$taskFrom]['nextfalsestep'] = $templateTaskFrom['nextfalsestep'];
$template->validated = FALSE;
$template
->save();
}
$response = new AjaxResponse();
$response
->addCommand(new FireJavascriptCommand('signalValidationRequired', []));
$response
->addCommand(new FireJavascriptCommand('maestroCloseTaskMenu', []));
return $response;
}
public function removeTaskComplete(array &$form, FormStateInterface $form_state) {
$taskToRemove = $form_state
->getValue('task_clicked');
$templateMachineName = $form_state
->getValue('template_machine_name');
$response = new AjaxResponse();
if ($taskToRemove == 'start') {
$response
->addCommand(new FireJavascriptCommand('maestroSignalError', [
'message' => t('You are not able to delete a Start task'),
]));
return $response;
}
$returnValue = MaestroEngine::removeTemplateTask($templateMachineName, $taskToRemove);
if ($returnValue === FALSE) {
$response
->addCommand(new FireJavascriptCommand('maestroSignalError', [
'message' => t('There was an error removing the Task. Unable to Complete the removal'),
]));
return $response;
}
else {
MaestroEngine::setTemplateToUnvalidated($templateMachineName);
$response
->addCommand(new FireJavascriptCommand('signalValidationRequired', []));
$response
->addCommand(new FireJavascriptCommand('maestroRemoveTask', [
'task' => $taskToRemove,
]));
return $response;
}
}
public function removeLines(array &$form, FormStateInterface $form_state) {
$taskToRemoveLines = $form_state
->getValue('task_clicked');
$templateMachineName = $form_state
->getValue('template_machine_name');
$template = MaestroEngine::getTemplate($templateMachineName);
$templateTask = MaestroEngine::getTemplateTaskByID($templateMachineName, $taskToRemoveLines);
$pointers = MaestroEngine::getTaskPointersFromTemplate($templateMachineName, $taskToRemoveLines);
$tasks = $template->tasks;
$tasks[$taskToRemoveLines]['nextstep'] = '';
$tasks[$taskToRemoveLines]['nextfalsestep'] = '';
foreach ($pointers as $pointer) {
$nextsteps = explode(',', $tasks[$pointer]['nextstep']);
$key = array_search($taskToRemoveLines, $nextsteps);
if ($key !== FALSE) {
unset($nextsteps[$key]);
}
$tasks[$pointer]['nextstep'] = implode(',', $nextsteps);
$nextfalsesteps = explode(',', $tasks[$pointer]['nextfalsestep']);
$key = array_search($taskToRemoveLines, $nextfalsesteps);
if ($key !== FALSE) {
unset($nextfalsesteps[$key]);
}
$tasks[$pointer]['nextfalsestep'] = implode(',', $nextfalsesteps);
}
$template->tasks = $tasks;
$template
->save();
$response = new AjaxResponse();
$response
->addCommand(new FireJavascriptCommand('maestroRemoveTaskLines', [
'task' => $taskToRemoveLines,
]));
$response
->addCommand(new FireJavascriptCommand('maestroCloseTaskMenu', []));
return $response;
}
public function buildForm(array $form, FormStateInterface $form_state, $templateMachineName = '') {
$template = MaestroEngine::getTemplate($templateMachineName);
if ($template == NULL) {
$form = [
'#title' => t('Error!'),
'#markup' => t("The template you are attempting to add a task to doesn't exist"),
];
return $form;
}
$validated_css = 'maestro-template-validation-div-hide';
if (property_exists($template, 'validated') && !$template->validated || !property_exists($template, 'validated')) {
$validated_css = '';
}
$form = [
'#markup' => '<div id="maestro-template-error" class="messages messages--error"></div>
<div id="maestro-template-validation" class="maestro-template-validation-div messages messages--error ' . $validated_css . '">' . $this
->t('This template requires validation before it can be used.') . '</div>',
];
$height = $template->canvas_height;
$width = $template->canvas_width;
$taskColours = [
'MaestroStart' => '#00ff00',
'MaestroEnd' => '#ff0000',
'MaestroSetProcessVariable' => '#a0a0a0',
'MaestroTaskTypeIf' => 'orange',
'MaestroInteractive' => '#0000ff',
];
$tasks = [];
foreach ($template->tasks as $taskID => $task) {
$this_task = MaestroEngine::getPluginTask($task['tasktype']);
$capabilities = $this_task
->getTemplateBuilderCapabilities();
foreach ($capabilities as $key => $c) {
$capabilities[$key] = 'maestro_template_' . $c;
}
$tasks[] = [
'taskname' => $task['label'],
'type' => $task['tasktype'],
'uilabel' => $this
->t(str_replace('Maestro', '', $task['tasktype'])),
'id' => $task['id'],
'left' => $task['left'],
'top' => $task['top'],
'raphael' => '',
'to' => explode(',', $task['nextstep']),
'pointedfrom' => '',
'falsebranch' => explode(',', $task['nextfalsestep']),
'lines' => [],
'capabilities' => $capabilities,
'participate_in_workflow_status_stage' => isset($task['participate_in_workflow_status_stage']) ? $task['participate_in_workflow_status_stage'] : '',
'workflow_status_stage_number' => isset($task['workflow_status_stage_number']) ? $task['workflow_status_stage_number'] : '',
'workflow_status_stage_message' => isset($task['workflow_status_stage_message']) ? $this
->t('Status Message') . ': ' . $task['workflow_status_stage_message'] : '',
];
}
$taskColours = [];
$manager = \Drupal::service('plugin.manager.maestro_tasks');
$plugins = $manager
->getDefinitions();
foreach ($plugins as $key => $taskPlugin) {
$task = $manager
->createInstance($taskPlugin['id'], [
0,
0,
]);
$taskColours[$key] = $task
->getTaskColours();
}
$form['add_new_task'] = [
'#type' => 'link',
'#title' => $this
->t('add task'),
'#url' => Url::fromRoute('maestro_template_builder.add_new', [
'templateMachineName' => $templateMachineName,
]),
'#attributes' => [
'title' => $this
->t('Add Task to Template'),
'class' => [
'use-ajax',
'maestro-add-new-button',
'maestro-add-new-task-button',
],
'data-dialog-type' => 'modal',
'data-dialog-options' => Json::encode([
'width' => 700,
]),
],
];
$form['change_canvas_size'] = [
'#type' => 'link',
'#title' => $this
->t('canvas'),
'#url' => Url::fromRoute('maestro_template_builder.canvas', [
'templateMachineName' => $templateMachineName,
]),
'#attributes' => [
'title' => $this
->t('Change Canvas Size'),
'class' => [
'use-ajax',
'maestro-canvas-button',
],
'data-dialog-type' => 'modal',
'data-dialog-options' => Json::encode([
'width' => 400,
]),
],
];
$form['run_validity_check'] = [
'#type' => 'link',
'#title' => $this
->t('validity check'),
'#url' => Url::fromRoute('maestro_template_builder.maestro_run_validity_check', [
'templateMachineName' => $templateMachineName,
]),
'#attributes' => [
'title' => $this
->t('Perform Validity Check'),
'class' => [
'use-ajax',
'maestro-canvas-button',
],
'data-dialog-type' => 'modal',
'data-dialog-options' => Json::encode([
'width' => 700,
]),
],
];
$form['edit_template'] = [
'#type' => 'link',
'#title' => $this
->t('edit template'),
'#suffix' => '<div id="maestro_div_template" style="width:' . $width . 'px; height: ' . $height . 'px;"></div>',
'#url' => Url::fromRoute('entity.maestro_template.edit_form', [
'maestro_template' => $templateMachineName,
'is_modal' => 'modal',
]),
'#attributes' => [
'title' => $this
->t('Edit Template'),
'class' => [
'use-ajax',
'maestro-canvas-button',
],
'data-dialog-type' => 'modal',
'data-dialog-options' => Json::encode([
'width' => 700,
]),
],
];
$legend = '';
$legend_render_array = [
'#theme' => 'template_task_legend',
];
$legend = \Drupal::service('renderer')
->renderPlain($legend_render_array);
$form['task_legend'] = [
'#type' => 'details',
'#title' => $this
->t('Legend'),
'#markup' => $legend,
'#attributes' => [
'class' => [
'maestro-task-legend',
],
],
];
$form['template_machine_name'] = [
'#type' => 'hidden',
'#default_value' => $templateMachineName,
];
$form['menu'] = [
'#type' => 'fieldset',
'#title' => '',
'#attributes' => [
'class' => [
'maestro-popup-menu',
],
],
'#prefix' => '
<div id="maestro-task-menu" class="ui-dialog ui-widget ui-widget-content ui-corner-all ui-front">
<div class="ui-dialog-titlebar ui-widget-header ui-corner-all ui-helper-clearfix">
<span id="task-menu-title" class="ui-dialog-title">' . t('Task Menu') . '</span>
<span id="close-task-menu" class="ui-button-icon-primary ui-icon ui-icon-closethick"></span></div>',
'#suffix' => '</div>',
];
$form['menu']['task_clicked'] = [
'#type' => 'hidden',
];
$form['menu']['task_line_from'] = [
'#type' => 'hidden',
];
$form['menu']['task_line_to'] = [
'#type' => 'hidden',
];
$form['menu']['task_top'] = [
'#type' => 'hidden',
];
$form['menu']['task_left'] = [
'#type' => 'hidden',
];
$form['remove_task_complete'] = [
'#type' => 'submit',
'#value' => 'Remove',
'#ajax' => [
'callback' => [
$this,
'removeTaskComplete',
],
'wrapper' => '',
],
'#prefix' => '<div class="maestro_hidden_element">',
'#suffix' => '</div>',
];
$form['draw_line_complete'] = [
'#type' => 'submit',
'#value' => 'Submit Draw Line',
'#ajax' => [
'callback' => [
$this,
'drawLineComplete',
],
'wrapper' => '',
],
'#prefix' => '<div class="maestro_hidden_element">',
'#suffix' => '</div>',
];
$form['draw_false_line_complete'] = [
'#type' => 'submit',
'#value' => 'Submit False Draw Line',
'#ajax' => [
'callback' => [
$this,
'drawFalseLineComplete',
],
'wrapper' => '',
],
'#prefix' => '<div class="maestro_hidden_element">',
'#suffix' => '</div>',
];
$form['move_task_complete'] = [
'#type' => 'submit',
'#value' => 'Submit Task Move Coordinates',
'#ajax' => [
'callback' => [
$this,
'moveTaskComplete',
],
'wrapper' => '',
],
'#prefix' => '<div class="maestro_hidden_element">',
'#suffix' => '</div>',
];
$form['edit_task_complete'] = [
'#type' => 'link',
'#title' => 'Edit Task',
'#prefix' => '<div class="maestro_hidden_element">',
'#suffix' => '</div>',
'#url' => Url::fromRoute('maestro_template_builder.edit_task', [
'templateMachineName' => $templateMachineName,
]),
'#attributes' => [
'class' => [
'use-ajax',
],
'data-dialog-type' => 'modal',
'data-dialog-options' => Json::encode([
'width' => 700,
'height' => 500,
]),
],
];
$form['menu']['edit_this_task'] = [
'#type' => 'button',
'#value' => t('Edit Task'),
'#ajax' => [
'callback' => [
$this,
'editTask',
],
'wrapper' => '',
],
'#attributes' => [
'maestro_capabilities_id' => 'maestro_template_edit',
],
];
$form['menu']['draw_line_to'] = [
'#type' => 'button',
'#value' => t('Draw Line To'),
'#ajax' => [
'callback' => [
$this,
'drawLineTo',
],
'wrapper' => '',
],
'#attributes' => [
'maestro_capabilities_id' => 'maestro_template_drawlineto',
],
];
$form['menu']['draw_false_line_to'] = [
'#type' => 'button',
'#value' => t('Draw False Line To'),
'#ajax' => [
'callback' => [
$this,
'drawFalseLineTo',
],
'wrapper' => '',
],
'#attributes' => [
'maestro_capabilities_id' => 'maestro_template_drawfalselineto',
],
];
$form['menu']['remove_lines'] = [
'#type' => 'button',
'#value' => t('Remove Lines'),
'#ajax' => [
'callback' => [
$this,
'removeLines',
],
'wrapper' => '',
],
'#attributes' => [
'maestro_capabilities_id' => 'maestro_template_removelines',
],
];
$form['menu']['remove_task_link'] = [
'#type' => 'html_tag',
'#tag' => 'a',
'#value' => t('Remove Task'),
'#attributes' => [
'style' => 'margin-top: 20px;',
'onclick' => 'maestro_submit_form(event)',
'class' => [
'button',
],
'maestro_capabilities_id' => 'maestro_template_remove',
'id' => 'maestro_template_remove',
],
];
$form['#attached'] = [
'library' => [
'maestro_template_builder/maestrojs',
'maestro_template_builder/maestro_raphael',
'maestro_template_builder/maestro_tasks_css',
],
'drupalSettings' => [
'maestro' => $tasks,
'maestroTaskColours' => $taskColours,
'baseURL' => base_path(),
'canvasHeight' => $height,
'canvasWidth' => $width,
],
];
$form['#cache'] = [
'max-age' => 0,
];
$form['#prefix'] = '<div id="template-message-area-one" class="maestro-template-message-area messages messages--status"></div>';
$form['#suffix'] = '<div id="template-message-area-two" class="maestro-template-message-area messages messages--status"></div>';
return $form;
}
}