You are here

public function SmartDateRule::getTextRule in Smart Date 3.4.x

Same name and namespace in other branches
  1. 8.2 modules/smart_date_recur/src/Entity/SmartDateRule.php \Drupal\smart_date_recur\Entity\SmartDateRule::getTextRule()
  2. 3.x modules/smart_date_recur/src/Entity/SmartDateRule.php \Drupal\smart_date_recur\Entity\SmartDateRule::getTextRule()
  3. 3.0.x modules/smart_date_recur/src/Entity/SmartDateRule.php \Drupal\smart_date_recur\Entity\SmartDateRule::getTextRule()
  4. 3.1.x modules/smart_date_recur/src/Entity/SmartDateRule.php \Drupal\smart_date_recur\Entity\SmartDateRule::getTextRule()
  5. 3.2.x modules/smart_date_recur/src/Entity/SmartDateRule.php \Drupal\smart_date_recur\Entity\SmartDateRule::getTextRule()
  6. 3.3.x modules/smart_date_recur/src/Entity/SmartDateRule.php \Drupal\smart_date_recur\Entity\SmartDateRule::getTextRule()

Use the transformer to get text output of the rule.

File

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

Class

SmartDateRule
Defines the Smart date rule entity.

Namespace

Drupal\smart_date_recur\Entity

Code

public function getTextRule() {
  $freq = $this
    ->get('freq')
    ->getString();
  $repeat = $freq;
  $params = $this
    ->getParametersArray();
  $day_labels = [
    'MO' => $this
      ->t('Monday'),
    'TU' => $this
      ->t('Tuesday'),
    'WE' => $this
      ->t('Wednesday'),
    'TH' => $this
      ->t('Thursday'),
    'FR' => $this
      ->t('Friday'),
    'SA' => $this
      ->t('Saturday'),
    'SU' => $this
      ->t('Sunday'),
  ];

  // Convert the stored repeat value to something human-readable.
  if ($params['interval'] && $params['interval'] > 1) {
    switch ($repeat) {
      case 'MINUTELY':
        $period = $this
          ->t('minutes');
        break;
      case 'HOURLY':
        $period = $this
          ->t('hours');
        break;
      case 'DAILY':
        $period = $this
          ->t('days');
        break;
      case 'WEEKLY':
        $period = $this
          ->t('weeks');
        break;
      case 'MONTHLY':
        $period = $this
          ->t('months');
        break;
      case 'YEARLY':
        $period = $this
          ->t('years');
        break;
    }
    $repeat = $this
      ->t('every :num :period', [
      ':num' => $params['interval'],
      ':period' => $period,
    ]);
  }
  else {
    $frequency_labels = static::getFrequencyLabels();
    $repeat = $frequency_labels[$repeat];
  }
  $start_ts = $this->start;

  // TODO: proper timezone handling, allowing for field override.
  $tz_string = \Drupal::config('system.date')
    ->get('timezone')['default'];
  $format = SmartDateFormat::load('time_only');
  $time_set = FALSE;

  // Add extra time parameters, if set.
  if ($params['byhour']) {
    $current_time = DrupalDateTime::createFromTimestamp($start_ts, $tz_string);
    $ranges = $this
      ->makeRanges($params['byhour']);
    $range_text = [];
    foreach ($ranges as $range) {
      $range_start = array_shift($range);
      $current_time
        ->setTime($range_start, 0);
      $range_start_ts = $current_time
        ->getTimestamp();
      if ($range) {
        $range_end = array_pop($range);
        $current_time
          ->setTime($range_end + 1, 0);
        $range_end_ts = $current_time
          ->getTimestamp();
      }
      else {
        $range_end_ts = $range_start_ts;
      }
      $range_text[] = SmartDateTrait::formatSmartDate($range_start_ts, $range_end_ts, $format
        ->getOptions(), $tz_string, 'string');
    }
    $repeat .= ' ' . $this
      ->t('within') . ' ' . implode(', ', $range_text);
    $time_set = TRUE;
  }
  if ($params['byminute']) {
    $ranges = $this
      ->makeRanges($params['byminute']);
    $range_text = [];
    foreach ($ranges as $range) {
      $range_start = array_shift($range);
      if ($range) {
        $range_end = array_pop($range);
        $range_text[] = $this
          ->t(':start to :end', [
          ':start' => $range_start,
          ':end' => $range_end,
        ], [
          'context' => 'Rule text',
        ]);
      }
      else {
        $range_text[] = $range_start;
      }
    }
    $repeat .= ' ' . $this
      ->t('at :ranges past the hour', [
      ':ranges' => implode(', ', $range_text),
    ], [
      'context' => 'Rule text',
    ]);
    $time_set = TRUE;
  }

  // Convert the stored day modifier to something human-readable.
  if ($params['which']) {
    switch ($params['which']) {
      case '1':
        $params['which'] = $this
          ->t('first');
        break;
      case '2':
        $params['which'] = $this
          ->t('second');
        break;
      case '3':
        $params['which'] = $this
          ->t('third');
        break;
      case '4':
        $params['which'] = $this
          ->t('fourth');
        break;
      case '5':
        $params['which'] = $this
          ->t('fifth');
        break;
      case '-1':
        $params['which'] = $this
          ->t('last');
        break;
    }
  }

  // Convert the stored day value to something human-readable.
  if (isset($params['day'])) {
    switch ($params['day']) {
      case 'SU':
      case 'MO':
      case 'TU':
      case 'WE':
      case 'TH':
      case 'FR':
      case 'SA':
        $params['day'] = $day_labels[$params['day']];
        break;
      case 'MO,TU,WE,TH,FR':
        $params['day'] = $this
          ->t('weekday');
        break;
      case 'SA,SU':
        $params['day'] = $this
          ->t('weekend day');
        break;
      case '':
        $params['day'] = $this
          ->t('day');
        break;
    }
  }

  // Format the day output.
  if (in_array($freq, [
    'MINUTELY',
    'HOURLY',
    'DAILY',
    'WEEKLY',
  ])) {
    if (!empty($params['byday']) && is_array($params['byday'])) {
      switch (count($params['byday'])) {
        case 1:
          $day_output = $day_labels[array_pop($params['byday'])];
          break;
        case 2:
          $day_output = $day_labels[$params['byday'][0]] . ' ' . $this
            ->t('and') . ' ' . $day_labels[$params['byday'][1]];
          break;
        default:
          $day_output = '';
          foreach ($params['byday'] as $key => $day) {
            if ($key === array_key_last($params['byday'])) {
              $day_output .= $this
                ->t('and') . ' ' . $day_labels[$day];
            }
            else {
              $day_output .= $day_labels[$day] . ', ';
            }
          }
          break;
      }
    }
    else {

      // Default to getting the day from the start date.
      $day_labels_by_day_of_week = array_values($day_labels);
      $day_output = $day_labels_by_day_of_week[date('N', $start_ts) - 1];
    }
    $day = $this
      ->t('on :day', [
      ':day' => $day_output,
    ], [
      'context' => 'Rule text',
    ]);
  }
  else {
    $day = date('jS', $start_ts);
    if ($params['which']) {
      $day = $params['which'] . ' ' . $params['day'];
    }
    $day = $this
      ->t('on the :day', [
      ':day' => $day,
    ], [
      'context' => 'Rule text',
    ]);
  }

  // Format the month display, if needed.
  if ($freq == 'YEARLY') {
    $month = ' ' . $this
      ->t('of :month', [
      ':month' => date('F', $start_ts),
    ], [
      'context' => 'Rule text',
    ]);
  }
  else {
    $month = '';
  }
  if ($time_set) {
    $time = '';
  }
  else {

    // Format the time display.
    // Use the "Time Only" Smart Date Format to allow better formatting.
    $end_ts = $this->end
      ->getValue()[0]['value'];
    if (SmartDateTrait::isAllDay($start_ts, $end_ts, $tz_string)) {
      $time = SmartDateTrait::formatSmartDate($start_ts, $end_ts, $format
        ->getOptions(), $tz_string, 'string');
    }
    else {
      $time_string = SmartDateTrait::formatSmartDate($start_ts, $start_ts, $format
        ->getOptions(), $tz_string, 'string');
      $time = $this
        ->t('at :time', [
        ':time' => '',
      ], [
        'context' => 'Rule text',
      ]) . $time_string;
    }
  }

  // Process the limit value, if present.
  $limit = '';
  if ($this->limit) {
    list($limit_type, $limit_val) = explode('=', $this->limit);
    switch ($limit_type) {
      case 'UNTIL':
        $limit_ts = strtotime($limit_val);
        $format = SmartDateFormat::load('date_only');
        $date_string = SmartDateTrait::formatSmartDate($limit_ts, $limit_ts, $format
          ->getOptions(), $tz_string, 'string');
        $limit = ' ' . $this
          ->t('until :date', [
          ':date' => $date_string,
        ]);
        break;
      case 'COUNT':
        $limit = ' ' . $this
          ->t('for :num times', [
          ':num' => $limit_val,
        ]);
    }
  }
  return [
    '#theme' => 'smart_date_recurring_text_rule',
    '#repeat' => $repeat,
    '#day' => $day,
    '#month' => $month,
    '#time' => $time,
    '#limit' => $limit,
  ];
}