You are here

public static function SocialEventInviteBulkHelper::bulkInviteUsers in Open Social 10.1.x

Same name and namespace in other branches
  1. 8.9 modules/social_features/social_event/modules/social_event_invite/src/SocialEventInviteBulkHelper.php \Drupal\social_event_invite\SocialEventInviteBulkHelper::bulkInviteUsers()
  2. 10.3.x modules/social_features/social_event/modules/social_event_invite/src/SocialEventInviteBulkHelper.php \Drupal\social_event_invite\SocialEventInviteBulkHelper::bulkInviteUsers()
  3. 10.0.x modules/social_features/social_event/modules/social_event_invite/src/SocialEventInviteBulkHelper.php \Drupal\social_event_invite\SocialEventInviteBulkHelper::bulkInviteUsers()
  4. 10.2.x modules/social_features/social_event/modules/social_event_invite/src/SocialEventInviteBulkHelper.php \Drupal\social_event_invite\SocialEventInviteBulkHelper::bulkInviteUsers()

Send the invites to existing users in a batch.

Parameters

array $users: Array containing users.

string $nid: The node id.

array $context: The context.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

\Drupal\Core\Entity\EntityStorageException

File

modules/social_features/social_event/modules/social_event_invite/src/SocialEventInviteBulkHelper.php, line 33

Class

SocialEventInviteBulkHelper
Class SocialEventBulkInvite.

Namespace

Drupal\social_event_invite

Code

public static function bulkInviteUsers(array $users, $nid, array &$context) {
  $results = [];
  foreach ($users as $uid => $target_id) {

    // Default values.
    $fields = [
      'field_event' => $nid,
      'field_enrollment_status' => '0',
      'field_request_or_invite_status' => EventEnrollmentInterface::INVITE_PENDING_REPLY,
      'user_id' => $uid,
      'field_account' => $uid,
    ];

    // 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' => $uid,
      '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 . '-' . $uid;
    $tags[] = 'event_content_list:entity:' . $uid;
    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;
}