You are here

public function GDPRController::requestPage in General Data Protection Regulation 8.2

Same name and namespace in other branches
  1. 8 modules/gdpr_tasks/src/Controller/GDPRController.php \Drupal\gdpr_tasks\Controller\GDPRController::requestPage()
  2. 3.0.x modules/gdpr_tasks/src/Controller/GDPRController.php \Drupal\gdpr_tasks\Controller\GDPRController::requestPage()

Request a GDPR Task.

Parameters

\Drupal\Core\Session\AccountInterface $user: The user for whom the request is being made.

string $gdpr_task_type: Type of task to be created.

Return value

array|\Symfony\Component\HttpFoundation\RedirectResponse Either the task request form or a redirect response to requests page.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

1 string reference to 'GDPRController::requestPage'
gdpr_tasks.routing.yml in modules/gdpr_tasks/gdpr_tasks.routing.yml
modules/gdpr_tasks/gdpr_tasks.routing.yml

File

modules/gdpr_tasks/src/Controller/GDPRController.php, line 100

Class

GDPRController
Returns responses for Views UI routes.

Namespace

Drupal\gdpr_tasks\Controller

Code

public function requestPage(AccountInterface $user, $gdpr_task_type) {
  $tasks = $this->taskManager
    ->getUserTasks($user, $gdpr_task_type);
  $pending = FALSE;
  $statuses = [
    'requested',
    'processed',
    'reviewing',
  ];
  foreach ($tasks as $task) {
    if (in_array($task->status
      ->getString(), $statuses, TRUE)) {
      $pending = TRUE;
    }
  }
  if ($pending) {
    $this->messenger
      ->addWarning('You already have a pending task.');
  }
  else {

    // If the current user is making a request for themselves, just create it.
    // However, if we're a member of staff making a request on behalf
    // of someone else, we need to collect further details
    // so render a form to get the notes.
    if ($this
      ->currentUser()
      ->id() !== $user
      ->id()) {
      return [
        'form' => $this
          ->formBuilder()
          ->getForm(CreateGdprRequestOnBehalfOfUserForm::class),
      ];
    }
    $values = [
      'type' => $gdpr_task_type,
      'user_id' => $user
        ->id(),
    ];
    $newTask = $this->entityTypeManager
      ->getStorage('gdpr_task')
      ->create($values);
    try {
      $newTask
        ->save();
      $this->messenger
        ->addStatus($this
        ->t('Your request has been logged.'));
      if ($gdpr_task_type === 'gdpr_sar') {
        $this->queue
          ->createQueue();
        $this->queue
          ->createItem($newTask
          ->id());
      }
    } catch (EntityStorageException $exception) {
      $this->messenger
        ->addError($this
        ->t('There was an error while logging your request.'));
      $this->loggerFactory
        ->get('gdpr_tasks')
        ->error($this
        ->t('Error while trying to create a(n) "@taskType" GDPR task for user "@userName (@userId)."', [
        '@taskType' => $gdpr_task_type,
        '@userName' => $user
          ->getDisplayName(),
        '@userId' => $user
          ->id(),
      ]));
    }
  }
  return $this
    ->redirect('view.gdpr_tasks_my_data_requests.page_1', [
    'user' => $user
      ->id(),
  ]);
}