From: Coleman Watts Date: Fri, 19 Jun 2015 17:34:35 +0000 (-0400) Subject: CRM-16401 - Modify day of week ordering based on user prefs X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=1ef73b88715df1ba6d0c3a992848ad488ebf9089;p=civicrm-core.git CRM-16401 - Modify day of week ordering based on user prefs --- diff --git a/CRM/Core/Form/RecurringEntity.php b/CRM/Core/Form/RecurringEntity.php index edab31c046..2fb0d75d2f 100644 --- a/CRM/Core/Form/RecurringEntity.php +++ b/CRM/Core/Form/RecurringEntity.php @@ -170,8 +170,9 @@ class CRM_Core_Form_RecurringEntity { * @param CRM_Core_Form $form */ public static function buildQuickForm(&$form) { - // For some reason this is using the following as keys rather than the standard numeric keys returned by CRM_Utils_Date - $dayOfTheWeek = array( + // FIXME: this is using the following as keys rather than the standard numeric keys returned by CRM_Utils_Date + $dayOfTheWeek = array(); + $dayKeys = array( 'sunday', 'monday', 'tuesday', @@ -180,7 +181,9 @@ class CRM_Core_Form_RecurringEntity { 'friday', 'saturday', ); - $dayOfTheWeek = array_combine($dayOfTheWeek, CRM_Utils_Date::getAbbrWeekdayNames()); + foreach (CRM_Utils_Date::getAbbrWeekdayNames() as $k => $label) { + $dayOfTheWeek[$dayKeys[$k]] = $label; + } $form->add('select', 'repetition_frequency_unit', ts('Repeats every'), CRM_Core_SelectValues::getRecurringFrequencyUnits(), FALSE, array('class' => 'required')); $numericOptions = CRM_Core_SelectValues::getNumericOptions(1, 30); $form->add('select', 'repetition_frequency_interval', NULL, $numericOptions, FALSE, array('class' => 'required')); diff --git a/CRM/Utils/Date.php b/CRM/Utils/Date.php index 898045b738..31c6c95763 100644 --- a/CRM/Utils/Date.php +++ b/CRM/Utils/Date.php @@ -168,43 +168,59 @@ class CRM_Utils_Date { /** * Return abbreviated weekday names according to the locale. * + * Array will be in localized order according to 'weekBegins' setting, + * but array keys will always match to: + * 0 => Sun + * 1 => Mon + * etc. + * * @return array * 0-based array with abbreviated weekday names * */ - public static function &getAbbrWeekdayNames() { - static $abbrWeekdayNames; - if (!isset($abbrWeekdayNames)) { + public static function getAbbrWeekdayNames() { + static $days = array(); + if (!$days) { + // First day of the week + $firstDay = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::LOCALIZATION_PREFERENCES_NAME, 'weekBegins', NULL, 0); // set LC_TIME and build the arrays from locale-provided names // June 1st, 1970 was a Monday CRM_Core_I18n::setLcTime(); - for ($i = 0; $i < 7; $i++) { - $abbrWeekdayNames[$i] = strftime('%a', mktime(0, 0, 0, 6, $i, 1970)); + for ($i = $firstDay; count($days) < 7; $i = $i > 6 ? 0 : $i + 1) { + $days[$i] = strftime('%a', mktime(0, 0, 0, 6, $i, 1970)); } } - return $abbrWeekdayNames; + return $days; } /** * Return full weekday names according to the locale. * + * Array will be in localized order according to 'weekBegins' setting, + * but array keys will always match to: + * 0 => Sunday + * 1 => Monday + * etc. + * * @return array * 0-based array with full weekday names * */ - public static function &getFullWeekdayNames() { - static $fullWeekdayNames; - if (!isset($fullWeekdayNames)) { + public static function getFullWeekdayNames() { + static $days = array(); + if (!$days) { + // First day of the week + $firstDay = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::LOCALIZATION_PREFERENCES_NAME, 'weekBegins', NULL, 0); // set LC_TIME and build the arrays from locale-provided names // June 1st, 1970 was a Monday CRM_Core_I18n::setLcTime(); - for ($i = 0; $i < 7; $i++) { - $fullWeekdayNames[$i] = strftime('%A', mktime(0, 0, 0, 6, $i, 1970)); + for ($i = $firstDay; count($days) < 7; $i = $i > 6 ? 0 : $i + 1) { + $days[$i] = strftime('%A', mktime(0, 0, 0, 6, $i, 1970)); } } - return $fullWeekdayNames; + return $days; } /**