<?php
/*
    Customers Migration v 1.0
    from VirtueMart 1.0.6 on Joomla! 1.0.10
    to xt:Commerce v3.0.4 SP2.1

    Autor:  Russlan Akiev 
            aruss.labs - www.aruss.net        

    This program is distributed WITHOUT ANY WARRANTY;
    without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    See the GNU General Public License for more details. 
    http://www.gnu.org/copyleft/gpl.html

*/

    /* SETUP */ 
    // Joomla/VirtueMart db 
    
$db1_name   "";  
    
$db1_prefix ""
    
// xtCommerce db, If you have only one data base leave this setting empty    
    
$db2_name   "";
    
$db2_prefix ""
    
    
$host ""
    
$user ""
    
$pass ""

    if (
$db2_name == "")
        
$db2_name $db1_name
    
/* END SETUP */ 

    
$conn mysql_connect($host$user$pass) or die ('Error: connecting to mysql');
    
mysql_select_db($db1_name);
    
    
$joomla_users_table get_table('SELECT * FROM '.$db1_name.'.'.$db1_prefix.'users WHERE '.$db1_name.'.'.$db1_prefix.'users.usertype = "Registered"');
    
$vm_user_info_table get_table('SELECT * FROM '.$db1_name.'.'.$db1_prefix.'vm_user_info'); 
        
    if (
$db1_name != $db2_name
    {
        
mysql_close($conn);
        
$conn mysql_connect($host$user$pass) or die ('Error: connecting to mysql');
        
mysql_select_db($db2_name); 
    }
    
    
$xt_countries get_countries(); 

    foreach(
$joomla_users_table as $row)
    {
        
$info_for_user_id get_infos_for_user_id($row['id']);
        
// add customer data 
        
$xt_customers_row['customers_gender'] = get_gender($info_for_user_id[0]['title']);
        
$xt_customers_row['customers_firstname'] = $info_for_user_id[0]['first_name'];
        
$xt_customers_row['customers_lastname'] = $info_for_user_id[0]['last_name'];
        
$xt_customers_row['customers_email_address'] = $row['email'];
        
$xt_customers_row['customers_status'] = 2// new customer 
        
$xt_customers_row['customers_default_address_id'] = 0
        
$xt_customers_row['customers_telephone'] = $info_for_user_id[0]['phone_1'];
        
$xt_customers_row['customers_fax'] = $info_for_user_id[0]['fax'];
        
$xt_customers_row['customers_password'] = $row['password']; 
        
$xt_customers_row['password_request_key'] = strtoupper($info_for_user_id[0]['user_info_id']);
        
$xt_customers_row['payment_unallowed'] = '';
        
$xt_customers_row['shipping_unallowed'] = '';
        
$xt_customers_row['customers_date_added'] = $row['registerDate'];
        
$xt_customers_row['customers_last_modified'] = date("Y-m-d H:i:s"); 
        
        
$customer_id insert_row($db2_prefix.'customers'$xt_customers_row); 
        
        
// add customer_info data
        
$xt_customer_info_row['customers_info_id'] = $customer_id
        
$xt_customer_info_row['customers_info_number_of_logons'] = 0
        
$xt_customer_info_row['customers_info_date_account_created'] = $row['registerDate'];
        
        
insert_row($db2_prefix.'customers_info'$xt_customer_info_row); 
        
        
// add address_book data 
        
foreach($info_for_user_id as $info_row)
        {
            
$xt_address_row['customers_id'] = $customer_id
            
$xt_address_row['entry_gender'] =  get_gender($info_row['title']); 
            
$xt_address_row['entry_company'] = $info_row['company'];
            
$xt_address_row['entry_firstname'] = $info_row['first_name'];
            
$xt_address_row['entry_lastname'] = $info_row['last_name'];
            
$xt_address_row['entry_street_address'] = $info_row['address_1'];
            
$xt_address_row['entry_suburb'] = $info_row[''];
            
$xt_address_row['entry_postcode'] = $info_row['zip'];
            
$xt_address_row['entry_city'] = $info_row['city'];
            
$xt_address_row['entry_state'] = $info_row['state'];
            
$xt_address_row['entry_country_id'] = $xt_countries_table[$info_row['country']];
            
$xt_address_row['entry_zone_id'] = $info_row[''];
            
$xt_address_row['address_date_added'] = $info_row[''];
            
$xt_address_row['address_last_modified'] = $info_row['country'];

            
$address_id insert_row($db2_prefix.'address_book'$xt_address_row);         
        }
        
mysql_query('UPDATE customers SET customers_default_address_id = '.$address_id.' WHERE customers_id = '.$customer_id); 
    }    
    
mysql_close($conn);
    
    print 
'<h1>DONE !!!</h1>I hope you did a data base backup ;)'

    function 
get_countries()
    {
        
$rtn = array();
        
$xt_countries_table get_table('SELECT countries_id, countries_iso_code_3 FROM '.$db2_name.'.'.$db2_prefix.'countries'); 
        
$ckey ''
        
$cvalue ''
        foreach(
$xt_countries_table as $countries_row)
        {
            foreach (
$countries_row as $key => $value
            {
                if (
$key == 'countries_id')
                    
$cvalue $value
                if (
$key == 'countries_iso_code_3')
                    
$ckey $value
            }
            
$rtn[$ckey] = $cvalue
        }
        
$xt_countries_table null
        return 
$rtn
    }
        
    function 
insert_row($table_name null$row null
    {
        if (
$row == null || $table_name == null
            return 
null;        
        
        
$keys ''
        
$values ''
        
        foreach (
$row as $key => $value
        {
            
$keys .= $key.','
            
$values .= "'$value',"
        }
        
        
$query 'INSERT INTO '.$table_name.' ('substr($keys0, -1).') values ('substr($values0, -1).')'
        
mysql_query($query);
        return 
mysql_insert_id(); 
    }

    function 
get_infos_for_user_id($id null)
    {
        if (
$id == null)
            return 
null
            
        global 
$vm_user_info_table
        
$result = array(); 
        
        foreach(
$vm_user_info_table as $row)
            if (
$row['user_id'] == $id)
                
array_push($result$row);                 
        
        return 
$result
    }
    
    function 
get_table($query null
    {
        if (
$query == null
            return 
null
            
        
$tbl = array(); 
        
$result mysql_query($query);
        while(
$row mysql_fetch_array($resultMYSQL_ASSOC))
            
array_push($tbl$row); 
        
mysql_free_result($result);
        return 
$tbl;         
    }
    
    function 
get_gender($title null)
    {
        
//extend this 
        
        
switch($title)    
        {
            case 
"Herr": return "m";
            case 
"Frau": return "w";
            default: return 
""
        }
    }
    
    
// DEBUG FUNCTION 
    
function print_2D_array_as_table($arr null)
    {
        if (
$arr == null
            return; 

        
$print_head true
        print 
"<table border='1'>";
         foreach(
$arr as $row)
        {
            print 
"<tr>"
            if (
$print_head
            {
                foreach (
$row as $key => $value
                    if (
$print_head)
                    {
                        print 
"<td><b>"
                        print 
$key
                        print 
"</b></td>"
                    }
                print 
"</tr><tr>"
            }
            
            foreach (
$row as $key => $value
            {
                print 
"<td>"
                if (
$value == "")
                    print 
"&nbsp;"
                else
                    print 
$value
                print 
"</td>"
                
            }
            print 
"</tr>"
            
$print_head false
        }
        print 
"</table>";
    }

?>