/**
  * @param $language_code
  * @param $path
  *
  * @return array
  * @throws ErrorException
  * @since 1.0.0
  */
 private static function _setAllData($language_code, $path)
 {
     $file = $path . DIRECTORY_SEPARATOR . 'phrase_' . $language_code . '.json';
     if (!file_exists($file)) {
         $data = null;
     } else {
         $data = file_get_contents($file);
     }
     if ($data == null || $data == '') {
         /**@var $models Phrase[] */
         $models = Phrase::find()->all();
         $code = $language_code;
         foreach ($models as $model) {
             $model->setDynamicField();
             $data[$model->name] = $model->{$code};
         }
     } else {
         $data = [];
         /**@var $models Phrase[] */
         $models = Phrase::find()->all();
         $code = $language_code;
         foreach ($models as $model) {
             $model->setDynamicField();
             if (!array_key_exists($model->name, $data)) {
                 $data[$model->name] = $model->{$code};
             }
         }
     }
     file_put_contents($file, Json::encode($data));
     return $data;
 }
Пример #2
0
 /**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  * @since 1.0.0
  * @throws \yii\base\InvalidParamException
  */
 public function search($params)
 {
     $query = Phrase::find();
     $dataProvider = new ActiveDataProvider(['query' => $query, 'sort' => ['defaultOrder' => ['id' => SORT_DESC]]]);
     $this->load($params);
     if (!$this->validate()) {
         return $dataProvider;
     }
     foreach ($this->_dynamicData as $key => $value) {
         if ($this->{$key} !== '') {
             $language_id = Language::getIdByCode($key);
             if ($language_id !== 0) {
                 $query->join('LEFT JOIN', 'phrase_translate as lang_' . $key, 'lang_' . $key . '.phrase_id = {{%phrase}}.id AND lang_' . $key . '.language_id = ' . $language_id);
                 $query->andFilterWhere(['like', 'lang_' . $key . '.value', $this->{$key}]);
             }
         }
     }
     $query->andFilterWhere(['id' => $this->id]);
     $query->andFilterWhere(['like', 'name', $this->name]);
     return $dataProvider;
 }
 /**
  * @return ActiveQuery
  * @since 1.0.0
  */
 public function getPhrase()
 {
     return $this->hasOne(Phrase::className(), ['id' => 'phrase_id']);
 }
 /**
  * Finds the Language model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  *
  * @param integer $id
  *
  * @return Phrase the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  * @since 1.0.0
  */
 protected function findModel($id)
 {
     if (($model = Phrase::findOne($id)) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }