public function actionAjaxImport()
 {
     @ini_set("max_execution_time", 18000);
     try {
         $timeStart = time();
         $fileId = Yii::app()->request->getParam('fileId', 0);
         $log = new KLogger('LOG_IMPORT_FILE_SONG_PNV', KLogger::INFO);
         $log->LogInfo("Start Ajax Import", false);
         $model = new AdminImportSongModel();
         $result = ImportSongModel::getSongsAll($fileId);
         $totalRow = count($result);
         //echo '<pre>';print_r($result);die();
         $path = Yii::app()->params['importsong']['store_path'];
         $is_error = 0;
         $imported = array();
         $notImport = array();
         $data = "";
         $success = 0;
         if ($totalRow > 0) {
             $song = $result[0];
             $log->LogInfo("Start import | " . CJSON::encode($song), false);
             $status = 0;
             $insert_id = $model->importSong($song, $path);
             $insert_id = !$insert_id ? -1 : $insert_id;
             // save inserted id to a string, for updating Updated_time column when finish
             if ($insert_id > 0 && !in_array($insert_id, array(2, 3, 4))) {
                 $this->updateTime($insert_id, $song['updated_time']);
                 $imported = array('stt' => $song['stt'], 'name' => $song['name'], 'path' => $song['path'], 'songId' => $insert_id);
                 $this->updateSongImportSource($song['id'], 1, 0, 'Success', $insert_id, $timeStart);
             } else {
                 $errorDesc = array(-1 => 'File không tồn tại', 2 => 'Không save được vào song', 3 => 'Hết quyền upload bài hát', 4 => 'File mp3 trống');
                 $this->updateSongImportSource($song['id'], 2, $insert_id, $errorDesc[$insert_id], 0, $timeStart);
                 $is_error = 1;
                 $notImport = array('stt' => $song['stt'], 'name' => $song['name'], 'path' => $song['path'], 'errorDesc' => $errorDesc[$insert_id]);
                 //$log->LogInfo('Not import > (' . $song['stt'] . ')' . $song['path'] . $song['file'], false);
             }
             $data = $this->renderPartial('ajaxResultRow', array('imported' => $imported, 'notImport' => $notImport), true, true);
         } else {
             //completed
             $success = 1;
             $log->LogError("Error | " . CJSON::encode($result), false);
         }
         $dataJson = array('is_error' => $is_error, 'success' => $success, 'data' => $data);
         echo CJSON::encode($dataJson);
         Yii::app()->end();
     } catch (Exception $e) {
         $log->LogError("actionAjaxImport | Exception Error: " . $e->getMessage(), false);
         $dataJson = array('is_error' => 1, 'success' => 0, 'data' => "actionAjaxImport | Exception Error: " . $e->getMessage());
         echo CJSON::encode($dataJson);
         Yii::app()->end();
     }
     exit;
 }
 public function actionNewimport()
 {
     //$path = _APP_PATH_ . DS . "data";
     //$pathSource = 'E:\phuongnv\Vega\chacha_cloud\src\trunk\chacha\data';
     $pathSource = _APP_PATH_ . DS . "data";
     try {
         $log = new KLogger('LOG_IMPORT_FILE_SONG_PNV', KLogger::INFO);
         $log->LogInfo("Start New Import", false);
         $model = new AdminImportSongModel();
         $importer = self::IMPORT_SONG_CACHE . Yii::app()->user->id;
         $result = array();
         if (isset($_POST['AdminImportSongModel'])) {
             $this->layout = false;
             $autoconfirm = Yii::app()->request->getParam('autoconfirm');
             $autoconfirm = isset($autoconfirm) ? 1 : 0;
             $created_time = $_POST['AdminSongModel']['created_time'];
             $updated_time = $_POST['AdminSongModel']['updated_time'];
             $path = Yii::app()->params['importsong']['store_path'];
             $file_path = $pathSource . DS . "tmp" . DS . $_POST['AdminImportSongModel']['source_path'];
             $fileName = explode(DS, $file_path);
             $fileName = $fileName[count($fileName) - 1];
             if (file_exists($file_path)) {
                 $count = 0;
                 $start_row = $_POST['AdminImportSongModel']['start_row'] > 0 ? $_POST['AdminImportSongModel']['start_row'] : 0;
                 $start_row += self::START_ROW;
                 $limit_row = 65000;
                 $limit_row += $start_row;
                 $log->LogInfo("Start Read File and put Memcache | " . $file_path, false);
                 $data = new ExcelReader($file_path);
                 $resultSql = array();
                 //insert file
                 $sql = "INSERT INTO import_song_file(file_name,importer,status,created_time)\n                \t\t\tVALUE('" . $fileName . "', '{$importer}',0,NOW())\n\t                \t\t\t";
                 $insertFileRess = Yii::app()->db->createCommand($sql)->execute();
                 $fileImportId = Yii::app()->db->getLastInsertID();
                 for ($i = $start_row; $i < $limit_row; $i++) {
                     if ($data->val($i, 'B') != "" && $data->val($i, 'G') != "" && $data->val($i, 'C') != "") {
                         $stt = $data->val($i, Yii::app()->params['importsong']['excelcolumns']['stt']);
                         $name = $model->my_encoding($data->val($i, Yii::app()->params['importsong']['excelcolumns']['name']));
                         $category = $model->my_encoding($data->val($i, Yii::app()->params['importsong']['excelcolumns']['category']));
                         $sub_category = $model->my_encoding($data->val($i, Yii::app()->params['importsong']['excelcolumns']['sub_category']));
                         $composer = $model->my_encoding($data->val($i, Yii::app()->params['importsong']['excelcolumns']['composer']));
                         $artist = $model->my_encoding($data->val($i, Yii::app()->params['importsong']['excelcolumns']['artist']));
                         $album = $model->my_encoding($data->val($i, Yii::app()->params['importsong']['excelcolumns']['album']));
                         $path = str_replace('\\', DS, $data->val($i, Yii::app()->params['importsong']['excelcolumns']['path']));
                         $file = $data->val($i, Yii::app()->params['importsong']['excelcolumns']['file']);
                         $sql = "(";
                         $sql .= "'" . $autoconfirm . "'";
                         $sql .= ",'" . $created_time . "'";
                         $sql .= ",'" . $updated_time . "'";
                         $sql .= ",'" . $stt . "'";
                         $sql .= ",'" . addslashes($name) . "'";
                         $sql .= ",'" . $category . "'";
                         $sql .= ",'" . $sub_category . "'";
                         $sql .= ",'" . addslashes($composer) . "'";
                         $sql .= ",'" . addslashes($artist) . "'";
                         $sql .= ",'" . addslashes($album) . "'";
                         //$sql .= ",'".str_replace('\\', '\\\\', $path)."'";
                         $sql .= ",'" . addslashes($path) . "'";
                         //$sql .= ",'".str_replace('\\', '\\\\', $file)."'";
                         $sql .= ",'" . addslashes($file) . "'";
                         $sql .= ",'" . $importer . "'";
                         //$sql .= ",'".str_replace('\\', '\\\\', $file_path)."'";
                         $sql .= ",'" . addslashes($file_path) . "'";
                         $sql .= ",'" . $fileImportId . "'";
                         $sql .= ")";
                         $resultSql[] = $sql;
                         $count++;
                     }
                     /* if($count==10)
                     		 echo '<pre>';print_r($result);die(); */
                 }
                 //insert data to db
                 if ($insertFileRess) {
                     $sql = "INSERT INTO import_song(autoconfirm,created_time,updated_time,stt,name,category,sub_category,composer,artist,album,path,file,importer,file_name,file_id) VALUES";
                     $sql .= implode(',', $resultSql);
                     if (Yii::app()->db->createCommand($sql)->execute()) {
                         $sql = "UPDATE import_song_file set total_song={$count} WHERE id={$fileImportId}";
                         Yii::app()->db->createCommand($sql)->execute();
                     }
                     //insert false
                 }
                 //remove file source after insert
                 $fileSystem = new Filesystem();
                 $fileSystem->remove($file_path);
                 echo CJSON::encode(array('errorCode' => 0, 'errorDesc' => 'Success imported Total Record: ' . count($resultSql)));
             } else {
                 //if ($_POST['AdminImportSongModel']['ajax'])
                 echo CJSON::encode(array('errorCode' => 1, 'errorDesc' => 'Chưa upload file excel'));
             }
             Yii::app()->end();
         }
     } catch (Exception $e) {
         $log->LogError("actionAjaxImport | Exception Error: " . $e->getMessage(), false);
         echo CJSON::encode(array('errorCode' => 1, 'errorDesc' => 'Chưa upload file excel'));
         Yii::app()->end();
     }
     $uploadModel = new XUploadForm();
     $this->render('newimport', array('model' => $model, 'listSong' => $result, 'uploadModel' => $uploadModel));
 }
 public function actionNewimport()
 {
     //$path = _APP_PATH_ . DS . "data";
     //$pathSource = 'E:\phuongnv\Vega\chacha_cloud\src\trunk\chacha\data';
     $pathSource = _APP_PATH_ . DS . "data";
     $log = new KLogger('LOG_CHECK_SONG_IMPORT', KLogger::INFO);
     $log->LogInfo("Start New Import", false);
     try {
         $model = new AdminImportSongModel();
         $importer = self::IMPORT_SONG_CACHE . Yii::app()->user->id;
         $result = array();
         if (isset($_POST['AdminImportSongModel'])) {
             $this->layout = false;
             $file_path = $pathSource . DS . "tmp" . DS . $_POST['AdminImportSongModel']['source_path'];
             $fileName = explode(DS, $file_path);
             $fileName = $fileName[count($fileName) - 1];
             if (file_exists($file_path)) {
                 $log->LogInfo("Start Read File and put Memcache | " . $file_path, false);
                 $data = new ExcelReader($file_path);
                 //insert file
                 $sql = "INSERT INTO import_song_file(file_name,importer,status,created_time,type)\n                \t\t\tVALUE('" . $fileName . "', '{$importer}',0,NOW(),'CHECK')\n\t                \t\t\t";
                 $log->LogInfo("SQL: " . $sql, false);
                 $insertFileRess = Yii::app()->db->createCommand($sql)->execute();
                 $fileImportId = Yii::app()->db->getLastInsertID();
                 $i = 1;
                 $err = 0;
                 $count = 0;
                 $total = 0;
                 $sqlAr = array();
                 $flag = true;
                 while ($flag) {
                     if ($data->val($i, 'A') != "" && $data->val($i, 'B') != "" && $data->val($i, 'C') != "") {
                         $stt = $data->val($i, 'A');
                         $songName = $model->my_encoding($data->val($i, 'B'));
                         $artistName = $model->my_encoding($data->val($i, 'C'));
                         $artistId = $this->getArtistId($artistName);
                         $artistId = implode(',', $artistId);
                         $sqlAr[] = "('{$stt}','" . addslashes($songName) . "', '" . addslashes($artistName) . "', {$fileImportId}, '{$importer}','{$artistId}')";
                         $count++;
                         $total++;
                     } else {
                         $err++;
                     }
                     $i++;
                     if ($err == 3) {
                         $flag = false;
                     }
                     if ($count == 200 || !$flag) {
                         $sql = "INSERT INTO import_song(stt,name,artist,file_id,importer,album) VALUES";
                         $sql .= implode(',', $sqlAr);
                         $log->LogInfo("SQL: " . $sql, false);
                         if (Yii::app()->db->createCommand($sql)->execute()) {
                             //reset
                             $count = 0;
                             $sqlAr = array();
                         }
                     }
                 }
                 $sql = "UPDATE import_song_file set total_song={$total} WHERE id={$fileImportId}";
                 Yii::app()->db->createCommand($sql)->execute();
                 //remove file source after insert
                 /* $fileSystem = new Filesystem();
                 			$fileSystem->remove($file_path); */
                 echo CJSON::encode(array('errorCode' => 0, 'errorDesc' => 'Success imported Total Record: ' . $total . ' Go to <a href="' . Yii::app()->createUrl('/tools/importSong/index&fileId=' . $fileImportId) . '">Scan</a>'));
             } else {
                 echo CJSON::encode(array('errorCode' => 1, 'errorDesc' => 'File không tồn tại:' . $file_path));
                 $log->LogInfo("File không tồn tại: " . $file_path, false);
             }
             Yii::app()->end();
         }
     } catch (Exception $e) {
         echo CJSON::encode(array('errorCode' => 1, 'errorDesc' => 'Exception:' . $e->getMessage()));
         $log->LogError("actionAjaxImport | Exception Error: " . $e->getMessage(), false);
         Yii::app()->end();
     }
     $uploadModel = new XUploadForm();
     $this->render('newimport', array('model' => $model, 'listSong' => $result, 'uploadModel' => $uploadModel));
 }