/**
  * Handle missing translations
  *
  * @param MissingTranslationEvent $event
  */
 public static function handleMissingTranslation(MissingTranslationEvent $event)
 {
     $sourceMessage = SourceMessage::get($event->category, $event->message);
     if (!$sourceMessage) {
         SourceMessage::create($event->category, $event->message);
     }
 }
 /**
  * Finds the SourceMessage model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  * @param integer $id
  * @return SourceMessage the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findModel($id)
 {
     if (($model = SourceMessage::findOne($id)) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException(Module::t('The requested page does not exist.'));
     }
 }
 /**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = SourceMessage::find()->joinWith(['messages']);
     $dataProvider = new ActiveDataProvider(['query' => $query]);
     $this->load($params);
     if (!$this->validate()) {
         // uncomment the following line if you do not want to return any records when validation fails
         // $query->where('0=1');
         return $dataProvider;
     }
     $query->andFilterWhere(['like', 'category', $this->category])->andFilterWhere(['like', 'message', $this->message]);
     $translations = array_filter((array) $this->translation);
     $or = ['or'];
     foreach ($translations as $key => $text) {
         $or[] = ['and', ['message.language' => $key], ['or like', 'message.translation', $text]];
     }
     $query->andWhere($or);
     return $dataProvider;
 }
 /**
  * @param string $category
  * @param string $message
  * @return null|SourceMessage
  */
 public static function get($category, $message)
 {
     $driver = Yii::$app->getDb()->getDriverName();
     $condition = $driver === 'mysql' ? '= BINARY' : '=';
     $sourceMessage = SourceMessage::find()->where(['category' => $category])->andWhere([$condition, 'message', $message])->one();
     return $sourceMessage;
 }
 /**
  * @param string $category
  * @param string $message
  * @return SourceMessage
  */
 protected function getSourceMessage($category, $message)
 {
     $sourceMessage = SourceMessage::get($category, $message);
     if (!$sourceMessage) {
         $sourceMessage = SourceMessage::create($category, $message);
     }
     return $sourceMessage;
 }
 /**
  * @return bool
  */
 public function save()
 {
     SourceMessage::create($this->category, $this->message);
     return true;
 }