You are here

public function MaestroEngine::newProcess in Maestro 8.2

Same name and namespace in other branches
  1. 3.x src/Engine/MaestroEngine.php \Drupal\maestro\Engine\MaestroEngine::newProcess()

NewProcess Creates a new process in the Maestro content entities based on the template name which is mandatory. This method will only create a new process if the template has the validated property set.

Parameters

string $templateName: Machine name of the template you wish to kick off.

string $startTask: The offset starting task you wish to use as your first task. Default is 'start'.

Return value

int|bool Returns the process ID if the engine has started the process. FALSE if there was an issue. Please use === to ensure that you are testing for FALSE and not 0 as there may be other issues with the save.

File

src/Engine/MaestroEngine.php, line 1222

Class

MaestroEngine
Class MaestroEngine.

Namespace

Drupal\maestro\Engine

Code

public function newProcess($templateName, $startTask = 'start') {

  // Pessimistic return.
  $process_id = FALSE;
  $template = $this
    ->getTemplate($templateName);
  if (!isset($template->validated) || $template->validated == FALSE) {
    if ($this->debug) {
      \Drupal::messenger()
        ->addError(t('This template has not been validated.  You must validate before launching.'));
    }
    return FALSE;
  }
  if ($template !== NULL) {
    $values = [
      'process_name' => $template->label,
      'template_id' => $template->id,
      'complete' => 0,
      'initiator_uid' => \Drupal::currentUser()
        ->id(),
    ];
    $new_process = \Drupal::entityTypeManager()
      ->getStorage('maestro_process')
      ->create($values);
    $new_process
      ->save();
    if ($new_process
      ->id()) {

      // The process has been kicked off and we're ready to add the particulars to the queue and variables.
      $process_id = $new_process
        ->id();

      // Now to add variables.
      $variables = $template->variables;
      foreach ($variables as $variable) {
        $values = [
          'process_id' => $process_id,
          'variable_name' => $variable['variable_id'],
          'variable_value' => $variable['variable_value'],
        ];

        // Handle any mandatory variable presetting here.
        switch ($variable['variable_id']) {
          case 'initiator':
            $values['variable_value'] = \Drupal::currentUser()
              ->getAccountName();
            break;

          // Pull from the workflow template and populate the variable.
          case 'workflow_timeline_stage_count':
            $values['variable_value'] = $template->default_workflow_timeline_stage_count;
            break;

          // Starting of any process is step 0.
          case 'workflow_current_stage':
            $values['variable_value'] = 0;
            break;

          // Blank out the current stage/status message.
          case 'workflow_current_stage_message':
            $values['variable_value'] = '';
            break;
        }
        $new_var = \Drupal::entityTypeManager()
          ->getStorage('maestro_process_variables')
          ->create($values);
        $new_var
          ->save();
        if (!$new_var
          ->id()) {

          // Throw a maestro exception
          // completion should technically end here for this initiation.
          throw new MaestroSaveEntityException('maestro_process_variable', $variable['variable_id'] . ' failed saving during new process creation.');
        }
      }

      // Now to add the process status and stage information
      // we do this by looping through the tasks and creating an array of values for which we then set
      // the maestro_process_status entity.
      $status_message_array = [];
      foreach ($template->tasks as $task) {

        // Relates to the checkbox on the task editor.
        if (isset($task['participate_in_workflow_status_stage']) && $task['participate_in_workflow_status_stage'] == 1) {
          if (isset($task['workflow_status_stage_number'])) {
            $status_message_array[$task['workflow_status_stage_number']] = $task['workflow_status_stage_message'];
          }
        }
      }
      if (count($status_message_array)) {

        // We have status messages to set.
        foreach ($status_message_array as $stage_number => $stage_message) {
          $values = [
            'process_id' => $process_id,
            'stage_number' => $stage_number,
            'stage_message' => $stage_message,
          ];
          $new_stage = \Drupal::entityTypeManager()
            ->getStorage('maestro_process_status')
            ->create($values);
          $new_stage
            ->save();
          if (!$new_stage
            ->id()) {

            // Throw a maestro exception
            // completion should technically end here for this initiation.
            throw new MaestroSaveEntityException('maestro_process_status', 'Stage ' . $variable['stage_number'] . ' status message failed saving during new process creation.');
          }
        }
      }

      // Now to add the initiating task.
      $start_task = $template->tasks[$startTask];
      if (is_array($start_task)) {
        $values = [
          'process_id' => $process_id,
          'task_class_name' => $start_task['tasktype'],
          'task_id' => $start_task['id'],
          'task_label' => $start_task['label'],
          'engine_version' => 2,
          'is_interactive' => $start_task['assignto'] == 'engine' ? 0 : 1,
          'show_in_detail' => isset($start_task['showindetail']) ? $start_task['showindetail'] : 0,
          'handler' => isset($start_task['handler']) ? $start_task['handler'] : '',
          'task_data' => isset($start_task['data']) ? $start_task['data'] : '',
          'status' => 0,
          'run_once' => isset($start_task['runonce']) ? $start_task['runonce'] : 0,
          'uid' => \Drupal::currentUser()
            ->id(),
          'archived' => 0,
          'started_date' => time(),
        ];
        $queue = \Drupal::entityTypeManager()
          ->getStorage('maestro_queue')
          ->create($values);
        $queue
          ->save();
        if ($queue
          ->id()) {

          // Successful queue insertion
          // do any assignments here.
          $this
            ->productionAssignments($templateName, $startTask, $queue
            ->id());
        }
        else {

          // Throw a maestro exception.
          throw new MaestroSaveEntityException('maestro_queue', $start_task['tasktype'] . ' failed saving new task during new process creation.');
        }
      }
      else {

        // We have an issue here.  Throw some sort of exception that we can catch.
        // for now, ignore this case.
        throw new MaestroGeneralException('Start task for template ' . $template->id . ' may be corrupt.');
      }
    }
  }
  return $process_id;
}