CRM-9683 add functions to set timezone in mysql based on UF, note that this is not...
[civicrm-core.git] / CRM / Utils / System / Drupal.php
index be31ffaebf869f423b1eeac01c1b1993db687d4f..fda57c0385727286cb67e173ed0b285bf2121983 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.3                                                |
+ | CiviCRM version 4.4                                                |
  +--------------------------------------------------------------------+
  | Copyright CiviCRM LLC (c) 2004-2013                                |
  +--------------------------------------------------------------------+
 /**
  * Drupal specific stuff goes here
  */
-class CRM_Utils_System_Drupal extends CRM_Utils_System_Base {
-  function __construct() {
-    $this->is_drupal = TRUE;
-    $this->supports_form_extensions = TRUE;
-  }
+class CRM_Utils_System_Drupal extends CRM_Utils_System_DrupalBase {
 
   /**
    * Function to create a user in Drupal.
@@ -662,6 +658,20 @@ AND    u.status = 1
     user_login_finalize($params);
   }
 
+  /**
+   * Determine the native ID of the CMS user
+   *
+   * @param $username
+   * @return int|NULL
+   */
+  function getUfId($username) {
+    $user = user_load_by_name($username);
+    if (empty($user->uid)) {
+      return NULL;
+    }
+    return $user->uid;
+  }
+
   /**
    * Set a message in the UF to display to a user
    *
@@ -1055,6 +1065,32 @@ AND    u.status = 1
     }
   }
 
+  /**
+   * Get timezone from Drupal
+   * @return boolean|string
+   */
+  function getTimeZoneOffset(){
+    global $user;
+    if (variable_get('configurable_timezones', 1) && $user->uid && strlen($user->timezone)) {
+      $timezone = $user->timezone;
+    } else {
+      $timezone = variable_get('date_default_timezone', null);
+    }
+    $tzObj = new DateTimeZone($timezone);
+    $dateTime = new DateTime("now", $tzObj);
+    $tz = $tzObj->getOffset($dateTime);
+
+    if(empty($tz)){
+      return false;
+    }
+
+    $timeZoneOffset = sprintf("%02d:%02d", $tz / 3600, ($tz/60)%60 );
+
+    if($timeZoneOffset > 0){
+      $timeZoneOffset = '+' . $timeZoneOffset;
+    }
+    return $timeZoneOffset;
+  }
   /**
    * Reset any system caches that may be required for proper CiviCRM
    * integration.