/** * Updates texts for file in given language * @param $id integer id of file * @return string|Response */ public function actionUpdateTexts($id) { /** @var FileRecord $file */ $file = FileRecord::findOne($id); if ($file) { $session = Yii::$app->session; if (!$session['language_id']) { $session['language_id'] = LanguageRecord::getMainLanguageId(); } $model = FileTextRecord::find()->where(['file_id' => $id, 'language_id' => $session['language_id']])->one(); if (!$model) { $model = new FileTextRecord(); $model->title = $file->title; $model->file_id = $id; $model->language_id = $session['language_id']; } if ($model->load(Yii::$app->request->post()) && $model->save()) { if ($model->file->type == FileRecord::TYPE_IMAGE) { return $this->redirect(['images']); } else { return $this->redirect(['files']); } } elseif (Yii::$app->request->isAjax) { return $this->renderAjax('_textsForm', compact('model')); } return $this->render('_textsForm', compact('model')); } else { throw new InvalidValueException(Yii::t('back', 'No file record found of this ID') . ': ' . $id); } }
/** * @return \yii\db\ActiveQuery */ public function getFileTexts() { return $this->hasMany(FileTextRecord::className(), ['file_id' => 'id']); }