Added inline company switching with set_global_connection()
authorJanusz Dobrowolski <janusz@frontaccounting.eu>
Wed, 13 Oct 2010 12:39:50 +0000 (12:39 +0000)
committerJanusz Dobrowolski <janusz@frontaccounting.eu>
Wed, 13 Oct 2010 12:39:50 +0000 (12:39 +0000)
admin/inst_upgrade.php
includes/current_user.inc
includes/db/connect_db.inc
includes/errors.inc

index eeee26a214b88152e2a01d84e9d607643f40eeb7..bd9e1b4ba4f6a610b639e52944ed548177a06e91 100644 (file)
@@ -121,26 +121,16 @@ function upgrade_step($index, $conn)
        return $ret;
 }
 
-function db_open($conn)
-{
-       $db = mysql_connect($conn["host"] ,$conn["dbuser"], $conn["dbpassword"]);
-       if (!$db)
-               return false;
-       if (!mysql_select_db($conn["dbname"], $db))
-               return false;
-       return $db;
-}
-
 $installers = get_installers();
 
 if (get_post('Upgrade')) 
 {
 
        $ret = true;
-       foreach ($db_connections as $conn) 
+       foreach ($db_connections as $comp => $conn) 
        {
        // connect to database
-               if (!($db = db_open($conn))) 
+               if (!(set_global_connection($comp))) 
                {
                        display_error(_("Cannot connect to database for company")
                                ." '".$conn['name']."'");
@@ -162,6 +152,7 @@ if (get_post('Upgrade'))
 //             db_close($conn); ?
                if (!$ret) break;
        }
+       set_global_connection();
        if($ret)
        {       // re-read the prefs
                global $path_to_root;
index ef14c821402b12db3cfb428fb5af3069945d2719..8b94366f83178483e398a34bf3b755373d582717 100644 (file)
@@ -9,8 +9,11 @@
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
     See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
 ***********************************************************************/
-include_once($path_to_root . "/includes/prefs/userprefs.inc");
 
+include_once($path_to_root . "/includes/prefs/userprefs.inc");
+if (!defined('TB_PREF')) {
+       define('TB_PREF', '&TB_PREF&');
+}
 //--------------------------------------------------------------------------
 
 class current_user
@@ -19,17 +22,18 @@ class current_user
        var $loginname;
        var $username;
        var $name;
-       var $company;
+       var $company; // user's company
        var $pos;
        var $access;
        var $timeout;
        var $last_act;
        var $role_set = false;
-       var $old_db;    
+       var $old_db;
        var $logged;
        var $ui_mode = 0;
        
        var $prefs;
+       var $cur_con; // current db connection (can be different from $company for superuser)
 
        function current_user()
        {
@@ -153,16 +157,13 @@ class current_user
        {
        global $db_connections;
 
-       $connection = $db_connections[$id == -1 ? $this->company : $id];
+               $this->cur_con = $id == -1 ? $this->company : $id;
+       $connection = $db_connections[$this->cur_con];
 
        //print_r($connection);
 
-       $db = mysql_connect($connection["host"] ,
-               $connection["dbuser"], $connection["dbpassword"]);
-       mysql_select_db($connection["dbname"],$db);
-
-               if (!defined('TB_PREF'))
-                       define('TB_PREF', $connection["tbpref"]);
+               $db = mysql_connect($connection["host"], $connection["dbuser"], $connection["dbpassword"]);
+               mysql_select_db($connection["dbname"], $db);
 
        return $db;
        }
index 5babac1bd4f3676bd5f6d2874df2e37cb6ca736f..1e6c2c4a099d2913cc8ce2fe4ff166b9796cdab0 100644 (file)
     See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
 ***********************************************************************/
 
-function set_global_connection()
+function set_global_connection($company=-1)
 {
        global $db, $transaction_level;
 
        cancel_transaction(); // cancel all aborted transactions if any
        $transaction_level = 0;
 
-       $db = $_SESSION["wa_current_user"]->get_db_connection();
+       $db = $_SESSION["wa_current_user"]->get_db_connection($company);
+
+       return $db;
 }
 
 $db_duplicate_error_code = 1062;
@@ -26,8 +28,13 @@ $db_duplicate_error_code = 1062;
 
 function db_query($sql, $err_msg=null)
 {
-       global $db, $show_sql, $sql_trail, $select_trail, $go_debug, $sql_queries, $Ajax;
+       global $db, $show_sql, $sql_trail, $select_trail, $go_debug, $sql_queries, $Ajax,
+               $db_connections;
        
+       // set current db prefix
+       $cur_prefix = $db_connections[$_SESSION["wa_current_user"]->cur_con]['tbpref'];
+       $sql = str_replace(TB_PREF, $cur_prefix, $sql);
+
        if ($show_sql)
        {
                $Ajax->activate('footer_debug');
@@ -38,7 +45,7 @@ function db_query($sql, $err_msg=null)
        if($sql_trail) {
                if ($select_trail || (strstr($sql, 'SELECT') === false)) {
                        mysql_query(
-                       "INSERT INTO ".TB_PREF."sql_trail
+                       "INSERT INTO ".$cur_prefix."sql_trail
                                (`sql`, `result`, `msg`)
                                VALUES(".db_escape($sql).",".($result ? 1 : 0).",
                                ".db_escape($err_msg).")", $db);
index cafa86f48cb266eb3c492e92b5dfcac95ebeb722..c584d175f79e831d3855d0fb672910e55c8df844 100644 (file)
@@ -108,7 +108,7 @@ function end_flush() {
 
 function display_db_error($msg, $sql_statement=null, $exit=true)
 {
-       global $db, $debug, $go_debug;
+       global $db, $debug, $go_debug, $db_connections;
 
        $warning = $msg==null;
        $db_error = db_error_no();
@@ -127,7 +127,9 @@ function display_db_error($msg, $sql_statement=null, $exit=true)
        
        if ($debug == 1) 
        {
-               $str .= "sql that failed was : " . $sql_statement . "<br>";
+               $cur_prefix = $db_connections[$_SESSION["wa_current_user"]->cur_con]['tbpref'];
+
+               $str .= "sql that failed was : ".str_replace(TB_PREF, $cur_prefix, $sql_statement)."<br>";
                if ($go_debug > 1) display_backtrace();
        }