/**
  * Migrate Categories
  */
 public function actionStep4()
 {
     $step = MigrateSteps::model()->find("sorder = 4");
     $result = MigrateSteps::checkStep($step->sorder);
     if ($result['allowed']) {
         //variables to log
         $migrated_category_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 all categories from magento1 with level > 0
         $categories = Mage1CatalogCategoryEntity::model()->findAll("level > 0");
         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 all categories from magento1
             $categories = Mage1CatalogCategoryEntity::model()->findAll();
             /*
              * Get black list attribute ids
              * We do not migrate bellow attributes
              */
             $entity_type_id = MigrateSteps::getMage1EntityTypeId(MigrateSteps::CATEGORY_TYPE_CODE);
             $checkList = array(MigrateSteps::getMage1AttributeId('display_mode', $entity_type_id) => 'PRODUCTS', MigrateSteps::getMage1AttributeId('landing_page', $entity_type_id) => '', 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) => '', MigrateSteps::getMage1AttributeId('custom_apply_to_products', $entity_type_id) => 1, MigrateSteps::getMage1AttributeId('custom_use_parent_settings', $entity_type_id) => 1);
             $keyCheckList = array_keys($checkList);
             //handle selected category ids
             $category_ids = Yii::app()->request->getPost('category_ids', array());
             //catalog_category_entity
             // if has selected category to migrate
             if ($category_ids) {
                 if ($categories) {
                     foreach ($categories as $category) {
                         if (in_array($category->entity_id, $category_ids)) {
                             $condition = "entity_id = {$category->entity_id}";
                             $category2 = Mage2CatalogCategoryEntity::model()->find($condition);
                             if (!$category2) {
                                 $category2 = new Mage2CatalogCategoryEntity();
                                 $category2->entity_id = $category->entity_id;
                                 $category2->attribute_set_id = MigrateSteps::getMage2AttributeSetId($category->attribute_set_id, MigrateSteps::CATEGORY_TYPE_CODE);
                                 $category2->parent_id = $category->parent_id;
                                 $category2->created_at = $category->created_at;
                                 $category2->updated_at = $category->updated_at;
                             }
                             $category2->path = $category->path;
                             $category2->position = $category->position;
                             $category2->level = $category->level;
                             $category2->children_count = $category->children_count;
                             //update or save
                             if ($category2->save()) {
                                 //update to log
                                 $migrated_category_ids[] = $category->entity_id;
                                 //catalog_category_entity_datetime
                                 $condition = "entity_id = {$category->entity_id}";
                                 if ($migrated_store_ids) {
                                     $str_store_ids = implode(',', $migrated_store_ids);
                                     $condition .= " AND store_id IN ({$str_store_ids})";
                                 }
                                 $models = Mage1CatalogCategoryEntityDatetime::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, 3);
                                         if ($attribute_id2) {
                                             $condition = "entity_id= {$model->entity_id} AND attribute_id = {$attribute_id2} AND store_id = {$store_id2}";
                                             $model2 = Mage2CatalogCategoryEntityDatetime::model()->find($condition);
                                             if (!$model2) {
                                                 $model2 = new Mage2CatalogCategoryEntityDatetime();
                                                 $model2->attribute_id = $attribute_id2;
                                                 $model2->store_id = $store_id2;
                                                 $model2->entity_id = $model->entity_id;
                                                 //note: 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_category_entity_decimal
                                 $condition = "entity_id = {$category->entity_id}";
                                 if ($migrated_store_ids) {
                                     $str_store_ids = implode(',', $migrated_store_ids);
                                     $condition .= " AND store_id IN ({$str_store_ids})";
                                 }
                                 $models = Mage1CatalogCategoryEntityDecimal::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, 3);
                                         if ($attribute_id2) {
                                             $condition = "entity_id= {$model->entity_id} AND attribute_id = {$attribute_id2} AND store_id = {$store_id2}";
                                             $model2 = Mage2CatalogCategoryEntityDecimal::model()->find($condition);
                                             if (!$model2) {
                                                 $model2 = new Mage2CatalogCategoryEntityDecimal();
                                                 $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_category_entity_int
                                 $condition = "entity_id = {$category->entity_id}";
                                 if ($migrated_store_ids) {
                                     $str_store_ids = implode(',', $migrated_store_ids);
                                     $condition .= " AND store_id IN ({$str_store_ids})";
                                 }
                                 $models = Mage1CatalogCategoryEntityInt::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, 3);
                                         if ($attribute_id2) {
                                             $condition = "entity_id= {$model->entity_id} AND attribute_id = {$attribute_id2} AND store_id = {$store_id2}";
                                             $model2 = Mage2CatalogCategoryEntityInt::model()->find($condition);
                                             if (!$model2) {
                                                 $model2 = new Mage2CatalogCategoryEntityInt();
                                                 $model2->attribute_id = $attribute_id2;
                                                 $model2->store_id = $store_id2;
                                                 $model2->entity_id = $model->entity_id;
                                                 //note: 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_category_entity_text
                                 $condition = "entity_id = {$category->entity_id}";
                                 if ($migrated_store_ids) {
                                     $str_store_ids = implode(',', $migrated_store_ids);
                                     $condition .= " AND store_id IN ({$str_store_ids})";
                                 }
                                 $models = Mage1CatalogCategoryEntityText::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, 3);
                                         if ($attribute_id2) {
                                             $condition = "entity_id= {$model->entity_id} AND attribute_id = {$attribute_id2} AND store_id = {$store_id2}";
                                             $model2 = Mage2CatalogCategoryEntityText::model()->find($condition);
                                             if (!$model2) {
                                                 $model2 = new Mage2CatalogCategoryEntityText();
                                                 $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_category_entity_varchar
                                 $condition = "entity_id = {$category->entity_id}";
                                 if ($migrated_store_ids) {
                                     $str_store_ids = implode(',', $migrated_store_ids);
                                     $condition .= " AND store_id IN ({$str_store_ids})";
                                 }
                                 $models = Mage1CatalogCategoryEntityVarchar::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, 3);
                                         if ($attribute_id2) {
                                             $condition = "entity_id= {$model->entity_id} AND attribute_id = {$attribute_id2} AND store_id = {$store_id2}";
                                             $model2 = Mage2CatalogCategoryEntityVarchar::model()->find($condition);
                                             if (!$model2) {
                                                 $model2 = new Mage2CatalogCategoryEntityVarchar();
                                                 $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();
                                             }
                                         }
                                     }
                                 }
                                 //url_rewrite for category
                                 $condition = "category_id = {$category->entity_id} AND product_id IS NULL";
                                 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);
                                         $condition = "store_id = {$store_id2} AND entity_id = {$url->category_id} AND entity_type = 'category'";
                                         $url2 = Mage2UrlRewrite::model()->find($condition);
                                         if (!$url2) {
                                             $url2 = new Mage2UrlRewrite();
                                             $url2->entity_type = 'category';
                                             $url2->entity_id = $url->category_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;
                                             $url2->metadata = null;
                                             $url2->save();
                                         }
                                     }
                                 }
                             }
                             // end save a category
                         }
                     }
                 }
             } else {
                 Yii::app()->user->setFlash('note', Yii::t('frontend', 'You have to select at least one Category to migrate.'));
             }
             //Update step status
             if ($migrated_category_ids) {
                 $step->status = MigrateSteps::STATUS_DONE;
                 $step->migrated_data = json_encode(array('category_ids' => $migrated_category_ids));
                 if ($step->update()) {
                     //update session
                     Yii::app()->session['migrated_category_ids'] = $migrated_category_ids;
                     //check foreign key
                     Yii::app()->mage2->createCommand("SET FOREIGN_KEY_CHECKS=1")->execute();
                     $message = "Migrated successfully. Total Categories migrated: %s1";
                     $message = Yii::t('frontend', $message, array('%s1' => sizeof($migrated_category_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, 'categories' => $categories);
         $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;
 }