public static function getMage2StoreId($mage1StoreId)
 {
     $id = NULL;
     if (isset($mage1StoreId)) {
         if (is_null($mage1StoreId)) {
             $id = 'NULL';
         } else {
             $cacheId = "store_id2_{$mage1StoreId}";
             $val = Yii::app()->cache->get($cacheId);
             if (!$val) {
                 $store1 = Mage1Store::model()->find("store_id = {$mage1StoreId}");
                 if ($store1) {
                     $store2 = Mage2Store::model()->find("code = '{$store1->code}'");
                     if ($store2) {
                         $id = $store2->store_id;
                     }
                 } else {
                     $id = $mage1StoreId;
                 }
                 //save to cache for later
                 Yii::app()->cache->set($cacheId, $id, 86400);
                 // one day
             } else {
                 $id = $val;
             }
         }
     }
     return $id;
 }
 public static function getMage2StoreId($mage1StoreId)
 {
     $id = NULL;
     if (isset($mage1StoreId)) {
         if (is_null($mage1StoreId)) {
             $id = 'NULL';
         } else {
             $store1 = Mage1Store::model()->find("store_id = {$mage1StoreId}");
             if ($store1) {
                 $store2 = Mage2Store::model()->find("code = '{$store1->code}'");
                 if ($store2) {
                     $id = $store2->store_id;
                 }
             } else {
                 $id = $mage1StoreId;
             }
         }
     }
     return $id;
 }
 /**
  * Migrate Websites & Store groups & Store views
  */
 public function actionStep2()
 {
     $step = MigrateSteps::model()->find("sorder = 2");
     $result = MigrateSteps::checkStep($step->sorder);
     if ($result['allowed']) {
         //variables to log
         $migrated_website_ids = $migrated_store_group_ids = $migrated_store_ids = array();
         //Get list front-end websites from magento1
         $condition = "code <> 'admin'";
         $websites = Mage1Website::model()->findAll($condition);
         if (Yii::app()->request->isPostRequest && $step->status == MigrateSteps::STATUS_NOT_DONE) {
             //uncheck foreign key
             Yii::app()->mage2->createCommand("SET FOREIGN_KEY_CHECKS=0")->execute();
             //start migrate process
             $website_ids = Yii::app()->request->getParam('website_ids', array());
             $store_group_ids = Yii::app()->request->getParam('store_group_ids', array());
             $store_ids = Yii::app()->request->getParam('store_ids', array());
             // if has selected websites, store groups, stores
             if (sizeof($website_ids) > 0 and sizeof($store_group_ids) > 0 and sizeof($store_ids) > 0) {
                 foreach ($websites as $website) {
                     if (in_array($website->website_id, $website_ids)) {
                         $website2 = Mage2Website::model()->find("code = '{$website->code}'");
                         if (!$website2) {
                             // if not found
                             $website2 = new Mage2Website();
                         }
                         $website2->website_id = $website->website_id;
                         $website2->code = $website->code;
                         $website2->name = $website->name;
                         $website2->sort_order = $website->sort_order;
                         $website2->default_group_id = $website->default_group_id;
                         $website2->is_default = $website->is_default;
                         if ($website2->save()) {
                             //update to log
                             $migrated_website_ids[] = $website->website_id;
                             if ($store_group_ids && isset($store_group_ids[$website->website_id])) {
                                 //Migrate store group of this website
                                 $str_group_ids = implode(',', $store_group_ids[$website->website_id]);
                                 $condition = "website_id = {$website->website_id} AND group_id IN ({$str_group_ids})";
                                 $storeGroups = Mage1StoreGroup::model()->findAll($condition);
                                 if ($storeGroups) {
                                     foreach ($storeGroups as $storeGroup) {
                                         $condition = "website_id = {$website->website_id} AND group_id = {$storeGroup->group_id}";
                                         $storeGroup2 = Mage2StoreGroup::model()->find($condition);
                                         if (!$storeGroup2) {
                                             $storeGroup2 = new Mage2StoreGroup();
                                         }
                                         $storeGroup2->group_id = $storeGroup->group_id;
                                         $storeGroup2->website_id = $storeGroup->website_id;
                                         $storeGroup2->name = $storeGroup->name;
                                         $storeGroup2->root_category_id = $storeGroup->root_category_id;
                                         $storeGroup2->default_store_id = $storeGroup->default_store_id;
                                         if ($storeGroup2->save()) {
                                             //update to log
                                             $migrated_store_group_ids[] = $storeGroup->group_id;
                                             if ($store_ids && isset($store_ids[$storeGroup->group_id])) {
                                                 //Migrate stores of current store group
                                                 $str_store_ids = implode(',', $store_ids[$storeGroup->group_id]);
                                                 $condition = "website_id = {$website->website_id} AND store_id IN ({$str_store_ids})";
                                                 $stores = Mage1Store::model()->findAll($condition);
                                                 if ($stores) {
                                                     foreach ($stores as $store) {
                                                         $condition = "code = '{$store->code}'";
                                                         $store2 = Mage2Store::model()->find($condition);
                                                         if (!$store2) {
                                                             $store2 = new Mage2Store();
                                                         }
                                                         $store2->code = $store->code;
                                                         $store2->website_id = $store->website_id;
                                                         $store2->group_id = $store->group_id;
                                                         $store2->name = $store->name;
                                                         $store2->sort_order = $store->sort_order;
                                                         $store2->is_active = $store->is_active;
                                                         if ($store2->save()) {
                                                             //update to log
                                                             $migrated_store_ids[] = $store->store_id;
                                                         }
                                                     }
                                                 }
                                             }
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
                 //Update step status
                 if ($migrated_website_ids || $migrated_store_group_ids || $migrated_store_ids) {
                     $step->status = MigrateSteps::STATUS_DONE;
                     //add admin website id
                     array_push($migrated_website_ids, '0');
                     //add store group admin
                     array_push($migrated_store_group_ids, '0');
                     //add admin store id
                     array_push($migrated_store_ids, '0');
                     $migrated_data = array('website_ids' => $migrated_website_ids, 'store_group_ids' => $migrated_store_group_ids, 'store_ids' => $migrated_store_ids);
                     $step->migrated_data = json_encode($migrated_data);
                     if ($step->update()) {
                         //Update session
                         Yii::app()->session['migrated_website_ids'] = $migrated_website_ids;
                         Yii::app()->session['migrated_store_group_ids'] = $migrated_store_group_ids;
                         Yii::app()->session['migrated_store_ids'] = $migrated_store_ids;
                         //check foreign key
                         Yii::app()->mage2->createCommand("SET FOREIGN_KEY_CHECKS=1")->execute();
                         //alert message
                         $message = "Migrated successfully. Total Website: %s1, Total Store Groups: %s2, Total Store Views: %s3";
                         $message = Yii::t('frontend', $message, array('%s1' => sizeof($migrated_website_ids) - 1, '%s2' => sizeof($migrated_store_group_ids) - 1, '%s3' => sizeof($migrated_store_ids) - 1));
                         Yii::app()->user->setFlash('success', $message);
                     }
                 }
             } else {
                 Yii::app()->user->setFlash('note', Yii::t('frontend', 'You have to select at least one website, one store group, one store to migrate.'));
             }
         } else {
             if ($step->status == MigrateSteps::STATUS_DONE) {
                 Yii::app()->user->setFlash('note', Yii::t('frontend', "This step was finished. If you want to update data of this step, the first you have to click to 'Reset' button."));
             }
         }
         $assign_data = array('step' => $step, 'websites' => $websites);
         $this->render("step{$step->sorder}", $assign_data);
     } else {
         Yii::app()->user->setFlash('note', Yii::t('frontend', "The first you need to finish the %s.", array("%s" => ucfirst($result['back_step']))));
         $this->redirect(array($result['back_step']));
     }
 }