You are here

protected function MaestroEngine::productionAssignments in Maestro 8.2

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

Using the queueID, the task's machine name and the template machine name, we assign the task using the appropriate method defined in the template.

Parameters

string $templateMachineName: Machine name of the template.

string $taskID: Machine name of the task.

int $queueID: The ID of the queue entity this task belongs to.

2 calls to MaestroEngine::productionAssignments()
MaestroEngine::createProductionTask in src/Engine/MaestroEngine.php
Creates a task in the Maestro Queue table.
MaestroEngine::newProcess in src/Engine/MaestroEngine.php
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.

File

src/Engine/MaestroEngine.php, line 1667

Class

MaestroEngine
Class MaestroEngine.

Namespace

Drupal\maestro\Engine

Code

protected function productionAssignments($templateMachineName, $taskID, $queueID) {
  $task = $this
    ->getTemplateTaskByID($templateMachineName, $taskID);
  $executableTask = MaestroEngine::getPluginTask($task['tasktype']);
  $assigned = '';

  // For tasks that have not set the assignment as they're engine tasks.
  if (array_key_exists('assigned', $task)) {
    $assigned = $task['assigned'];
  }

  // If the assignment is blank ir set to engine, and it's not an interactive task this is an engine assignment.
  if (($assigned == '' || $assigned == 'engine') && !$executableTask
    ->isInteractive()) {
    return;
  }
  if ($assigned == '' && $executableTask
    ->isInteractive()) {

    // hmm.  this is a condition where the template says there's nobody assigned, yet the task says it's an interactive.
    // TODO:  Throw an error here?  Do an invoke to modules to see if they know why?
    // for now, we return and let it be assigned to the engine.
    return;
  }

  // The format of the assignment is as follows
  // towhat:by:who   example:  user:fixed:admin,role:variable:var_name.
  $assignments = explode(',', $assigned);
  foreach ($assignments as $assignment) {
    $thisAssignment = explode(':', $assignment);

    // [0] is user, role etc.  [1] is how such as fixed or by variable.  [2] is to who or which var
    // Assigned by fixed name.
    if ($thisAssignment[1] == 'fixed') {
      $values = [
        'queue_id' => $queueID,
        'assign_type' => $thisAssignment[0],
        'by_variable' => 0,
        'assign_id' => $thisAssignment[2],
        'process_variable' => 0,
        'assign_back_id' => 0,
        'task_completed' => 0,
      ];
      $prodAssignments = \Drupal::entityTypeManager()
        ->getStorage('maestro_production_assignments')
        ->create($values);
      $prodAssignments
        ->save();
    }
    elseif ($thisAssignment[1] == 'variable') {
      $var = MaestroEngine::getProcessVariable($thisAssignment[2], MaestroEngine::getProcessIdFromQueueId($queueID));
      $varID = MaestroEngine::getProcessVariableID($thisAssignment[2], MaestroEngine::getProcessIdFromQueueId($queueID));

      // Now to use the information supplied to us from [0] to determine is this a user or role and then also let other modules do their own thing here.
      $assignmentsByVar = explode(',', $var);
      foreach ($assignmentsByVar as $assignTo) {
        if ($assignTo != '') {
          $values = [
            'queue_id' => $queueID,
            'assign_type' => $thisAssignment[0],
            'by_variable' => 1,
            'assign_id' => $assignTo,
            'process_variable' => $varID,
            'assign_back_id' => 0,
            'task_completed' => 0,
          ];
          $prodAssignments = \Drupal::entityTypeManager()
            ->getStorage('maestro_production_assignments')
            ->create($values);
          $prodAssignments
            ->save();
        }
      }
    }
  }

  // And now we do a module invoke for any add-on modules to do their own assignments or tweak the assignments.
  \Drupal::moduleHandler()
    ->invokeAll('maestro_post_production_assignments', [
    $templateMachineName,
    $taskID,
    $queueID,
  ]);
}