Exemplo n.º 1
0
 public function register()
 {
     if (!$this->league) {
         $redirectUrl = $this->request->env('HTTP_REFERER') ?: '/';
         $this->flashMessage('League not found.', array('alertType' => 'error'));
         return $this->redirect($redirectUrl);
     }
     return $this->redirect('/leagues/' . $this->league->_id . '/register/');
     # ALL OTHER CODE IN THIS METHOD DEPRECATED:
     $league = $this->league;
     $user = $this->CURRENT_USER;
     if (!$league->registrationOpen()) {
         throw new \Exception('Registration is not open for that league.');
     }
     if (!isset($user)) {
         throw new \Exception('You must be logged in to register for a league.');
     }
     if (!$user->can('leagues.register')) {
         throw new \Exception('You do not have permission to register for leagues.');
     }
     $registration = $league->getUserRegistration($user);
     $newRegistration = false;
     if (!isset($registration)) {
         $newRegistration = true;
         $registration = Registrations::create();
         $registration->user_id = $user->_id;
         $registration->league_id = $league->_id;
         $registration->signup_timestamp = time();
         $registration->gender = $user->gender;
         // Copy User Metadata to reduce queries for things like team rosters
         $user_metadata = array();
         foreach (Registrations::$userMetadataFields as $f) {
             if (isset($user->{$f})) {
                 $user_metadata[$f] = $user->{$f};
             }
             $registration->user_data = $user_metadata;
         }
     }
     // Waiver Check:
     if ($this->request->data) {
         if (isset($registration->waiver_acceptance_date) and is_object($registration->waiver_acceptance_date)) {
             #User has already accepted the waiver, is probably just editing the registration
         } else {
             if ($this->request->data['accepted_waiver'] == 1) {
                 #User has clicked the checkbox and has accepted the waiver
                 unset($this->request->data['accepted_waiver']);
                 $this->request->data['waiver_acceptance_date'] = time();
             } else {
                 // User didn't click the waiver checkbox. Let's populate the form fields, run validation, and send it back with an extra error
                 $registration->set($this->request->data);
                 $registration->validates();
                 $registration->errors('accepted_waiver', 'You must accept the waiver to register.');
                 return compact('registration');
             }
         }
     }
     if ($this->request->data and $registration->save($this->request->data)) {
         $cartItem = CartItems::first(array('conditions' => array('reference_class' => 'registrations', 'reference_id' => $registration->_id)));
         // Free registrations
         if ($this->CURRENT_USER->can('register.free')) {
             if ($cartItem and $cartItem->isValid()) {
                 // User has been added to this group after registering
                 $cartItem->status = CartItems::STATUS_DONE;
                 $cartItem->save();
             }
             if ($registration->isValid()) {
                 $registration->status = 'active';
                 $registration->save();
                 $this->flashMessage('Your registration has been comped by the AFDC, you are now registered.', array('alertType' => 'success'));
                 return $this->redirect('Leagues::index');
             } else {
                 $this->flashMessage('Your registration has been comped by the AFDC, but the league is currently full.', array('alertType' => 'error'));
                 return $this->redirect('Leagues::index');
             }
         }
         if (!isset($cartItem)) {
             $cartItem = CartItems::create();
             $cartItem->name = $league->name . ' registration for ' . $user->firstname . ' ' . $user->lastname;
             $cartItem->price = $league->price;
             $cartItem->reference_class = 'registrations';
             $cartItem->reference_id = $registration->_id;
         }
         $cart = $user->getShoppingCart(true);
         $cartItem->carts = array($cart->_id);
         $cartItem->save();
         // Notify the user
         $regMessage = 'Registration ' . ($newRegistration ? 'saved' : 'updated') . ' for <strong>' . $league->name . '</strong>. ';
         if ($registration->paid == false) {
             $regMessage .= ' Please pay via the checkout page to complete your registration.';
         }
         $this->flashMessage($regMessage, array('alertType' => 'success'));
         if (!$registration->paid) {
             return $this->redirect('Carts::index');
         }
         return $this->redirect('Leagues::index');
     } else {
         if ($this->request->data) {
             $this->flashMessage('There was an error.', array('alertType' => 'error'));
             // If we get here, the user definitely checked the box at one time or another, make sure it stays checked.
             $registration->set(array('accepted_waiver' => true));
         }
     }
     return compact('registration');
 }