public function getInvitationCount($UserID)
{
// If this user is master admin, they should have unlimited invites.
if ($this->SQL->select('UserID')->from('User')->where('UserID', $UserID)->where('Admin', '1')->get()->numRows() > 0) {
return -1;
}
// Get the Registration.InviteRoles settings:
$InviteRoles = Gdn::config('Garden.Registration.InviteRoles', []);
if (!is_array($InviteRoles) || count($InviteRoles) == 0) {
return 0;
}
// Build an array of roles that can send invitations
$CanInviteRoles = [];
foreach ($InviteRoles as $RoleID => $Invites) {
if ($Invites > 0 || $Invites == -1) {
$CanInviteRoles[] = $RoleID;
}
}
if (count($CanInviteRoles) == 0) {
return 0;
}
// See which matching roles the user has
$UserRoleData = $this->SQL->select('RoleID')->from('UserRole')->where('UserID', $UserID)->whereIn('RoleID', $CanInviteRoles)->get();
if ($UserRoleData->numRows() == 0) {
return 0;
}
// Define the maximum number of invites the user is allowed to send
$InviteCount = 0;
foreach ($UserRoleData->result() as $UserRole) {
$Count = $InviteRoles[$UserRole->RoleID];
if ($Count == -1) {
$InviteCount = -1;
} elseif ($InviteCount != -1 && $Count > $InviteCount) {
$InviteCount = $Count;
}
}
// If the user has unlimited invitations, return that value
if ($InviteCount == -1) {
return -1;
}
// Get the user's current invitation settings from their profile
$User = $this->SQL->select('CountInvitations, DateSetInvitations')->from('User')->where('UserID', $UserID)->get()->firstRow();
// If CountInvitations is null (ie. never been set before) or it is a new month since the DateSetInvitations
if ($User->CountInvitations == '' || is_null($User->DateSetInvitations) || Gdn_Format::date($User->DateSetInvitations, '%m %Y') != Gdn_Format::date('', '%m %Y')) {
// Reset CountInvitations and DateSetInvitations
$this->SQL->put($this->Name, ['CountInvitations' => $InviteCount, 'DateSetInvitations' => Gdn_Format::date('', '%Y-%m-01')], ['UserID' => $UserID]);
return $InviteCount;
} else {
// Otherwise return CountInvitations
return $User->CountInvitations;
}
}