View source
<?php
namespace Drupal\webform_protected_downloads\Plugin\WebformHandler;
use Drupal\Component\Utility\Crypt;
use Drupal\Core\Form\FormStateInterface;
use Drupal\file\FileInterface;
use Drupal\file\FileStorageInterface;
use Drupal\file\FileUsage\FileUsageInterface;
use Drupal\webform\Annotation\WebformHandler;
use Drupal\webform\Plugin\WebformHandlerBase;
use Drupal\webform\WebformSubmissionInterface;
use Drupal\webform_protected_downloads\Entity\WebformProtectedDownloads;
use Symfony\Component\DependencyInjection\ContainerInterface;
class WebformProtectedDownloadsHandler extends WebformHandlerBase {
protected $fileStorage;
protected $fileUsage;
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
$instance = parent::create($container, $configuration, $plugin_id, $plugin_definition);
$entityTypeManager = $container
->get('entity_type.manager');
$instance->fileStorage = $entityTypeManager
->getStorage('file');
$instance->fileUsage = $container
->get('file.usage');
return $instance;
}
public function defaultConfiguration() {
return [
'expiration_one_time' => false,
'expiration_time' => 0,
'expiration_page' => 'page_reload',
'expiration_page_custom' => '',
'expiration_error_message' => 'This link has expired.',
'token_text' => 'Download',
'protected_files' => [],
'protected_files_allowed_extensions' => 'gif png jpg jpeg',
'debug' => false,
];
}
public function getSummary() {
$configuration = $this
->getConfiguration();
$settings = $configuration['settings'];
return [
'#settings' => $settings,
] + parent::getSummary();
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form['expiration'] = [
'#type' => 'details',
'#title' => $this
->t('Expiration'),
'#open' => TRUE,
];
$form['expiration']['expiration_onetime'] = [
'#type' => 'checkbox',
'#title' => $this
->t('One time visit link'),
'#default_value' => $this->configuration['expiration_onetime'],
];
$form['expiration']['expiration_time'] = [
'#type' => 'number',
'#title' => $this
->t('Expire after X minutes'),
'#default_value' => $this->configuration['expiration_time'],
'#required' => TRUE,
'#description' => 'Set this to 0 for unlimited.',
];
$options = [
'404' => $this
->t('404 page'),
'homepage' => $this
->t('Homepage with error message'),
'page_reload' => $this
->t('Form page with error message'),
'custom' => $this
->t('Custom page'),
];
$form['expiration']['expiration_page'] = [
'#type' => 'radios',
'#title' => $this
->t('Link expired page'),
'#required' => TRUE,
'#description' => t('Select a page to be routed when link expires.'),
'#options' => $options,
'#default_value' => $this->configuration['expiration_page'],
'#attributes' => [
'name' => 'field_expiration_page',
],
];
$form['expiration']['expiration_page_custom'] = [
'#type' => 'textfield',
'#title' => $this
->t('Custom link page'),
'#options' => $options,
'#default_value' => $this->configuration['expiration_page_custom'],
'#states' => [
'visible' => [
':input[name="field_expiration_page"]' => [
'value' => 'custom',
],
],
'required' => [
':input[name="field_expiration_page"]' => [
'value' => 'custom',
],
],
],
];
$form['expiration']['expiration_error_message'] = array(
'#type' => 'textfield',
'#title' => $this
->t('Error message'),
'#description' => $this
->t('Error message to display.'),
'#default_value' => $this->configuration['expiration_error_message'],
'#states' => [
'visible' => [
':input[name="field_expiration_page"]' => [
[
'value' => 'homepage',
],
[
'value' => 'page_reload',
],
],
],
'required' => [
':input[name="field_expiration_page"]' => [
[
'value' => 'homepage',
],
[
'value' => 'page_reload',
],
],
],
],
);
$form['token'] = [
'#type' => 'details',
'#title' => $this
->t('Tokens'),
'#open' => TRUE,
];
$form['token']['token_text'] = [
'#type' => 'textfield',
'#title' => $this
->t('Token text title'),
'#description' => $this
->t('This title will be shown when token is replaced, default title is Download'),
'#default_value' => $this->configuration['token_text'],
];
$form['protected'] = [
'#type' => 'details',
'#title' => $this
->t('Protected Files'),
'#open' => TRUE,
];
$form['protected']['protected_files'] = [
'#name' => 'protected_files',
'#type' => 'managed_file',
'#title' => $this
->t('Choose file(s) for protected download'),
'#multiple' => TRUE,
'#upload_validators' => [
'file_validate_extensions' => $this->configuration['protected_files_allowed_extensions'],
],
'#upload_location' => 'private://webform_protected_downloads/[date:custom:Y]-[date:custom:m]',
'#default_value' => $this->configuration['protected_files'],
];
$form['protected']['protected_files_allowed_extensions'] = [
'#type' => 'textfield',
'#title' => $this
->t('Valid File extensions'),
'#description' => $this
->t("Separate extensions with a space."),
'#default_value' => $this->configuration['protected_files_allowed_extensions'],
'#required' => TRUE,
];
$form['development'] = [
'#type' => 'details',
'#title' => $this
->t('Development settings'),
'#open' => true,
];
$form['development']['debug'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Enable debugging'),
'#description' => $this
->t('If checked, debugging will be displayed onscreen to all users.'),
'#return_value' => true,
'#default_value' => $this->configuration['debug'],
];
return $this
->setSettingsParents($form);
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
$this->configuration = $this
->defaultConfiguration();
parent::submitConfigurationForm($form, $form_state);
$this
->applyFormStateToConfiguration($form_state);
$protectedFiles = $form_state
->getValue('protected_files');
foreach ($protectedFiles as $protectedFile) {
$file = $this->fileStorage
->load($protectedFile);
if (!empty($file)) {
$this->fileUsage
->add($file, 'webform_protected_downloads', 'webform', $this
->getWebform()
->id());
}
}
}
public function postSave(WebformSubmissionInterface $webform_submission, $update = TRUE) {
if (!$update) {
return;
}
if (count($this->configuration['protected_files']) > 0) {
return;
}
$expiration = 0;
if ($this->configuration['expiration_time'] > 0) {
$expiration = time() + $this->configuration['expiration_time'] * 60;
}
}
}