/** * Given a home (and optional mailing) address validator (which is unlinked from any db entry), go ahead and make updates * to this person record accordingly. * * If this person is part of multiple households, it will throw an error. * * If this person is part of one household, it will update the information for the household. * * If this person is not part of any houseold, it will create one for him/her. * * @param AddressValidator $objHomeAddressValidator * @param AddressValidator $objMailingAddressValidator optional * @param string $strHomePhone optional */ public function UpdateAddressInformation(AddressValidator $objHomeAddressValidator, AddressValidator $objMailingAddressValidator = null, $strHomePhone = null) { $objHouseholdArray = array(); foreach ($this->GetHouseholdParticipationArray() as $objHouseholdParticipation) { $objHouseholdArray[] = $objHouseholdParticipation->Household; } // Figure Out Household Information if (count($objHouseholdArray) > 1) { throw new QCallerException('Cannot call UpdateAddressInformation on a person who is part of multiple households: ' . $this->intId); } if (count($objHouseholdArray)) { $objHousehold = $objHouseholdArray[0]; } else { $objHousehold = Household::CreateHousehold($this); } // Home Address $objHomeAddress = $objHousehold->GetCurrentAddress(); $objAddress = $objHomeAddressValidator->CreateAddressRecord(); // Are we using the existing Household CurrentAddress record? if ($objHomeAddress && $objHomeAddress->IsEqualTo($objAddress)) { // Yes -- so all we're handling is the phones $objHomePhoneArray = $objHomeAddress->GetPhoneArray(); // Are we setting a home phone? if ($strHomePhone) { // Try and find the phone within the array foreach ($objHomePhoneArray as $objPhone) { $blnFound = false; if ($objPhone->Number == $strHomePhone) { $objPhone->SetAsPrimary(null, $objHomeAddress); $blnFound = true; } } // If we didn't find an existing home phone, we will update the current primary (if applicable) // Or create a new one as primary if (!$blnFound) { if (count($objHomePhoneArray)) { $objHomePhoneArray[0]->Number = $strHomePhone; $objHomePhoneArray[0]->Save(); } else { $objPhone = new Phone(); $objPhone->Number = $strHomePhone; $objPhone->Address = $objHomeAddress; $objPhone->PhoneTypeId = PhoneType::Home; $objPhone->Save(); $objPhone->SetAsPrimary(null, $objHomeAddress); } } // Nope - we are deleting the home phone } else { foreach ($objHomePhoneArray as $objPhone) { $objPhone->Delete(); } } // Not an existing Household CurrentAddress record that matches -- so we are creating a new one } else { $objAddress->Household = $objHousehold; $objAddress->CurrentFlag = true; $objAddress->AddressTypeId = AddressType::Home; $objAddress->Save(); $objHousehold->SetAsCurrentAddress($objAddress); // Add the phone if applicable if ($strHomePhone) { $objPhone = new Phone(); $objPhone->Number = $strHomePhone; $objPhone->Address = $objAddress; $objPhone->PhoneTypeId = PhoneType::Home; $objPhone->Save(); $objPhone->SetAsPrimary(null, $objAddress); } } // Mailing Address? if ($objMailingAddressValidator) { $objAddress = $objMailingAddressValidator->CreateAddressRecord(); $blnFound = false; foreach ($this->GetAllAssociatedAddressArray($objHousehold) as $objExistingAddress) { if ($objExistingAddress->IsEqualTo($objAddress)) { $this->MailingAddress = $objExistingAddress; $this->RefreshPrimaryContactInfo(); $blnFound = true; } } if (!$blnFound) { $objAddress->AddressTypeId = AddressType::Other; $objAddress->Person = $this; $objAddress->CurrentFlag = true; $objAddress->Save(); $this->MailingAddress = $objAddress; $this->RefreshPrimaryContactInfo(); } } else { if ($this->MailingAddress) { $this->MailingAddress = null; $this->RefreshPrimaryContactInfo(); } } }
protected static function GenerateAddressesForHousehold(Household $objHousehold) { // Add address and phone $intAddressCount = rand(1, 5); for ($i = 0; $i < $intAddressCount; $i++) { $objAddress = new Address(); $objAddress->AddressTypeId = AddressType::Home; $objAddress->Household = $objHousehold; $objAddress->Address1 = QDataGen::GenerateStreetAddress(); if (rand(0, 1)) { $objAddress->Address2 = QDataGen::GenerateAddressLine2(); } $objAddress->City = QDataGen::GenerateCity(); $objAddress->State = QDataGen::GenerateUsState(); $objAddress->ZipCode = rand(10000, 99999); $objAddress->Country = 'US'; $objAddress->InvalidFlag = false; $objAddress->VerificationCheckedFlag = true; $objAddress->CurrentFlag = $i + 1 == $intAddressCount; $objAddress->Save(); $intPhoneCount = rand(1, 3); for ($j = 0; $j < $intPhoneCount; $j++) { $objPhone = new Phone(); $objPhone->PhoneTypeId = PhoneType::Home; $objPhone->Address = $objAddress; $objPhone->Number = QDataGen::GeneratePhone(); $objPhone->Save(); } // Set the last one we created as "Primary" $objPhone->SetAsPrimary(); } }
/** * Given data points supplied this will attempt to reconcile this PublicLogin record to an actual person... or if not possible, * it will create a new person record. * * There are several stesp to this process. * First, do a Search of All People for the given Email * - if 1 and only 1 person was found: * + Check the personal details of that persons record * - If matched against First/Last or Nickname/Last, setup the match * - If matched against DOB and/or Gender and/or Mobile, setup the match and update the name * - If not matched, create as new * - if no people were found * + in all the DB, do a search to find matches against First/Last or Nickname/Last AND any address * - if exactly one is found, setup the match * - if non were found, create as new * - if multiple were found, find the first one that also match DOB and/or Gender and/or Mobile (or if none just find the first one) and setup the match * - if multiple people were found * + WITHIN the set of found people, do a search to find matches against First/Last or Nickname/Last AND any address * - if exactly one is found, setup the match * - if non were found, create as new * - if multiple were found, find the first one that also match DOB and/or Gender and/or Mobile (or if none just find the first one) and setup the match * * It will then check and see if the Person found (if any) already has a login object... if so, it will return that found Person record and do nothing further. * * Otherwise, it will then update all data accordingly (including creating a new Person record, if needed) and return the Person that was created. * * @param string $strPassword * @param string $strQuestion * @param string $strAnswer * @param string $strHomePhone * @param string $strMobilePhone * @param Address $objHomeAddress * @param Address $objMailingAddress optional * @param QDateTime $dttDateOfBirth optional * @param string $strGenderFlag optional * @return Person the person that was matched */ public function Reconcile($strPassword, $strQuestion, $strAnswer, $strHomePhone, $strMobilePhone, Address $objHomeAddress, Address $objMailingAddress = null, QDateTime $dttDateOfBirth = null, $strGenderFlag = null) { // Try and Find a Person based on Email $objPersonArray = array(); foreach (Email::LoadArrayByAddress($this->strEmailAddress) as $objEmail) { $objPersonArray[] = $objEmail->Person; } // Let's try and find a single Person object to reconcile for // 1 and only 1 person was found with the email if (count($objPersonArray) == 1) { if ($objPersonArray[0]->IsNameMatch($this->FirstName, $this->LastName)) { $objPerson = $objPersonArray[0]; } else { if ($objPersonArray[0]->ScoreDobGenderMobileMatch($dttDateOfBirth, $strGenderFlag, $strMobilePhone) > 0) { $objPerson = $objPersonArray[0]; } else { $objPerson = null; } } // MORE than one person was found with the email } else { if (count($objPersonArray)) { // Go through each of them and find a name-match and address-match $objMatchedPersonArray = array(); foreach ($objPersonArray as $objPerson) { if ($objPerson->IsNameAndAddressMatch($this->FirstName, $this->LastName, $objHomeAddress, $objMailingAddress)) { $objMatchedPersonArray[] = $objPerson; } } if (count($objMatchedPersonArray) == 1) { $objPerson = $objMatchedPersonArray[0]; } else { if (!count($objMatchedPersonArray)) { $objPerson = null; } else { $objPerson = $objMatchedPersonArray[0]; $intCurrentScore = 0; foreach ($objMatchedPersonArray as $objMatchedPerson) { $intScore = $objMatchedPerson->ScoreDobGenderMobileMatch($dttDateOfBirth, $strGenderFlag, $strMobilePhone); if ($intScore > $intCurrentScore) { $intCurrentScore = $intScore; $objPerson = $objMatchedPerson; } } } } // NO ONE was found with the email } else { // First pull the ones with Name Matched $objPersonArray = Person::LoadArrayByNameMatch($this->FirstName, $this->LastName); // Go through each of those and find address-match records $objMatchedPersonArray = array(); foreach ($objPersonArray as $objPerson) { if ($objPerson->IsNameAndAddressMatch($this->FirstName, $this->LastName, $objHomeAddress, $objMailingAddress)) { $objMatchedPersonArray[] = $objPerson; } } if (count($objMatchedPersonArray) == 1) { $objPerson = $objMatchedPersonArray[0]; } else { if (!count($objMatchedPersonArray)) { $objPerson = null; } else { $objPerson = $objMatchedPersonArray[0]; $intCurrentScore = 0; foreach ($objMatchedPersonArray as $objMatchedPerson) { $intScore = $objMatchedPerson->ScoreDobGenderMobileMatch($dttDateOfBirth, $strGenderFlag, $strMobilePhone); if ($intScore > $intCurrentScore) { $intCurrentScore = $intScore; $objPerson = $objMatchedPerson; } } } } } } // If we have a person, make sure it's not already linked if ($objPerson) { if ($objPerson->PublicLogin) { return $objPerson; } } // We now have either a unlinked Person record or no person record // Let's make all the DB updates we need to Person::GetDatabase()->TransactionBegin(); // Do we have a single Person object? // If not, let's create it if (!$objPerson) { $blnMaleFlag = null; if ($strGenderFlag = trim(strtoupper($strGenderFlag))) { $blnMaleFlag = $strGenderFlag == 'M'; } $intPhoneTypeId = $strMobilePhone ? PhoneType::Mobile : null; $objPerson = Person::CreatePerson($this->FirstName, null, $this->LastName, $blnMaleFlag, $this->EmailAddress, $strMobilePhone, $intPhoneTypeId); $objPerson->DateOfBirth = $dttDateOfBirth; if ($objPerson->DateOfBirth) { $objPerson->DobGuessedFlag = false; $objPerson->DobYearApproximateFlag = false; } $objPerson->PublicCreationFlag = true; $objPerson->Save(); } ////////////////////////////////// // Let's update the information ////////////////////////////////// // Email Address $objPerson->ChangePrimaryEmailTo($this->EmailAddress, false); // Gender and DOB if ($strGenderFlag = trim(strtoupper($strGenderFlag))) { $objPerson->Gender = $strGenderFlag; } if ($dttDateOfBirth) { $objPerson->DateOfBirth = $dttDateOfBirth; $objPerson->DobGuessedFlag = false; $objPerson->DobYearApproximateFlag = false; } // Mobile Phone if ($strMobilePhone) { $blnFound = false; foreach ($objPerson->GetAllAssociatedPhoneArray() as $objPhone) { if ($objPhone->Number == $strMobilePhone) { $objPhone->PhoneTypeId = PhoneType::Mobile; $objPhone->Save(); $blnFound = true; } } if (!$blnFound) { $objPhone = new Phone(); $objPhone->Number = $strMobilePhone; $objPhone->PhoneTypeId = PhoneType::Mobile; $objPhone->Save(); $objPerson->PrimaryPhone = $objPhone; } } // Mailing Address if ($objMailingAddress) { $blnFound = false; foreach ($objPerson->GetAllAssociatedAddressArray() as $objAddress) { if ($objAddress->IsEqualTo($objMailingAddress)) { $blnFound = true; $objAddress->CurrentFlag = true; $objAddress->AddressTypeId = AddressType::Other; $objAddress->Save(); $objPerson->MailingAddress = $objAddress; $objPerson->StewardshipAddress = $objAddress; } } if (!$blnFound) { $objMailingAddress->Person = $objPerson; $objMailingAddress->CurrentFlag = true; $objMailingAddress->AddressTypeId = AddressType::Other; $objMailingAddress->Save(); $objPerson->MailingAddress = $objMailingAddress; $objPerson->StewardshipAddress = $objMailingAddress; } } // Home Address and Phone $objHouseholdParticipationArray = $objPerson->GetHouseholdParticipationArray(); if (count($objHouseholdParticipationArray) == 1) { $objHousehold = $objHouseholdParticipationArray[0]->Household; $objAddress = $objHousehold->GetCurrentAddress(); if ($objAddress && $objAddress->IsEqualTo($objHomeAddress)) { $objHomeAddress = $objAddress; } else { $objHomeAddress->AddressTypeId = AddressType::Home; $objHomeAddress->Household = $objHousehold; $objHomeAddress->CurrentFlag = true; $objHomeAddress->Save(); if ($objAddress) { $objAddress->CurrentFlag = false; $objAddress->Save(); } } if ($strHomePhone) { $blnFound = false; foreach ($objHomeAddress->GetPhoneArray() as $objPhone) { if ($objPhone->Number == $strHomePhone) { $blnFound = true; $objPhone->SetAsPrimary(null, $objHomeAddress); } } if (!$blnFound) { $objPhone = new Phone(); $objPhone->PhoneTypeId = PhoneType::Home; $objPhone->Number = $strHomePhone; $objPhone->Address = $objHomeAddress; $objPhone->Save(); $objPhone->SetAsPrimary(null, $objHomeAddress); } } } else { if (count($objHouseholdParticipationArray) == 0) { $objHousehold = Household::CreateHousehold($objPerson); $objHomeAddress->AddressTypeId = AddressType::Home; $objHomeAddress->Household = $objHousehold; $objHomeAddress->CurrentFlag = true; $objHomeAddress->Save(); $objHousehold->SetAsCurrentAddress($objHomeAddress); if ($strHomePhone) { $objPhone = new Phone(); $objPhone->PhoneTypeId = PhoneType::Home; $objPhone->Number = $strHomePhone; $objPhone->Address = $objHomeAddress; $objPhone->Save(); $objPhone->SetAsPrimary(null, $objHomeAddress); } } } // Wire to PublicLogin $this->PublicLogin->Person = $objPerson; $this->PublicLogin->SetPassword($strPassword); $this->PublicLogin->LostPasswordQuestion = trim($strQuestion); $this->PublicLogin->LostPasswordAnswer = trim(strtolower($strAnswer)); $this->PublicLogin->DateLastLogin = QDateTime::Now(); $this->PublicLogin->Save(); // Save the person and delete the provision $objPerson->RefreshPrimaryContactInfo(); $objPerson->RefreshAge(false); $objPerson->RefreshNameItemAssociations(true); $this->Delete(); // Commit to DB Person::GetDatabase()->TransactionCommit(); // Return the now-linked person! return $objPerson; }