You are here

function gdpr_tasks_collect_rta_data in General Data Protection Regulation 7

Collect RTA data for a specific user.

1 call to gdpr_tasks_collect_rta_data()
GdprTasksSarWorker::build in modules/gdpr_tasks/src/Plugin/QueueWorker/GdprTasksSarWorker.php
Build the export files.

File

modules/gdpr_tasks/gdpr_tasks.module, line 447
Module file for the GDPR Tasks module.

Code

function gdpr_tasks_collect_rta_data($user) {
  ctools_include('plugins');
  ctools_include('export');
  $plugins = ctools_export_load_object('gdpr_fields_field_data');
  $fields = array(
    '_assets' => array(),
  );
  $gdpr_entities = gdpr_fields_collect_gdpr_entities('user', $user);
  foreach ($gdpr_entities as $entity_type => $entities) {
    foreach ($entities as $entity) {

      /* @var \EntityDrupalWrapper $wrapper */
      $wrapper = entity_metadata_wrapper($entity_type, $entity);
      foreach ($wrapper
        ->getPropertyInfo() as $property => $property_info) {
        $entity_id = $wrapper
          ->getIdentifier();
        $plugin_name = "{$entity_type}|{$wrapper->getBundle()}|{$property}";
        if (isset($plugins[$plugin_name])) {
          $field_config = $plugins[$plugin_name];

          /* @var GDPRFieldData $field_config */
          $allowed_values = array(
            'inc',
            'maybe',
          );
          if ($field_config->disabled || !in_array($field_config
            ->getSetting('gdpr_fields_rta'), $allowed_values)) {
            continue;
          }
          if ($field_config
            ->getSetting('gdpr_fields_rta')) {

            // Figure out what to do based on the type.
            $type = isset($property_info['type']) ? $property_info['type'] : 'string';
            $is_list = substr($type, 0, 5) == 'list<';
            if ($is_list) {
              $type = entity_property_extract_innermost_type($type);
            }

            // Check if the type is an entity, except for files.
            if ($type != 'file' && ($type == 'entity' || entity_get_info($type))) {
              $type = 'entity';
            }

            // If this is a list, loop over getting the output.
            $values = array();
            if ($is_list) {
              foreach ($wrapper->{$property} as $value) {
                $values[] = gdpr_tasks_collect_rta_data_extract_value($type, $value, $fields['_assets']);
              }
            }
            else {
              $values[] = gdpr_tasks_collect_rta_data_extract_value($type, $wrapper->{$property}, $fields['_assets']);
            }
            $filename = 'main';
            if ($entity->_gdpr_source_plugin && isset($plugins[$entity->_gdpr_source_plugin])) {
              $filename = $plugins[$entity->_gdpr_source_plugin]
                ->getSetting('gdpr_sars_filename', $filename);
            }
            $data = array(
              'plugin_name' => $plugin_name,
              'entity_type' => $entity_type,
              'entity_id' => $entity_id,
              // @todo: Make this configurable.
              'file' => $filename,
              'row_id' => $entity->_gdpr_row_id,
              'label' => $field_config
                ->getSetting('label'),
              'value' => implode(', ', array_filter($values)),
              'notes' => $field_config
                ->getSetting('notes'),
              'rta' => $field_config
                ->getSetting('gdpr_fields_rta'),
            );
            $fields["{$plugin_name}|{$entity_id}"] = $data;
          }
        }
      }
    }
  }
  return $fields;
}