public function SmartDateRule::getTextRule in Smart Date 3.0.x
Same name and namespace in other branches
- 8.2 modules/smart_date_recur/src/Entity/SmartDateRule.php \Drupal\smart_date_recur\Entity\SmartDateRule::getTextRule()
- 3.x modules/smart_date_recur/src/Entity/SmartDateRule.php \Drupal\smart_date_recur\Entity\SmartDateRule::getTextRule()
- 3.1.x modules/smart_date_recur/src/Entity/SmartDateRule.php \Drupal\smart_date_recur\Entity\SmartDateRule::getTextRule()
- 3.2.x modules/smart_date_recur/src/Entity/SmartDateRule.php \Drupal\smart_date_recur\Entity\SmartDateRule::getTextRule()
- 3.3.x modules/smart_date_recur/src/Entity/SmartDateRule.php \Drupal\smart_date_recur\Entity\SmartDateRule::getTextRule()
- 3.4.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 310
Class
- SmartDateRule
- Defines the Smart date rule entity.
Namespace
Drupal\smart_date_recur\EntityCode
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 '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];
}
// 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;
}
}
$start_ts = $this->start;
// TODO: proper timezone handling, allowing for field override.
$tz_string = \Drupal::config('system.date')
->get('timezone')['default'];
// Format the day output.
if ($freq == 'DAILY') {
// No day output required.
$day = '';
}
elseif ($freq == '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 = '';
}
// Format the time display.
// Use the "Time Only" Smart Date Format to allow better formatting.
$format = SmartDateFormat::load('time_only');
$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,
];
}