You are here

class RulesDateOffsetProcessor in Rules 7.2

A data processor for applying date offsets.

Hierarchy

Expanded class hierarchy of RulesDateOffsetProcessor

Related topics

2 string references to 'RulesDateOffsetProcessor'
hook_rules_data_processor_info in ./rules.api.php
Declare provided rules data processors.
rules_rules_core_data_processor_info in modules/rules_core.rules.inc
Implements hook_rules_data_processor_info() on behalf of the pseudo rules_core module.

File

modules/rules_core.eval.inc, line 145
Contains rules core integration needed during evaluation.

View source
class RulesDateOffsetProcessor extends RulesDataProcessor {

  /**
   * Overrides RulesDataProcessor::form().
   */
  protected static function form($settings, $var_info) {
    $settings += array(
      'value' => '',
    );
    $form = array(
      '#type' => 'fieldset',
      '#title' => t('Add offset'),
      '#collapsible' => TRUE,
      '#collapsed' => empty($settings['value']),
      '#description' => t('Add an offset to the selected date.'),
    );
    $form['value'] = array(
      '#type' => 'rules_duration',
      '#title' => t('Offset'),
      '#description' => t('Note that you can also specify negative numbers.'),
      '#default_value' => $settings['value'],
      '#weight' => 5,
    );
    return $form;
  }

  /**
   * Overrides RulesDataProcessor::process().
   */
  public function process($value, $info, RulesState $state, RulesPlugin $element) {
    $value = isset($this->processor) ? $this->processor
      ->process($value, $info, $state, $element) : $value;
    return RulesDateOffsetProcessor::applyOffset($value, $this->setting['value']);
  }

  /**
   * Intelligently applies the given date offset in seconds.
   *
   * Intelligently apply duration values > 1 day, i.e. convert the duration
   * to its biggest possible unit (months, days) and apply it to the date with
   * the given unit. That's necessary as the number of days in a month
   * differs, as well as the number of hours for a day (on DST changes).
   */
  public static function applyOffset($timestamp, $offset) {
    if (abs($offset) >= 86400) {

      // Get the days out of the seconds.
      $days = intval($offset / 86400);
      $sec = $offset % 86400;

      // Get the months out of the number of days.
      $months = intval($days / 30);
      $days = $days % 30;

      // Apply the offset using the DateTime::modify and convert it back to a
      // timestamp.
      $date = date_create("@{$timestamp}");
      $date
        ->modify("{$months} months {$days} days {$sec} seconds");
      return $date
        ->format('U');
    }
    else {
      return $timestamp + $offset;
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
RulesDataProcessor::$processor protected property Allows chaining processors. If set, the next processor to invoke.
RulesDataProcessor::$setting protected property The processors' setting value.
RulesDataProcessor::access public static function Return whether the current user has permission to use the processor. 1
RulesDataProcessor::attachForm public static function Attaches the form of applicable data processors. 1
RulesDataProcessor::dependencies public function Returns an array of modules which we depend on.
RulesDataProcessor::editAccess public function Determines whether the current user has permission to edit this chain of data processors. 2
RulesDataProcessor::getChainSettings public function Gets the settings array for this and all contained chained processors.
RulesDataProcessor::getPreparedValue protected function Return $this or skip this processor by returning the next processor. 1
RulesDataProcessor::getSetting public function Gets the settings of this processor.
RulesDataProcessor::prepareSetting public static function Prepares the processor for parameters. 1
RulesDataProcessor::processors public static function Returns defined data processors applicable for the given parameter. 1
RulesDataProcessor::unchain protected function
RulesDataProcessor::_item_sort public static function
RulesDataProcessor::__construct protected function Constructor. 1
RulesDateOffsetProcessor::applyOffset public static function Intelligently applies the given date offset in seconds.
RulesDateOffsetProcessor::form protected static function Overrides RulesDataProcessor::form(). Overrides RulesDataProcessor::form
RulesDateOffsetProcessor::process public function Overrides RulesDataProcessor::process(). Overrides RulesDataProcessor::process