/**
  * Creates a new Alias model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $languages = Yii::$app->params['languages'];
     // Load the model, default to 'user-defined' type
     $model = new Alias(['entity' => 'page', 'type' => Alias::TYPE_USER_DEFINED]);
     // Load all the translations
     $model->loadTranslations(array_keys($languages));
     // Load the entities
     $entities = [];
     $entities['pages'] = (new Query())->select('page.id, page_lang.name')->from(['page' => 'pages'])->innerJoin(['page_lang' => 'pages_lang'], "page.id = page_lang.page_id AND page_lang.language = '" . Yii::$app->language . "'")->orderBy(['page_lang.name' => SORT_ASC])->all();
     if (Yii::$app->request->getIsPost()) {
         $post = Yii::$app->request->post();
         // Ajax request, validate the models
         if (Yii::$app->request->isAjax) {
             // Populate the model with the POST data
             $model->load($post);
             // Create an array of translation models
             $translationModels = [];
             foreach ($languages as $languageId => $languageName) {
                 $translationModels[$languageId] = new AliasLang(['language' => $languageId]);
             }
             // Populate the translation models
             Model::loadMultiple($translationModels, $post);
             // Validate the model and translation models
             $response = array_merge(ActiveForm::validate($model), ActiveForm::validateMultiple($translationModels));
             // Return validation in JSON format
             Yii::$app->response->format = Response::FORMAT_JSON;
             return $response;
             // Normal request, save models
         } else {
             // Wrap the everything in a database transaction
             $transaction = Yii::$app->db->beginTransaction();
             // Save the main model
             if (!$model->load($post) || !$model->save()) {
                 return $this->render('create', ['model' => $model, 'entities' => $entities]);
             }
             // Save the translations
             foreach ($languages as $languageId => $languageName) {
                 $data = $post['AliasLang'][$languageId];
                 // Set the translation language and attributes
                 $model->language = $languageId;
                 $model->url = $data['url'];
                 if (!$model->saveTranslation()) {
                     return $this->render('create', ['model' => $model, 'entities' => $entities]);
                 }
             }
             $transaction->commit();
             // Switch back to the main language
             $model->language = Yii::$app->language;
             // Set flash message
             Yii::$app->getSession()->setFlash('alias', Yii::t('app', '"{item}" has been created', ['item' => $model->url]));
             // Take appropriate action based on the pushed button
             if (isset($post['close'])) {
                 return $this->redirect(['index']);
             } elseif (isset($post['new'])) {
                 return $this->redirect(['create']);
             } else {
                 return $this->redirect(['update', 'id' => $model->id]);
             }
         }
     }
     return $this->render('create', ['model' => $model, 'entities' => $entities]);
 }