/** * Creates a new Translation model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Translation(); if ($model->load(Yii::$app->request->post()) && $model->save()) { return $this->redirect(['index']); } else { return $this->render('create', ['model' => $model]); } }
public function actionImport() { $imported = 0; $dropped = 0; $postData = Yii::$app->request->post(); $file = UploadedFile::getInstanceByName('filename'); if ($file) { $lang = $postData['lang']; if ($lang == '') { return $this->render('import', ['error' => "Language is not specified", 'imported' => 0, 'dropped' => 0]); } $content = file($file->tempName); $lines = 0; try { foreach ($content as $line) { $lines++; $transl = explode('@', $line); $oTranslation = Translation::findOne(['t_l_id' => $lang, 't_fr_id' => $transl[0]]); if ($oTranslation) { $oTranslation->t_l_id = $lang; $oTranslation->antworten = $transl[2]; $oTranslation->frage = $transl[1]; } else { $oTranslation = new Translation(); $oTranslation->t_fr_id = $transl[0]; $oTranslation->t_l_id = $lang; $oTranslation->antworten = $transl[2]; $oTranslation->frage = $transl[1]; } if ($oTranslation->save()) { $imported++; } else { $dropped++; } } } catch (ErrorException $e) { return $this->render('import', ['warning' => "Invalid character was found at line " . $lines . ". Imported questions - " . $imported, 'imported' => $imported, 'dropped' => $dropped]); } } elseif (!empty($postData)) { return $this->render('import', ['error' => "No file was selected to import", 'imported' => $imported, 'dropped' => $dropped]); } return $this->render('import', ['imported' => $imported, 'dropped' => $dropped]); }
public function actionImport() { $imported = 0; $dropped = 0; $file = UploadedFile::getInstanceByName('filename'); if ($file) { $lang = Yii::$app->request->post('lang'); $content = file($file->tempName); foreach ($content as $line) { $transl = explode('@', $line); $oTranslation = Translation::findOne(['t_l_id' => $lang, 't_fr_id' => $transl[0]]); if ($oTranslation) { $oTranslation->antworten = $transl[2]; $oTranslation->frage = $transl[1]; } else { $oTranslation = new Translation(); $oTranslation->t_fr_id = $transl[0]; $oTranslation->t_l_id = $lang; $oTranslation->antworten = $transl[2]; $oTranslation->frage = $transl[1]; } $oTranslation->save(); $imported++; } } return $this->render('import', ['imported' => $imported, 'dropped' => $dropped]); }