/** * @param $newInsteadMissing bool * @return \AlexanderEmelyanov\yii\modules\i18n\models\Message[] * ex.: ['en-US' => AlexanderEmelyanov\yii\modules\i18n\models\Message, * 'ru-RU' => AlexanderEmelyanov\yii\modules\i18n\models\Message, * 'en-GB' => null, // It mean that this SourceMessage haven't translation for en-Gb language * ... * ] * @throws \yii\base\ErrorException */ public function getMessagesMap($newInsteadMissing = false) { /** @var \AlexanderEmelyanov\yii\modules\i18n\models\Message[] $messages */ $messages = $this->getMessages()->all(); $messageMap = []; foreach ($messages as $message) { $relatedModelsKey = $message->language; $messageMap[$relatedModelsKey] = $message; } /** @var array $relatedModelsKeys */ $languages = Message::getSupportedLanguages(); foreach ($languages as $language) { if (!isset($messageMap[$language])) { if ($newInsteadMissing) { $message = new Message(); $message->language = $language; $message->link('id0', $this); $messageMap[$language] = $message; } else { $messageMap[$language] = null; } } } return $messageMap; }
/** * Updates an existing SourceMessage model. * * If update is successful, the browser will be redirected to the 'view' page. * @param integer $id * @return mixed * @throws \yii\base\ErrorException */ public function actionUpdate($id) { $model = $this->findModel($id); $relatedModels = $model->getMessagesMap(true); if ($model->load(Yii::$app->request->post())) { Message::loadMultiple($relatedModels, Yii::$app->request->post()); foreach ($relatedModels as $relatedModel) { if (!$relatedModel->save()) { throw new ErrorException(Yii::t('app', 'Model {modelName} saving failed', ['modelName' => 'Message'])); } } if (!$model->save()) { throw new ErrorException(Yii::t('app', 'Model {SourceMessage} saving failed', ['modelName' => 'Message'])); } return $this->redirect(['view', 'id' => $model->id]); } else { return $this->render('update', ['model' => $model, 'relatedModels' => $relatedModels]); } }
/** * Finds the Message model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @param string $language * @return Message the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id, $language) { if (($model = Message::findOne(['id' => $id, 'language' => $language])) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }