Example #1
0
 /**
  * Finds user by [[ROLE_DEMO]]
  *
  * @return User|null
  */
 public function getDemoUser()
 {
     if ($this->_user === false) {
         //$this->_user = User::findOne(['role' => User::ROLE_DEMO]);
         //return $this->_user;
         $settingForm = new SettingForm();
         $demo_account_id = $settingForm->getValue('demo_account_id', 0);
         $default_currency = $settingForm->getValue('default_currency', 'UAH');
         if ($demo_account_id) {
             $user = User::findOne(['id' => $demo_account_id]);
             if ($user) {
                 $new_demo_user = new User();
                 // $new_demo_user->id=0;
                 $new_demo_user->username = $user->username;
                 // $new_demo_user->auth_key=$user->auth_key;
                 $new_demo_user->email = md5(time() . rand(0, 1.0E+20)) . '@sensation.com.ua';
                 //$user->email;
                 $new_demo_user->role = User::ROLE_DEMO;
                 $new_demo_user->status = User::STATUS_ACTIVE;
                 //$user->status;
                 $new_demo_user->default_currency = $default_currency;
                 //$user->default_currency;
                 $new_demo_user->setPassword('123456');
                 $new_demo_user->generateAuthKey();
                 if ($new_demo_user->save(false)) {
                     //Нужно скопирывать фотокнигу
                     $settingUserForm = new UserSettingForm();
                     $settingUserForm->user_id = $new_demo_user->id;
                     $settingUserForm->save();
                     /*
                                              *
                                              *
                                              * $settingForm=new SettingForm();
                     
                     
                                              $demo_account_id=$settingForm->getValue('demo_account_id', 0);
                     
                                             $photobooks=Photobook::find()->where(['status'=>Photobook::STATUS_NEW])->all();
                     
                     
                                             if(count($photobooks)>0){
                     
                     
                                                 $photobook=$photobooks[0];
                     
                     
                                                 $photobookForm=new PhotobookForm();
                     
                     
                     
                                                 if($photobookForm->loadById($photobook->id)){
                     
                     
                                                     $photobookForm->copyToUser($new_demo_user->id);
                     
                     
                                                 }
                     
                     
                                             }*/
                     $this->_user = $new_demo_user;
                 }
             }
             //$this->_user = User::findOne(['role' => User::ROLE_DEMO]);
         }
         /*if(!$this->_user)
           $this->_user = User::findByEmail($this->username);*/
     }
     return $this->_user;
 }
Example #2
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]);
 }