public function approve($UserID, $Email)
{
$applicantRoleIDs = RoleModel::getDefaultRoles(RoleModel::TYPE_APPLICANT);
// Make sure the $UserID is an applicant
$RoleData = $this->getRoles($UserID);
if ($RoleData->numRows() == 0) {
throw new Exception(t('ErrorRecordNotFound'));
} else {
$AppRoles = $RoleData->result(DATASET_TYPE_ARRAY);
$ApplicantFound = false;
foreach ($AppRoles as $AppRole) {
if (in_array(val('RoleID', $AppRole), $applicantRoleIDs)) {
$ApplicantFound = true;
}
}
}
if ($ApplicantFound) {
// Retrieve the default role(s) for new users
$RoleIDs = RoleModel::getDefaultRoles(RoleModel::TYPE_MEMBER);
// Wipe out old & insert new roles for this user
$this->saveRoles($UserID, $RoleIDs, false);
// Send out a notification to the user
$User = $this->getID($UserID);
if ($User) {
$Email = new Gdn_Email();
$Email->subject(sprintf(t('[%1$s] Membership Approved'), c('Garden.Title')));
$Email->to($User->Email);
$message = sprintf(t('Hello %s!'), val('Name', $User)) . ' ' . t('You have been approved for membership.');
$emailTemplate = $Email->getEmailTemplate()->setMessage($message)->setButton(externalUrl(signInUrl()), t('Sign In Now'))->setTitle(t('Membership Approved'));
$Email->setEmailTemplate($emailTemplate);
try {
$Email->send();
} catch (Exception $e) {
if (debug()) {
throw $e;
}
}
// Report that the user was approved.
$ActivityModel = new ActivityModel();
$ActivityModel->save(['ActivityUserID' => $UserID, 'ActivityType' => 'Registration', 'HeadlineFormat' => t('HeadlineFormat.Registration', '{ActivityUserID,You} joined.'), 'Story' => t('Welcome Aboard!')], false, ['GroupBy' => 'ActivityTypeID']);
// Report the approval for moderators.
$ActivityModel->save(['ActivityType' => 'Registration', 'ActivityUserID' => Gdn::session()->UserID, 'RegardingUserID' => $UserID, 'NotifyUserID' => ActivityModel::NOTIFY_MODS, 'HeadlineFormat' => t('HeadlineFormat.RegistrationApproval', '{ActivityUserID,user} approved the applications for {RegardingUserID,user}.')], false, ['GroupBy' => ['ActivityTypeID', 'ActivityUserID']]);
Gdn::userModel()->saveAttribute($UserID, 'ApprovedByUserID', Gdn::session()->UserID);
}
}
return true;
}