You are here

webform_scheduled_tasks.module in Webform Scheduled Tasks 8.2

Same filename and directory in other branches
  1. 8 webform_scheduled_tasks.module

Module file.

File

webform_scheduled_tasks.module
View source
<?php

/**
 * @file
 * Module file.
 */
use Drupal\webform_scheduled_tasks\Entity\WebformScheduledTask;
use Drupal\webform_scheduled_tasks\Plugin\WebformScheduledTasks\Task\EmailedExport;

/**
 * Implements hook_cron().
 */
function webform_scheduled_tasks_cron() {

  /** @var \Drupal\webform_scheduled_tasks\TaskRunnerInterface $task_runner */
  $task_runner = \Drupal::service('webform_scheduled_tasks.task_runner');
  $task_runner
    ->executeTasks($task_runner
    ->getPendingTasks());
}

/**
 * Implements hook_mail().
 */
function webform_scheduled_tasks_mail($key, &$message, $params) {
  if ($key === 'export_summary_filesystem') {
    $task = WebformScheduledTask::load($message['params']['task_id']);
    $message['subject'] = t('Export generated for @webform_name', [
      '@webform_name' => $task
        ->getWebform()
        ->label(),
    ]);
    $message['body'][] = t('A new export has been created and can be downloaded from: @url', [
      '@url' => $message['params']['file_url'],
    ]);
  }
}

/**
 * Implements hook_file_download().
 */
function webform_scheduled_tasks_file_download($uri) {

  // Only grant access to files saved in the emailed export directory.
  if (strpos($uri, EmailedExport::DESTINATION_DIRECTORY) !== 0) {
    return NULL;
  }

  // Load the file and deny access by default if a single file with the given
  // URI was not found.
  $file_storage = \Drupal::service('entity_type.manager')
    ->getStorage('file');
  $files = $file_storage
    ->loadByProperties([
    'uri' => $uri,
  ]);
  if (count($files) !== 1) {
    return -1;
  }
  $file = reset($files);

  // Grant access to the file if the user can administer all submissions. This
  // explicitly doesn't take into account the access of the whole submission,
  // given the exports are whole groups of submissions. The permission is
  // restricted, so should cover a sufficient level of access.
  return \Drupal::currentUser()
    ->hasPermission('administer webform submission') ? file_get_content_headers($file) : -1;
}