You are here

public function MaestroProcessStatusBlock::build in Maestro 3.x

Same name and namespace in other branches
  1. 8.2 src/Plugin/Block/MaestroProcessStatusBlock.php \Drupal\maestro\Plugin\Block\MaestroProcessStatusBlock::build()

Builds and returns the renderable array for this block plugin.

If a block should not be rendered because it has no content, then this method must also ensure to return no content: it must then only return an empty array, or an empty array with #cache set (with cacheability metadata indicating the circumstances for it being empty).

Return value

array A renderable array representing the content of the block.

Overrides BlockPluginInterface::build

See also

\Drupal\block\BlockViewBuilder

File

src/Plugin/Block/MaestroProcessStatusBlock.php, line 75

Class

MaestroProcessStatusBlock
Provides a Maestro block that shows the linear status output for processes this user belongs to or in general.

Namespace

Drupal\maestro\Plugin\Block

Code

public function build() {
  $conf = $this
    ->getConfiguration();
  $processID = intval(\Drupal::request()->query
    ->get('process_id', 0));
  $usr = \Drupal::currentUser();
  if ($usr
    ->id()) {
    if ($conf['maestro_skip_execute_check'] == 0) {
      $query = \Drupal::database()
        ->select('maestro_production_assignments', 'a');
      $query
        ->join('maestro_queue', 'b', 'a.queue_id = b.id');
      $query
        ->join('maestro_process', 'c', 'b.process_id = c.process_id');
      $query
        ->leftJoin('maestro_process_variables', 'd', 'c.process_id = d.process_id');
      $andConditionGroup1 = $query
        ->andConditionGroup();
      $andConditionGroup1
        ->condition('a.assign_id', $usr
        ->getAccountName());
      $andConditionGroup1
        ->condition('b.status', '0');
      if (isset($conf['maestro_filter_process_names']) && $conf['maestro_filter_process_names'] != '') {
        $filter = str_replace('[uid]', $usr
          ->id(), $conf['maestro_filter_process_names']);
        $filter = str_replace('[username]', $usr
          ->getAccountName(), $filter);
        $andConditionGroup1
          ->condition('c.process_name', '%' . $query
          ->escapeLike($filter) . '%', 'LIKE');
      }
      if ($processID > 0) {
        $andConditionGroup1
          ->condition('c.process_id', intval($processID));
      }
      $andConditionGroup2 = $query
        ->andConditionGroup();
      $andConditionGroup2
        ->condition('d.variable_name', 'initiator');
      $andConditionGroup2
        ->condition('d.variable_value', $usr
        ->getAccountName());
      $orConditionGroup = $query
        ->orConditionGroup();
      $orConditionGroup
        ->condition($andConditionGroup1);
      $orConditionGroup
        ->condition($andConditionGroup2);
      $query
        ->condition($orConditionGroup);
      $query
        ->addField('b', 'process_id');
      $query
        ->addField('c', 'process_name');
      $query
        ->groupBy('b.process_id');
      $query
        ->groupBy('c.process_name');
    }
    else {
      $query = \Drupal::database()
        ->select('maestro_queue', 'b');
      $query
        ->join('maestro_process', 'c', 'b.process_id = c.process_id');
      $query
        ->leftJoin('maestro_process_variables', 'd', 'c.process_id = d.process_id');
      $andConditionGroup1 = $query
        ->andConditionGroup();
      $andConditionGroup1
        ->condition('b.status', '0');
      if (isset($conf['maestro_filter_process_names']) && $conf['maestro_filter_process_names'] != '') {
        $filter = str_replace('[uid]', $usr
          ->id(), $conf['maestro_filter_process_names']);
        $filter = str_replace('[username]', $usr
          ->getAccountName(), $filter);
        $andConditionGroup1
          ->condition('c.process_name', '%' . $query
          ->escapeLike($filter) . '%', 'LIKE');
      }
      if ($processID > 0) {
        $andConditionGroup1
          ->condition('c.process_id', intval($processID));
      }
      $andConditionGroup2 = $query
        ->andConditionGroup();
      $andConditionGroup2
        ->condition('d.variable_name', 'initiator');
      $andConditionGroup2
        ->condition('d.variable_value', $usr
        ->getAccountName());
      $orConditionGroup = $query
        ->orConditionGroup();
      $orConditionGroup
        ->condition($andConditionGroup1);
      $orConditionGroup
        ->condition($andConditionGroup2);
      $query
        ->condition($orConditionGroup);
      $query
        ->addField('b', 'process_id');
      $query
        ->addField('c', 'process_name');
      $query
        ->groupBy('b.process_id');
      $query
        ->groupBy('c.process_name');
    }
    $result = $query
      ->execute();
    $output = [];
    foreach ($result as $row) {
      $status_bar = MaestroStatus::getMaestroStatusBar($row->process_id, 0, TRUE);
      $templateName = MaestroEngine::getTemplateIdFromProcessId($row->process_id);
      $process_name = $row->process_name;
      $link_start = '';
      $link_end = '';
      if (isset($conf['maestro_provide_link']) && $conf['maestro_provide_link'] != '') {
        $interim_link = str_replace('[uid]', $usr
          ->id(), $conf['maestro_provide_link']);
        $interim_link = str_replace('[username]', $usr
          ->getAccountName(), $interim_link);
        $link_start = '<a href="' . $interim_link . '" title="' . $this
          ->t($this->configuration['maestro_link_tooltip']) . '">';
        $link_end = '</a>';
      }
      $output['status'][] = [
        '#prefix' => $link_start . '<div id="processid-' . $row->process_id . '" class="maestro-block-process ' . $templateName . '">
            <div class="maestro-block-process-title">' . $process_name . '</div>',
        '#suffix' => '</div>' . $link_end,
        '#markup' => $status_bar['status_bar']['#children'],
      ];
    }
    $output['#attached']['library'][] = 'maestro/maestro-engine-css';

    // Keeping the cache lines commented for now to ensure caching for this block is indeed disabled
    // $output['#cache'] = ['contexts' => ['url.query_args:process_id']];  //don't cache the process_id url parameter
    // $output['#cache']['max-age'] = 0;  //just disable the cache completely for this module.
    return $output;
  }
}