Пример #1
0
 public function register()
 {
     $f3 = \Base::instance();
     $checkout_method = strtolower($this->input->get('checkout_method', null, 'alnum'));
     switch ($checkout_method) {
         // if $checkout_method == guest
         // store email in cart object and then continue
         // create a guest mongoid
         case "guest":
             $real_email = trim(strtolower($this->input->get('email_address', null, 'string')));
             if (\Users\Models\Users::emailExists($real_email)) {
                 \Dsc\System::addMessage('This email is already registered. Please login to continue.  <a href="./user/forgot-password">If necessary, you can recover your password here.</a>', 'error');
                 $this->app->reroute('/shop/checkout');
                 return;
             }
             $mongo_id = (string) new \MongoId();
             $email = 'guest-' . $mongo_id . '@' . $mongo_id . '.' . $mongo_id;
             $password = \Users\Models\Users::generateRandomString();
             $data = array('first_name' => 'Guest', 'last_name' => 'User', 'email' => $email, 'guest_email' => $real_email, 'new_password' => $password, 'confirm_new_password' => $password);
             $user = (new \Users\Models\Users())->bind($data);
             try {
                 // this will handle other validations, such as username uniqueness, etc
                 $user->guest = true;
                 $user->active = false;
                 $user->save();
             } catch (\Exception $e) {
                 \Dsc\System::addMessage('Could not create guest account', 'error');
                 \Dsc\System::addMessage($e->getMessage(), 'error');
                 \Dsc\System::instance()->setUserState('shop.checkout.register.flash_filled', true);
                 $flash = \Dsc\Flash::instance();
                 $flash->store(array());
                 $this->app->reroute('/shop/checkout');
                 return;
             }
             // if we have reached here, then all is right with the form
             $flash = \Dsc\Flash::instance();
             $flash->store(array());
             // login the user, trigger Listeners
             \Dsc\System::instance()->get('auth')->login($user);
             $this->app->reroute('/shop/checkout');
             break;
             // if $checkout_method == register
             // validate data
             // create user
             // redirect back to checkout
         // if $checkout_method == register
         // validate data
         // create user
         // redirect back to checkout
         case "register":
             $email = trim(strtolower($this->input->get('email_address', null, 'string')));
             $data = array('first_name' => $this->input->get('first_name', null, 'string'), 'last_name' => $this->input->get('last_name', null, 'string'), 'email' => $email, 'new_password' => $this->input->get('new_password', null, 'string'), 'confirm_new_password' => $this->input->get('confirm_new_password', null, 'string'));
             $user = (new \Users\Models\Users())->bind($data);
             // Check if the email already exists and give a custom message if so
             if (!empty($user->email) && ($existing = $user->emailExists($user->email))) {
                 if (empty($user->id) || $user->id != $existing->id) {
                     \Dsc\System::addMessage('This email is already registered.', 'error');
                     \Dsc\System::instance()->setUserState('shop.checkout.register.flash_filled', true);
                     $flash = \Dsc\Flash::instance();
                     $flash->store($user->cast());
                     $this->app->reroute('/shop/checkout');
                     return;
                 }
             }
             try {
                 // this will handle other validations, such as username uniqueness, etc
                 $settings = \Users\Models\Settings::fetch();
                 $registration_action = $settings->{'general.registration.action'};
                 switch ($registration_action) {
                     case "auto_login":
                         $user->active = true;
                         $user->save();
                         break;
                     case "auto_login_with_validation":
                         $user->active = false;
                         $user->save();
                         $user->sendEmailValidatingEmailAddress();
                         break;
                     default:
                         $user->active = false;
                         $user->save();
                         $user->sendEmailValidatingEmailAddress();
                         break;
                 }
             } catch (\Exception $e) {
                 \Dsc\System::addMessage('Could not create account.', 'error');
                 \Dsc\System::addMessage($e->getMessage(), 'error');
                 \Dsc\System::instance()->setUserState('shop.checkout.register.flash_filled', true);
                 $flash = \Dsc\Flash::instance();
                 $flash->store($user->cast());
                 $f3->reroute('/shop/checkout');
                 return;
             }
             // if we have reached here, then all is right with the form
             $flash = \Dsc\Flash::instance();
             $flash->store(array());
             // login the user, trigger Listeners
             \Dsc\System::instance()->get('auth')->login($user);
             $this->app->reroute('/shop/checkout');
             break;
             // if $checkout_method something else,
             // add message?
             // redirect back to checkout
         // if $checkout_method something else,
         // add message?
         // redirect back to checkout
         default:
             \Dsc\System::addMessage('Invalid Checkout Method', 'error');
             $this->app->reroute('/shop/checkout');
             break;
     }
 }