You are here

public function GdprTasksSarWorker::build in General Data Protection Regulation 8

Same name and namespace in other branches
  1. 8.2 modules/gdpr_tasks/src/Plugin/QueueWorker/GdprTasksSarWorker.php \Drupal\gdpr_tasks\Plugin\QueueWorker\GdprTasksSarWorker::build()
  2. 3.0.x modules/gdpr_tasks/src/Plugin/QueueWorker/GdprTasksSarWorker.php \Drupal\gdpr_tasks\Plugin\QueueWorker\GdprTasksSarWorker::build()

Build the export files.

Parameters

\Drupal\gdpr_tasks\Entity\TaskInterface $task: The task.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

\Drupal\Core\Entity\EntityStorageException

2 calls to GdprTasksSarWorker::build()
GdprTasksSarWorker::initialise in modules/gdpr_tasks/src/Plugin/QueueWorker/GdprTasksSarWorker.php
Initialise our request.
GdprTasksSarWorker::processItem in modules/gdpr_tasks/src/Plugin/QueueWorker/GdprTasksSarWorker.php
Works on a single queue item.

File

modules/gdpr_tasks/src/Plugin/QueueWorker/GdprTasksSarWorker.php, line 248

Class

GdprTasksSarWorker
Processes SARs tasks when data processing is required.

Namespace

Drupal\gdpr_tasks\Plugin\QueueWorker

Code

public function build(TaskInterface $task) {

  /* @var \Drupal\file\Plugin\Field\FieldType\FileFieldItemList $field */
  $field = $task
    ->get('sar_export');

  /* @var \Drupal\Core\Field\FieldDefinitionInterface $field_definition */
  $field_definition = $field
    ->getFieldDefinition();
  $settings = $field_definition
    ->getSettings();
  $config = [
    'field_definition' => $field_definition,
    'name' => $field
      ->getName(),
    'parent' => $field
      ->getParent(),
  ];

  /* @var \Drupal\file\Plugin\Field\FieldType\FileItem $field_type */
  $field_type = $this->fieldTypePluginManager
    ->createInstance($field_definition
    ->getType(), $config);

  // Prepare destination.
  $directory = $field_type
    ->getUploadLocation();
  $directory .= '/' . basename($field->entity->uri->value, '.zip');

  // Gather our entities.
  // @todo: Move this inline.
  $rtaTraversal = $this->rtaTraversal
    ->getTraversal($task
    ->getOwner());
  $rtaTraversal
    ->traverse();
  $all_data = $rtaTraversal
    ->getResults();

  // Build our export files.
  $csvs = [];
  foreach ($all_data as $plugin_id => $data) {
    if ($plugin_id == '_assets') {
      $task->sar_export_assets = $data;
      continue;
    }

    // Build the headers if required.
    if (!isset($csvs[$data['file']]['_header'][$data['plugin_name']])) {
      $csvs[$data['file']]['_header'][$data['plugin_name']] = $data['label'];
    }

    // Initialise and fill out the row to make sure things come in a
    // consistent order.
    if (!isset($csvs[$data['file']][$data['row_id']])) {
      $csvs[$data['file']][$data['row_id']] = [];
    }
    $csvs[$data['file']][$data['row_id']] += array_fill_keys(array_keys($csvs[$data['file']]['_header']), '');

    // Put our piece of information in place.
    $csvs[$data['file']][$data['row_id']][$data['plugin_name']] = $data['value'];
  }

  // Gather existing files.
  $files = [];
  if (!empty($task->sar_export_parts)) {
    foreach ($task->sar_export_parts as $item) {
      $filename = basename($item->entity->uri->value, '.csv');
      $files[$filename] = $item->entity;
    }
  }

  // Write our CSV files.
  foreach ($csvs as $filename => $data) {
    if (!isset($files[$filename])) {

      // Create an empty file.
      $file = _gdpr_tasks_file_save_data('', $task
        ->getOwner(), "{$directory}/{$filename}.csv", FILE_EXISTS_REPLACE);
      $values = [
        'target_id' => $file
          ->id(),
        'display' => (int) $settings['display_default'],
        'description' => '',
      ];

      // Track the file.
      $task->sar_export_parts[] = $values;
    }
    else {
      $file = $files[$filename];
    }
    $this
      ->writeCsv($file->uri->value, $data);
    $file
      ->save();
  }

  // Update the status.
  $task->status = 'reviewing';
  $task
    ->save();
}