You are here

public function WebformProtectedDownloadsController::protectedFileDownload in Webform Protected Downloads 8

Protected file download controller.

1 string reference to 'WebformProtectedDownloadsController::protectedFileDownload'
webform_protected_downloads.routing.yml in ./webform_protected_downloads.routing.yml
webform_protected_downloads.routing.yml

File

src/Controller/WebformProtectedDownloadsController.php, line 52

Class

WebformProtectedDownloadsController
Class WebformProtectedDownloadsController.

Namespace

Drupal\webform_protected_downloads\Controller

Code

public function protectedFileDownload($hash) {

  // Get corresponding protected file entry from given URL hash.
  $webformProtectedDownload = $this->webformProtectedDownloadsManager
    ->getSubmissionByHash($hash);
  $webformSubmission = $webformProtectedDownload
    ->getWebformSubmission();
  $webform = $webformSubmission
    ->getWebform();

  // Get webform.
  $wpd_settings = $webform
    ->getThirdPartySettings('webform_protected_downloads');

  // Return error page if no results, inactive, expired, no webform found
  // or file not found.
  $expired = $webformProtectedDownload->expire->value < time() && $webformProtectedDownload->expire->value != "0";
  if (!$webformProtectedDownload || !$webformProtectedDownload
    ->isActive() || $expired || !$webform || !$wpd_settings['protected_file']) {
    switch ($wpd_settings['expired_link_page']) {
      case "homepage":
        $this
          ->messenger()
          ->addError($wpd_settings['error_message']);
        return $this
          ->redirect('<front>');
      case "page_reload":
        $this
          ->messenger()
          ->addError($wpd_settings['error_message']);
        return new RedirectResponse($webform
          ->toUrl()
          ->setAbsolute()
          ->toString());
      case "custom":
        $this
          ->messenger()
          ->addError($wpd_settings['error_message']);
        return new TrustedRedirectResponse($wpd_settings['custom_link_page']);
      default:
        throw new Exception\NotFoundHttpException();
    }
  }

  // Get file response.
  $response = $this
    ->sendProtectedFileResponse(current($wpd_settings['protected_file']));

  // Set onetime entry to inactive before returning.
  if ($webformProtectedDownload
    ->isOneTimeLink()) {
    $webformProtectedDownload
      ->set('active', FALSE)
      ->save();
  }
  return $response;
}