| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCommunity\Modules\Auth\Classes; |
| 4 |
|
| 5 |
use FluentCommunity\App\Http\Controllers\Controller; |
| 6 |
use FluentCommunity\App\Models\BaseSpace; |
| 7 |
use FluentCommunity\App\Models\SpaceUserPivot; |
| 8 |
use FluentCommunity\App\Services\Helper; |
| 9 |
use FluentCommunity\App\Services\ProfileHelper; |
| 10 |
use FluentCommunity\Framework\Http\Request\Request; |
| 11 |
use FluentCommunity\Framework\Support\Arr; |
| 12 |
|
| 13 |
class InvitationController extends Controller |
| 14 |
{ |
| 15 |
public function getInvitations(Request $request) |
| 16 |
{ |
| 17 |
$user = $this->getUser(true); |
| 18 |
$isMod = $user->isCommunityModerator(); |
| 19 |
$spaceId = intval($request->get('space_id')); |
| 20 |
|
| 21 |
$status = $request->get('status', 'pending'); |
| 22 |
if ($status == 'all') { |
| 23 |
$status = ''; |
| 24 |
} |
| 25 |
|
| 26 |
$inviatations = Invitation::where('user_id', $user->ID) |
| 27 |
->when($status, function ($query) use ($status) { |
| 28 |
$query->where('status', $status); |
| 29 |
}) |
| 30 |
->with(['xprofile' => function ($query) { |
| 31 |
$query->select(ProfileHelper::getXProfilePublicFields()); |
| 32 |
}]) |
| 33 |
->when(!$isMod, function ($query) use ($user) { |
| 34 |
$query->where('user_id', $user->ID); |
| 35 |
}) |
| 36 |
->when($spaceId, function ($query) use ($spaceId) { |
| 37 |
$query->where('post_id', $spaceId); |
| 38 |
}) |
| 39 |
->orderBy('id', 'desc') |
| 40 |
->paginate(); |
| 41 |
|
| 42 |
return [ |
| 43 |
'invitations' => $inviatations, |
| 44 |
'is_mod' => $isMod |
| 45 |
]; |
| 46 |
} |
| 47 |
|
| 48 |
public function delete(Request $request, $invitationId) |
| 49 |
{ |
| 50 |
$invitationId = intval($invitationId); |
| 51 |
Invitation::findOrFail($invitationId)->delete(); |
| 52 |
|
| 53 |
return [ |
| 54 |
'message' => __('Invitation deleted successfully', 'fluent-community') |
| 55 |
]; |
| 56 |
} |
| 57 |
|
| 58 |
public function store(Request $request) |
| 59 |
{ |
| 60 |
$data = $request->all(); |
| 61 |
$this->validate($data, [ |
| 62 |
'email' => 'required|email', |
| 63 |
]); |
| 64 |
|
| 65 |
$spaceId = (int)Arr::get($data, 'space_id'); |
| 66 |
$user = $this->getUser(true); |
| 67 |
|
| 68 |
$inviteeEmail = sanitize_email(Arr::get($data, 'email')); |
| 69 |
|
| 70 |
$indivatationData = array_filter([ |
| 71 |
'email' => $inviteeEmail, |
| 72 |
'user_id' => $user->ID, |
| 73 |
'invitee_name' => sanitize_text_field(Arr::get($data, 'invitee_name')), |
| 74 |
]); |
| 75 |
|
| 76 |
if ($spaceId) { |
| 77 |
$space = BaseSpace::findOrFail($spaceId); |
| 78 |
$spaceRole = $user->getSpaceRole($space); |
| 79 |
|
| 80 |
if (!$spaceRole || $spaceRole == 'pending') { |
| 81 |
$this->sendError([ |
| 82 |
'message' => __('You are not permitted to invite', 'fluent-community') |
| 83 |
]); |
| 84 |
} |
| 85 |
|
| 86 |
$isMod = in_array($spaceRole, ['admin', 'moderator']); |
| 87 |
|
| 88 |
if (!$isMod && $space->privacy != 'public') { |
| 89 |
$this->sendError([ |
| 90 |
'message' => __('You are not permitted to invite', 'fluent-community') |
| 91 |
]); |
| 92 |
} |
| 93 |
|
| 94 |
$indivatationData['space_id'] = $space->id; |
| 95 |
} |
| 96 |
|
| 97 |
$invitation = InvitationService::invite($indivatationData); |
| 98 |
if (is_wp_error($invitation)) { |
| 99 |
return $this->sendError([ |
| 100 |
'message' => $invitation->get_error_message() |
| 101 |
]); |
| 102 |
} |
| 103 |
|
| 104 |
// Now send the email for the invitation |
| 105 |
InvitationService::sendInvitationEmail($invitation); |
| 106 |
|
| 107 |
return [ |
| 108 |
'message' => __('Invitation has been sent successfully', 'fluent-community'), |
| 109 |
'invitation' => $invitation |
| 110 |
]; |
| 111 |
} |
| 112 |
|
| 113 |
public function resend(Request $request, $invitationId) |
| 114 |
{ |
| 115 |
$invitation = Invitation::findOrFail($invitationId); |
| 116 |
|
| 117 |
if ($invitation->reactions_count > 5) { |
| 118 |
return $this->sendError([ |
| 119 |
'message' => __('You can not resend this invitation', 'fluent-community') |
| 120 |
]); |
| 121 |
} |
| 122 |
|
| 123 |
InvitationService::sendInvitationEmail($invitation); |
| 124 |
|
| 125 |
return [ |
| 126 |
'message' => __('Invitation has been resent successfully', 'fluent-community') |
| 127 |
]; |
| 128 |
} |
| 129 |
|
| 130 |
private function validateExistingWpUser($email, $spaceId = null) |
| 131 |
{ |
| 132 |
$user = get_user_by('email', $email); |
| 133 |
|
| 134 |
if ($user) { |
| 135 |
if ($spaceId && !$this->userExistInSpace($spaceId, $user->ID)) { |
| 136 |
return true; |
| 137 |
} |
| 138 |
|
| 139 |
wp_send_json([ |
| 140 |
'message' => __('You are already a member of this site', 'fluent-community') |
| 141 |
], 400); |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
private function isInviteMembers() |
| 146 |
{ |
| 147 |
$settings = Helper::getOnboardingSettings(); |
| 148 |
|
| 149 |
return Arr::get($settings, 'is_onboarding_enabled', '') == 'yes'; |
| 150 |
} |
| 151 |
|
| 152 |
private function isTodayInvitationLimitExceeded($userId) |
| 153 |
{ |
| 154 |
$todayInvitationCount = Invitation::getTodayInvitationCount($userId); |
| 155 |
|
| 156 |
return $todayInvitationCount > 10; |
| 157 |
} |
| 158 |
|
| 159 |
private function userExistInSpace($spaceId, $userId) |
| 160 |
{ |
| 161 |
return SpaceUserPivot::where('space_id', $spaceId) |
| 162 |
->where('user_id', $userId) |
| 163 |
->exists(); |
| 164 |
} |
| 165 |
} |
| 166 |
|