CIVICRM-1763 Add Event component check for missing timezones.
authorFrancis Whittle <francis@agileware.com.au>
Wed, 2 Jun 2021 01:28:55 +0000 (11:28 +1000)
committerFrancis Whittle <francis@agileware.com.au>
Tue, 25 Jan 2022 06:07:10 +0000 (06:07 +0000)
CRM/Utils/Check/Component/Env.php
CRM/Utils/Check/Component/Event.php [new file with mode: 0644]
api/v3/Event.php

index cdac188f0b9d42a7f0575a01b8df1bd45ea25d0b..18b7bd0d8dc5c11db8b2238c4cbf8b8432ef7b1c 100644 (file)
@@ -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 <a href="%1">PHP List of Supported Timezones</a>.', [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 (file)
index 0000000..1ac9f94
--- /dev/null
@@ -0,0 +1,54 @@
+<?php
+
+/**
+ * @package CRM
+ */
+class CRM_Utils_Check_Component_Event extends CRM_Utils_Check_Component {
+
+  /**
+   * Check events have timezone set.
+   *
+   * @return CRM_Utils_Check_Message[]
+   * @throws \API_Exception
+   */
+  public function checkTimezones() {
+    $messages = [];
+
+    try {
+      $count = \Civi\Api4\Event::get(FALSE)
+        ->selectRowCount()
+        ->addWhere('event_tz', 'IS EMPTY')
+        ->execute()
+        ->rowCount;
+
+      if ($count) {
+        $msg = new CRM_Utils_Check_Message(
+          __FUNCTION__,
+          '<p>' . ts('%count Event has no timezone set', ['count' => $count, 'plural' => '%count Events have no timezone set.']) . '</p>',
+          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;
+  }
+
+}
index 0c10531f5c89bdb00125d1b53c47339d20a10ec3..c10266c680d75667f71eaabecce8560b95cb9b16 100644 (file)
@@ -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');
+}