You are here

protected function DateRecurModularSierraWidget::guessRecurrenceOptionFromRrule in Recurring Date Field Modular Widgets 8

Same name and namespace in other branches
  1. 3.x src/Plugin/Field/FieldWidget/DateRecurModularSierraWidget.php \Drupal\date_recur_modular\Plugin\Field\FieldWidget\DateRecurModularSierraWidget::guessRecurrenceOptionFromRrule()
  2. 2.x src/Plugin/Field/FieldWidget/DateRecurModularSierraWidget.php \Drupal\date_recur_modular\Plugin\Field\FieldWidget\DateRecurModularSierraWidget::guessRecurrenceOptionFromRrule()

Attempt to guess a suitable recurrence option in getRecurrenceOptions.

Parameters

\DateTime $startDate: A start date.

string $rrule: A rule string.

Return value

string|null An option, falls back to 'custom' if no suitable recurrence could be determined. If a field value is non recurring then this helper shouldn't be called.

1 call to DateRecurModularSierraWidget::guessRecurrenceOptionFromRrule()
DateRecurModularSierraWidget::formElement in src/Plugin/Field/FieldWidget/DateRecurModularSierraWidget.php
Returns the form for a single field widget.

File

src/Plugin/Field/FieldWidget/DateRecurModularSierraWidget.php, line 894

Class

DateRecurModularSierraWidget
Date recur sierra widget.

Namespace

Drupal\date_recur_modular\Plugin\Field\FieldWidget

Code

protected function guessRecurrenceOptionFromRrule(\DateTime $startDate, string $rrule) : ?string {
  try {
    $helper = DateRecurHelper::create($rrule, $startDate);

    /** @var \Drupal\date_recur\DateRecurRuleInterface[] $rules */
    $rules = $helper
      ->getRules();
    $rule = reset($rules);
    if (!isset($rule)) {
      return 'custom';
    }
  } catch (\Exception $e) {
    return 'custom';
  }
  $parts = array_filter($rule
    ->getParts());
  $frequency = $rule
    ->getFrequency();
  $interval = $parts['INTERVAL'] ?? 1;
  $count = $parts['COUNT'] ?? 1;
  $byParts = array_filter($parts, function ($value, $key) : bool {
    return strpos($key, 'BY', 0) === 0;
  }, \ARRAY_FILTER_USE_BOTH);
  $byPartCount = count($byParts);
  $weekdaysKeys = [
    'SU',
    'MO',
    'TU',
    'WE',
    'TH',
    'FR',
    'SA',
  ];
  $byDay = explode(',', $byParts['BYDAY'] ?? '');
  $byDay = array_unique(array_intersect($weekdaysKeys, $byDay));
  $byDayStr = implode(',', $byDay);
  $byMonth = array_unique(explode(',', $byParts['BYMONTH'] ?? ''));
  sort($byMonth);
  $byMonthDay = array_unique(explode(',', $byParts['BYMONTHDAY'] ?? ''));
  sort($byMonthDay);
  $bySetPos = array_unique(explode(',', $byParts['BYSETPOS'] ?? ''));
  sort($bySetPos);
  $startDayWeekday = $weekdaysKeys[$startDate
    ->format('w')];
  if ($interval == 1 && $count == 1) {
    if ($byPartCount === 0 && $frequency === 'DAILY') {
      return 'daily';
    }
    elseif ($frequency === 'WEEKLY' && $byDayStr === 'MO,TU,WE,TH,FR' && $byPartCount === 1) {
      return 'weekdayly';
    }
    elseif ($frequency === 'WEEKLY' && $byPartCount === 1 && count($byDay) === 1 && $byDayStr === $startDayWeekday) {

      // Only if weekday is same as start day.
      return 'weekly_oneday';
    }
    elseif ($frequency === 'MONTHLY' && $byPartCount === 2 && count($bySetPos) === 1 && count($byDay) === 1) {
      return 'monthly_th_weekday';
    }
    elseif ($frequency === 'YEARLY' && $byPartCount === 2 && count($byMonth) === 1 && count($byMonthDay) === 1) {
      return 'yearly_monthday';
    }
  }
  return 'custom';
}