From cd2d8d98cc4f9a9c8bb643dcc49b60a74c89f70b Mon Sep 17 00:00:00 2001 From: Francis Whittle Date: Wed, 2 Jun 2021 11:28:55 +1000 Subject: [PATCH] CIVICRM-1763 Add Event component check for missing timezones. --- CRM/Utils/Check/Component/Env.php | 22 ++++++++++++ CRM/Utils/Check/Component/Event.php | 54 +++++++++++++++++++++++++++++ api/v3/Event.php | 12 +++++++ 3 files changed, 88 insertions(+) create mode 100644 CRM/Utils/Check/Component/Event.php diff --git a/CRM/Utils/Check/Component/Env.php b/CRM/Utils/Check/Component/Env.php index cdac188f0b..18b7bd0d8d 100644 --- a/CRM/Utils/Check/Component/Env.php +++ b/CRM/Utils/Check/Component/Env.php @@ -1016,4 +1016,26 @@ class CRM_Utils_Check_Component_Env extends CRM_Utils_Check_Component { return $messages; } + /** + * Ensure that the CMS is providing a supported timezone. + * + * @return CRM_Utils_Check_Message[] + */ + public function checkUFTimezoneValid() { + $messages = []; + $check_tz = CRM_Core_Config::singleton()->userSystem->getTimeZoneString(); + + if (!array_key_exists($check_tz, CRM_Core_SelectValues::timezone())) { + $messages[] = new CRM_Utils_Check_Message( + __FUNCTION__, + ts('This system has an invalid timezone set. Please verify that your CMS has a timezone configured that is listed under the PHP List of Supported Timezones.', [1 => 'https://www.php.net/manual/en/timezones.php']), + ts('Missing or Invalid Timezone'), + \Psr\Log\LogLevel::ERROR, + 'fa-clock-o' + ); + } + + return $messages; + } + } diff --git a/CRM/Utils/Check/Component/Event.php b/CRM/Utils/Check/Component/Event.php new file mode 100644 index 0000000000..1ac9f94d26 --- /dev/null +++ b/CRM/Utils/Check/Component/Event.php @@ -0,0 +1,54 @@ +selectRowCount() + ->addWhere('event_tz', 'IS EMPTY') + ->execute() + ->rowCount; + + if ($count) { + $msg = new CRM_Utils_Check_Message( + __FUNCTION__, + '

' . ts('%count Event has no timezone set', ['count' => $count, 'plural' => '%count Events have no timezone set.']) . '

', + ts('Events with Missing Timezone'), + \Psr\Log\LogLevel::WARNING, + 'fa-calendar' + ); + $msg->addAction( + ts('Fix Events with Missing Timezone'), + ts('This will set the system default timezone "%1" for all Events with no timezone set.', [1 => CRM_Core_Config::singleton()->userSystem->getTimeZoneString()]), + 'api3', + ['Event', 'addMissingTimezones'] + ); + $messages[] = $msg; + } + } + catch (API_Exception $e) { + $messages[] = new CRM_Utils_Check_Message( + __FUNCTION__, + ts('API Exception: %1 while checking events for timezones.', ['1' => $e->getMessage()]), + ts('Event timezone check failed'), + \Psr\Log\LogLevel::ERROR, + 'fa-calendar' + ); + } + + return $messages; + } + +} diff --git a/api/v3/Event.php b/api/v3/Event.php index 0c10531f5c..c10266c680 100644 --- a/api/v3/Event.php +++ b/api/v3/Event.php @@ -262,3 +262,15 @@ function _civicrm_api3_event_getlist_output($result, $request) { } return $output; } + +/** + * Add missing timezones to all events. + * + * @return array + */ +function civicrm_api3_event_addmissingtimezones($params) { + $defaultTZ = CRM_Core_Config::singleton()->userSystem->getTimeZoneString(); + + CRM_Core_DAO::executeQuery('UPDATE civicrm_event SET event_tz = %1 WHERE event_tz IS NULL OR event_tz = ""', [1 => [$defaultTZ, 'String']]); + return civicrm_api3_create_success($events, $params, 'Event', 'addMissingTimezones'); +} -- 2.25.1