Ejemplo n.º 1
0
 public function actionServices()
 {
     if (!\Yii::$app->user->isGuest) {
         $services = [];
         $user = User::findOne(\Yii::$app->user->id);
         $model = false;
         if (empty($user->address)) {
             $show = "address";
             $model = new PersonalAccountAddress();
             $model->user_id = $user->id;
             if ($model->load(\Yii::$app->request->post()) && $model->save()) {
                 $show = "personalAccAdd";
                 $services = Services::find()->all();
                 $model = new PersonalAccount();
             }
         } elseif (empty($user->accs)) {
             $show = "personalAccAdd";
             $services = Services::find()->all();
             $model = new PersonalAccount();
             if (!empty($_POST['acc'])) {
                 $accs = $_POST['acc'];
                 $validate = true;
                 $accounts = [];
                 foreach ($accs as $index => $acc) {
                     if ($acc) {
                         $account[$index] = new PersonalAccount();
                         $account[$index]->value = $acc;
                         $account[$index]->service_id = $index;
                         $account[$index]->user_id = $user->id;
                         $validate = $validate && $account[$index]->validate();
                     }
                 }
                 if ($validate) {
                     foreach ($account as $_account) {
                         $_account->save();
                     }
                     $show = "personalAccShow";
                     $model = PersonalAccount::find()->where('user_id = :user_id', [':user_id' => $user->id])->all();
                 }
             }
         } else {
             $show = "personalAccShow";
             $services = PersonalAccount::find()->where('user_id = :user_id', [':user_id' => $user->id])->all();
         }
         return $this->render('services', ['model' => $model, 'services' => $services, 'show' => $show]);
     } else {
         return $this->redirect('/');
     }
 }
Ejemplo n.º 2
0
 public function actionSaveProfile()
 {
     $token = $this->getToken();
     if ($token) {
         $user = User::find()->where('password_reset_token = :token', [':token' => $token])->one();
         if (!empty($user)) {
             if (!empty($_POST['fio'])) {
                 $user->username = $_POST['fio'];
             }
             $address = PersonalAccountAddress::find()->where('user_id = :user_id', [':user_id' => $user->id])->one();
             if (empty($address)) {
                 $address = new PersonalAccountAddress();
                 $address->user_id = $user->id;
             }
             if (!empty($_POST['city'])) {
                 $address->city = $_POST['city'];
             }
             if (!empty($_POST['street'])) {
                 $address->street = $_POST['street'];
             }
             if (!empty($_POST['house'])) {
                 $address->house = $_POST['house'];
             }
             if (!empty($_POST['corpse'])) {
                 $address->area = $_POST['corpse'];
             }
             if (!empty($_POST['flat'])) {
                 $address->flat_number = $_POST['flat'];
             }
             if ($user->save() && $address->save()) {
                 $result = ['status' => ['code' => 200, 'message' => "ОК"]];
             } else {
                 $result = ['status' => ['code' => 405, 'message' => "Введены неверные данные"]];
             }
         } else {
             $result = ['status' => ['code' => 402, 'message' => 'Пользователь не найден']];
         }
     } else {
         $result = ['status' => ['code' => 400, 'message' => 'Значение Token не задано']];
     }
     return Json::encode($result);
 }