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]); }
public static function translateQuestion(&$question, $lang) { if (isset($question['fr_id'])) { $translation = Translation::findOne(['t_l_id' => $lang, 't_fr_id' => $question['fr_id']]); if ($translation) { $question['frage'] = $translation->frage; $question['antworten'] = $translation->antworten; } } }
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]); }
/** * Finds the Translation model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Translation the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Translation::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }