You are here

public function UpgradeStatusForm::exportReport in Upgrade Status 8.2

Same name and namespace in other branches
  1. 8.3 src/Form/UpgradeStatusForm.php \Drupal\upgrade_status\Form\UpgradeStatusForm::exportReport()
  2. 8 src/Form/UpgradeStatusForm.php \Drupal\upgrade_status\Form\UpgradeStatusForm::exportReport()

Export generator.

Parameters

array $selected: Selected projects from the form.

string $format: The format of export to do: html or ascii.

Return value

\Symfony\Component\HttpFoundation\Response Response object for this export.

2 calls to UpgradeStatusForm::exportReport()
UpgradeStatusForm::exportReportASCII in src/Form/UpgradeStatusForm.php
Form submission handler.
UpgradeStatusForm::exportReportHTML in src/Form/UpgradeStatusForm.php
Form submission handler.

File

src/Form/UpgradeStatusForm.php, line 860

Class

UpgradeStatusForm

Namespace

Drupal\upgrade_status\Form

Code

public function exportReport(array $selected, string $format) {
  $extensions = [];
  $projects = $this->projectCollector
    ->collectProjects();
  foreach ([
    'custom',
    'contrib',
  ] as $type) {
    $states = [
      'uninstalled',
      'installed',
    ];
    foreach ($states as $state) {
      if (!empty($selected[$type]['data'][$state])) {
        foreach ($selected[$type]['data'][$state] as $project => $checked) {
          if ($checked !== 0) {

            // If the checkbox was checked, add it to the list.
            $extensions[$type][$project] = $format == 'html' ? $this->resultFormatter
              ->formatResult($projects[$type][$project]) : $this->resultFormatter
              ->formatAsciiResult($projects[$type][$project]);
          }
        }
      }
    }
  }
  if (empty($extensions)) {
    $this
      ->messenger()
      ->addError('No projects selected to export.');
    return;
  }
  $build = [
    '#theme' => 'upgrade_status_' . $format . '_export',
    '#projects' => $extensions,
  ];
  $fileDate = $this->resultFormatter
    ->formatDateTime(0, 'html_datetime');
  $extension = $format == 'html' ? '.html' : '.txt';
  $filename = 'upgrade-status-export-' . $fileDate . $extension;
  $response = new Response($this->renderer
    ->renderRoot($build));
  $response->headers
    ->set('Content-Disposition', 'attachment; filename="' . $filename . '"');
  return $response;
}