You are here

public function MaestroTemplateBuilderForm::removeLines in Maestro 3.x

Same name and namespace in other branches
  1. 8.2 modules/maestro_template_builder/src/Form/MaestroTemplateBuilderForm.php \Drupal\maestro_template_builder\Form\MaestroTemplateBuilderForm::removeLines()

Ajax callback to remove the lines pointing to and from a task.

File

modules/maestro_template_builder/src/Form/MaestroTemplateBuilderForm.php, line 206

Class

MaestroTemplateBuilderForm

Namespace

Drupal\maestro_template_builder\Form

Code

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;

  // We now have $templateTask that needs the 'nextstep' parameter cleared.
  $tasks[$taskToRemoveLines]['nextstep'] = '';
  $tasks[$taskToRemoveLines]['nextfalsestep'] = '';

  // Now to remove this task from the tasks that point to it.
  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);

    // How about false branches now.
    $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;
}