예제 #1
0
 public function run($id, $lang_id = null)
 {
     $controller = $this->controller;
     $modelName = $controller::mlConf('model');
     $contentModelName = $controller::mlConf('contentModel');
     if (!$lang_id) {
         $lang_id = LangHelper::getLanguage('id');
     }
     $model = $controller->findModel($id, $lang_id);
     if (!$model->content) {
         $model->populateRelation('content', new $contentModelName());
         $model->content->lang_id = $lang_id;
         $model->content->parent_id = $model->id;
     }
     if ($model->load(Yii::$app->request->post()) && $model->content->load(Yii::$app->request->post())) {
         $isSaveSuccess = false;
         $transaction = Yii::$app->db->beginTransaction();
         try {
             $model->save();
             $model->content->save();
             $transaction->commit();
             $isSaveSuccess = true;
         } catch (\Exception $e) {
             $transaction->rollBack();
             throw $e;
         }
         if ($isSaveSuccess) {
             // We will redirect to view action if it exists. Otherwise, to update
             $redirectAction = $this->isControllerHasViewAction() ? 'view' : 'update';
             return $controller->redirect([$redirectAction, 'id' => $model->id, 'lang_id' => $lang_id]);
         }
     }
     return $controller->render('update', ['model' => $model, 'modelContent' => $model->content]);
 }
예제 #2
0
 /**
  * Tries to determine ID of the current user's language by using cookies or browser language
  * @return  integer|null    Null - could not determine the language
  */
 public static function run()
 {
     // We are looking for language in the user's cookie
     if (isset($_COOKIE['x-language-id'])) {
         $isset = LangHelper::getLanguageByParam('id', $_COOKIE['x-language-id']);
         if ($isset !== null) {
             return $isset['id'];
         }
     }
     // Let's look at the language of the browser
     if (getenv('HTTP_ACCEPT_LANGUAGE')) {
         $arr = explode(';', getenv('HTTP_ACCEPT_LANGUAGE'));
         foreach ($arr as $val) {
             $val = preg_replace("/^q=[\\d\\.]+,?/", '', $val);
             $isset = LangHelper::getLanguageByParam('locale', substr($val, 0, 2));
             if ($isset !== null) {
                 return $isset['id'];
             }
         }
     }
     // Default language
     $lang_id = LangHelper::getLanguage('id');
     if ($lang_id !== null) {
         return $lang_id;
     }
 }
예제 #3
0
 public function run()
 {
     $controller = $this->controller;
     $modelName = $controller::mlConf('model');
     $contentModelName = $controller::mlConf('contentModel');
     $model = new $modelName();
     $model->populateRelation('content', new $contentModelName());
     $model->content->lang_id = LangHelper::getLanguage('id');
     if ($model->load(Yii::$app->request->post()) && $model->content->load(Yii::$app->request->post())) {
         $isSaveSuccess = false;
         $transaction = Yii::$app->db->beginTransaction();
         try {
             $res = $model->save();
             if ($res) {
                 $model->content->parent_id = $model->id;
                 $model->content->save();
                 $transaction->commit();
                 $isSaveSuccess = true;
             } else {
                 throw new \Exception('Не удалось сохранить модель - ошибка валидации');
             }
         } catch (\Exception $e) {
             $transaction->rollBack();
             //throw $e;
         }
         if ($isSaveSuccess) {
             // We will redirect to view action if it exists. Otherwise, to update
             $redirectAction = $this->isControllerHasViewAction() ? 'view' : 'update';
             return $controller->redirect([$redirectAction, 'id' => $model->id, 'lang_id' => $model->content->lang_id]);
         }
     }
     return $controller->render('create', ['model' => $model]);
 }