Beispiel #1
0
 public function actionSettings()
 {
     if (Yii::$app->user->isGuest) {
         $this->redirect(Url::toRoute(['intro/index']));
         return;
     }
     if (Yii::$app->user->identity->role == User::ROLE_DEMO) {
         $this->redirect(Url::toRoute(['user/signup-demo']));
         return;
     }
     $this->layout = 'default';
     $less_content = file_get_contents(Yii::getAlias('@webroot') . DIRECTORY_SEPARATOR . 'less' . DIRECTORY_SEPARATOR . 'pb-color-template.less');
     $model = new UserSettingForm();
     if ($model->load(Yii::$app->request->post(), 'UserSettingForm')) {
         if ($model->validate()) {
             $model->user_id = Yii::$app->user->identity->getId();
             $file = UploadedFile::getInstance($model, 'logo_url');
             if ($file) {
                 if ($file->size !== 0) {
                     $model->logo_url = $file;
                 } else {
                     $model->logo_url = null;
                 }
                 if ($file->size !== 0) {
                     $old_file_id = '';
                     $user_setting = UserSetting::findByUserId($model->user_id);
                     if ($user_setting) {
                         $old_file_id = $user_setting->logo_url;
                     }
                     $file_id = AlphaId::id(rand(10000000000, 9999999999999));
                     $file_path = UserUrl::logo() . DIRECTORY_SEPARATOR . UserUrl::imageFile($file_id, UserUrl::IMAGE_ORIGINAL, 'png');
                     $file->saveAs($file_path);
                     $image = Yii::$app->image->load($file_path);
                     $image->resize(0, 49, Yii\image\drivers\Image::HEIGHT);
                     $image->save(UserUrl::logo() . DIRECTORY_SEPARATOR . UserUrl::imageFile($file_id, UserUrl::IMAGE_SMALL, 'png'));
                     $model->logo_url = $file_id;
                     if (!empty($old_file_id)) {
                         if (file_exists(UserUrl::logo() . DIRECTORY_SEPARATOR . UserUrl::imageFile($old_file_id, UserUrl::IMAGE_ORIGINAL, 'png'))) {
                             unlink(UserUrl::logo() . DIRECTORY_SEPARATOR . UserUrl::imageFile($old_file_id, UserUrl::IMAGE_ORIGINAL, 'png'));
                         }
                         if (file_exists(UserUrl::logo() . DIRECTORY_SEPARATOR . UserUrl::imageFile($old_file_id, UserUrl::IMAGE_SMALL, 'png'))) {
                             unlink(UserUrl::logo() . DIRECTORY_SEPARATOR . UserUrl::imageFile($old_file_id, UserUrl::IMAGE_SMALL, 'png'));
                         }
                     }
                 }
             } else {
                 $model->logo_url = null;
             }
             if (Yii::$app->request->post('defaultLogo', 0) == 1) {
                 $model->logo_url = 'default-logo';
             }
             if ($setting = $model->save()) {
                 Yii::$app->getSession()->setFlash('success', Yii::t('app', 'Настройки успешно сохранены.'));
                 return $this->redirect(Url::toRoute('user/settings'));
             }
         }
     } else {
         if (!$model->loadByUserId(Yii::$app->user->identity->getId())) {
         }
     }
     return $this->render('settings', ['model' => $model, 'less_content' => $less_content]);
 }