/** * @param $attributes * @param MissingTranslationEvent $event * @return bool */ protected static function saveMessage($attributes, MissingTranslationEvent $event) { /** @var Message $message */ $message = Message::findOne(['language' => $attributes['language'], 'id' => $attributes['id']]); if (!$message) { $message = new Message(); } $message->attributes = $attributes; if ($event->category === 'app' && $attributes['language'] === Language::getDefaultLanguage()->varCode) { $message->translation = $event->message; } elseif ($event->category === 'admin') { $message->translation = self::$autoTranslate ? (new ApiTranslation($message->language))->run($event->message) : $event->message; } return $message->save(); }
/** * Deletes an existing Message model. * If deletion is successful, the browser will be redirected to the 'index' page. * @param integer $id * @return mixed */ public function actionDelete($id) { $this->findModel($id)->delete(); Message::deleteAll(['id' => $id]); Yii::$app->getSession()->setFlash('success', Yii::t('admin', 'Entry has been deleted successfully')); return $this->redirect(Url::previous()); }
/** * 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.'); } }
/** * @param $id * @param array $data * @return bool * @throws \Exception * @throws \yii\db\Exception */ public static function addTranslate($id, array $data) { $transaction = self::getDb()->beginTransaction(); try { foreach ($data as $language => $translation) { $model = self::findOne(['id' => $id, 'language' => $language]); if ($model) { $model->translation = $translation; $model->save(); } else { $model = new Message(); $model->id = $id; $model->language = $language; $model->translation = $translation; $model->save(); } } $transaction->commit(); } catch (\Exception $e) { $transaction->rollBack(); throw $e; } return true; }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Message::find(); $query->joinWith('sourceMessage'); $dataProvider = new ActiveDataProvider(['query' => $query, 'pagination' => ['pageSize' => 15]]); $dataProvider->sort->attributes['sourceMessage.message'] = ['asc' => ['source_message.message' => SORT_ASC], 'desc' => ['source_message.message' => SORT_DESC]]; $dataProvider->sort->attributes['sourceMessage.category'] = ['asc' => ['source_message.category' => SORT_ASC], 'desc' => ['source_message.category' => SORT_DESC]]; $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to any records when validation fails // $query->where('0=1'); return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'source_message.category' => 'admin']); $query->andFilterWhere(['like', 'language', Setting::getValue('languageAdminPanel')])->andFilterWhere(['like', 'translation', $this->translation])->andFilterWhere(['like', 'source_message.message', $this->getAttribute('sourceMessage.message')])->andFilterWhere(['like', 'source_message.category', $this->getAttribute('sourceMessage.category')]); return $dataProvider; }
?> <div class="message-view"> <p> <?php echo Html::a(Yii::t('admin', 'Back to list'), ['index'], ['class' => 'btn btn-info']); ?> <?php echo Yii::$app->user->can('/admin/translation-public/update') ? Html::a(Yii::t('admin', 'Update'), ['update', 'id' => $model->id], ['class' => 'btn btn-primary']) : null; ?> <?php echo Yii::$app->user->can('/admin/translation-public/delete') ? Html::a(Yii::t('admin', 'Delete'), ['delete', 'id' => $model->id], ['class' => 'btn btn-danger', 'data' => ['confirm' => Yii::t('admin', 'Are you sure you want to delete this item?'), 'method' => 'post']]) : null; ?> </p> <div class="row"> <div class="col-xs-12"> <?php Box::begin(['bodyOptions' => ['class' => 'table-responsive']]); ?> <?php echo DetailView::widget(['model' => $model, 'attributes' => ['id', 'message', ['label' => 'RU', 'value' => Message::getTranslate($model, 'ru')], ['label' => 'KZ', 'value' => Message::getTranslate($model, 'kz')]]]); ?> <?php Box::end(); ?> </div> </div> </div>
/** * @return \yii\db\ActiveQuery */ public function getMessages() { return $this->hasMany(Message::className(), ['id' => 'id']); }