You are here

webform_scheduled_tasks.module in Webform Scheduled Tasks 8

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

This hook will fire whenever cron fires, usually 20 mins.

File

webform_scheduled_tasks.module
View source
<?php

/**
 * @file
 * This hook will fire whenever cron fires, usually 20 mins.
 */

/**
 * This method makes sure we only fire at a set frequency.
 */
function cqc_webform_scheduled_tasks_cron() {
  $run_interval = webform_scheduled_tasks_get_config('run_interval');
  $last_run = new DateTime(webform_scheduled_tasks_get_config('last_run'));
  $since_start = $last_run
    ->diff(new DateTime(date('Y-m-d H:i:s')));

  // Run every 1440 mins (24 hours).
  if ($since_start->i > $run_interval) {
    webform_scheduled_tasks_do_actions();
  }
}

/**
 * Function that does the actions, must also be triggerable by a drush command.
 */
function webform_scheduled_tasks_do_actions($from_drush = FALSE) {
  $field = webform_scheduled_tasks_get_config('field_name');
  $date = date('Y-m-d H:i:s');
  if (strlen($field) > 1) {
    sanitize_webform_field($field);
    webform_scheduled_tasks_set_config('last_run', $date);
  }
  if ($from_drush) {
    return $date;
  }
}

/**
 * Gets the value of a config item.
 */
function webform_scheduled_tasks_get_config($name) {
  $config = \Drupal::config('webform_scheduled_tasks.config');
  return $config
    ->get($name);
}

/**
 * Sets the value of a config item.
 */
function webform_scheduled_tasks_set_config($name, $value) {
  $config = \Drupal::service('config.factory')
    ->getEditable('webform_scheduled_tasks.config');
  $config
    ->set($name, $value)
    ->save();
}

/**
 * Function to perform the DB Update.
 */
function sanitize_webform_field($field) {
  $sanitizedValue = webform_scheduled_tasks_get_config('sanitized_value');
  \Drupal::database()
    ->update('webform_submission_data')
    ->fields([
    'value' => $sanitizedValue,
  ])
    ->condition('name', $field, '=')
    ->condition('value', $sanitizedValue, '<>')
    ->condition('value', '', '<>')
    ->execute();
}

Functions

Namesort descending Description
cqc_webform_scheduled_tasks_cron This method makes sure we only fire at a set frequency.
sanitize_webform_field Function to perform the DB Update.
webform_scheduled_tasks_do_actions Function that does the actions, must also be triggerable by a drush command.
webform_scheduled_tasks_get_config Gets the value of a config item.
webform_scheduled_tasks_set_config Sets the value of a config item.