View source
<?php
namespace Drupal\webform\Utility;
use Drupal\Core\Datetime\DateHelper;
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\Core\Form\OptGroup;
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface;
class WebformDateHelper {
protected static $intervalOptions;
protected static $intervalOptionsFlattened;
public static function format($timestamp, $type = 'medium', $format = '', $timezone = NULL, $langcode = NULL) {
$date_formatter = \Drupal::service('date.formatter');
return $timestamp ? $date_formatter
->format($timestamp, $type, $format, $timezone, $langcode) : '';
}
public static function formatStorage(DrupalDateTime $date) {
return $date
->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT);
}
public static function getDaysOfWeek() {
return [
'0' => t('Sunday'),
'1' => t('Monday'),
'2' => t('Tuesday'),
'3' => t('Wednesday'),
'4' => t('Thursday'),
'5' => t('Friday'),
'6' => t('Saturday'),
];
}
public static function createFromFormat($format, $time, $timezone = NULL, array $settings = []) {
$english_time = WebformDateHelper::convertDateStringToEnglish($format, $time);
try {
return DrupalDateTime::createFromFormat($format, $english_time, $timezone, $settings);
} catch (\Exception $exception) {
return FALSE;
}
}
public static function getIntervalOptions() {
self::initIntervalOptions();
return self::$intervalOptions;
}
public static function getIntervalOptionsFlattened() {
self::initIntervalOptions();
return self::$intervalOptionsFlattened;
}
public static function getIntervalText($interval) {
$interval = (string) $interval ?: '';
$intervals = self::getIntervalOptionsFlattened();
return isset($intervals[$interval]) ? $intervals[$interval] : $intervals[''];
}
protected static function initIntervalOptions() {
if (!isset(self::$intervalOptions)) {
$options = [
'' => t('ever'),
];
$seconds_optgroup = (string) t('Second');
$increment = 0;
while ($increment < 55) {
$increment += 5;
$options[$seconds_optgroup][$increment] = t('every @increment seconds', [
'@increment' => $increment,
]);
}
$minute = 60;
$minute_optgroup = (string) t('Minute');
$options[$minute_optgroup][$minute] = t('every minute');
$increment = 5;
while ($increment < 55) {
$increment += 5;
$options[$minute_optgroup][$increment * $minute] = t('every @increment minutes', [
'@increment' => $increment,
]);
}
$hour = $minute * 60;
$hour_optgroup = (string) t('Hour');
$options[$hour_optgroup][$hour] = t('every hour');
$increment = 1;
while ($increment < 23) {
$increment += 1;
$options[$hour_optgroup][$increment * $hour] = t('every @increment hours', [
'@increment' => $increment,
]);
}
$day = $hour * 24;
$day_optgroup = (string) t('Day');
$options[$day_optgroup][$day] = t('every day');
$increment = 1;
while ($increment < 6) {
$increment += 1;
$options[$day_optgroup][$increment * $day] = t('every @increment days', [
'@increment' => $increment,
]);
}
$week = $day * 7;
$week_optgroup = (string) t('Week');
$options[$week_optgroup][$week] = t('every week');
$increment = 1;
while ($increment < 51) {
$increment += 1;
$options[$week_optgroup][$increment * $week] = t('every @increment weeks', [
'@increment' => $increment,
]);
}
$year = $day * 365;
$year_optgroup = (string) t('Year');
$options[$year_optgroup][$year] = t('every year');
$increment = 1;
while ($increment < 10) {
$increment += 1;
$options[$year_optgroup][$increment * $year] = t('every @increment years', [
'@increment' => $increment,
]);
}
self::$intervalOptions = $options;
self::$intervalOptionsFlattened = OptGroup::flattenOptions($options);
}
}
protected static function convertDateStringToEnglish($format, $value) {
if (\Drupal::languageManager()
->getCurrentLanguage()
->getId() === 'en') {
return $value;
}
if (strpos($format, 'F') !== FALSE) {
$month_names_untranslated = DateHelper::monthNamesUntranslated();
$month_names_translated = DateHelper::monthNames();
foreach ($month_names_untranslated as $index => $month_name_untranslated) {
$value = str_ireplace((string) $month_names_translated[$index], $month_name_untranslated, $value);
}
}
if (strpos($format, 'M') !== FALSE) {
$month_names_abbr_untranslated = DateHelper::monthNamesAbbrUntranslated();
$month_names_abbr_translated = DateHelper::monthNamesAbbr();
foreach ($month_names_abbr_untranslated as $index => $month_name_abbr_untranslated) {
$value = str_ireplace((string) $month_names_abbr_translated[$index], $month_name_abbr_untranslated, $value);
}
}
if (strpos($format, 'l') !== FALSE) {
$week_days_untranslated = DateHelper::weekDaysUntranslated();
$week_days_translated = DateHelper::weekDays();
foreach ($week_days_untranslated as $index => $week_day_untranslated) {
$value = str_ireplace((string) $week_days_translated[$index], $week_day_untranslated, $value);
}
}
if (strpos($format, 'D') !== FALSE) {
$week_days_abbr_untranslated = DateHelper::weekDaysUntranslated();
$week_days_abbr_translated = DateHelper::weekDaysAbbr();
foreach ($week_days_abbr_untranslated as $index => $week_day_abbr_untranslated) {
$week_days_abbr_untranslated[$index] = (string) substr($week_days_abbr_untranslated[$index], 0, 3);
$value = str_ireplace((string) $week_days_abbr_translated[$index], $week_days_abbr_untranslated[$index], $value);
}
}
return $value;
}
}