/
var
/
www
/
vhosts
/
nabawater
/
backend
/
modules
/
translation
/
controllers
/
/var/www/vhosts/nabawater/backend/modules/translation/controllers
mkdir
upload
Name
Size
Mode
Actions
TranslationController.php
8784
0644
edit
dl
rm
Edit:
/var/www/vhosts/nabawater/backend/modules/translation/controllers/TranslationController.php
(8784B)
<?php namespace backend\modules\translation\controllers; use backend\models\Language; use backend\models\ItemFileUploadForm; use yii\data\ArrayDataProvider; use backend\modules\translation\models\Message; use backend\modules\translation\models\SourceMessage; use backend\modules\translation\models\SourceMessageSearch; use common\components\CController; use common\helpers\Com; use yii\helpers\Url; use Yii; use yii\helpers\Json; use yii\web\UploadedFile; use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice\PhpSpreadsheet\IOFactory; use common\helpers\ChunkHelper; use common\helpers\UploadHelper; use common\helpers\FileUploadHelper; /** * Class TranslationController * @package backend\controllers * * @Title("Translation Management") */ class TranslationController extends CController { /** * @return string * @throws \yii\base\InvalidParamException * @author Senthil T <senthilkumar.t@technoduce.com> * * @Title("List/View Translation") */ public function actionIndex() { $searchModel = new SourceMessageSearch(); $params = Yii::$app->request->queryParams; if ( $params === [] ) { $params = Com::getFilter($searchModel::className()); } Yii::$app->request->queryParams = $params; return $this->render('index', [ 'searchModel' => $searchModel, 'dataProvider' => $searchModel->search(Yii::$app->request->queryParams) ]); } /** * @return string * @throws \yii\base\InvalidParamException * @author Senthil T <senthilkumar.t@technoduce.com> * * @Title("Create") */ public function actionCreate() { $model = new SourceMessage(); $modelMessage = new Message(); $post = Yii::$app->getRequest()->post(); if ( $model->load($post) && $modelMessage->load($post) && $model->save() ) { foreach ((array)$modelMessage->translation as $lang => $translation) { $modelTranslation = new Message(); $modelTranslation->id = $model->getPrimaryKey(); $modelTranslation->language = $lang; $modelTranslation->translation = $translation; $modelTranslation->save(false); } $this->redirect('index'); } return $this->render('create', [ 'modelMessage' => $modelMessage, 'languages' => Language::getAll(), 'model' => $model, ]); } /** * @return string * @throws \yii\base\InvalidParamException * @author Senthil T <senthilkumar.t@technoduce.com> * * @Title("Update") */ public function actionUpdate($id) { $model = SourceMessage::findOne($id); $modelMessage = new Message(); $modelTranslation = Message::findAll(['id' => $model->getPrimaryKey()]); $translationArr = []; foreach ((array)$modelTranslation as $translation) { $translationArr[$translation->language] = $translation->translation; } $modelMessage->translation = $translationArr; $post = Yii::$app->getRequest()->post(); if ( $model->load($post) && $modelMessage->load($post) && $model->save() ) { foreach ((array)$modelMessage->translation as $lang => $translation) { $lang = strtolower($lang); /* @var $modelTranslation Message */ $modelTranslation = Message::find() ->where(['id' => $model->getPrimaryKey(), 'language' => $lang]) ->one(); if ( $modelTranslation === null ) { $modelTranslation = new Message(); $modelTranslation->id = $model->getPrimaryKey(); $modelTranslation->language = strtolower($lang); } $modelTranslation->translation = $translation; $modelTranslation->save(false); } $this->redirect('index'); } return $this->render('update', [ 'modelMessage' => $modelMessage, 'languages' => Language::getAll(), 'model' => $model, ]); } public function actionChange() { Com::setSession(APP_LANGUAGE, $_POST['language']); } public function actionInsert(){ $modelItemFileUploadForm = new ItemFileUploadForm(); $post = Yii::$app->getRequest()->post(); if($modelItemFileUploadForm->load($post)) { $uploadedFile = UploadedFile::getInstance($modelItemFileUploadForm, 'item_file'); if (!$uploadedFile == null) { $uploadHelper = UploadHelper::getInstance(); $uploadHelper->setPath($uploadHelper::ITEM_IMPORT_FILE); //$uploadHelper->setUploadedFile($uploadedFile); $name = sprintf('%s%s', time(), Com::generateRandomString(5, true)); $path = sprintf('%s%s%s.%s', $uploadHelper->getPath(), DIRECTORY_SEPARATOR, $name, isset($upload->extension)); $uploadedFile->saveAs($path); $realpath = $uploadHelper->getRealPath($path); $filePath = UploadHelper::getInstance()->getAbsPath($realpath); $chunkSize = 10000; $chunkHelper = ChunkHelper::getInstance(); $reader = IOFactory::createReaderForFile($filePath); $reader->setReadFilter($chunkHelper); $i = 2; $chunkHelper->setRows(1, $chunkSize); $spredSheet = $reader->load($filePath); $rowCount = $spredSheet->setActiveSheetIndex(0)->getHighestRow(); $worksheet = $spredSheet->getActiveSheet(); foreach ($worksheet->getRowIterator() as $key => $row) { $activeSheet = $spredSheet->getActiveSheet(); $category = $activeSheet->getCell('A'.$i)->getValue(); $en = $activeSheet->getCell('B'.$i)->getValue(); $ar = $activeSheet->getCell('C'.$i)->getValue(); $modelSource = SourceMessage::find()->where(['category' => $category, 'message' => $en])->one(); if($modelSource != null) { //if ( $modelTranslation === null ) { $modelTranslation = new Message(); $modelTranslation->id = $modelSource->getPrimaryKey(); $modelTranslation->language = 'My'; //} $modelTranslation->translation = $ar; $modelTranslation->save(false); } $i++; } Yii::$app->session->setFlash('success', Yii::t('backend', "successfully Uploaded")); return $this->redirect(['import-item']); } } return $this->render('import-item', ['modelItemFileUploadForm' => $modelItemFileUploadForm]); } /** * @return string * * @Title("Export") */ public function actionExport() { $searchModel = new SourceMessageSearch(); $dataProvider = $searchModel->search(Yii::$app->request->queryParams); $model = $dataProvider->query->all(); // print_r($model); die; $thead = []; $tbody = []; $theading = [ 'Category', 'Message', 'malay', ]; $spreadsheet = new Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); $writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet); $index = 1; foreach ($theading as $key => $value) { $sheet->setCellValueByColumnAndRow($index, 1,$theading[$key]); $index++; } $fileName = 'language_file'.'-'.date('Y-m-d').'.Xlsx'; $positionValue = 2; foreach ($model as $key => $value) { $sheet->setCellValue("A$positionValue", $value['category']) ->setCellValue("B$positionValue", $value['message']); ++$positionValue; } // We'll be outputting an excel file header('Content-type: text/x-csv'); // It will be called file.xls header("Content-Disposition: attachment; filename=$fileName"); $writer->save('php://output'); exit; } }
Save
cmd:
run