[Jaws-commits] r13364 - branches/0.8/html/include

afz at jaws-project.com afz at jaws-project.com
Fri Aug 10 15:25:38 UTC 2012


Author: afz
Date: 2012-08-10 15:25:38 +0000 (Fri, 10 Aug 2012)
New Revision: 13364

Modified:
   branches/0.8/html/include/Jaws.php
Log:
[Jaws]:
- loaded/stored default application preferences separately
- added another argument default_timezone to UTC2UserTime/UserTime2UTC methods

Modified: branches/0.8/html/include/Jaws.php
===================================================================
--- branches/0.8/html/include/Jaws.php	2012-08-10 15:23:53 UTC (rev 13363)
+++ branches/0.8/html/include/Jaws.php	2012-08-10 15:25:38 UTC (rev 13364)
@@ -41,6 +41,18 @@
     var $_RequestAction = '';
 
     /**
+     * Default preferences
+     */
+    var $_Preferences = array(
+        'theme'             => 'jaws',
+        'language'          => 'en',
+        'editor'            => null,
+        'timezone'          => null,
+        'calendar_type'     => 'Gregorian',
+        'calendar_language' => 'en',
+    );
+
+    /**
      * The application's theme.
      * @var    string
      * @access protected
@@ -76,11 +88,11 @@
     var $_Editor = null;
 
     /**
-     * The user timezone
+     * The timezone
      * @var    string
      * @access protected
      */
-    var $_UserTimezone = null;
+    var $_Timezone = null;
 
     /**
      * Browser flag
@@ -137,6 +149,8 @@
         $this->loadClass('UTF8', 'Jaws_UTF8');
         $this->loadClass('Translate', 'Jaws_Translate');
         $this->loadClass('Registry', 'Jaws_Registry');
+
+        $this->loadPreferences();
         $this->Registry->Init();
         $this->InstanceSession();
   
@@ -148,6 +162,26 @@
     }
 
     /**
+     * Load the default application preferences(language, theme, ...)
+     *
+     * @return void
+     * @access public
+     */
+    function loadPreferences()
+    {
+        $this->_Preferences = array(
+            'theme'             => $this->Registry->Get('/config/theme'),
+            'language'          => $this->Registry->Get(JAWS_SCRIPT == 'index'?
+                                                        '/config/site_language':
+                                                        '/config/admin_language'),
+            'editor'            => $this->Registry->Get('/config/editor'),
+            'timezone'          => $this->Registry->Get('/config/timezone'),
+            'calendar_type'     => $this->Registry->Get('/config/calendar_type'),
+            'calendar_language' => $this->Registry->Get('/config/calendar_language'),
+        );
+    }
+
+    /**
      * Set the language and theme, first based on session data, then on application defaults.
      *
      * @return void
@@ -158,71 +192,84 @@
         if (APP_TYPE == 'web') {
             $cookie_precedence = ($this->Registry->Get('/config/cookie_precedence') == 'true');
 
+            // load from session
             $this->_Theme            = $this->Session->GetAttribute('theme');
+            $this->_Language         = $this->Session->GetAttribute('language');
+            $this->_Editor           = $this->Session->GetAttribute('editor');
+            $this->_Timezone         = $this->Session->GetAttribute('timezone');
+            $this->_CalendarType     = $this->Session->GetAttribute('calendartype');
             $this->_CalendarLanguage = $this->Session->GetAttribute('calendarlanguage');
-            $this->_Editor           = $this->Session->GetAttribute('editor');
-            $this->_UserTimezone     = $this->Session->GetAttribute('timezone');
 
-            if (is_null($this->_UserTimezone)) {
-                if ($cookie_precedence && !is_null($this->Session->GetCookie('timezone'))) {
-                    $this->_UserTimezone = $this->Session->GetCookie('timezone');
+            // load cookies preferences
+            $cookies = array(
+                'theme'             => $this->Session->GetCookie('theme'),
+                'language'          => $this->Session->GetCookie('language'),
+                'editor'            => $this->Session->GetCookie('editor'),
+                'timezone'          => $this->Session->GetCookie('timezone'),
+                'calendar_type'     => $this->Session->GetCookie('calendar_type'),
+                'calendar_language' => $this->Session->GetCookie('calendar_language'),
+            );
+
+            // theme
+            if (empty($this->_Theme)) {
+                if ($cookie_precedence && !empty($cookies['theme'])) {
+                    $this->_Theme = $cookies['theme'];
                 } else {
-                    $this->_UserTimezone = $this->Registry->Get('/config/timezone');
+                    $this->_Theme = $this->_Preferences['theme'];
                 }
             }
 
-            if (empty($this->_Theme)) {
-                if ($cookie_precedence && $this->Session->GetCookie('theme')) {
-                    $this->_Theme = $this->Session->GetCookie('theme');
+            if (empty($this->_Language)) {
+                if (JAWS_SCRIPT == 'index' &&
+                    $cookie_precedence &&
+                    !empty($cookies['language']))
+                {
+                    $this->_Language = $cookies['language'];
                 } else {
-                    $this->_Theme = $this->Registry->Get('/config/theme');
+                    $this->_Language = $this->_Preferences['language'];
                 }
             }
 
-            if (JAWS_SCRIPT == 'admin') {
-                $userLanguage    = $this->Session->GetAttribute('language');
-                $this->_Language = empty($userLanguage)? $this->Registry->Get('/config/admin_language') : $userLanguage;
-            } elseif (JAWS_SCRIPT == 'index') {
-                if ($cookie_precedence && $this->Session->GetCookie('language')) {
-                    $this->_Language = $this->Session->GetCookie('language');
+            if (empty($this->_Editor)) {
+                if ($cookie_precedence && !is_null($cookies['editor'])) {
+                    $this->_Editor = $cookies['editor'];
                 } else {
-                    $this->_Language = $this->Registry->Get('/config/site_language');
+                    $this->_Editor = $this->_Preferences['editor'];
                 }
-            } else {
-                $this->_Language = 'en';
             }
 
-            if ($cookie_precedence && $this->Session->GetCookie('calendar_type')) {
-                $this->_CalendarType = $this->Session->GetCookie('calendar_type');
-            } else {
-                $this->_CalendarType = $this->Registry->Get('/config/calendar_type');
+            if (is_null($this->_Timezone)) {
+                if ($cookie_precedence && !is_null($cookies['timezone'])) {
+                    $this->_Timezone = $cookies['timezone'];
+                } else {
+                    $this->_Timezone = $this->_Preferences['timezone'];
+                }
             }
 
+            if (empty($this->_CalendarType)) {
+                if ($cookie_precedence && !is_null($cookies['calendar_type'])) {
+                    $this->_CalendarType = $cookies['calendar_type'];
+                } else {
+                    $this->_CalendarType = $this->_Preferences['calendar_type'];
+                }
+            }
+
             if (empty($this->_CalendarLanguage)) {
-                if ($cookie_precedence && $this->Session->GetCookie('calendar_language')) {
-                    $this->_CalendarLanguage = $this->Session->GetCookie('calendar_language');
+                if ($cookie_precedence && !is_null($cookies['calendar_language'])) {
+                    $this->_CalendarLanguage = $cookies['calendar_language'];
                 } else {
-                    $this->_CalendarLanguage = $this->Registry->Get('/config/calendar_language');
+                    $this->_CalendarLanguage = $this->_Preferences['calendar_language'];
                 }
             }
         } else {
-            $this->_Theme    = $this->Registry->Get('/config/theme');
-            if (JAWS_SCRIPT == 'admin') {
-                $this->_Language = $this->Registry->Get('/config/admin_language');
-            } elseif (JAWS_SCRIPT == 'index') {
-                $this->_Language = $this->Registry->Get('/config/site_language');
-            } else {
-                $this->_Language = 'en';
-            }
-
-            $this->_CalendarType = $this->Registry->Get('/config/calendar_type');
-            $this->_CalendarLanguage = $this->Registry->Get('/config/calendar_language');
+            $this->_Theme            = $this->_Preferences['theme'];
+            $this->_Language         = $this->_Preferences['language'];
+            $this->_Editor           = $this->_Preferences['editor'];
+            $this->_Timezone         = $this->_Preferences['timezone'];
+            $this->_CalendarType     = $this->_Preferences['calendar_type'];
+            $this->_CalendarLanguage = $this->_Preferences['calendar_language'];
         }
 
-        if (empty($this->_Editor)) {
-            $this->_Editor = $this->Registry->Get('/config/editor');
-        }
-
         require_once 'Net/Detect.php';
         $bFlags = explode(',', $this->Registry->Get('/config/browsers_flag'));
         $this->_BrowserFlag = Net_UserAgent_Detect::getBrowser($bFlags);
@@ -413,7 +460,7 @@
                     break;
 
                 case 'timezone':
-                    $this->_UserTimezone = $value;
+                    $this->_Timezone = $value;
                     break;
             }
         }
@@ -1165,9 +1212,10 @@
      * @access  private
      * @param   mixed   $time   timestamp
      * @param   string  $format date format
+     * @param   boolean $default_timezone   use default timezone instead of user timezone
      * @return  boolean True or False
      */
-    function UTC2UserTime($time = '', $format = '')
+    function UTC2UserTime($time = '', $format = '', $default_timezone = false)
     {
         $time = empty($time)? time() : $time;
         if (is_array($time)) {
@@ -1179,11 +1227,13 @@
                            $time[0]);
         }
         $time = is_numeric($time)? $time : strtotime($time);
+
         // GMT offset
-        if (is_numeric($this->_UserTimezone)) {
-            $gmt_offset = $this->_UserTimezone * 3600;
+        $timezone = $default_timezone? $this->_Preferences['timezone'] : $this->_Timezone;
+        if (is_numeric($timezone)) {
+            $gmt_offset = $timezone * 3600;
         } else {
-            @date_default_timezone_set($this->_UserTimezone);
+            @date_default_timezone_set($timezone);
             $gmt_offset = date('Z', $time);
             date_default_timezone_set('UTC');
         }
@@ -1197,9 +1247,10 @@
      * @access  private
      * @param   mixed   $time   timestamp
      * @param   string  $format date format
+     * @param   boolean $default_timezone   use default timezone instead of user timezone
      * @return  boolean True or False
      */
-    function UserTime2UTC($time, $format = '')
+    function UserTime2UTC($time, $format = '', $default_timezone = false)
     {
         if (is_array($time)) {
             $time = mktime(isset($time[5])? $time[5] : 0,
@@ -1210,11 +1261,13 @@
                            $time[0]);
         }
         $time = is_numeric($time)? $time : strtotime($time);
+
         // GMT offset
-        if (is_numeric($this->_UserTimezone)) {
-            $gmt_offset = $this->_UserTimezone * 3600;
+        $timezone = $default_timezone? $this->_Preferences['timezone'] : $this->_Timezone;
+        if (is_numeric($timezone)) {
+            $gmt_offset = $timezone * 3600;
         } else {
-            @date_default_timezone_set($this->_UserTimezone);
+            @date_default_timezone_set($timezone);
             $gmt_offset = date('Z', $time);
             date_default_timezone_set('UTC');
         }



More information about the Jaws-commits mailing list