public function run() { $items = []; $preContent = ''; $isPost = \Yii::$app->request->isPost; $defaultLang = Lang::getLang()->code; foreach ($this->models as $k => $model) { $preContent .= $isPost && !$model->validate() ? '<div class="alert alert-warning" role="alert"><button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button><h4>' . $model->langModel->title . '</h4>' . Html::errorSummary($model) . '</div>' : ''; $content = $this->view->render($this->template, ['model' => $model, 'k' => $k, 'form' => $this->form]); $items[] = ['label' => $model->langModel->title, 'content' => $content, 'active' => $model->lang == $defaultLang]; } return $preContent . Tabs::widget(['items' => $items]); }
/** * Creates a new Page model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { /** * @var $model Model */ $class = $this->modelClass; $model = new $class(); $model->id = $model->nextId; $model->lang = Lang::getLang()->code; $this->modelSetGetVariable($model); if (in_array('position', $model->attributes())) { $model->position = $model->positionNext; } $models = [$model]; if (\Yii::$app->request->isAjax && $class::loadMultiple($models, $_POST)) { \Yii::$app->response->format = Response::FORMAT_JSON; return ActiveForm::validateMultiple($models); } if ($class::loadMultiple($models, $_POST) && $class::validateMultiple($models)) { $model->create(); return $this->redirect($this->generateRedirectParams($model, $this->redirectCreate)); } else { $renderMethod = \Yii::$app->request->get('partial') ? 'renderPartial' : 'render'; $renderMethod = \Yii::$app->request->isAjax ? 'renderPartial' : $renderMethod; return $this->{$renderMethod}('create', ['models' => $models, 'model' => $model]); } }
public static function t($category, $t, $resetCategory = false) { if (empty($t)) { return 'Empty text'; } if (empty(self::$t[$category]) || $resetCategory) { $categoryList = self::find()->where(['lang' => \Yii::$app->language])->all(); self::$t[$category] = []; foreach ($categoryList as $categoryItem) { self::$t[$categoryItem->category][$categoryItem->alias] = $categoryItem->t; } $resetCategory = false; } $t = trim($t); $alias = md5(gzcompress($t)); if (!empty(self::$t[$category][$alias])) { return self::$t[$category][$alias]; } $model = new Language(); $model->id = $model->nextId; $model->lang = Lang::getLang()->code; $model->alias = $alias; $model->category = $category; $model->t = $t; $model->create(); return self::t($category, $t, true); }
public function lang($code = null) { $class = $this->modelClass; $lang = Lang::getLang($code); if (isset($lang)) { $this->andWhere([$class::tableName() . '.lang' => $lang->code]); } return $this; }
public function bootstrap($app) { $app->on(Application::EVENT_BEFORE_ACTION, function () { $langCode = \Yii::$app->request->get('lang'); $langModel = Lang::getLang($langCode); \Yii::$app->language = $langModel->code; \Yii::$app->sourceLanguage = $langModel->locale; }); }
public static function to($url = '', $scheme = false) { if (is_array($url)) { $lang = Lang::getLang(); if ($lang->code != \Yii::$app->language) { $url['lang'] = \Yii::$app->language; } } return parent::to($url, $scheme); }
public function run() { $default = Lang::getLang()->code; $currentCode = \Yii::$app->language; foreach ($this->links as &$link) { if (is_array($link) && $default != $currentCode) { $link['url']['lang'] = $currentCode; } } return parent::run(); }
public function updateAliasOther() { if (!in_array('alias', $this->attributes())) { return true; } $main = self::find()->where(['id' => $this->id])->lang(Lang::getLang()->code)->one(); $others = self::find()->where(['id' => $this->id])->andWhere(['<>', 'lang', Lang::getLang()->code])->all(); foreach ($others as $other) { $other->alias = $main->alias; $other->save(); } return true; }