You are here

public function InviteAccept::accept in Invite 8

Accepts an invitation.

1 string reference to 'InviteAccept::accept'
invite.routing.yml in ./invite.routing.yml
invite.routing.yml

File

src/Controller/InviteAccept.php, line 54

Class

InviteAccept
Class InviteAccept.

Namespace

Drupal\invite\Controller

Code

public function accept($invite) {
  $account = $this
    ->currentUser();
  $redirect = '<front>';
  $message = 'Hmm.';
  $type = 'status';

  // Current user is the inviter.
  if ($account
    ->id() == $invite
    ->getOwnerId()) {
    $message = $this
      ->t("You can't use your own invite.");
    $type = 'error';
  }
  elseif ($invite
    ->getStatus() == InviteConstants::INVITE_USED) {
    $message = $this
      ->t('Sorry, this invitation has already been used.');
    $type = 'error';
  }
  elseif ($invite
    ->getStatus() == InviteConstants::INVITE_WITHDRAWN) {
    $message = $this
      ->t('Sorry, this invitation has already been withdrawn.');
    $type = 'error';
  }
  elseif ($invite->expires->value < time()) {
    $message = $this
      ->t('Sorry, this invitation is expired.');
    $type = 'error';
    $invite
      ->setStatus(InviteConstants::INVITE_EXPIRED);
    $invite
      ->save();
  }
  else {
    $_SESSION['invite_code'] = $invite
      ->getRegCode();
    $redirect = 'user.register';
    $message = $this
      ->t('Please create an account to accept the invitation.');
  }

  // Let other modules act on the invite accepting before the user is created.
  $invite_accept = new InviteAcceptEvent([
    'redirect' => &$redirect,
    'message' => &$message,
    'type' => &$type,
    'invite' => &$invite,
  ]);
  $this->dispatcher
    ->dispatch('invite_accept', $invite_accept);
  $this->messenger
    ->addStatus($message, $type);
  return $this
    ->redirect($redirect);
}