diff --git a/application/controllers/EnrollmentForm.php b/application/controllers/EnrollmentForm.php index 1c9b89a..1a8bbf6 100644 --- a/application/controllers/EnrollmentForm.php +++ b/application/controllers/EnrollmentForm.php @@ -27,9 +27,10 @@ function __construct() log_message('debug', "-- from IP address: " . $this->input->ip_address()); } - public function test() { - $this->load->model("Repository_Model"); - $this->Repository_Model->initClass(MEMBERSHIP_SPREADSHEET_ID, MEMBERSHIP_SHEET_NAME, REGISTRATION_SPREADSHEET_ID); + public function test() + { + $this->load->model("Repository_Model"); + $this->Repository_Model->initClass(MEMBERSHIP_SPREADSHEET_ID, MEMBERSHIP_SHEET_NAME, REGISTRATION_SPREADSHEET_ID); // $event = new Event( // "id3", // "name", @@ -55,7 +56,7 @@ public function test() { // ); print_r("This is a test function."/*$this->Repository_Model->getOrganisation("1")*/); - } + } /** * The "home" page. @@ -85,6 +86,8 @@ public function validate() $this->load->model('Verification_Model'); + $member = $this->Repository_Model->getMemberByEmail($emailAddress); + // If the email is of invalid format, return 412 error if (!isset($emailAddress) || !$this->Verification_Model->isValidEmail($emailAddress)) { $this->createResponse(412, 'Error: Format of email is invalid'); @@ -92,7 +95,7 @@ public function validate() } // If the email does not exist or is not on the membership spreadsheet, return 404 error - if (!$this->Verification_Model->isEmailOnSheet($emailAddress, MEMBERSHIP_SPREADSHEET_ID, MEMBERSHIP_SHEET_NAME)) { + if ($member === null) { $this->createResponse(404, 'Error: Email incorrect or not found on sheet'); return; } @@ -104,14 +107,12 @@ public function validate() } // If membership payment status is checked, and user's membership fee has not been paid, return 403 error - if (CHECK_MEMBERSHIP_PAYMENT && !$this->Verification_Model->hasUserPaidMembership($emailAddress)) { + if (CHECK_MEMBERSHIP_PAYMENT && !$member->feePaid) { $this->createResponse(403, "Error: signed up but not paid"); return; } - [$fullName, $UPI] = $this->Verification_Model->getMemberInfo($emailAddress); - - $this->createResponse(200, "Success", $fullName); + $this->createResponse(200, "Success", $member->fullName); } /** @@ -124,30 +125,27 @@ public function makeStripePayment() $this->load->model('Verification_Model'); // Receive data from form, method=POST - $data['email'] = $this->input->post('email'); - [$data['name'], $data['upi']] = $this->Verification_Model->getMemberInfo($data["email"]); + $memberEmail = $this->input->post('email'); + $member = $this->Repository_Model->getMemberByEmail($memberEmail); // Stopping direct access to this method - if (!isset($data['name']) || !isset($data['email'])) { - show_error("Sorry, this page you are requesting is either not found or you don't have permission to access this page. Error Code:001", - "404"); + if (!isset($member->fullName) || !isset($member->email)) { + show_error("Sorry, this page you are requesting is either not found or you don't have permission to access this page. Error Code:001", "404"); } - if (CHECK_MEMBERSHIP_PAYMENT) { - $paid_member = ($this->Verification_Model->hasUserPaidMembership($data['email'])); - if (!$paid_member) { - show_error("Something went wrong, your email was not found in the ASPA member list or haven't paid. Error Code: 002", "500"); - } + if (CHECK_MEMBERSHIP_PAYMENT && !$member->feePaid) { + show_error("Something went wrong, your email was not found in the ASPA member list or haven't paid. Error Code: 002", "500"); } // Only record if the email is not found - if (!($this->Verification_Model->isEmailOnSheet($data['email'], REGISTRATION_SPREADSHEET_ID, $this->eventData['gsheet_name']))) { - $this->GoogleSheets_Model->addNewRecord($data['email'], $data['name'], $data['upi'], 'Stripe'); + //TODO: WE NEED TO PASS THE PROPER EVENT ID + if ($this->Repository_Model->getRecord($member->email, '') === null) { + $this->GoogleSheets_Model->addNewRecord($member->email, $member->fullName, $member->upi, 'Stripe'); } else { // Email is found, so find the cell // Then edit the "How would you like your payment" to be of Stripe payment // Get the row of the specific email from google sheets - $cell = $this->GoogleSheets_Model->getCellCoordinate($data['email'], 'B'); + $cell = $this->GoogleSheets_Model->getCellCoordinate($member->email, 'B'); if (!isset($cell)) { show_error("Something went wrong, your email was not found in the ASPA member list.Error Code: 002", "500"); } @@ -160,10 +158,15 @@ public function makeStripePayment() // Generate the stripe session ID $this->load->model('Stripe_Model'); - $data['session_id'] = $this->Stripe_Model->generateNewSessionId($data['email'], $this->eventData); + $stripeSessionId = $this->Stripe_Model->generateNewSessionId($member->email, $this->eventData); // Initiate the stripe payment - $this->load->view('stripe.php', $data); + $this->load->view('stripe.php', [ + 'email' => $member->email, + 'name' => $member->fullName, + 'upi' => $member->upi, + 'session_id' => $stripeSessionId + ]); } /** @@ -172,25 +175,24 @@ public function makeStripePayment() public function makeOfflinePayment() { $this->load->model("GoogleSheets_Model"); - $this->load->model("Verification_Model"); $this->load->model('Email_Model'); log_message('debug', "-- makeOfflinePayment function called"); - $this->load->model("GoogleSheets_Model"); - $this->load->model("Verification_Model"); - $this->load->model('Email_Model'); - $data['has_paid'] = false; $data["email"] = $this->input->post("email"); $data['paymentMethod'] = $this->input->post("paymentMethod"); - [$data['name'], $data['upi']] = $this->Verification_Model->getMemberInfo($data["email"]); + $member = $this->Repository_Model->getMemberByEmail($data["email"]); + // [$data['name'], $data['upi']] = $this->Verification_Model->getMemberInfo($data["email"]); + $data['name'] = $member->fullName; + $data['upi'] = $member->upi; if (!isset($data['name']) || !isset($data["email"]) || !isset($data['paymentMethod'])) { show_error("Something went wrong. Please contact uoa.wdcc@gmail.com. Error Code: 001", "500"); } // Only record if the email is not found - if (!($this->Verification_Model->isEmailOnSheet($data['email'], REGISTRATION_SPREADSHEET_ID, $this->eventData['gsheet_name']))) { + //TODO: WE NEED TO PASS THE PROPER EVENT ID + if ($this->Repository_Model->getRecord($member->email, '') === null) { $this->GoogleSheets_Model->addNewRecord($data['email'], $data['name'], $data['upi'], ucfirst($data['paymentMethod'])); } else { // Email is found, so find the cell then edit the "How would you like your payment" to be of Offline payment diff --git a/application/core/ASPA_Controller.php b/application/core/ASPA_Controller.php index d791d78..fc6e03d 100644 --- a/application/core/ASPA_Controller.php +++ b/application/core/ASPA_Controller.php @@ -23,7 +23,7 @@ function __construct() parent::__construct(); $this->load->model("Repository_Model"); $this->load->model("GoogleSheets_Model"); - + $this->Repository_Model->initClass(MEMBERSHIP_SPREADSHEET_ID, MEMBERSHIP_SHEET_NAME, REGISTRATION_SPREADSHEET_ID); $this->eventData = $this->loadEventData(); $this->orgData = $this->Repository_Model->getOrganisation("")->toArray(); } @@ -62,8 +62,8 @@ private function loadEventData(): array // Get event details from spreadsheet from range A2 to size of spreadsheet $this->GoogleSheets_Model->setCurrentSheetName("CurrentEventDetails"); - $data = $this->GoogleSheets_Model->getCellContents('A2', 'C' . ($this->GoogleSheets_Model->getNumberOfRecords() + 2)); + $data = $this->GoogleSheets_Model->getCellContents('A2', 'C'); // Important variables we care about $elements = ['time', 'date', 'location', 'title', 'tagline', 'price', 'acc_num', 'desc', 'gsheet_name', 'form_enabled']; @@ -82,6 +82,4 @@ private function loadEventData(): array } return $eventTemp; } - } - diff --git a/application/models/Repository_Model.php b/application/models/Repository_Model.php index f393ac7..fe19797 100644 --- a/application/models/Repository_Model.php +++ b/application/models/Repository_Model.php @@ -1,6 +1,6 @@ GoogleSheets_Model->setCurrentSheetName("Events"); - $records = $this->GoogleSheets_Model->getNumberOfRecords(); - $array2d = $this->GoogleSheets_Model->getCellContents("A2", "J" . ($records + 2)); - for ($i = 0; $i < $records; $i++) { + $array2d = $this->GoogleSheets_Model->getCellContents("A2", "J"); + + for ($i = 0; $i < sizeof($array2d); $i++) { $current = $array2d[$i]; $id = $current[0]; $name = $current[1]; @@ -65,11 +65,9 @@ public function initClass(String $membershipSpreadsheetId, String $membershipShe $this->GoogleSheets_Model->setCurrentSheetName($membershipSheetName); - $records = $this->GoogleSheets_Model->getNumberOfRecords(); - - $array2d = $this->GoogleSheets_Model->getCellContents("A2", "I" . ($records + 2)); + $array2d = $this->GoogleSheets_Model->getCellContents("A2", "I"); - for ($i = 0; $i < $records; $i++) { + for ($i = 0; $i < sizeof($array2d); $i++) { $current = $array2d[$i]; $signUpDate = intval($current[0]); @@ -89,21 +87,24 @@ public function initClass(String $membershipSpreadsheetId, String $membershipShe * @param string the email of the member * @return Member the member object */ - public function getMemberByEmail(string $memberEmail) { - return $this->members[$memberEmail]; + public function getMemberByEmail(string $memberEmail) + { + return array_key_exists($memberEmail, $this->members) ? $this->members[$memberEmail] : null; } /** * @return Member[] a list of all members */ - public function getMembers() { + public function getMembers() + { return $this->members; } /** * @return Event the event object that corrosponds to the event ID */ - public function getEventById(string $eventId) { + public function getEventById(string $eventId) + { return $this->events[$eventId]; } @@ -111,49 +112,49 @@ public function getEventById(string $eventId) { * Gets an organization */ // TODO: Replace this to return the actual organisation. - public function getOrganisation(string $orgId) { - return new Organisation( - "ASPA", - "01-0129-0469512-00", - $orgId, - "-", - "-", - "Auckland Students Pool Association", - "uoapool@gmail.com", - ); + public function getOrganisation(string $orgId) + { + return new Organisation( + "ASPA", + "01-0129-0469512-00", + $orgId, + "-", + "-", + "Auckland Students Pool Association", + "uoapool@gmail.com", + ); } /** * Get a member's record from an event using their email and the event id * @return Record the member's record, or NULL */ - public function getRecord(string $memberEmail, string $eventId) { + public function getRecord(string $memberEmail, string $eventId) + { $records = $this->getRecordsByEvent($eventId); if (array_key_exists($memberEmail, $records)) { return $records[$memberEmail]; - } - else { + } else { return NULL; } } /** * Get all the records for an event - * @return Record[] a list of all records for an event + * @return Record[] returns an map of Records where the member's email is the key. */ - public function getRecordsByEvent(string $eventId) { - $this->GoogleSheets_Model->setCurrentSheetName($eventId); - - $records = $this->GoogleSheets_Model->getNumberOfRecords(); + public function getRecordsByEvent(string $eventId) + { + $this->GoogleSheets_Model->setCurrentSheetName($eventId); - $array2d = $this->GoogleSheets_Model->getCellContents("A2","K" . ($records + 2)); + $array2d = $this->GoogleSheets_Model->getCellContents("A2", "K"); - $allRecords = []; + $allRecords = []; - for ($i = 0; $i < $records; $i++) { - $allRecords[$array2d[$i][1]] = new Record($array2d[$i][1], $eventId, $array2d[$i][0], $array2d[$i][2], $array2d[$i][4], $array2d[$i][5], $array2d[$i][10], $array2d[$i][9], isset($array2d[$i][6])); - } + for ($i = 0; $i < sizeof($array2d); $i++) { + $allRecords[$array2d[$i][1]] = new Record($array2d[$i][1], $eventId, $array2d[$i][0], $array2d[$i][2], $array2d[$i][4], $array2d[$i][5], $array2d[$i][10], $array2d[$i][9], isset($array2d[$i][6])); + } return $allRecords; } @@ -162,7 +163,8 @@ public function getRecordsByEvent(string $eventId) { * Save an event to the database. * @param event the event to save to the database. */ - public function saveEvent(Event $event): Event { + public function saveEvent(Event $event): Event + { $this->GoogleSheets_Model->saveEvent($event->id, EVENT_SHEET_ID_COLUMN, $event->toArray()); return $event; @@ -172,9 +174,10 @@ public function saveEvent(Event $event): Event { * Save a record to the database. * @param record the record to save to the database. */ - public function saveRecord(Record $record) { - $this->GoogleSheets_Model->saveRecord($record->eventID, $record->email, REGISTRATIONS_SHEET_ID_COLUMN, $record->toArray()); + public function saveRecord(Record $record) + { + $this->GoogleSheets_Model->saveRecord($record->eventID, $record->email, REGISTRATIONS_SHEET_ID_COLUMN, $record->toArray()); - return $record; + return $record; } } diff --git a/application/models/Verification_Model.php b/application/models/Verification_Model.php index 65b7bd6..718cd65 100644 --- a/application/models/Verification_Model.php +++ b/application/models/Verification_Model.php @@ -1,12 +1,13 @@ GoogleSheets_Model->getNumberOfRecords(); // An array of array of all existing emails, i.e. [[email1], [email2], [email3]] - $this->addresses = array_column($this->GoogleSheets_Model->getCellContents('B2', 'B' . ($sheetSize+1)), 0); + $this->addresses = array_column($this->GoogleSheets_Model->getCellContents('B2', 'B' . ($sheetSize + 1)), 0); // Return true if email exists in google sheet return in_array($emailAddress, $this->addresses); @@ -57,7 +58,7 @@ public function hasUserPaidMembership($emailAddress) $emailKey = array_search($emailAddress, $this->addresses); // Convert key to google coordinate - $emailIndex = 'B' . ($emailKey+2); + $emailIndex = 'B' . ($emailKey + 2); // Check the cell colour of the email cell $colourIs = $this->GoogleSheets_Model->getCellColour($emailIndex); @@ -89,7 +90,7 @@ public function hasUserPaidEvent($emailAddress, $sheetName) $emailKey = array_search($emailAddress, $this->addresses); // Convert key to google coordinate - $emailIndex = 'B' . ($emailKey+2); + $emailIndex = 'B' . ($emailKey + 2); log_message("debug", "INDEX: " . $emailIndex); @@ -124,12 +125,12 @@ public function getMemberInfo($emailAddress) log_message("error", "The member is was not found on the sheet when recording to sheet"); } - // Given that the email exists in the sheet, find its index + // Given that the email exists in the sheet, find its index $emailKey = array_search($emailAddress, $this->addresses); // Convert key to google coordinate - $nameIndex = 'C' . ($emailKey+2); - $upiIndex = 'H' . ($emailKey+2); + $nameIndex = 'C' . ($emailKey + 2); + $upiIndex = 'H' . ($emailKey + 2); // Get member's full name and UPI – these are by default blank string ('') if they do not exist $memberFullName = $this->GoogleSheets_Model->getCellContents($nameIndex, $nameIndex)[0][0] ?? ''; @@ -157,5 +158,4 @@ public static function isValidEmail($emailAddress) // Returns bool variable for whether the sanitised email is valid return (bool) filter_var($emailAddress, FILTER_VALIDATE_EMAIL); } - } diff --git a/application/models/entities/Member.php b/application/models/entities/Member.php index 68fb212..188e6dc 100644 --- a/application/models/entities/Member.php +++ b/application/models/entities/Member.php @@ -9,7 +9,7 @@ class Member public string $fullName; public string $upi; public int $signUpDate; // PHP int holds 32 bits. Current epoch time is just under 32 bits I believe - public bool $feePaid; + public bool $feePaid; //membership fee not event fee public function __construct(string $email, string $fullName, string $upi, int $signUpDate, bool $feePaid) { @@ -19,5 +19,4 @@ public function __construct(string $email, string $fullName, string $upi, int $s $this->signUpDate = $signUpDate; $this->feePaid = $feePaid; } - }