public static function SocialEventInviteBulkHelper::bulkInviteUsersEmails in Open Social 8.9
Same name and namespace in other branches
- 10.3.x modules/social_features/social_event/modules/social_event_invite/src/SocialEventInviteBulkHelper.php \Drupal\social_event_invite\SocialEventInviteBulkHelper::bulkInviteUsersEmails()
- 10.0.x modules/social_features/social_event/modules/social_event_invite/src/SocialEventInviteBulkHelper.php \Drupal\social_event_invite\SocialEventInviteBulkHelper::bulkInviteUsersEmails()
- 10.1.x modules/social_features/social_event/modules/social_event_invite/src/SocialEventInviteBulkHelper.php \Drupal\social_event_invite\SocialEventInviteBulkHelper::bulkInviteUsersEmails()
- 10.2.x modules/social_features/social_event/modules/social_event_invite/src/SocialEventInviteBulkHelper.php \Drupal\social_event_invite\SocialEventInviteBulkHelper::bulkInviteUsersEmails()
Send the invites to emails in a batch.
Parameters
array $users: Array containing user ids or user emails.
string $nid: The node id.
array $context: The context.
Throws
\Drupal\Core\Entity\EntityStorageException
File
- modules/
social_features/ social_event/ modules/ social_event_invite/ src/ SocialEventInviteBulkHelper.php, line 160
Class
- SocialEventInviteBulkHelper
- Class SocialEventBulkInvite.
Namespace
Drupal\social_event_inviteCode
public static function bulkInviteUsersEmails(array $users, $nid, array &$context) {
$results = [];
foreach ($users as $user) {
// Todo: Should be merged with extractEmailsFrom from InviteEmailBaseForm.
// Remove select2 ID parameter.
$user = str_replace('$ID:', '', $user);
preg_match_all("/[\\._a-zA-Z0-9+-]+@[\\._a-zA-Z0-9+-]+/i", $user, $email);
$email = $email[0];
// If the user is an email.
if ($email) {
$user = user_load_by_mail($email);
// Default values.
$fields = [
'field_event' => $nid,
'field_enrollment_status' => '0',
'field_request_or_invite_status' => EventEnrollmentInterface::INVITE_PENDING_REPLY,
];
if ($user instanceof UserInterface) {
// Add user information.
$fields['user_id'] = $user
->id();
$fields['field_account'] = $user
->id();
// Clear the cache.
$tags = [];
$tags[] = 'enrollment:' . $nid . '-' . $user
->id();
$tags[] = 'event_content_list:entity:' . $user
->id();
Cache::invalidateTags($tags);
}
else {
// Add email address.
$fields['field_email'] = $email;
}
}
else {
// Default values.
$fields = [
'field_event' => $nid,
'field_enrollment_status' => '0',
'field_request_or_invite_status' => EventEnrollmentInterface::INVITE_PENDING_REPLY,
'user_id' => $user,
'field_account' => $user,
];
// Check if this user has been invited before. It might be that the user
// declined the invite, or that the invite is now invalid and expired.
// We simply delete the outdated invite and create a new one.
$conditions = [
'field_account' => $user,
'field_event' => $nid,
];
$eventEnrollmentStorage = \Drupal::entityTypeManager()
->getStorage('event_enrollment');
$existing_enrollment = $eventEnrollmentStorage
->loadByProperties($conditions);
if (!empty($existing_enrollment)) {
/** @var \Drupal\social_event\Entity\EventEnrollment $enrollment */
$enrollment = end($existing_enrollment);
// Of course, only delete the previous invite if it was declined
// or if it was invalid or expired.
$status_checks = [
EventEnrollmentInterface::REQUEST_OR_INVITE_DECLINED,
EventEnrollmentInterface::INVITE_INVALID_OR_EXPIRED,
];
if (in_array($enrollment->field_request_or_invite_status->value, $status_checks)) {
$enrollment
->delete();
unset($existing_enrollment[$enrollment
->id()]);
}
}
// Clear the cache.
$tags = [];
$tags[] = 'enrollment:' . $nid . '-' . $user;
$tags[] = 'event_content_list:entity:' . $user;
Cache::invalidateTags($tags);
}
// Create a new enrollment for the event.
$enrollment = EventEnrollment::create($fields);
// In order for the notifications to be sent correctly we're updating the
// owner here. The account is still linked to the actual enrollee.
// The owner is always used as the actor.
// @see activity_creator_message_insert().
$enrollment
->setOwnerId(\Drupal::currentUser()
->id());
// Add the node id to the results so we have the nid available in the
// finished callback so we can redirect to the correct node.
$results[$nid] = $enrollment
->save();
}
$context['results'] = $results;
}