/**
  * Migrate Products
  */
 public function actionStep5()
 {
     $step = MigrateSteps::model()->find("sorder = 5");
     $result = MigrateSteps::checkStep($step->sorder);
     if ($result['allowed']) {
         //get migrated website ids from session if has
         $migrated_website_ids = isset(Yii::app()->session['migrated_website_ids']) ? Yii::app()->session['migrated_website_ids'] : array();
         //get migrated store_ids from session
         $migrated_store_ids = isset(Yii::app()->session['migrated_store_ids']) ? Yii::app()->session['migrated_store_ids'] : array();
         //get migrated category ids
         $migrated_category_ids = isset(Yii::app()->session['migrated_category_ids']) ? Yii::app()->session['migrated_category_ids'] : array();
         //product types
         $product_type_ids = array('simple', 'configurable', 'grouped', 'virtual', 'bundle', 'downloadable');
         //variables to log
         $migrated_product_type_ids = array();
         $migrated_product_ids = array();
         if (Yii::app()->request->isPostRequest && $step->status == MigrateSteps::STATUS_NOT_DONE) {
             //uncheck foreign key
             Yii::app()->mage2->createCommand("SET FOREIGN_KEY_CHECKS=0")->execute();
             /*
              * Get black list attribute ids
              * We do not migrate bellow attributes
              */
             $entity_type_id = MigrateSteps::getMage1EntityTypeId(MigrateSteps::PRODUCT_TYPE_CODE);
             $checkList = array(MigrateSteps::getMage1AttributeId('custom_design', $entity_type_id) => '', MigrateSteps::getMage1AttributeId('custom_design_from', $entity_type_id) => null, MigrateSteps::getMage1AttributeId('custom_design_to', $entity_type_id) => null, MigrateSteps::getMage1AttributeId('page_layout', $entity_type_id) => '', MigrateSteps::getMage1AttributeId('custom_layout_update', $entity_type_id) => null);
             $keyCheckList = array_keys($checkList);
             $selected_product_types = Yii::app()->request->getPost('product_type_ids', array());
             if ($selected_product_types) {
                 foreach ($selected_product_types as $type_id) {
                     // get products by type_id
                     //catalog_product_entity
                     $products = Mage1CatalogProductEntity::model()->findAll("type_id = '{$type_id}'");
                     if ($products) {
                         foreach ($products as $product) {
                             $product2 = new Mage2CatalogProductEntity();
                             foreach ($product2->attributes as $key => $value) {
                                 if (isset($product->{$key})) {
                                     $product2->{$key} = $product->{$key};
                                 }
                             }
                             $product2->attribute_set_id = MigrateSteps::getMage2AttributeSetId($product2->attribute_set_id, MigrateSteps::PRODUCT_TYPE_CODE);
                             //save or update
                             if ($product2->save()) {
                                 //update to log
                                 $migrated_product_ids[] = $product->entity_id;
                                 //catalog_product_entity_int
                                 $condition = "entity_id = {$product->entity_id}";
                                 if ($migrated_store_ids) {
                                     $str_store_ids = implode(',', $migrated_store_ids);
                                     $condition .= " AND store_id IN ({$str_store_ids})";
                                 }
                                 $models = Mage1CatalogProductEntityInt::model()->findAll($condition);
                                 if ($models) {
                                     foreach ($models as $model) {
                                         //we have to get correct attribute_id & store_id migrated
                                         $store_id2 = MigrateSteps::getMage2StoreId($model->store_id);
                                         $attribute_id2 = MigrateSteps::getMage2AttributeId($model->attribute_id, 4);
                                         if ($attribute_id2) {
                                             // if exists
                                             $model2 = new Mage2CatalogProductEntityInt();
                                             $model2->attribute_id = $attribute_id2;
                                             $model2->store_id = $store_id2;
                                             $model2->entity_id = $model->entity_id;
                                             //we need check and fixed for some attributes
                                             if (in_array($model->attribute_id, $keyCheckList)) {
                                                 $model2->value = $checkList[$model->attribute_id];
                                             } else {
                                                 $model2->value = $model->value;
                                             }
                                             $model2->save();
                                         }
                                     }
                                 }
                                 //catalog_product_entity_text
                                 $condition = "entity_id = {$product->entity_id}";
                                 if ($migrated_store_ids) {
                                     $str_store_ids = implode(',', $migrated_store_ids);
                                     $condition .= " AND store_id IN ({$str_store_ids})";
                                 }
                                 $models = Mage1CatalogProductEntityText::model()->findAll($condition);
                                 if ($models) {
                                     foreach ($models as $model) {
                                         //note: we have to get correct attribute_id & store_id migrated
                                         $store_id2 = MigrateSteps::getMage2StoreId($model->store_id);
                                         $attribute_id2 = MigrateSteps::getMage2AttributeId($model->attribute_id, 4);
                                         if ($attribute_id2) {
                                             $model2 = new Mage2CatalogProductEntityText();
                                             $model2->attribute_id = $attribute_id2;
                                             $model2->store_id = $store_id2;
                                             $model2->entity_id = $model->entity_id;
                                             //we need check and fixed for some attributes
                                             if (in_array($model->attribute_id, $keyCheckList)) {
                                                 $model2->value = $checkList[$model->attribute_id];
                                             } else {
                                                 $model2->value = $model->value;
                                             }
                                             $model2->save();
                                         }
                                     }
                                 }
                                 //catalog_product_entity_varchar
                                 $condition = "entity_id = {$product->entity_id}";
                                 if ($migrated_store_ids) {
                                     $str_store_ids = implode(',', $migrated_store_ids);
                                     $condition .= " AND store_id IN ({$str_store_ids})";
                                 }
                                 $models = Mage1CatalogProductEntityVarchar::model()->findAll($condition);
                                 if ($models) {
                                     foreach ($models as $model) {
                                         //note: we have to get correct attribute_id & store_id migrated
                                         $store_id2 = MigrateSteps::getMage2StoreId($model->store_id);
                                         $attribute_id2 = MigrateSteps::getMage2AttributeId($model->attribute_id, 4);
                                         if ($attribute_id2) {
                                             $model2 = new Mage2CatalogProductEntityVarchar();
                                             $model2->attribute_id = $attribute_id2;
                                             $model2->store_id = $store_id2;
                                             $model2->entity_id = $model->entity_id;
                                             //we need check and fixed for some attributes
                                             if (in_array($model->attribute_id, $keyCheckList)) {
                                                 $model2->value = $checkList[$model->attribute_id];
                                             } else {
                                                 $model2->value = $model->value;
                                             }
                                             $model2->save();
                                         }
                                     }
                                 }
                                 //catalog_product_entity_datetime
                                 $condition = "entity_id = {$product->entity_id}";
                                 if ($migrated_store_ids) {
                                     $str_store_ids = implode(',', $migrated_store_ids);
                                     $condition .= " AND store_id IN ({$str_store_ids})";
                                 }
                                 $models = Mage1CatalogProductEntityDatetime::model()->findAll($condition);
                                 if ($models) {
                                     foreach ($models as $model) {
                                         //we have to get correct attribute_id & store_id migrated
                                         $store_id2 = MigrateSteps::getMage2StoreId($model->store_id);
                                         $attribute_id2 = MigrateSteps::getMage2AttributeId($model->attribute_id, 4);
                                         if ($attribute_id2) {
                                             $model2 = new Mage2CatalogProductEntityDatetime();
                                             $model2->attribute_id = $attribute_id2;
                                             $model2->store_id = $store_id2;
                                             $model2->entity_id = $model->entity_id;
                                             //we need check and fixed for some attributes
                                             if (in_array($model->attribute_id, $keyCheckList)) {
                                                 $model2->value = $checkList[$model->attribute_id];
                                             } else {
                                                 $model2->value = $model->value;
                                             }
                                             $model2->save();
                                         }
                                     }
                                 }
                                 //catalog_product_entity_decimal
                                 $condition = "entity_id = {$product->entity_id}";
                                 if ($migrated_store_ids) {
                                     $str_store_ids = implode(',', $migrated_store_ids);
                                     $condition .= " AND store_id IN ({$str_store_ids})";
                                 }
                                 $models = Mage1CatalogProductEntityDecimal::model()->findAll($condition);
                                 if ($models) {
                                     foreach ($models as $model) {
                                         //we have to get correct attribute_id & store_id migrated
                                         $store_id2 = MigrateSteps::getMage2StoreId($model->store_id);
                                         $attribute_id2 = MigrateSteps::getMage2AttributeId($model->attribute_id, 4);
                                         if ($attribute_id2) {
                                             $model2 = new Mage2CatalogProductEntityDecimal();
                                             $model2->attribute_id = $attribute_id2;
                                             $model2->store_id = $store_id2;
                                             $model2->entity_id = $model->entity_id;
                                             //we need check and fixed for some attributes
                                             if (in_array($model->attribute_id, $keyCheckList)) {
                                                 $model2->value = $checkList[$model->attribute_id];
                                             } else {
                                                 $model2->value = $model->value;
                                             }
                                             $model2->save();
                                         }
                                     }
                                 }
                                 //catalog_product_entity_gallery
                                 $condition = "entity_id = {$product->entity_id}";
                                 if ($migrated_store_ids) {
                                     $str_store_ids = implode(',', $migrated_store_ids);
                                     $condition .= " AND store_id IN ({$str_store_ids})";
                                 }
                                 $models = Mage1CatalogProductEntityGallery::model()->findAll($condition);
                                 if ($models) {
                                     foreach ($models as $model) {
                                         //we have to get correct attribute_id & store_id migrated
                                         $store_id2 = MigrateSteps::getMage2StoreId($model->store_id);
                                         $attribute_id2 = MigrateSteps::getMage2AttributeId($model->attribute_id, 4);
                                         if ($attribute_id2) {
                                             $model2 = new Mage2CatalogProductEntityGallery();
                                             $model2->attribute_id = $attribute_id2;
                                             $model2->store_id = $store_id2;
                                             $model2->entity_id = $model->entity_id;
                                             $model2->position = $model->position;
                                             $model2->value = $model->value;
                                             $model2->save();
                                         }
                                     }
                                 }
                                 //catalog_product_entity_media_gallery
                                 $condition = "entity_id = {$product->entity_id}";
                                 $models = Mage1CatalogProductEntityMediaGallery::model()->findAll($condition);
                                 if ($models) {
                                     foreach ($models as $model) {
                                         //we have to get correct attribute_id migrated
                                         $attribute_id2 = MigrateSteps::getMage2AttributeId($model->attribute_id, 4);
                                         if ($attribute_id2) {
                                             $model2 = new Mage2CatalogProductEntityMediaGallery();
                                             $model2->value_id = $model->value_id;
                                             $model2->attribute_id = $attribute_id2;
                                             $model2->entity_id = $model->entity_id;
                                             $model2->value = $model->value;
                                             if ($model2->save()) {
                                                 //catalog_product_entity_media_gallery_value
                                                 //we have migrate by migrated stores
                                                 if ($migrated_store_ids) {
                                                     foreach ($migrated_store_ids as $store_id) {
                                                         $store_id2 = MigrateSteps::getMage2StoreId($store_id);
                                                         $gallery_value = Mage1CatalogProductEntityMediaGalleryValue::model()->find("value_id = {$model->value_id} AND store_id = {$store_id}");
                                                         if ($gallery_value) {
                                                             $gallery_value2 = new Mage2CatalogProductEntityMediaGalleryValue();
                                                             $gallery_value2->value_id = $gallery_value->value_id;
                                                             $gallery_value2->store_id = $store_id2;
                                                             $gallery_value2->entity_id = $model->entity_id;
                                                             $gallery_value2->label = $gallery_value->label;
                                                             $gallery_value2->position = $gallery_value->position;
                                                             $gallery_value2->disabled = $gallery_value->disabled;
                                                             $gallery_value2->save();
                                                         }
                                                     }
                                                 }
                                             }
                                         }
                                     }
                                 }
                                 //catalog_product_option
                                 $condition = "product_id = {$product->entity_id}";
                                 $product_options = Mage1CatalogProductOption::model()->findAll($condition);
                                 if ($product_options) {
                                     foreach ($product_options as $product_option) {
                                         $product_option2 = new Mage2CatalogProductOption();
                                         foreach ($product_option2->attributes as $key => $value) {
                                             if (isset($product_option->{$key})) {
                                                 $product_option2->{$key} = $product_option->{$key};
                                             }
                                         }
                                         if ($product_option2->save()) {
                                             //catalog_product_option_type_value
                                             $condition = "option_id = {$product_option->option_id}";
                                             $option_type_values = Mage1CatalogProductOptionTypeValue::model()->findAll($condition);
                                             if ($option_type_values) {
                                                 foreach ($option_type_values as $option_type_value) {
                                                     $option_type_value2 = new Mage2CatalogProductOptionTypeValue();
                                                     foreach ($option_type_value2->attributes as $key => $value) {
                                                         if (isset($option_type_value->{$key})) {
                                                             $option_type_value2->{$key} = $option_type_value->{$key};
                                                         }
                                                     }
                                                     if ($option_type_value2->save()) {
                                                         //catalog_product_option_type_price & catalog_product_option_type_title
                                                         if ($migrated_store_ids) {
                                                             foreach ($migrated_store_ids as $store_id) {
                                                                 $store_id2 = MigrateSteps::getMage2StoreId($store_id);
                                                                 //catalog_product_option_type_price
                                                                 $condition = "option_type_id = {$option_type_value->option_type_id} AND store_id = {$store_id}";
                                                                 $option_type_price = Mage1CatalogProductOptionTypePrice::model()->find($condition);
                                                                 if ($option_type_price) {
                                                                     $option_type_price2 = new Mage2CatalogProductOptionTypePrice();
                                                                     foreach ($option_type_price2->attributes as $key => $value) {
                                                                         if (isset($option_type_price->{$key})) {
                                                                             $option_type_price2->{$key} = $option_type_price->{$key};
                                                                         }
                                                                     }
                                                                     $option_type_price2->store_id = $store_id2;
                                                                     $option_type_price2->save();
                                                                 }
                                                                 //catalog_product_option_type_title
                                                                 $condition = "option_type_id = {$option_type_value->option_type_id} AND store_id = {$store_id}";
                                                                 $option_type_title = Mage1CatalogProductOptionTypeTitle::model()->find($condition);
                                                                 if ($option_type_title) {
                                                                     $option_type_title2 = new Mage2CatalogProductOptionTypeTitle();
                                                                     foreach ($option_type_title2->attributes as $key => $value) {
                                                                         if (isset($option_type_title->{$key})) {
                                                                             $option_type_title2->{$key} = $option_type_title->{$key};
                                                                         }
                                                                     }
                                                                     $option_type_title2->store_id = $store_id2;
                                                                     $option_type_title2->save();
                                                                 }
                                                             }
                                                         }
                                                     }
                                                 }
                                             }
                                             //we have to migrate by migrated stores
                                             if ($migrated_store_ids) {
                                                 foreach ($migrated_store_ids as $store_id) {
                                                     $store_id2 = MigrateSteps::getMage2StoreId($store_id);
                                                     //catalog_product_option_price
                                                     $option_price = Mage1CatalogProductOptionPrice::model()->find("option_id = {$product_option->option_id} AND store_id = {$store_id}");
                                                     if ($option_price) {
                                                         $option_price2 = new Mage2CatalogProductOptionPrice();
                                                         foreach ($option_price2->attributes as $key => $value) {
                                                             if (isset($option_price->{$key})) {
                                                                 $option_price2->{$key} = $option_price->{$key};
                                                             }
                                                         }
                                                         $option_price2->store_id = $store_id2;
                                                         $option_price2->save();
                                                     }
                                                     //catalog_product_option_title
                                                     $option_title = Mage1CatalogProductOptionTitle::model()->find("option_id = {$product_option->option_id} AND store_id = {$store_id}");
                                                     if ($option_title) {
                                                         $option_title2 = new Mage2CatalogProductOptionTitle();
                                                         foreach ($option_title2->attributes as $key => $value) {
                                                             if (isset($option_title->{$key})) {
                                                                 $option_title2->{$key} = $option_title->{$key};
                                                             }
                                                         }
                                                         $option_title2->store_id = $store_id2;
                                                         $option_title2->save();
                                                     }
                                                 }
                                             }
                                         }
                                     }
                                 }
                                 //cataloginventory_stock_status
                                 if ($migrated_website_ids) {
                                     foreach ($migrated_website_ids as $website_id) {
                                         $models = Mage1StockStatus::model()->findAll("website_id = {$website_id} AND product_id = {$product->entity_id}");
                                         if ($models) {
                                             foreach ($models as $model) {
                                                 $model2 = new Mage2StockStatus();
                                                 foreach ($model2->attributes as $key => $value) {
                                                     if (isset($model->{$key})) {
                                                         $model2->{$key} = $model->{$key};
                                                     }
                                                 }
                                                 if ($model2->save()) {
                                                     //cataloginventory_stock_item
                                                     $stock_item = Mage1StockItem::model()->find("product_id = {$model->product_id} AND stock_id = {$model->stock_id}");
                                                     if ($stock_item) {
                                                         $stock_item2 = new Mage2StockItem();
                                                         foreach ($stock_item2->attributes as $key => $value) {
                                                             if (isset($stock_item->{$key})) {
                                                                 $stock_item2->{$key} = $stock_item->{$key};
                                                             }
                                                         }
                                                         $stock_item2->website_id = $website_id;
                                                         $stock_item2->save();
                                                     }
                                                 }
                                             }
                                         }
                                     }
                                 }
                                 //url_rewrite
                                 $condition = "product_id = {$product->entity_id}";
                                 if ($migrated_store_ids) {
                                     $str_store_ids = implode(',', $migrated_store_ids);
                                     $condition .= " AND store_id IN ({$str_store_ids})";
                                 }
                                 $urls = Mage1UrlRewrite::model()->findAll($condition);
                                 if ($urls) {
                                     foreach ($urls as $url) {
                                         $store_id2 = MigrateSteps::getMage2StoreId($url->store_id);
                                         $url2 = new Mage2UrlRewrite();
                                         $url2->entity_type = 'product';
                                         $url2->entity_id = $url->product_id;
                                         $url2->request_path = $url->request_path;
                                         $url2->target_path = $url->target_path;
                                         $url2->redirect_type = 0;
                                         $url2->store_id = $store_id2;
                                         $url2->description = $url->description;
                                         $url2->is_autogenerated = $url->is_system;
                                         if ($url->category_id) {
                                             $url2->metadata = serialize(array('category_id' => $url->category_id));
                                         } else {
                                             $url2->metadata = null;
                                         }
                                         if ($url2->save()) {
                                             //catalog_url_rewrite_product_category
                                             $catalog_url2 = new Mage2CatalogUrlRewriteProductCategory();
                                             $catalog_url2->url_rewrite_id = $url2->url_rewrite_id;
                                             $catalog_url2->category_id = $url->category_id;
                                             $catalog_url2->product_id = $url->product_id;
                                             $catalog_url2->save();
                                         }
                                     }
                                 }
                             }
                             //end save a product
                         }
                         // endforeach products
                     }
                     // endif has products
                     //update to log
                     $migrated_product_type_ids[] = $type_id;
                 }
                 //end foreach product types
                 //Start migrate related data with a product
                 if ($migrated_product_ids) {
                     //make string product ids
                     $str_product_ids = implode(',', $migrated_product_ids);
                     //catalog_product_website
                     if ($migrated_website_ids) {
                         $str_website_ids = implode(',', $migrated_website_ids);
                         $condition = "product_id IN ({$str_product_ids}) AND website_id IN ({$str_website_ids})";
                         $models = Mage1CatalogProductWebsite::model()->findAll($condition);
                         if ($models) {
                             foreach ($models as $model) {
                                 $model2 = new Mage2CatalogProductWebsite();
                                 $model2->product_id = $model->product_id;
                                 $model2->website_id = $model->website_id;
                                 $model2->save();
                             }
                         }
                     }
                     //catalog_category_product
                     if ($migrated_category_ids) {
                         foreach ($migrated_category_ids as $category_id) {
                             $condition = "product_id IN ({$str_product_ids}) AND category_id = {$category_id}";
                             $models = Mage1CatalogCategoryProduct::model()->findAll($condition);
                             if ($models) {
                                 foreach ($models as $model) {
                                     $model2 = new Mage2CatalogCategoryProduct();
                                     $model2->category_id = $model->category_id;
                                     $model2->product_id = $model->product_id;
                                     $model2->position = $model->position;
                                     $model2->save();
                                 }
                             }
                         }
                     }
                     //Cross sell, Up sell, Related & Grouped Products
                     /** catalog_product_link_type:
                      * 1 - relation - Related Products
                      * 3 - super - Grouped Products
                      * 4 - up_sell - Up Sell Products
                      * 5 - cross_sell - Cross Sell Products
                      *
                      * Note: Tables: catalog_product_link_type & catalog_product_link_attribute was not changed.
                      * So, we don't migrate these tables. But careful with id was changed in catalog_product_link_attribute
                      */
                     //link type ids to migration
                     //catalog_product_link
                     $link_type_ids = array(1, 4, 5);
                     if (in_array('grouped', $migrated_product_type_ids)) {
                         $link_type_ids[] = 3;
                     }
                     $str_link_type_ids = implode(',', $link_type_ids);
                     $condition = "product_id IN ({$str_product_ids}) AND linked_product_id IN ({$str_product_ids}) AND link_type_id IN ({$str_link_type_ids})";
                     $models = Mage1CatalogProductLink::model()->findAll($condition);
                     if ($models) {
                         foreach ($models as $model) {
                             $model2 = new Mage2CatalogProductLink();
                             $model2->link_id = $model->link_id;
                             $model2->product_id = $model->product_id;
                             $model2->linked_product_id = $model->linked_product_id;
                             $model2->link_type_id = $model->link_type_id;
                             if ($model2->save()) {
                                 //catalog_product_link_attribute_decimal
                                 $condition = "link_id = {$model2->link_id}";
                                 $items = Mage1CatalogProductLinkAttributeDecimal::model()->findAll($condition);
                                 if ($items) {
                                     foreach ($items as $item) {
                                         $item2 = new Mage2CatalogProductLinkAttributeDecimal();
                                         $item2->value_id = $item->value_id;
                                         $item2->product_link_attribute_id = MigrateSteps::getMage2ProductLinkAttrId($item->product_link_attribute_id);
                                         $item2->link_id = $item->link_id;
                                         $item2->value = $item->value;
                                         $item2->save();
                                     }
                                 }
                                 //catalog_product_link_attribute_int
                                 $condition = "link_id = {$model2->link_id}";
                                 $items = Mage1CatalogProductLinkAttributeInt::model()->findAll($condition);
                                 if ($items) {
                                     foreach ($items as $item) {
                                         $item2 = new Mage2CatalogProductLinkAttributeInt();
                                         $item2->value_id = $item->value_id;
                                         $item2->product_link_attribute_id = MigrateSteps::getMage2ProductLinkAttrId($item->product_link_attribute_id);
                                         $item2->link_id = $item->link_id;
                                         $item2->value = $item->value;
                                         $item2->save();
                                     }
                                 }
                                 //catalog_product_link_attribute_varchar
                                 $condition = "link_id = {$model2->link_id}";
                                 $items = Mage1CatalogProductLinkAttributeVarchar::model()->findAll($condition);
                                 if ($items) {
                                     foreach ($items as $item) {
                                         $item2 = new Mage2CatalogProductLinkAttributeVarchar();
                                         $item2->value_id = $item->value_id;
                                         $item2->product_link_attribute_id = MigrateSteps::getMage2ProductLinkAttrId($item->product_link_attribute_id);
                                         $item2->link_id = $item->link_id;
                                         $item2->value = $item->value;
                                         $item2->save();
                                     }
                                 }
                             }
                         }
                     }
                     //End Cross sell, Up sell, Related & Grouped Products
                     //Configurable products
                     if (in_array('configurable', $migrated_product_type_ids)) {
                         //catalog_product_super_link
                         $condition = "product_id IN ({$str_product_ids}) AND parent_id IN ({$str_product_ids})";
                         $models = Mage1CatalogProductSuperLink::model()->findAll($condition);
                         if ($models) {
                             foreach ($models as $model) {
                                 $model2 = new Mage2CatalogProductSuperLink();
                                 $model2->link_id = $model->link_id;
                                 $model2->product_id = $model->product_id;
                                 $model2->parent_id = $model->parent_id;
                                 $model2->save();
                             }
                         }
                         //catalog_product_relation
                         $condition = "parent_id IN ({$str_product_ids}) AND child_id IN ({$str_product_ids})";
                         $models = Mage1CatalogProductRelation::model()->findAll($condition);
                         if ($models) {
                             foreach ($models as $model) {
                                 $model2 = new Mage2CatalogProductRelation();
                                 $model2->parent_id = $model->parent_id;
                                 $model2->child_id = $model->child_id;
                                 $model2->save();
                             }
                         }
                         //catalog_product_super_attribute
                         $condition = "product_id IN ({$str_product_ids})";
                         $models = Mage1CatalogProductSuperAttribute::model()->findAll($condition);
                         if ($models) {
                             foreach ($models as $model) {
                                 $model2 = new Mage2CatalogProductSuperAttribute();
                                 $model2->product_super_attribute_id = $model->product_super_attribute_id;
                                 $model2->product_id = $model->product_id;
                                 $model2->attribute_id = MigrateSteps::getMage2AttributeId($model->attribute_id, 4);
                                 $model2->position = $model->position;
                                 if ($model2->save()) {
                                     //catalog_product_super_attribute_label
                                     $condition = "product_super_attribute_id = {$model2->product_super_attribute_id}";
                                     if ($migrated_store_ids) {
                                         $str_store_ids = implode(',', $migrated_store_ids);
                                         $condition .= " AND store_id IN ({$str_store_ids})";
                                     }
                                     $super_attribute_labels = Mage1CatalogProductSuperAttributeLabel::model()->findAll($condition);
                                     if ($super_attribute_labels) {
                                         foreach ($super_attribute_labels as $super_attribute_label) {
                                             $store_id2 = MigrateSteps::getMage2StoreId($super_attribute_label->store_id);
                                             $super_attribute_label2 = new Mage2CatalogProductSuperAttributeLabel();
                                             $super_attribute_label2->value_id = $super_attribute_label->value_id;
                                             $super_attribute_label2->product_super_attribute_id = $super_attribute_label->product_super_attribute_id;
                                             $super_attribute_label2->store_id = $store_id2;
                                             $super_attribute_label2->use_default = $super_attribute_label->use_default;
                                             $super_attribute_label2->value = $super_attribute_label->value;
                                             $super_attribute_label2->save();
                                         }
                                     }
                                     //catalog_product_super_attribute_pricing
                                     //This was not used from merchant beta 100
                                     //                                        $condition = "product_super_attribute_id = {$model2->product_super_attribute_id}";
                                     //                                        if ($migrated_website_ids) {
                                     //                                            $str_website_ids = implode(',', $migrated_website_ids);
                                     //                                            $condition .= " AND website_id IN ({$str_website_ids})";
                                     //                                        }
                                     //                                        $super_attribute_pricing_models = Mage1CatalogProductSuperAttributePricing::model()->findAll($condition);
                                     //                                        if ($super_attribute_pricing_models) {
                                     //                                            foreach ($super_attribute_pricing_models as $super_attribute_pricing) {
                                     //                                                $super_attribute_pricing2 = new Mage2CatalogProductSuperAttributePricing();
                                     //                                                $super_attribute_pricing2->value_id = $super_attribute_pricing->value_id;
                                     //                                                $super_attribute_pricing2->product_super_attribute_id = $super_attribute_pricing->product_super_attribute_id;
                                     //                                                $super_attribute_pricing2->value_index = $super_attribute_pricing->value_index;
                                     //                                                $super_attribute_pricing2->is_percent = $super_attribute_pricing->is_percent;
                                     //                                                $super_attribute_pricing2->pricing_value = $super_attribute_pricing->pricing_value;
                                     //                                                $super_attribute_pricing2->website_id = $super_attribute_pricing->website_id;
                                     //                                                $super_attribute_pricing2->save();
                                     //                                            }
                                     //                                        }
                                 }
                             }
                         }
                     }
                     //End Configurable products
                     //Bundle products
                     if (in_array('bundle', $migrated_product_type_ids)) {
                         //catalog_product_bundle_option
                         $condition = "parent_id IN ({$str_product_ids})";
                         $models = Mage1CatalogProductBundleOption::model()->findAll($condition);
                         if ($models) {
                             foreach ($models as $model) {
                                 $model2 = new Mage2CatalogProductBundleOption();
                                 $model2->option_id = $model->option_id;
                                 $model2->parent_id = $model->parent_id;
                                 $model2->required = $model->required;
                                 $model2->position = $model->position;
                                 $model2->type = $model->type;
                                 if ($model2->save()) {
                                     //catalog_product_bundle_option_value
                                     $condition = "option_id = {$model2->option_id}";
                                     if ($migrated_store_ids) {
                                         $str_store_ids = implode(',', $migrated_store_ids);
                                         $condition .= " AND store_id IN ({$str_store_ids})";
                                     }
                                     $bundle_option_values = Mage1CatalogProductBundleOptionValue::model()->findAll($condition);
                                     if ($bundle_option_values) {
                                         foreach ($bundle_option_values as $bundle_option_value) {
                                             $bundle_option_value2 = new Mage2CatalogProductBundleOptionValue();
                                             $bundle_option_value2->value_id = $bundle_option_value->value_id;
                                             $bundle_option_value2->option_id = $bundle_option_value->option_id;
                                             $bundle_option_value2->store_id = MigrateSteps::getMage2StoreId($bundle_option_value->store_id);
                                             $bundle_option_value2->title = $bundle_option_value->title;
                                             $bundle_option_value2->save();
                                         }
                                     }
                                     //catalog_product_bundle_selection
                                     $condition = "option_id = {$model2->option_id} AND product_id IN ({$str_product_ids})";
                                     $bundle_selections = Mage1CatalogProductBundleSelection::model()->findAll($condition);
                                     if ($bundle_selections) {
                                         foreach ($bundle_selections as $bundle_selection) {
                                             $bundle_selection2 = new Mage2CatalogProductBundleSelection();
                                             $bundle_selection2->selection_id = $bundle_selection->selection_id;
                                             $bundle_selection2->option_id = $bundle_selection->option_id;
                                             $bundle_selection2->parent_product_id = $bundle_selection->parent_product_id;
                                             $bundle_selection2->product_id = $bundle_selection->product_id;
                                             $bundle_selection2->position = $bundle_selection->position;
                                             $bundle_selection2->is_default = $bundle_selection->is_default;
                                             $bundle_selection2->selection_price_type = $bundle_selection->selection_price_type;
                                             $bundle_selection2->selection_price_value = $bundle_selection->selection_price_value;
                                             $bundle_selection2->selection_qty = $bundle_selection->selection_qty;
                                             $bundle_selection2->selection_can_change_qty = $bundle_selection->selection_can_change_qty;
                                             if ($bundle_selection2->save()) {
                                                 if ($migrated_website_ids) {
                                                     $str_website_ids = implode(',', $migrated_website_ids);
                                                     //catalog_product_bundle_selection_price
                                                     $condition = "selection_id = {$bundle_selection2->selection_id} AND website_id IN ({$str_website_ids})";
                                                     $selection_prices = Mage1CatalogProductBundleSelectionPrice::model()->findAll($condition);
                                                     if ($selection_prices) {
                                                         foreach ($selection_prices as $selection_price) {
                                                             $selection_price2 = new Mage2CatalogProductBundleSelectionPrice();
                                                             $selection_price2->selection_id = $selection_price->selection_id;
                                                             $selection_price2->website_id = $selection_price->website_id;
                                                             $selection_price2->selection_price_type = $selection_price->selection_price_type;
                                                             $selection_price2->selection_price_value = $selection_price->selection_price_value;
                                                             $selection_price2->save();
                                                         }
                                                     }
                                                 }
                                             }
                                         }
                                     }
                                 }
                             }
                         }
                     }
                     //End Bundle products
                     //Downloadable products
                     if (in_array('downloadable', $migrated_product_type_ids)) {
                         //downloadable_link
                         $condition = "product_id IN ({$str_product_ids})";
                         $models = Mage1DownloadableLink::model()->findAll($condition);
                         if ($models) {
                             foreach ($models as $model) {
                                 $model2 = new Mage2DownloadableLink();
                                 foreach ($model2->attributes as $key => $value) {
                                     if (isset($model->{$key})) {
                                         $model2->{$key} = $model->{$key};
                                     }
                                 }
                                 if ($model2->save()) {
                                     if ($migrated_website_ids) {
                                         //downloadable_link_price
                                         $str_website_ids = implode(',', $migrated_website_ids);
                                         $condition = "link_id = {$model2->link_id} AND website_id IN ({$str_website_ids})";
                                         $link_prices = Mage1DownloadableLinkPrice::model()->findAll($condition);
                                         if ($link_prices) {
                                             foreach ($link_prices as $link_price) {
                                                 $link_price2 = new Mage2DownloadableLinkPrice();
                                                 $link_price2->price_id = $link_price->price_id;
                                                 $link_price2->link_id = $link_price->link_id;
                                                 $link_price2->website_id = $link_price->website_id;
                                                 $link_price2->price = $link_price->price;
                                                 $link_price2->save();
                                             }
                                         }
                                         //downloadable_link_title
                                         if ($migrated_store_ids) {
                                             $str_store_ids = implode(',', $migrated_store_ids);
                                             $condition = "link_id = {$model2->link_id} AND store_id IN ({$str_store_ids})";
                                             $link_titles = Mage1DownloadableLinkTitle::model()->findAll($condition);
                                             if ($link_titles) {
                                                 foreach ($link_titles as $link_title) {
                                                     $link_title2 = new Mage2DownloadableLinkTitle();
                                                     $link_title2->title_id = $link_title->title_id;
                                                     $link_title2->link_id = $link_title->link_id;
                                                     $link_title2->store_id = MigrateSteps::getMage2StoreId($link_title->store_id);
                                                     $link_title2->title = $link_title->title;
                                                     $link_title2->save();
                                                 }
                                             }
                                         }
                                         //downloadable_sample
                                         //downloadable_sample_title
                                     }
                                 }
                             }
                         }
                     }
                     //End Downloadable products
                 }
                 //End migrate related data with a product
             } else {
                 Yii::app()->user->setFlash('note', Yii::t('frontend', 'You have to select at least one Product type to migrate.'));
             }
             //Update step status
             if ($migrated_product_type_ids) {
                 $step->status = MigrateSteps::STATUS_DONE;
                 $step->migrated_data = json_encode(array('product_type_ids' => $migrated_product_type_ids, 'product_ids' => $migrated_product_ids));
                 if ($step->update()) {
                     //Update session
                     Yii::app()->session['migrated_product_type_ids'] = $migrated_product_type_ids;
                     Yii::app()->session['migrated_product_ids'] = $migrated_product_ids;
                     //check foreign key
                     Yii::app()->mage2->createCommand("SET FOREIGN_KEY_CHECKS=1")->execute();
                     $message = "Migrated successfully. Total Products migrated: %s1";
                     $message = Yii::t('frontend', $message, array('%s1' => sizeof($migrated_product_ids)));
                     Yii::app()->user->setFlash('success', $message);
                 }
             }
         } 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, 'product_type_ids' => $product_type_ids);
         $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']));
     }
 }
 /**
  * get Category Name in Magento1
  * @param $category_id
  * @return null|string
  */
 public static function getMage1CategoryName($category_id)
 {
     $name = null;
     if ($category_id) {
         $cacheId = "category_name1_{$category_id}";
         $val = Yii::app()->cache->get($cacheId);
         if (!$val) {
             $entity_type_id = MigrateSteps::getMage1EntityTypeId(self::CATEGORY_TYPE_CODE);
             $attribute_id = MigrateSteps::getMage1AttributeId('name', $entity_type_id);
             $model = Mage1CatalogCategoryEntityVarchar::model()->find("entity_id = {$category_id} AND attribute_id = {$attribute_id}");
             if ($model) {
                 $name = $model->value;
             }
             //save to cache for later
             Yii::app()->cache->set($cacheId, $name, 86400);
             // one day
         } else {
             $name = $val;
         }
     }
     return $name;
 }
 /**
  * get Category Name in Magento1
  * @param $category_id
  * @return null|string
  */
 public static function getMage1CategoryName($category_id)
 {
     $name = null;
     if ($category_id) {
         $entity_type_id = MigrateSteps::getMage1EntityTypeId(self::CATEGORY_TYPE_CODE);
         $attribute_id = MigrateSteps::getMage1AttributeId('name', $entity_type_id);
         $model = Mage1CatalogCategoryEntityVarchar::model()->find("entity_id = {$category_id} AND attribute_id = {$attribute_id}");
         if ($model) {
             $name = $model->value;
         }
     }
     return $name;
 }