/**
  * Database settings
  */
 public function actionStep1()
 {
     $step = MigrateSteps::model()->find("sorder = 1");
     if (Yii::app()->request->isPostRequest) {
         $step->migrated_data = json_encode($_POST);
         //validate databases
         mysqli_report(MYSQLI_REPORT_STRICT);
         $err_msg = array();
         $validate = true;
         //validate magento1 database
         try {
             $connection = mysqli_connect($_POST['mg1_host'], $_POST['mg1_db_user'], $_POST['mg1_db_pass'], $_POST['mg1_db_name']);
             if ($connection) {
                 //validate magento2 database
                 try {
                     $connection = mysqli_connect($_POST['mg2_host'], $_POST['mg2_db_user'], $_POST['mg2_db_pass'], $_POST['mg2_db_name']);
                 } catch (Exception $e) {
                     $err_msg[] = Yii::t('frontend', "Couldn't connected to Magento 2 database: " . $e->getMessage());
                     $validate = false;
                 }
             }
         } catch (Exception $e) {
             $err_msg[] = Yii::t('frontend', "Couldn't connected to Magento 1 database: " . $e->getMessage());
             $validate = false;
         }
         if (isset($connection) and $connection) {
             mysqli_close($connection);
         }
         if ($validate) {
             //save to config file
             $configTemplate = Yii::app()->basePath . DIRECTORY_SEPARATOR . "config" . DIRECTORY_SEPARATOR . "config.template";
             $configFilePath = Yii::app()->basePath . DIRECTORY_SEPARATOR . "config" . DIRECTORY_SEPARATOR . "config.php";
             if (file_exists($configTemplate)) {
                 if (file_exists($configFilePath) && is_writable($configFilePath)) {
                     $configs = file_get_contents($configTemplate);
                     //replace needed configs
                     $configs = str_replace('{MG1_HOST}', $_POST['mg1_host'], $configs);
                     $configs = str_replace('{MG1_DB_NAME}', $_POST['mg1_db_name'], $configs);
                     $configs = str_replace('{MG1_DB_USER}', $_POST['mg1_db_user'], $configs);
                     $configs = str_replace('{MG1_DB_PASS}', $_POST['mg1_db_pass'], $configs);
                     $configs = str_replace('{MG1_DB_PREFIX}', $_POST['mg1_db_prefix'], $configs);
                     $configs = str_replace('{MG1_VERSION}', $_POST['mg1_version'], $configs);
                     //Mage2
                     $configs = str_replace('{MG2_HOST}', $_POST['mg2_host'], $configs);
                     $configs = str_replace('{MG2_DB_NAME}', $_POST['mg2_db_name'], $configs);
                     $configs = str_replace('{MG2_DB_USER}', $_POST['mg2_db_user'], $configs);
                     $configs = str_replace('{MG2_DB_PASS}', $_POST['mg2_db_pass'], $configs);
                     $configs = str_replace('{MG2_DB_PREFIX}', $_POST['mg2_db_prefix'], $configs);
                     //save
                     if (file_put_contents($configFilePath, $configs)) {
                         //save settings to database
                         $step->status = MigrateSteps::STATUS_DONE;
                         if ($step->save()) {
                             //alert message
                             Yii::app()->user->setFlash('success', Yii::t('frontend', "Your settings was saved successfully."));
                         }
                     }
                 } else {
                     Yii::app()->user->setFlash('note', Yii::t('frontend', "The config file was not exists or not ablewrite permission.<br/>Please make writeable for config file and try again."));
                 }
             } else {
                 Yii::app()->user->setFlash('note', Yii::t('frontend', "The config.template file was not exists."));
             }
         } else {
             Yii::app()->user->setFlash('error', implode('</br>', $err_msg));
         }
     } else {
         if ($step->status == MigrateSteps::STATUS_NOT_DONE) {
             //auto load database 1 settings if exists
             //                $configFilePath = Yii::app()->basePath ."/../../app/etc/local.xml";
             //                if (file_exists($configFilePath)){
             //                    $configData = simplexml_load_file($configFilePath);
             //                    $settings = (object)json_decode($step->migrated_data);
             //                    $settings->mg1_host = $configData->global->resources->default_setup->connection->host;
             //                    $settings->mg1_db_user = $configData->global->resources->default_setup->connection->username;
             //                    $settings->mg1_db_pass = $configData->global->resources->default_setup->connection->password;
             //                    $settings->mg1_db_name = $configData->global->resources->default_setup->connection->dbname;
             //                    $settings->mg1_db_prefix = $configData->global->resources->db->table_prefix;
             //
             //                    $mageFilename = Yii::app()->basePath ."/../../app/Mage.php";
             //                    require_once $mageFilename;
             //                    $mageVersion = Mage::getVersionInfo();
             //                    if ($mageVersion['minor'] == '6'){
             //                        $settings->mg1_version = 'mage16x';
             //                    } elseif ($mageVersion['minor'] == '7'){
             //                        $settings->mg1_version = 'mage17x';
             //                    } elseif ($mageVersion['minor'] == '8'){
             //                        $settings->mg1_version = 'mage18x';
             //                    } else {
             //                        $settings->mg1_version = 'mage19x';
             //                    }
             //                }
             //auto load database 2 settings if exists
             $configFilePath = MigrateSteps::getMG2BasePath() . "app/etc/env.php";
             if (file_exists($configFilePath)) {
                 $configData = (require $configFilePath);
                 $settings = (object) json_decode($step->migrated_data);
                 $settings->mg2_host = isset($configData['db']['connection']['default']['host']) ? $configData['db']['connection']['default']['host'] : '';
                 $settings->mg2_db_user = isset($configData['db']['connection']['default']['username']) ? $configData['db']['connection']['default']['username'] : '';
                 $settings->mg2_db_pass = isset($configData['db']['connection']['default']['password']) ? $configData['db']['connection']['default']['password'] : '';
                 $settings->mg2_db_name = isset($configData['db']['connection']['default']['dbname']) ? $configData['db']['connection']['default']['dbname'] : '';
                 $settings->mg2_db_prefix = isset($configData['db']['table_prefix']) ? $configData['db']['table_prefix'] : '';
             }
         }
     }
     if (!isset($settings)) {
         $settings = (object) json_decode($step->migrated_data);
     }
     $assign_data = array('step' => $step, 'settings' => $settings);
     $this->render("step{$step->sorder}", $assign_data);
 }