You are here

public static function SmartDateRule::validateRecurring in Smart Date 3.2.x

Same name and namespace in other branches
  1. 3.1.x modules/smart_date_recur/src/Entity/SmartDateRule.php \Drupal\smart_date_recur\Entity\SmartDateRule::validateRecurring()
  2. 3.3.x modules/smart_date_recur/src/Entity/SmartDateRule.php \Drupal\smart_date_recur\Entity\SmartDateRule::validateRecurring()
  3. 3.4.x modules/smart_date_recur/src/Entity/SmartDateRule.php \Drupal\smart_date_recur\Entity\SmartDateRule::validateRecurring()

Validate recurring input, looking for values that will trigger a timeout.

Parameters

array $element: An associative array containing the properties and children of the generic form element.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

array $complete_form: The complete form structure.

File

modules/smart_date_recur/src/Entity/SmartDateRule.php, line 846

Class

SmartDateRule
Defines the Smart date rule entity.

Namespace

Drupal\smart_date_recur\Entity

Code

public static function validateRecurring(array &$element, FormStateInterface $form_state, array &$complete_form) {

  // Check that the value is set to recur.
  if (empty($element['repeat']['#value'])) {
    return;
  }

  // Only known issues are with DAILY recurring events and BYDAY values set.
  if ($element['repeat']['#value'] != 'DAILY' || empty($element['repeat-advanced']['byday']['#value'])) {
    return;
  }
  $start_time = $element['value']['#value']['object'];
  $end_time = $element['end_value']['#value']['object'];
  if (!$start_time instanceof DrupalDateTime || !$end_time instanceof DrupalDateTime) {

    // Unable to process if an invalid start or end.
    return;
  }

  // At this point, known issues involve provided BYDAY values that don't
  // include the start day.
  $start_day_num = $start_time
    ->format('N');
  $days_of_week = [
    1 => 'MO',
    2 => 'TU',
    3 => 'WE',
    4 => 'TH',
    5 => 'FR',
    6 => 'SA',
    7 => 'SU',
  ];
  $start_day = $days_of_week[$start_day_num];
  if (in_array($start_day, $element['repeat-advanced']['byday']['#value'])) {
    return;
  }

  // Daily repeats on a multiple of 7 where BYDAY doesn't include the start
  // day will cause the recurr library to time out, so check for this.
  if ($element['interval']['#value'] && $element['interval']['#value'] % 7 == 0) {
    $form_state
      ->setError($element, t('This recurrence pattern will yield zero instances.'));
  }

  // Daily repeats where BYDAY doesn't include the start day and the interval
  // is larger than the specified day range will create a rule with zero
  // instances, effectively creating an empty value and an orphaned rule.
  // Prevent this.
  if ($element['interval']['#value'] && $element['repeat-end']['#value'] == 'UNTIL' && !empty($element['repeat-end-date']['#value'])) {
    if ($element['repeat-end-date']['#value'] instanceof DrupalDateTime) {
      $stop_date = $element['repeat-end-date']['#value'];
    }
    else {
      $stop_date = new DrupalDateTime($element['repeat-end-date']['#value']);
    }
    $between = $start_time
      ->diff($stop_date, TRUE);
    if ($between->days < $element['interval']['#value']) {
      $form_state
        ->setError($element, t('This recurrence pattern will yield zero instances.'));
    }
  }
}