/** * Creates a new model. * If creation is successful, the browser will be redirected to the 'view' page. */ public function actionCreate() { $model = new ImportUserFileModel(); $content_type = Yii::app()->request->getParam('content_type', 'subscribe'); Yii::app()->session['content_type'] = $content_type; if (Yii::app()->request->isPostRequest) { $content_type = Yii::app()->session['content_type']; $package_id = Yii::app()->request->getParam('package_id', null); if (!isset($package_id)) { $model->addError("name", "Chưa chọn gói cước!"); } if (!$_FILES['file']['error']) { $ext = Utils::getExtension($_FILES['file']['name']); $type = $_FILES['file']['type']; if ($ext == 'xls' && ($type == 'application/xls' || $type == 'application/vnd.ms-excel')) { $storage = Yii::app()->params['storage']['baseStorage'] . "uploads" . DS . "user" . DS; Utils::makeDir($storage); $fileName = $_FILES['file']['name']; if (move_uploaded_file($_FILES['file']['tmp_name'], $storage . DS . $fileName)) { $transaction = Yii::app()->db->beginTransaction(); try { $file_path = $storage . DS . $fileName; $model->file_name = $fileName; $model->created_by = $this->userId; $model->file_path = $file_path; $model->package_id = $package_id; $model->created_time = new CDbExpression("NOW()"); $model->content_type = $content_type; if ($model->save(false)) { $data = new ExcelReader($file_path); $start_row = 2; $limit_row = $data->rowcount(); $cell_name = "A"; $arrayVal = array(); for ($i = $start_row; $i <= $limit_row; $i++) { if ($data->val($i, $cell_name) == "") { continue; } $name = $model->my_encoding($data->val($i, $cell_name)); $arrayVal[] = "('{$name}','{$model->id}','{$package_id}')"; } /** * Start insert here: split 200 line per command */ $arrs = array_chunk($arrayVal, 200); foreach ($arrs as $arr) { $vals = implode(",", $arr); $sql = "INSERT INTO import_user_content (`msisdn`,`file_id`,`package_id`) VALUES {$vals}"; $command = Yii::app()->db->createCommand($sql); $command->execute(); } $transaction->commit(); $this->redirect(array('view', 'id' => $model->id, 'content_type' => $content_type)); } else { $transaction->rollback(); } } catch (Exception $e) { $transaction->rollback(); $model->addError("exception", $e->getMessage()); } } else { $model->addError("name", "Không upload được file vào thư mục:" . $storage . DS . $fileName); } } else { $model->addError("name", "Chỉ upload file xls"); } } else { $model->addError("name", "Chưa upload file"); // $model->content_type = $content_type; } } $this->render('create', array('model' => $model, 'content_type' => $content_type)); }
/** * Creates a new model. * If creation is successful, the browser will be redirected to the 'view' page. */ public function actionCreate() { $model = new CopyrightInputFileModel(); if (Yii::app()->request->isPostRequest) { $content_type = Yii::app()->request->getParam('content_type', null); if (!isset($content_type)) { $model->addError("name", "Chưa chọn loại nội dung content_type!"); } else { if (!$_FILES['file']['error']) { $ext = Utils::getExtension($_FILES['file']['name']); $type = $_FILES['file']['type']; if ($ext == 'xls' && ($type == 'application/xls' || $type == 'application/vnd.ms-excel')) { $storage = Yii::app()->params['storage']['baseStorage'] . "uploads" . DS . "copyright"; Utils::makeDir($storage); $fileName = $_FILES['file']['name']; if (move_uploaded_file($_FILES['file']['tmp_name'], $storage . DS . $fileName)) { $transaction = Yii::app()->db->beginTransaction(); try { $model->file_name = $fileName; $model->content_type = $content_type; $model->created_by = $this->userId; $model->created_time = new CDbExpression("NOW()"); if ($model->save()) { $file_path = $storage . DS . $fileName; $data = new ExcelReader($file_path); $start_row = 2; $limit_row = $data->rowcount(); $cell_content_id = "A"; $cell_name = "B"; $cell_artist = "C"; $cell_copyright_code = "F"; $copyright_id = "J"; $arrayVal = array(); for ($i = $start_row; $i < $limit_row; $i++) { if ($data->val($i, $cell_name) == "" || $data->val($i, $cell_artist) == "" || $data->val($i, $copyright_id) == "") { continue; } $contentId = $model->my_encoding($data->val($i, $cell_content_id)); $name = $model->my_encoding($data->val($i, $cell_name)); $name = '"' . $name . '"'; $artist = $model->my_encoding($data->val($i, $cell_artist)); $ccode = $model->my_encoding($data->val($i, $cell_copyright_code)); $ccid = $model->my_encoding($data->val($i, $copyright_id)); $arrayVal[] = "('" . $contentId . " ', " . $name . " ,'" . $artist . " ','" . $ccode . " '," . $ccid . " ,'{$model->id}')"; } /** * Start insert here: split 200 line per command */ $arrs = array_chunk($arrayVal, 200); foreach ($arrs as $arr) { $vals = implode(",", $arr); $sql = "INSERT INTO copyright_input_content (`content_id`,`name`,`artist`,`copyright_code`,`copyright_id`,`input_file`) VALUES {$vals}"; $command = Yii::app()->db->createCommand($sql); $command->execute(); } $transaction->commit(); $this->redirect(array('view', 'id' => $model->id)); } else { $transaction->rollback(); } } catch (Exception $e) { $transaction->rollback(); $model->addError("exception", $e->getMessage()); } } else { $model->addError("name", "Không upload được file vào thư mục:" . $storage . DS . $fileName); } } else { $model->addError("name", "Chỉ upload file xls"); } } else { $model->addError("name", "Chưa upload file"); $model->content_type = $content_type; } } } $this->render('create', array('model' => $model)); }