public function insertForApproval($FormPostValues, $Options = [])
{
$RoleIDs = RoleModel::getDefaultRoles(RoleModel::TYPE_APPLICANT);
if (empty($RoleIDs)) {
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);
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]);
if (!$this->validateUniqueFields($Username, $Email)) {
return false;
}
// If in Captcha registration mode, check the captcha value.
if (val('CheckCaptcha', $Options, true) && Captcha::enabled()) {
$captchaIsValid = Captcha::validate();
if ($captchaIsValid !== true) {
$this->Validation->addValidationResult('Garden.Registration.CaptchaPublicKey', 'The captcha was not completed correctly. Please try again.');
return false;
}
}
// Define the other required fields:
$Fields['Email'] = $Email;
$Fields['Roles'] = (array) $RoleIDs;
// And insert the new user
$UserID = $this->insertInternal($Fields, $Options);
} else {
$UserID = false;
}
return $UserID;
}