public function insertForInvite($FormPostValues, $Options = [])
{
$RoleIDs = RoleModel::getDefaultRoles(RoleModel::TYPE_MEMBER);
if (!is_array($RoleIDs) || count($RoleIDs) == 0) {
throw new Exception(t('The default role has not been configured.'), 400);
}
// Define the primary key in this model's table.
$this->defineSchema();
// Add & apply any extra validation rules:
$this->Validation->applyRule('Email', 'Email');
// Make sure that the checkbox val for email is saved as the appropriate enum
if (array_key_exists('ShowEmail', $FormPostValues)) {
$FormPostValues['ShowEmail'] = forceBool($FormPostValues['ShowEmail'], '0', '1', '0');
}
if (array_key_exists('Banned', $FormPostValues)) {
$FormPostValues['Banned'] = forceBool($FormPostValues['Banned'], '0', '1', '0');
}
$this->addInsertFields($FormPostValues);
// Make sure that the user has a valid invitation code, and also grab
// the user's email from the invitation:
$InvitationCode = val('InvitationCode', $FormPostValues, '');
$Invitation = $this->SQL->getWhere('Invitation', ['Code' => $InvitationCode])->firstRow();
// If there is no invitation then bail out.
if (!$Invitation) {
$this->Validation->addValidationResult('InvitationCode', 'Invitation not found.');
return false;
}
// Get expiration date in timestamp. If nothing set, grab config default.
$InviteExpiration = $Invitation->DateExpires;
if ($InviteExpiration != null) {
$InviteExpiration = Gdn_Format::toTimestamp($InviteExpiration);
} else {
$DefaultExpire = '1 week';
$InviteExpiration = strtotime(c('Garden.Registration.InviteExpiration', '1 week'), Gdn_Format::toTimestamp($Invitation->DateInserted));
if ($InviteExpiration === false) {
$InviteExpiration = strtotime($DefaultExpire);
}
}
if ($InviteExpiration <= time()) {
$this->Validation->addValidationResult('DateExpires', 'The invitation has expired.');
}
$InviteUserID = $Invitation->InsertUserID;
$FormPostValues['Email'] = $Invitation->Email;
if ($this->validate($FormPostValues, true)) {
// Check for spam.
$Spam = SpamModel::isSpam('Registration', $FormPostValues);
if ($Spam) {
$this->Validation->addValidationResult('Spam', 'You are not allowed to register at this time.');
return;
}
$Fields = $this->Validation->validationFields();
// All fields on the form that need to be validated (including non-schema field rules defined above)
$Username = val('Name', $Fields);
$Email = val('Email', $Fields);
$Fields = $this->Validation->schemaValidationFields();
// Only fields that are present in the schema
unset($Fields[$this->PrimaryKey]);
// Make sure the username & email aren't already being used
if (!$this->validateUniqueFields($Username, $Email)) {
return false;
}
// Define the other required fields:
if ($InviteUserID > 0) {
$Fields['InviteUserID'] = $InviteUserID;
}
// And insert the new user.
if (!isset($Options['NoConfirmEmail'])) {
$Options['NoConfirmEmail'] = true;
}
// Use RoleIDs from Invitation table, if any. They are stored as a
// serialized array of the Role IDs.
$InvitationRoleIDs = $Invitation->RoleIDs;
if (strlen($InvitationRoleIDs)) {
$InvitationRoleIDs = dbdecode($InvitationRoleIDs);
if (is_array($InvitationRoleIDs) && count(array_filter($InvitationRoleIDs))) {
// Overwrite default RoleIDs set at top of method.
$RoleIDs = $InvitationRoleIDs;
}
}
$Fields['Roles'] = $RoleIDs;
$UserID = $this->insertInternal($Fields, $Options);
// Associate the new user id with the invitation (so it cannot be used again)
$this->SQL->update('Invitation')->set('AcceptedUserID', $UserID)->where('InvitationID', $Invitation->InvitationID)->put();
// Report that the user was created.
$ActivityModel = new ActivityModel();
$ActivityModel->save(['ActivityUserID' => $UserID, 'ActivityType' => 'Registration', 'HeadlineFormat' => t('HeadlineFormat.Registration', '{ActivityUserID,You} joined.'), 'Story' => t('Welcome Aboard!')], false, ['GroupBy' => 'ActivityTypeID']);
} else {
$UserID = false;
}
return $UserID;
}