/**
  * Creates a new Term model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     // Get the languages
     $languages = Yii::$app->params['languages'];
     $root = Yii::$app->session->get('taxonomy.taxonomy-id');
     // Load the model
     $model = new Term(['active' => 1]);
     try {
         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 Lang(['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();
                 $parent = Term::findOne($post['Term']['parent_id']);
                 // Save the main model
                 if (!$model->load($post) || !$model->appendTo($parent)) {
                     throw new Exception(Yii::t('app', 'Failed to save the node'));
                 }
                 // Save the translations
                 foreach ($languages as $languageId => $languageName) {
                     $data = $post['Lang'][$languageId];
                     // Set the translation language and attributes
                     $model->language = $languageId;
                     $model->name = $data['name'];
                     $model->content = $data['content'];
                     if (!$model->saveTranslation()) {
                         throw new Exception(Yii::t('app', 'Failed to save the translation'));
                     }
                 }
                 $transaction->commit();
                 // Switch back to the main language
                 $model->language = Yii::$app->language;
                 // Set flash message
                 Yii::$app->getSession()->setFlash('term', Yii::t('app', '"{item}" has been created', ['item' => $model->name]));
                 // 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]);
                 }
             }
         }
     } catch (Exception $e) {
         if (isset($transaction)) {
             $transaction->rollBack();
         }
         // Set flash message
         Yii::$app->getSession()->setFlash('term-error', $e->getMessage());
     }
     return $this->render('create', ['model' => $model, 'terms' => $model::find()->getDropDownListItems($root), 'parent_id' => $root]);
 }
 /**
  * Creates a new Term 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 with default values
     // @todo Setting root to 0 is not necessary, but returns an error when you don't
     $model = new Term(['root' => 0]);
     $returnOptions = ['model' => $model];
     try {
         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 Lang(['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->makeRoot()) {
                     return $this->render('create', $returnOptions);
                 }
                 // Save the translations
                 foreach ($languages as $languageId => $languageName) {
                     $data = $post['Lang'][$languageId];
                     // Set the translation language and attributes
                     $model->language = $languageId;
                     $model->name = $data['name'];
                     $model->content = $data['content'];
                     if (!$model->saveTranslation()) {
                         return $this->render('create', $returnOptions);
                     }
                 }
                 $transaction->commit();
                 // Switch back to the main language
                 $model->language = Yii::$app->language;
                 // Set flash message
                 Yii::$app->getSession()->setFlash('term', Yii::t('app', '"{item}" has been created', ['item' => $model->name]));
                 // 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]);
                 }
             }
         }
     } catch (Exception $e) {
         if (isset($transaction)) {
             $transaction->rollBack();
         }
         // Set flash message
         Yii::$app->getSession()->setFlash('term-error', $e->getMessage());
     }
     return $this->render('create', $returnOptions);
 }