/** * @return string * @since 1.0.0 * @throws ExitException|InvalidParamException */ public function actionIndex() { $searchModel = new PhraseSearch(); $dataProvider = $searchModel->search(Yii::$app->request->queryParams); if (Yii::$app->request->post('hasEditable')) { $post = Yii::$app->request->post(); $out = Json::encode(['output' => '', 'message' => '']); $language_id = Language::getIdByCode($post['editableAttribute']); $phrase_id = $post['editableKey']; if ($language_id !== 0 && $phrase_id !== 0) { $model = PhraseTranslate::findOne(['phrase_id' => $phrase_id, 'language_id' => $language_id]); if ($model === null) { $model = new PhraseTranslate(); $model->language_id = $language_id; $model->phrase_id = $phrase_id; } $model->value = $post['Phrase'][$post['editableIndex']][$post['editableAttribute']]; $model->save(); $out = Json::encode(['output' => $model->value, 'message' => '']); } echo $out; Yii::$app->end(); } return $this->render('index', ['searchModel' => $searchModel, 'dataProvider' => $dataProvider]); }
/** * @return array */ protected function getData() { /**@var $currentLanguage Language */ list($route, $params) = Yii::$app->getUrlManager()->parseRequest(Yii::$app->getRequest()); $route = Yii::$app->controller->getRoute() != '' ? Yii::$app->controller->route : $route; $params = ArrayHelper::merge($_GET, $params); $data = [0]; foreach ($this->languages as $language) { if ($language['code'] === Yii::$app->language) { $this->current = ArrayHelper::merge(['url' => $this->urlManager->createUrl(ArrayHelper::merge($params, [array_key_exists('route', $params) ? $params['route'] : $route, 'language' => $language['code']]))], $language); $data[0] = $this->current; } else { $data[] = ArrayHelper::merge(['url' => $this->urlManager->createUrl(ArrayHelper::merge($params, [array_key_exists('route', $params) ? $params['route'] : $route, 'language' => $language['code']]))], $language); } } if (!array_key_exists(0, $data) || $data[0] === 0) { $currentLanguage = Language::findOne(['code' => Yii::$app->language]); if ($currentLanguage) { $data[0] = ['url' => $this->urlManager->createUrl(ArrayHelper::merge($params, [array_key_exists('route', $params) ? $params['route'] : $route, 'language' => $currentLanguage->code])), 'code' => $currentLanguage->code, 'name' => $currentLanguage->name, 'country' => $currentLanguage->country]; } } if (count($data) > count(Language::getLanguages())) { return array_unique($data); } else { return $data; } }
/** * Initializes the object. * This method is called at the end of the constructor. * The default implementation will trigger an [[EVENT_INIT]] event. * If you override this method, make sure you call the parent implementation at the end * to ensure triggering of the event. */ public function init() { $this->languages = Language::getLanguages(); foreach ($this->languages as $language) { $this->_dynamicField[$language->code] = $language->name; } }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider * @since 1.0.0 * @throws InvalidParamException */ public function search($params) { $query = Language::find(); $dataProvider = new ActiveDataProvider(['query' => $query, 'sort' => ['defaultOrder' => ['id' => SORT_DESC]]]); $this->load($params); if (!$this->validate()) { return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'status' => $this->status]); $query->andFilterWhere(['like', 'name', $this->name]); $query->andFilterWhere(['like', 'code', $this->code]); $query->andFilterWhere(['like', 'country', $this->country]); return $dataProvider; }
/** * 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; }
/** * {@inheritDoc} */ public function init() { parent::init(); $languages = ArrayHelper::map(Language::getLanguages(), 'code', 'name'); if (isset($_GET['language']) && isset($languages[$_GET['language']]) && $_GET['language'] != Yii::$app->request->getCookies()->getValue('_language')) { Yii::$app->session['language'] = $_GET['language']; $cookie = new Cookie(['name' => '_language', 'value' => $_GET['language'], 'path' => '/']); Yii::$app->language = $_GET['language']; Yii::$app->response->cookies->add($cookie); Yii::$app->response->refresh(); } if (!Yii::$app->request->cookies->has('_language')) { if (Module::hasSetting()) { $cookie = new Cookie(['name' => '_language', 'value' => Yii::$app->setting->general_language, 'path' => '/']); Yii::$app->language = Yii::$app->setting->general_language; } else { $cookie = new Cookie(['name' => '_language', 'value' => Yii::$app->language, 'path' => '/']); } Yii::$app->response->cookies->add($cookie); } else { Yii::$app->language = Yii::$app->request->getCookies()->getValue('_language'); } }
/** * Return all translated attributes include value * * @param ActiveRecord $model * @param null $language_code * * @return array * @throws InvalidConfigException */ public static function attributes($model, $language_code = null) { $behavior = $model->behaviors(); if (!isset($behavior['ml'])) { throw new InvalidConfigException("MultiLanguage was not defined in " . get_class($model) . "."); } $response = []; foreach (Language::getLanguages() as $language) { foreach ($behavior['ml']['attributes'] as $mlAttribute) { $attribute = $mlAttribute . '_' . $language->code; if ($language_code != null) { if ($language_code == $language->code) { $response[$attribute] = isset($model->behaviors['ml']) ? $model->{$attribute} : ''; } } else { $response[$attribute] = isset($model->behaviors['ml']) ? $model->{$attribute} : ''; } } } return $response; }
/** * @return ActiveQuery * @since 1.0.0 */ public function getLanguage() { return $this->hasOne(Language::className(), ['id' => 'language_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 Language the loaded model * @throws NotFoundHttpException if the model cannot be found * @since 1.0.0 */ protected function findModel($id) { if (($model = Language::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }