You are here

protected function UpgradeStatusForm::buildResultSummary in Upgrade Status 8.3

Build a result summary table for quick overview display to users.

Parameters

bool|null $environment_status: The status of the environment. Whether to put it into the Fix or Relax columns or omit it.

Return value

array Render array.

1 call to UpgradeStatusForm::buildResultSummary()
UpgradeStatusForm::buildForm in src/Form/UpgradeStatusForm.php
Form constructor.

File

src/Form/UpgradeStatusForm.php, line 529

Class

UpgradeStatusForm

Namespace

Drupal\upgrade_status\Form

Code

protected function buildResultSummary($environment_status = TRUE) {
  $projects = $this->projectCollector
    ->collectProjects();
  $next_steps = $this->projectCollector
    ->getNextStepInfo();
  $last = $this->state
    ->get('update.last_check') ?: 0;
  if ($last == 0) {
    $last_checked = $this
      ->t('Never checked');
  }
  else {
    $time = $this->dateFormatter
      ->formatTimeDiffSince($last);
    $last_checked = $this
      ->t('Last checked @time ago', [
      '@time' => $time,
    ]);
  }
  $update_time = [
    [
      '#type' => 'link',
      '#title' => $this
        ->t('Check available updates'),
      '#url' => Url::fromRoute('update.manual_status', [], [
        'query' => $this->destination
          ->getAsArray(),
      ]),
    ],
    [
      '#type' => 'markup',
      '#markup' => ' (' . $last_checked . ')',
    ],
  ];
  $header = [
    ProjectCollector::SUMMARY_ANALYZE => [
      'data' => $this
        ->t('Gather data'),
      'class' => 'summary-' . ProjectCollector::SUMMARY_ANALYZE,
    ],
    ProjectCollector::SUMMARY_ACT => [
      'data' => $this
        ->t('Fix incompatibilities'),
      'class' => 'status-' . ProjectCollector::SUMMARY_ACT,
    ],
    ProjectCollector::SUMMARY_RELAX => [
      'data' => $this
        ->t('Relax'),
      'class' => 'status-' . ProjectCollector::SUMMARY_RELAX,
    ],
  ];
  $build = [
    '#type' => 'table',
    '#attributes' => [
      'class' => [
        'upgrade-status-overview',
      ],
    ],
    '#header' => $header,
    '#rows' => [
      [
        'data' => [
          ProjectCollector::SUMMARY_ANALYZE => [
            'data' => [],
          ],
          ProjectCollector::SUMMARY_ACT => [
            'data' => [],
          ],
          ProjectCollector::SUMMARY_RELAX => [
            'data' => [],
          ],
        ],
      ],
    ],
  ];
  foreach ($header as $key => $value) {
    $cell_data = $cell_items = [];
    foreach ($next_steps as $next_step => $step_label) {

      // If this next step summary belongs in this table cell, collect it.
      if ($step_label[2] == $key) {
        foreach ($projects as $name => $project) {
          if ($project->info['upgrade_status_next'] == $next_step) {
            @$cell_data[$next_step]++;
          }
        }
      }
    }
    if ($key == ProjectCollector::SUMMARY_ANALYZE) {

      // If neither Composer Deploy nor Git Deploy are available and installed, suggest installing one.
      if (empty($projects['git_deploy']->status) && empty($projects['composer_deploy']->status)) {
        $cell_items[] = [
          '#markup' => $this
            ->t('Install <a href=":composer_deploy">Composer Deploy</a> or <a href=":git_deploy">Git Deploy</a> as appropriate for accurate update recommendations', [
            ':composer_deploy' => 'https://drupal.org/project/composer_deploy',
            ':git_deploy' => 'https://drupal.org/project/git_deploy',
          ]),
        ];
      }

      // Add available update info.
      $cell_items[] = $update_time;
    }
    if ($key == ProjectCollector::SUMMARY_ACT && !is_null($environment_status) && !$environment_status) {
      $cell_items[] = [
        '#markup' => '<a href="#edit-environment" class="upgrade-status-summary-label">' . $this
          ->t('Environment is incompatible') . '</a>',
      ];
    }
    if (count($cell_data)) {
      foreach ($cell_data as $next_step => $count) {
        $cell_items[] = [
          '#markup' => '<a href="#edit-' . $next_step . '" class="upgrade-status-summary-label upgrade-status-summary-label-' . $next_step . '">' . $this
            ->formatPlural($count, '@type: 1 project', '@type: @count projects', [
            '@type' => $next_steps[$next_step][0],
          ]) . '</a>',
        ];
      }
    }
    if ($key == ProjectCollector::SUMMARY_ANALYZE) {
      $cell_items[] = [
        '#markup' => 'Select any of the projects to rescan as needed below',
      ];
    }
    if ($key == ProjectCollector::SUMMARY_RELAX) {

      // Calculate how done is this site assuming the environment as
      // "one project" for simplicity.
      $done_count = (!empty($cell_data[ProjectCollector::NEXT_RELAX]) ? $cell_data[ProjectCollector::NEXT_RELAX] : 0) + (int) $environment_status;
      $percent = round($done_count / (count($projects) + 1) * 100);
      $build['#rows'][0]['data'][$key]['data'][] = [
        '#type' => 'markup',
        '#allowed_tags' => [
          'svg',
          'path',
          'text',
        ],
        '#markup' => <<<MARKUP
        <div class="upgrade-status-result-chart">
        <svg viewBox="0 0 36 36" class="upgrade-status-result-circle">
          <path class="circle-bg"
            d="M18 2.0845
              a 15.9155 15.9155 0 0 1 0 31.831
              a 15.9155 15.9155 0 0 1 0 -31.831"
          />
          <path class="circle"
            stroke-dasharray="{<span class="php-variable">$percent</span>}, 100"
            d="M18 2.0845
              a 15.9155 15.9155 0 0 1 0 31.831
              a 15.9155 15.9155 0 0 1 0 -31.831"
          />
          <text x="18" y="20.35" class="percentage">{<span class="php-variable">$percent</span>}%</text>
        </svg>
      </div>
MARKUP
,
      ];
      if (!empty($environment_status)) {
        $cell_items[] = [
          '#markup' => '<a href="#edit-environment" class="upgrade-status-summary-label">' . $this
            ->t('Environment checks passed') . '</a>',
        ];
      }
    }
    if (count($cell_items)) {
      $build['#rows'][0]['data'][$key]['data'][] = [
        '#theme' => 'item_list',
        '#items' => $cell_items,
      ];
    }
    else {
      $build['#rows'][0]['data'][$key]['data'][] = [
        '#type' => 'markup',
        '#markup' => $this
          ->t('N/A'),
      ];
    }
  }
  return $build;
}