Example #1
0
 public function actionRegister()
 {
     if (!Yii::$app->user->isGuest) {
         return $this->goHome();
     }
     $role = Yii::$app->request->getQueryParam('role', UserJob::$roleDefault);
     if (!in_array($role, UserJob::$roleAllows)) {
         $role = UserJob::$roleDefault;
     }
     $model = new User();
     $model->scenario = "register_{$role}";
     $jobModel = new UserJob();
     $jobModel->scenario = "register_{$role}";
     $jobModel->role = $role;
     $jobModel->setDefaultValues();
     $oauth = false;
     $oauthData = [];
     if (($oauthParam = Yii::$app->request->getQueryParam('oauth')) != null) {
         if (($oauthData = $this->_decrypt($oauthParam)) != null) {
             $oauth = true;
             if ($model->email === null) {
                 $model->email = $oauthData['email'];
             }
         }
     }
     if ($model->load(Yii::$app->request->post()) && $jobModel->load(Yii::$app->request->post())) {
         $modelValidate = $model->validate();
         $jobModelValidate = $jobModel->validate();
         if ($modelValidate && $jobModelValidate) {
             $model->status = User::STATUS_ACTIVE;
             $jobModel->email = $model->email;
             //set user display name from email, and user can change later
             $model->display_name = substr($model->email, 0, strpos($model->email, '@'));
             if ($oauth === true && !empty($oauthData)) {
                 $model->openids = [$oauthData['provider'] => $oauthData['uid']];
             }
             if ($model->save(false)) {
                 $jobModel->_id = $model->_id;
                 $jobModel->save(false);
                 if ($oauth === true && !empty($oauthData)) {
                     Yii::$app->user->login($model);
                     return $this->goHome();
                 }
                 // Send email
                 $mailer = \Yii::$app->mailer;
                 $mailer->viewPath = '@app/modules/job/mail';
                 $sendEmail = $mailer->compose(['html' => 'registerSuccess-html', 'text' => 'registerSuccess-text'], ['model' => $model])->setFrom([\Yii::$app->params['supportEmail'] => \Yii::$app->name])->setTo($model->email)->setSubject(Yii::t('account', 'Thank you for signing up with ') . \Yii::$app->name)->send();
                 if ($sendEmail) {
                     Yii::$app->getSession()->setFlash('flash', ['type' => 'success', 'title' => Yii::t('account', 'Thank you for signing up'), 'message' => Yii::t('account', 'Check your email for further instructions.'), 'duration' => 10000]);
                 } else {
                     Yii::$app->getSession()->setFlash('flash', ['type' => 'error', 'title' => Yii::t('account', 'Thank you for signing up'), 'message' => Yii::t('account', 'Sorry, we are temporarily unable to send mail.'), 'duration' => 10000]);
                 }
                 return $this->redirect(['login', 'role' => $role]);
             }
         }
     }
     $renderView = $jobModel->role == 'seeker' ? 'register-seeker' : 'register-agent';
     return $this->render($renderView, ['model' => $model, 'jobModel' => $jobModel]);
 }