Bug in the migrator: calculates wrong state_id from VM1 to VM2 migration

Started by Thomas Kuschel, April 24, 2012, 01:08:11 AM

Previous topic - Next topic

Thomas Kuschel

Hello,

Version: VM 2.0.6; latest snapshot from VirtueMart-virtuemart-16ad754
Description:
During migration from VM1 to VM2, the table #__vm_user_info should migrate to #__virtuemart_userinfos with data of user's country and state. Unfortunately, the user's state not only changed to 0 (no state) but also mysterious values are shown: I have users all over the world (# 6000 customers). Several users from Washington, DC - they live in Washington, Catanzaro now. I can't believe, it is in Italy!  ;)

Bug:
administrator/components/com_virtuemart/helpers/migrator.php
Inside function portUsers(), the private function getCountryIdByCode() is called twice for user's country and user's state. But the user's state should be called via getStateIdByCode(). However the private function getStateByCode() is corrupt too.

Solution:
Remove both private functions getCountryIdByCode() and getStateByCode(). Usage of class ShopFunctions::getCountryIDByName() and getStateIDByName().

Patch:
Using Linux, save the following code in the migrator.php's folder as migrator-patch.php
Call the patch: $ cat migrator-patch.php | patch -p0

--- old/migrator.php 2012-04-23 23:06:57.700221222 +0200
+++ migrator.php 2012-04-23 23:46:30.329039342 +0200
@@ -498,6 +498,8 @@
$this->_stop = true;
return false;
}
+ //needed for ShopFunctions:
+ if (!class_exists('ShopFunctions')) require(JPATH_VM_ADMINISTRATOR . DS . 'helpers' . DS . 'shopfunctions.php');
//declaration _vm_userfield >> _virtuemart_userfields`

// vendor_id >> virtuemart_vendor_id
@@ -578,8 +580,8 @@

foreach($oldUsers as $user){

- $user['virtuemart_country_id'] = $this->getCountryIdByCode($user['country']);
- $user['virtuemart_state_id'] = $this->getCountryIdByCode($user['state']);
+ $user['virtuemart_country_id'] = ShopFunctions::getCountryIDByName($user['country']);
+ $user['virtuemart_state_id'] = ShopFunctions::getStateIDByName($user['state']);

if(!empty($user['shopper_group_id'])){
$user['virtuemart_shoppergroups_id'] = $oldToNewShoppergroups[$user['shopper_group_id']];
@@ -666,8 +668,8 @@
// if(!array_key_exists($oldcategory['virtuemart_userinfo_id'],$alreadyKnownIds)){
$oldUsersAddi['virtuemart_user_id'] = $oldUsersAddi['user_id'];

- $oldUsersAddi['virtuemart_country_id'] = $this->getCountryIdByCode($oldUsersAddi['country']);
- $oldUsersAddi['virtuemart_state_id'] = $this->getCountryIdByCode($oldUsersAddi['state']);
+ $oldUsersAddi['virtuemart_country_id'] = ShopFunctions::getCountryIDByName($oldUsersAddi['country']);
+ $oldUsersAddi['virtuemart_state_id'] = ShopFunctions::getStateIDByName($oldUsersAddi['state']);

$userfielddata = $userModel->_prepareUserFields($oldUsersAddi, 'ST');

@@ -1563,56 +1565,6 @@
if(!empty($limit)) return $limit; else return 0;
}

- /**
- * Gets the virtuemart_country_id by a country 2 or 3 code
- *
- * @author Max Milbers
- * @param string $name Country 3 or Country 2 code (example US for United States)
- * return int virtuemart_country_id
- */
- private function getCountryIdByCode($name){
- if(empty($name)){
- return 0;
- }
-
- if(strlen($name) == 2){
- $countryCode = 'country_2_code';
- }else {
- $countryCode = 'country_3_code';
- }
-
- $q = 'SELECT `virtuemart_country_id` FROM `#__virtuemart_countries`
- WHERE `' . $countryCode . '` = "' . $this->_db->getEscaped($name) . '" ';
- $this->_db->setQuery($q);
-
- return $this->_db->loadResult();
- }
-
- /**
- * Gets the virtuemart_country_id by a country 2 or 3 code
- *
- * @author Max Milbers
- * @param string $name Country 3 or Country 2 code (example US for United States)
- * return int virtuemart_country_id
- */
- private function getStateIdByCode($name){
- if(empty($name)){
- return 0;
- }
-
- if(strlen($name) == 2){
- $code = 'country_2_code';
- }else {
- $code = 'country_3_code';
- }
-
- $q = 'SELECT `virtuemart_state_id` FROM `#__virtuemart_states`
- WHERE `' . $code . '` = "' . $this->_db->getEscaped($name) . '" ';
- $this->_db->setQuery($q);
-
- return $this->_db->loadResult();
- }
-
private function getCurrencyIdByCode($name){
if(empty($name)){
return 0;

Or unzip the attachment.

Best regards, and thx to the community Virtuemart

Thomas

[attachment cleanup by admin]

Milbo

So in short, we use accidently the country function for the states and you wonder, why we not directly use the functions already provided in the shopfunctions. Thanks, I take a look.
Should I fix your bug, please support the VirtueMart project and become a member
______________________________________
Extensions approved by the core team: http://extensions.virtuemart.net/

Thomas Kuschel

Yes, I wondered, in the same file, the shopfuctions already were in use. 73 Thomas