Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public function registration($clubId = null) {
- // Find the club as specified in the URL
- // TODO: Change this to use *** class as findByIdOrSubdomain is deprecated
- $club = $this->Club->findByIdOrSubdomain($clubId, [
- 'contain' => [
- 'Season',
- 'Season.MemberType' => ['conditions' => ['MemberType.active' => 1], 'order' => 'MemberType.weight']],
- ]);
- if (empty($club))
- throw new NotFoundException('Unfortunately the registration form you attempted to access is no longer available.');
- //--------------- SAVING A REGISTRATION ---------------
- if ($this->request->isPost()) {
- // We need to create member and registration records (only 1 registration record is submitted by the form)
- $dataToSave = $this->request->data;
- // debug($dataToSave);
- // If the only key in the user model is acceptTerms, we'll trash that (don't want to save a User model unnecessarily)
- if (array_keys($dataToSave[0]['User']) == ['acceptTerms'])
- unset($dataToSave[0]['User']);
- $paymentMethod = $dataToSave[0]['Member']['Registration'][0]['payment_method'];
- $totalAmount = 0;
- foreach($dataToSave as $i => $member) {
- $dataToSave[$i]['Member']['club_id'] = $club['Club']['id'];
- // If we are saving a primary (parent) user, we also need to copy their email address to the users table
- if ($i === 0) {
- $dataToSave[$i]['User']['email'] = $member['Member']['email'];
- $dataToSave[$i]['User']['user_type'] = 'member';
- }
- /*
- Here we restructure the array of registrations slightly
- (create a separate registration entry for each member type they selected)
- FROM:
- 'Member' => array(
- ...
- 'Registration' => array(
- (int) 0 => array(
- 'MemberType' => array(
- (int) 0 => '70',
- (int) 1 => '123',
- ),
- 'payment_method' => 'paypal'
- )))
- TO:
- 'Member' => array(
- ...
- 'Registration' => array(
- (int) 0 => array(
- 'payment_method' => 'paypal',
- 'member_type_id' => '70'
- ),
- (int) 1 => array(
- 'payment_method' => 'paypal',
- 'member_type_id' => '123'
- ))
- */
- $registrations = [];
- // Loop through the checked member types (registrations)
- // May be empty if doing a renewal for only certain members in group
- if (is_array($member['Member']['Registration'][0]['MemberType'])) {
- foreach($member['Member']['Registration'][0]['MemberType'] as $memberTypeId) {
- // Fetch the member type data
- $this->MemberType->contain('Season');
- $memberType = $this->MemberType->findById($memberTypeId);
- // Create a new registration
- $rego = $member['Member']['Registration'][0];
- $rego['member_type_id'] = $memberTypeId;
- $rego['amount_due'] = $memberType['MemberType']['price'];
- $rego['payment_method'] = $paymentMethod;
- $totalAmount += -$rego['amount_due'];
- unset($rego['MemberType']);
- $registrations[] = $rego;
- //----- Create a new transaction (negative amounts, as they will not have paid yet) -----
- /* if ($i == 0) {*/
- // For primary members
- $dataToSave[$i]['Member']['Payment'][] = [
- 'payment_method' => $paymentMethod,
- 'amount' => -$rego['amount_due'],
- 'season_id' => $memberType['Season']['id'],
- 'membertype_id' => $memberType['MemberType']['id'],
- 'extra_notes' => 'Registered for ' . $memberType['Season']['name'] . ': ' . $memberType['MemberType']['name'],
- ];
- /* } else {
- // Additional members ($0 amount, cos they won't be paying)
- $dataToSave[$i]['Member']['Payment'][] = [
- 'payment_method' => $paymentMethod,
- 'amount' => 0,
- 'season_id' => $memberType['Season']['id'],
- 'membertype_id' => $memberType['MemberType']['id'],
- 'extra_notes' => 'Registered for ' . $memberType['Season']['name'] . ': ' . $memberType['MemberType']['name']
- . ', fees of $' . $rego['amount_due'] . ' assigned to primary member',
- ];
- // Transaction for their primary member (who will be paying the bill)
- $dataToSave[0]['Member']['Payment'][] = [
- 'payment_method' => $paymentMethod,
- 'amount' => -$rego['amount_due'],
- 'season_id' => $memberType['Season']['id'],
- 'membertype_id' => $memberType['MemberType']['id'],
- 'extra_notes' => 'Additional member ('
- . $member['Member']['first_name'] . ' ' . $member['Member']['first_name']
- . ') registered for '
- . $memberType['Season']['name'] . ': ' . $memberType['MemberType']['name'],
- ];
- }*/
- }
- }
- $dataToSave[$i]['Member']['Registration'] = $registrations;
- }
- // Fix: now that we're performing renewals and new regos in same function, the money outstanding gets adjusted later
- // $dataToSave[0]['Member']['money_outstanding'] = $totalAmount;
- // We now want to restructure the numerical array of members so that all
- // the *additional* members sit underneath the primary member,
- // so that Cake assigns the parent_id properly
- // (could have done by altering the front-end form, but think it might be easier here)
- /*
- * FROM:
- * [0 => ['Member' => ['name' => 'X', ['Registration' => []]]],
- * 1 => ['Member' => ['name' => 'X', ['Registration' => []]]]]
- *
- * TO:
- * ['Member' => [name => 'X'],
- * 'ChildMember' => [['name' => 'x', 'Registration' => []]],
- * 'Registration' => []]]
- */
- $dataToSave['ChildMember'] = Set::extract(array_slice($dataToSave, 1), '{n}.Member'); // all members from 1 onwards
- $dataToSave = $dataToSave[0] + ['ChildMember' => $dataToSave['ChildMember']];
- $dataToSave['Registration'] = $dataToSave['Member']['Registration'];
- unset($dataToSave['Member']['Registration']);
- // debug($dataToSave);
- // If validation succeeds and data is successfully saved...
- if ($this->Member->saveAssociated($dataToSave, ['deep' => true])) {
- // Increment the parent member's balance by the total amount of all registrations
- $this->Member->updateAll(
- ['Member.money_outstanding' => "Member.money_outstanding + $totalAmount"], ['Member.id' => $this->Member->id]);
- $this->view = 'registration_complete';
- //----------------- PAYPAL ------------------------------------
- // Members who chose Paypal will see an extra step here
- if ($paymentMethod == 'paypal') {
- $this->view = 'registration_paypal';
- $this->set('memberId', $this->Member->id);
- $this->set('amountDue', $totalAmount);
- $this->Session->write('PaypalController.ApiLogin', $club['Club']['payment_settings']['paypal']);
- }
- //--------------------------- EMAIL THE MEMBER -----------------------------
- $memberRegoFields = [];
- // We will display a section for each additional member
- // (each member is a different index in $memberRegoFields)
- foreach($this->request->data as $i => $member) {
- $memberRegoFields[$i]['First Name'] = $member['Member']['first_name'];
- $memberRegoFields[$i]['Last Name'] = $member['Member']['last_name'];
- $memberRegoFields[$i]['Email'] = $member['Member']['email'];
- // Will be displaying unhashed password in email (only for primary user)
- if (isset($member['User']['password']))
- $memberRegoFields[$i]['Password'] = $member['User']['password'];
- // Loop through the members types they subscribed to and display this as a row in the email
- // $types = Hash::extract($member['Member']['Registration'], '{n}.MemberType.{n}');
- $memberTypeIds = (array)$member['Member']['Registration'][0]['MemberType'];
- // debug($memberTypeIds);
- foreach($memberTypeIds as &$t) {
- $memberType = Hash::extract($club, 'Season.{n}.MemberType.{n}[id=' . $t . ']')[0];
- $seasonName = Hash::extract($club, 'Season.{n}[id=' . $memberType['season_id'] . ']')[0]['name'];
- $memberRegoFields[$i]['Regos'][] = [
- 'Season' => $seasonName,
- 'Type' => $memberType['name'],
- 'Price' => '$' . $memberType['price'],
- ];
- // $t = "$seasonName: $memberType[name] ($$memberType[price])"; // assigning by reference
- }
- /* * ***** no longer displaying custom fields on rego email ***************
- // Loop through the custom fields and use the labels the admin has specified
- foreach ($member['Member'] as $key => $value) {
- $label = Hash::extract($club['Club']['rego_form'], '{n}[dbField=' . $key . '].label');
- // debug($label);
- if ($label)
- // $emailFields[$label[0]] = $value;
- $memberRegoFields[$i][$label[0]] = $value;
- }
- /************************************************************************ */
- }
- /*
- * At this point, the $memberRegoFields array looks something like this...
- * (ready for use in the email template)
- *
- * array(
- * (int) 0 => array(
- * 'First Name' => 'Haviva',
- * 'Last Name' => 'Kidd',
- * 'Email' => 'tyva@gmail.com',
- * 'Password' => 'pass',
- * 'Regos' => array(
- * (int) 0 => array(
- * 'Season' => 'Season 1',
- * 'Type' => 'Family',
- * 'Price' => '$200.00'
- * ),
- * (int) 1 => array(
- * 'Season' => 'Season 1',
- * 'Type' => 'Junior',
- * 'Price' => '$123.00'
- * )
- * ),
- * ),
- * (int) 1 => array(
- * 'First Name' => 'Ivan',
- * 'Last Name' => 'Franks',
- * 'Email' => 'mojo@yahoo.com',
- * 'Regos' => array(
- * (int) 0 => array(
- * 'Season' => 'Season 1',
- * 'Type' => 'Additional Family member',
- * 'Price' => '$0.00'
- * ),
- * (int) 1 => array(
- * 'Season' => 'Season 1',
- * 'Type' => 'Junior',
- * 'Price' => '$123.00'
- * )
- * ),
- * )
- * )
- */
- // debug($memberRegoFields);
- // Club email addresses are a recent addition (April 2013), so for clubs that don't have them yet,
- // we'll send the email "from" a non-existent **** address
- if (!Validation::email($club['Club']['email']))
- $club['Club']['email'] = 'NO-REPLY@****';
- //----- Render and send the actual email -----
- App::uses('CakeEmail', 'Network/Email');
- $email = new CakeEmail('DebugAndLog');
- $email->to($dataToSave['Member']['email'])
- ->from([$club['Club']['email'] => $club['Club']['name'] . ' Registrations (via ***)'])
- ->subject('Your registration was successful, here\'s your receipt.')
- ->viewVars([
- 'memberRegoFields' => $memberRegoFields,
- 'paymentMethod' => $paymentMethod,
- 'club' => h($club),
- 'memberId' => $this->Member->id,
- 'totalFees' => $totalAmount,
- ])
- // TODO: use a club-specific template
- ->template('member_registration') // View/Emails/html/member_registration.ctp
- // and View/Layouts/Emails/html/default.ctp
- ->emailFormat('html')
- ->send();
- // TODO: send email notification to the club?
- } else { // if validation failed
- // debug($this->Member->validationErrors);
- }
- }
- //---- end of saving to DB -----
- // $this->layout = '../../webroot/themes/***/standard_header_footer';
- $club['Club']['rego_form'] = Hash::filter($club['Club']['rego_form']); // remove empty items from array (why?)
- $this->set('club', $club);
- // Always display at least one blank member form
- if (empty($this->request->data))
- $this->request->data = [null];
- // Find the payment methods this club is willing to accept and send these to the view
- $paymentMethods = [];
- foreach($club['Club']['payment_settings'] as $paymentType => $options)
- if ($options['enabled'])
- if ($paymentType == 'EFT')
- $paymentMethods[$paymentType] = 'Electronic Funds Transfer (EFT)';
- elseif ($paymentType == 'paypal')
- $paymentMethods[$paymentType] = 'VISA / Mastercard (PayPal)';
- elseif ($paymentType == 'CH')
- $paymentMethods[$paymentType] = 'Cheque';
- elseif ($paymentType == 'CA')
- $paymentMethods[$paymentType] = 'Cash';
- $this->set('paymentMethods', $paymentMethods);
- $this->set('title_for_layout', $club['Club']['name'] . ' Member Registration Form');
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement