public function getUserRegistration($entity, $user) { if (!isset($user->_id) or !isset($entity->_id)) { return null; } $conditions = array('league_id' => $entity->_id, 'user_id' => $user->_id); return Registrations::first(compact('conditions')); }
public function index() { // [ "active", "pending", null, "canceled" ] $league_id = '4f9ffe2b406eb74b4100001d'; $a['male'] = array('active' => Registrations::count(array('conditions' => array('league_id' => $league_id, 'status' => 'active', 'gender' => 'male'))), 'pending' => Registrations::count(array('conditions' => array('league_id' => $league_id, 'status' => 'pending', 'gender' => 'male'))), 'canceled' => Registrations::count(array('conditions' => array('league_id' => $league_id, 'status' => 'canceled', 'gender' => 'male'))), 'null' => Registrations::count(array('conditions' => array('league_id' => $league_id, 'status' => null, 'gender' => 'male')))); $a['female'] = array('active' => Registrations::count(array('conditions' => array('league_id' => $league_id, 'status' => 'active', 'gender' => 'female'))), 'pending' => Registrations::count(array('conditions' => array('league_id' => $league_id, 'status' => 'pending', 'gender' => 'female'))), 'canceled' => Registrations::count(array('conditions' => array('league_id' => $league_id, 'status' => 'canceled', 'gender' => 'female'))), 'null' => Registrations::count(array('conditions' => array('league_id' => $league_id, 'status' => null, 'gender' => 'female')))); return compact('a', 'b', 'c'); }
public function getReference($entity) { switch ($entity->reference_class) { case 'registrations': // TODO: change this to ::find($id); return Registrations::find('first', array('conditions' => array('_id' => $entity->reference_id))); break; default: return null; break; } }
protected function _init() { parent::_init(); if (!isset($this->CURRENT_USER)) { $this->flashMessage('You must be logged in to view that page.', array('alertType' => 'error')); return $this->redirect('Leagues::index'); } if (!isset($this->request->id)) { #$this->flashMessage('Registration ID not supplied.', array('alertType' => 'error')); return $this->redirect('Leagues::index'); } $this->_registration = Registrations::find($this->request->id); if (!isset($this->_registration)) { $this->flashMessage('Could not load that registration.', array('alertType' => 'error')); return $this->redirect('Leagues::index'); } $this->set(array('registration' => $this->_registration, 'league' => $this->_registration->getLeague(), 'user' => $this->_registration->getUser())); }
public function isValid($entity) { $league = $entity->getLeague(); $user = $entity->getUser(); if (is_null($league)) { $entity->errors('League not found for registration validity.'); } # Check Player Limits if (isset($league->player_limit)) { $limits = $league->player_limit->to('array'); if (isset($limits['male']) and $user->gender == 'male') { if ($limits['male'] == 0) { $entity->regErrors('This is a women-only league.'); } else { // TODO: Change this to ::count(); $men_count = Registrations::all(array('conditions' => array('league_id' => $league->_id, 'status' => 'active', 'gender' => 'male')))->count(); if ($men_count >= $limits['male']) { $entity->regErrors('Leauge cannot accommodate any more men.'); } } } if (isset($limits['female']) and $user->gender == 'female') { if ($limits['female'] == 0) { $entity->regErrors('This is a men-only league.'); } else { $women_count = Registrations::all(array('conditions' => array('league_id' => $league->_id, 'status' => 'active', 'gender' => 'female')))->count(); if ($women_count >= $limits['female']) { $entity->regErrors('Leauge cannot accommodate any more men.'); } } } } return count($entity->regErrors(null, false)) == 0; }
public function ipn() { if ($this->request->data) { $paypal_txn_id = $this->request->data['txn_id']; $payment = Payments::first(array('conditions' => array(array('txn_id' => $paypal_txn_id)))); if (!isset($payment)) { Logger::debug('New transaction #' . $paypal_txn_id); # Put the payment in the DB $payment = Payments::create($this->request->data); if ($payment->invoice) { # Map invoices to cart_ids, right now this is direct $payment->shopping_cart_id = $payment->invoice; } $payment->save(); } else { Logger::debug('Transaction play-back (txn #' . $paypal_txn_id . ')'); } Logger::debug('$payment->_id = ' . $payment->_id); $cart = ShoppingCarts::find($payment->invoice); Logger::debug('$cart->_id = ' . $cart->_id); $items = $cart->getItems(); if (strtolower($payment->payment_status) == 'pending' and strtolower($payment->pending_reason) == 'authorization') { $captureAmount = 0; $remainder = 0; Logger::debug('authorization transaction'); foreach ($items as $ci) { Logger::debug('.....cart_item ' . $ci->_id); $refObj = $ci->getReference(); if ($ci->isValid()) { Logger::debug('..........valid purchase -- auto-capture'); $ci->save(array('status' => CartItems::STATUS_CAPT)); // Pass payment status down to referenced object $conditions = array('_id' => $refObj->_id); $query = array('$set' => array('payment_status' => CartItems::STATUS_CAPT, 'payment_timestamps.pending' => $payment->payment_date), '$push' => array('payments' => $payment->_id)); Registrations::update($query, $conditions); $captureAmount += $ci->price; } else { Logger::debug('..........not valid, hold as pending'); $ci->save(array('status' => CartItems::STATUS_AUTH)); // Pass payment status down to referenced object $conditions = array('_id' => $refObj->_id); $query = array('$set' => array('payment_status' => CartItems::STATUS_AUTH, 'payment_timestamps.pending' => $payment->payment_date), '$push' => array('payments' => $payment->_id)); Registrations::update($query, $conditions); $remainder += $ci->price; } $cart->save(array('is_authorized' => true)); } if ($captureAmount > 0) { Logger::debug('Capturing $' . $captureAmount); $result = Paypal::doCapture($payment->auth_id, $captureAmount, $payment->mc_currency, $remainder == 0); Logger::debug(print_r($result, true)); // Log NVP transaction result to the payment $query = array('$push' => array('nvp' => $result)); $conditions = array('_id' => $payment->_id); Payments::update($query, $conditions); Logger::debug('Captured!'); } } else { if (strtolower($payment->payment_status) == 'completed') { $unpaid_items = 0; foreach ($items as $ci) { Logger::debug('.....cart_item ' . $ci->_id); $refObj = $ci->getReference(); if ($ci->status == CartItems::STATUS_CAPT) { Logger::debug('..........PAID!'); $ci->save(array('status' => CartItems::STATUS_PAID)); // Pass payment status down to referenced object // TODO: this stuff should really be handled by the registration object $conditions = array('_id' => $refObj->_id); $query = array('$set' => array('paid' => true, 'payment_status' => CartItems::STATUS_PAID, 'status' => 'active', 'payment_timestamps.completed' => $payment->payment_date), '$push' => array('payments' => $payment->_id)); Registrations::update($query, $conditions); } else { if ($ci->status != CartItems::STATUS_PAID) { $unpaid_items++; } } } if ($unpaid_items == 0) { $cart->save(array('status' => 'closed')); } } else { if (strtolower($payment->payment_status) == 'refunded') { foreach ($items as $ci) { Logger::debug('.....cart_item ' . $ci->_id); $refObj = $ci->getReference(); $conditions = array('_id' => $refObj->_id); $query = array('$set' => array('paid' => false, 'payment_status' => CartItems::STATUS_RFND, 'payment_timestamps.refunded' => $payment->payment_date), '$push' => array('payments' => $payment->_id)); Registrations::update($query, $conditions); Logger::debug('..........refunded.'); } } } } } return $this->render(array('layout' => false)); }
Registrations::addModules(array('gRank' => array('description' => 'gRank registration survey module.', 'filters' => array('save' => function ($self, $params, $chain) { // Check league to see if grank is enabled if (empty($params['entity']->getLeague()->modules->gRank)) { $enabled = false; } else { $enabled = true; } if ($enabled and isset($params['data']['gRank'])) { if (isset($params['data']['gRank']['score'])) { unset($params['data']['gRank']['score']); } # Calculate the actual score, store in secondary_rank_data.grank $score = \app\modules\Registrations\GRankModule::calculateScore($params['data']['gRank']['answers']); if (is_numeric($score)) { $params['data']['secondary_rank_data']['grank'] = $score; $params['data']['gRank']['score'] = $score; } } return $chain->next($self, $params, $chain); }, 'validates' => function ($self, $params, $chain) { $validationResult = $chain->next($self, $params, $chain); extract($params); // Check league to see if grank is enabled if (empty($entity->getLeague()->modules->gRank)) { $enabled = false; } else { $enabled = true; } $valid = true; if ($enabled) { if (!isset($entity->gRank->answers)) { $valid = false; $error = 'gRank data missing.'; } else { $score = \app\modules\Registrations\GRankModule::calculateScore($entity->gRank->answers->to('array')); if (!is_numeric($score)) { $valid = false; $params['entity']->errors('gRank.score', implode(', ', $score)); } } } return $valid && $validationResult; }))));
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'); }