Fixed initialization of company specific settings in SysPrefs.
authorJanusz Dobrowolski <janusz@frontaccounting.eu>
Tue, 17 Feb 2015 22:27:06 +0000 (23:27 +0100)
committerJanusz Dobrowolski <janusz@frontaccounting.eu>
Sat, 28 Feb 2015 17:04:10 +0000 (18:04 +0100)
admin/db/company_db.inc
includes/prefs/sysprefs.inc
includes/session.inc
sales/sales_order_entry.php

index a983436090a0335a134b0890739b3c81c12bbfb3..479f92ac55f343120f47f0acc77c38ac3e30c3e3 100644 (file)
@@ -33,28 +33,11 @@ function update_company_prefs($params)
 function get_company_pref($prefs = null)
 {
        global $SysPrefs, $db_version;
-       
-       if (!isset($_SESSION['SysPrefs']->prefs)) { // cached preferences
-
-//             $_SESSION['SysPrefs'] = new sys_prefs();
-
-               $sql = "SELECT name, value FROM ".TB_PREF."sys_prefs";
-               $result = @db_query($sql); // supress errors before 2.3 db structure upgrade
 
-               if(!$result)
-                       return null;
-
-               while($pref = db_fetch_assoc($result)) {
-                       $_SESSION['SysPrefs']->prefs[$pref['name']] = $pref['value'];
-               }
+       if (!isset($SysPrefs->prefs))    // just after first login or reset
+               $SysPrefs->refresh_company_prefs();
 
-               $SysPrefs = &$_SESSION['SysPrefs'];
-
-               // update current db status for info in log file
-               $SysPrefs->db_ok = $SysPrefs->prefs['version_id'] == $db_version;
-       }
-
-       $all = $_SESSION['SysPrefs']->prefs;
+       $all = $SysPrefs->prefs;
 
        if (!$prefs)
                return $all;
@@ -65,7 +48,7 @@ function get_company_pref($prefs = null)
        foreach($prefs as $name)
                $ret[$name] = $all[$name];
 
-               return $ret;
+       return $ret;
 }
 
 function get_company_prefs()
@@ -82,10 +65,13 @@ function set_company_pref($pref, $category, $type, $length, $value)
 
 function refresh_sys_prefs()
 {
+       global $SysPrefs;
+
 //     flush_dir(user_js_cache()); // clear cache
        unset($_SESSION['SysPrefs']);
        $_SESSION['SysPrefs'] = new sys_prefs();
-       get_company_prefs();
+       $SysPrefs = &$_SESSION['SysPrefs'];
+       $SysPrefs->refresh_company_prefs();
 }
 
 function get_base_sales_type()
index 0ff68cbdc3bafe972d9a7ac012ba17c1683be576..9a8213c8ab01b11da8edf3b17bfef4a2f49865d3 100644 (file)
 ***********************************************************************/
 include_once($path_to_root . "/admin/db/company_db.inc");
 
+/*
+       System and per company prefferences.
+       Object is created only with site wide preffernces.
+       After user login refresh_company_prefs method have to be called to initialize company specific settings.
+*/
 class sys_prefs 
 {
        var $prefs;
@@ -18,13 +23,13 @@ class sys_prefs
 
        function sys_prefs()
        {
-               global $path_to_root;
+               global $path_to_root, $db_version;
 
+               // retrieve set system wide preferences
                include $path_to_root.'/config.default.php';
                if (file_exists($path_to_root.'/config.php'))
                        include $path_to_root.'/config.php';
 
-               // set system wide preferences
                foreach(get_defined_vars() as $name => $value)
                        $this->$name = $value;
 
@@ -37,6 +42,27 @@ class sys_prefs
            ini_set('date.timezone', $this->time_zone);
        }
 
+       /*
+               Initialize company specific preferrencies.
+       */
+       function refresh_company_prefs()
+       {
+               global $db_version;
+
+               $sql = "SELECT name, value FROM ".TB_PREF."sys_prefs";
+               $result = @db_query($sql); // supress errors before 2.3 db structure upgrade
+
+               if(!$result)
+                       return null;
+
+               while($pref = db_fetch_assoc($result)) {
+                       $this->prefs[$pref['name']] = $pref['value'];
+               }
+
+               // update current db status for info in log file
+               $this->db_ok = $this->prefs['version_id'] == $db_version;
+       }
+
        function allow_negative_stock() 
        {
                return $this->prefs['allow_negative_stock'];
index d9fd8c305724cbabd3d34efa96a9f711ac164b58..65bc9929fa39c00b9d3695c0c48bdee400265170 100644 (file)
@@ -240,7 +240,7 @@ function check_page_security($page_security)
                         _("Security settings have not been defined for your user account.")
                                . "<br>" . _("Please contact your system administrator.")       
                        : _("Please remove \$security_groups and \$security_headings arrays from config.php file!");
-       } elseif (!$_SESSION['SysPrefs']->db_ok && !$_SESSION["wa_current_user"]->can_access('SA_SOFTWAREUPGRADE')) 
+       } elseif (!$SysPrefs->db_ok && !$_SESSION["wa_current_user"]->can_access('SA_SOFTWAREUPGRADE')) 
        {
                $msg = _('Access to application has been blocked until database upgrade is completed by system administrator.');
        }
@@ -262,7 +262,7 @@ function check_page_security($page_security)
                end_page(@$_REQUEST['popup']);
                exit;
        }
-       if (!$_SESSION['SysPrefs']->db_ok 
+       if (!$SysPrefs->db_ok 
                && !in_array($page_security, array('SA_SOFTWAREUPGRADE', 'SA_OPEN', 'SA_BACKUP')))
        {
                display_error(_('System is blocked after source upgrade until database is updated on System/Software Upgrade page'));
@@ -380,6 +380,8 @@ foreach ($installed_extensions as $ext)
        if (file_exists($path_to_root.'/'.$ext['path'].'/hooks.php'))
                include_once($path_to_root.'/'.$ext['path'].'/hooks.php');
 }
+$Session_manager = new SessionManager();
+$Session_manager->sessionStart('FA'.md5(dirname(__FILE__)));
 
 $_SESSION['SysPrefs'] = new sys_prefs();
 
@@ -418,9 +420,6 @@ ini_set('session.gc_maxlifetime', 36000); // 10hrs
 
 hook_session_start(@$_POST["company_login_name"]);
 
-$Session_manager = new SessionManager();
-$Session_manager->sessionStart('FA'.md5(dirname(__FILE__)));
-
 // this is to fix the "back-do-you-want-to-refresh" issue - thanx PHPFreaks
 header("Cache-control: private");
 
@@ -554,18 +553,20 @@ if (!defined('FA_LOGOUT_PHP_FILE')){
                        $lang->set_language($_SESSION['language']->code);
                }
        } else
-       {       set_global_connection();
-                       if (db_fixed())
-                               db_set_encoding($_SESSION['language']->encoding);
-       }
+       {
+               set_global_connection();
+
+               if (db_fixed())
+                       db_set_encoding($_SESSION['language']->encoding);
 
+               $SysPrefs->refresh_company_prefs();
+       }
        if (!isset($_SESSION["App"])) {
                $_SESSION["App"] = new front_accounting();
                $_SESSION["App"]->init();
        }
 }
 
-
 // POST vars cleanup needed for direct reuse.
 // We quote all values later with db_escape() before db update.
 $_POST = strip_quotes($_POST);
index 7a33997245a2073a3b865efcc3a3cea0b19cb7b1..586ca10510ab12cf3dbb84dc8a2b407a836d031f 100644 (file)
@@ -633,9 +633,9 @@ function  handle_cancel_order()
 
 function create_cart($type, $trans_no)
 { 
-       global $Refs;
+       global $Refs, $SysPrefs;
 
-       if (!$_SESSION['SysPrefs']->db_ok) // create_cart is called before page() where the check is done
+       if (!$SysPrefs->db_ok) // create_cart is called before page() where the check is done
                return;
 
        processing_start();