You are here

webform_protected_downloads.module in Webform Protected Downloads 8

File

webform_protected_downloads.module
View source
<?php

use Drupal\Component\Utility\Crypt;
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\Core\Link;
use Drupal\Core\Url;
use Drupal\webform_protected_downloads\Entity\WebformProtectedDownloads;
use Drupal\file\Entity\File;

/**
 * Implements hook_theme().
 */
function webform_protected_downloads_theme() {
  return [
    'webform_protected_downloads_token' => [
      'variables' => [
        'link' => '',
      ],
    ],
    'webform_handler_webform_protected_downloads_summary' => [
      'variables' => [
        'settings' => NULL,
        'handler' => [],
      ],
    ],
  ];
}

/**
 * Implements hook_webform_submission_insert().
 */
function webform_protected_downloads_webform_submission_insert($submission) {

  // Get submission's corresponding webform's settings.
  $webform = $submission
    ->getWebform();
  $protected_downloads_settings = $webform
    ->getThirdPartySettings('webform_protected_downloads');

  // Return if protected downloads setting is not enabled.
  if (!isset($protected_downloads_settings['enabled_protected_files']) || !$protected_downloads_settings['enabled_protected_files']) {
    return;
  }
  elseif (!isset($protected_downloads_settings['protected_file']) || empty($protected_downloads_settings['protected_file'])) {
    return;
  }
  WebformProtectedDownloads::create([
    'webform_submission' => $submission,
    'file' => File::load(current($protected_downloads_settings['protected_file'])),
    'hash' => Crypt::hashBase64($submission
      ->id() . time()),
    'active' => $protected_downloads_settings['enabled_protected_files'],
    'expire' => !$protected_downloads_settings['expire_after'] ? 0 : time() + $protected_downloads_settings['expire_after'] * 60,
    'onetime' => $protected_downloads_settings['enabled_onetime'],
  ])
    ->save();
}

/**
 * Implements hook_token_info().
 */
function webform_protected_downloads_token_info() {
  $submission['protected_download_url'] = array(
    'name' => t("Protected download link"),
    'description' => t("Download link for webform protected downloads."),
  );
  return array(
    'tokens' => array(
      'webform_submission' => $submission,
    ),
  );
}

/**
 * Implements hook_tokens().
 */
function webform_protected_downloads_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
  $replacements = [];
  if (isset($tokens['protected_download_url']) && isset($data['entity'])) {
    foreach ($tokens as $name => $original) {
      switch ($name) {
        case 'protected_download_url':

          // Find a file hash for current submission.
          $webformSubmission = $data['entity'];
          $webformProtectedDownloadsManager = \Drupal::service('webform_protected_downloads.manager');
          $webformProtectedDownload = $webformProtectedDownloadsManager
            ->getSubmissionByUuid($webformSubmission
            ->uuid());
          $hash = $webformProtectedDownload
            ->getHash();
          if (is_null($hash)) {
            $replacements[$original] = t("Download link not available");
            return $replacements;
          }
          $webform = $webformSubmission
            ->getWebform();
          $webformSettings = $webform
            ->getThirdPartySettings('webform_protected_downloads');
          $url = Url::fromRoute('webform_protected_downloads.download', [
            'hash' => $hash,
          ], [
            'absolute' => TRUE,
          ]);
          $link = Link::fromTextAndUrl(t($webformSettings['token_text']), $url)
            ->toString();
          $element = [
            '#theme' => 'webform_protected_downloads_token',
            '#link' => $link,
          ];
          $link = \Drupal::service('renderer')
            ->render($element);

          // Replace token.
          $replacements[$original] = $link;
          break;
      }
    }
  }
  return $replacements;
}