You are here

class UserEnrollInviteController in Open Social 10.0.x

Same name and namespace in other branches
  1. 8.9 modules/social_features/social_event/modules/social_event_invite/src/Controller/UserEnrollInviteController.php \Drupal\social_event_invite\Controller\UserEnrollInviteController
  2. 10.3.x modules/social_features/social_event/modules/social_event_invite/src/Controller/UserEnrollInviteController.php \Drupal\social_event_invite\Controller\UserEnrollInviteController
  3. 10.1.x modules/social_features/social_event/modules/social_event_invite/src/Controller/UserEnrollInviteController.php \Drupal\social_event_invite\Controller\UserEnrollInviteController
  4. 10.2.x modules/social_features/social_event/modules/social_event_invite/src/Controller/UserEnrollInviteController.php \Drupal\social_event_invite\Controller\UserEnrollInviteController

Accepts or declines an event enrollment invite.

@package Drupal\social_event_invite\Controller

Hierarchy

Expanded class hierarchy of UserEnrollInviteController

File

modules/social_features/social_event/modules/social_event_invite/src/Controller/UserEnrollInviteController.php, line 18

Namespace

Drupal\social_event_invite\Controller
View source
class UserEnrollInviteController extends CancelEnrollInviteController {

  /**
   * {@inheritdoc}
   */
  public function updateEnrollmentInvite(EventEnrollmentInterface $event_enrollment, $accept_decline) {

    // Just some sanity checks.
    if (!empty($event_enrollment)) {

      // When the user accepted the invite,
      // we set the field_request_or_invite_status to approved.
      if ($accept_decline === '1') {
        $event_enrollment->field_request_or_invite_status->value = EventEnrollmentInterface::INVITE_ACCEPTED_AND_JOINED;
        $event_enrollment->field_enrollment_status->value = '1';
        $statusMessage = $this
          ->getMessage($event_enrollment, $accept_decline);
        if (!empty($statusMessage)) {

          // Lets delete all messages to keep the messages clean.
          $this
            ->messenger()
            ->deleteAll();
          $this
            ->messenger()
            ->addStatus($statusMessage);
        }
      }
      elseif ($accept_decline === '0') {
        $event_enrollment->field_request_or_invite_status->value = EventEnrollmentInterface::REQUEST_OR_INVITE_DECLINED;
        $statusMessage = $this
          ->getMessage($event_enrollment, $accept_decline);
        if (!empty($statusMessage)) {

          // Lets delete all messages to keep the messages clean.
          $this
            ->messenger()
            ->deleteAll();
          $this
            ->messenger()
            ->addStatus($statusMessage);
        }
      }

      // And finally save (update) this updated $event_enrollment.
      // @todo maybe think of deleting approved/declined records from the db?
      $event_enrollment
        ->save();

      // Invalidate cache.
      $tags = [];
      $tags[] = 'enrollment:' . $event_enrollment->field_event->value . '-' . $this->currentUser
        ->id();
      $tags[] = 'event_content_list:entity:' . $this->currentUser
        ->id();
      Cache::invalidateTags($tags);
    }

    // Get the redirect destination we're given in the request for the response.
    $destination = Url::fromRoute('view.user_event_invites.page_user_event_invites', [
      'user' => $this->currentUser
        ->id(),
    ])
      ->toString();
    return new RedirectResponse($destination);
  }

  /**
   * Generates a nice message for the user.
   *
   * @param \Drupal\social_event\EventEnrollmentInterface $event_enrollment
   *   The event enrollment.
   * @param string $accept_decline
   *   The approve (1) or decline (0) number.
   *
   * @return \Drupal\Core\StringTranslation\TranslatableMarkup|null
   *   The message.
   */
  public function getMessage(EventEnrollmentInterface $event_enrollment, $accept_decline) {
    $statusMessage = NULL;

    // Get the target event id.
    $target_event_id = $event_enrollment
      ->get('field_event')
      ->getValue();

    // Get the event node.
    $event = $this
      ->entityTypeManager()
      ->getStorage('node')
      ->load($target_event_id[0]['target_id']);

    // Only if we have an event, we perform the rest of the logic.
    if (!empty($event)) {

      // Build the link to the event node.
      $link = Link::createFromRoute($this
        ->t('@node', [
        '@node' => $event
          ->get('title')->value,
      ]), 'entity.node.canonical', [
        'node' => $event
          ->id(),
      ])
        ->toString();

      // Nice message with link to the event the user has enrolled in.
      if (!empty($event
        ->get('title')->value) && $accept_decline === '1') {
        $statusMessage = $this
          ->t('You have accepted the invitation for the @event event.', [
          '@event' => $link,
        ]);
      }
      elseif (!empty($event
        ->get('title')->value) && $accept_decline === '0') {
        $statusMessage = $this
          ->t('You have declined the invitation for the @event event.', [
          '@event' => $link,
        ]);
      }
    }
    return $statusMessage;
  }

  /**
   * {@inheritdoc}
   */
  public function access(AccountInterface $account) {

    // Get the parameter from the request that has been done.
    $user_parameter = $this->requestStack
      ->getCurrentRequest()->attributes
      ->get('user');

    // Check if it's the same that is in the current session's account.
    if ($account
      ->id() === $user_parameter) {
      return AccessResult::allowed();
    }
    return AccessResult::neutral();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CancelEnrollInviteController::$currentUser protected property The current user. Overrides ControllerBase::$currentUser
CancelEnrollInviteController::$requestStack protected property The request stack.
CancelEnrollInviteController::cancelEnrollmentInvite public function Updates the enrollment request.
CancelEnrollInviteController::create public static function Instantiates a new instance of this class. Overrides ControllerBase::create
CancelEnrollInviteController::__construct public function UpdateEnrollRequestController constructor.
ControllerBase::$configFactory protected property The configuration factory.
ControllerBase::$entityFormBuilder protected property The entity form builder.
ControllerBase::$entityTypeManager protected property The entity type manager.
ControllerBase::$formBuilder protected property The form builder. 2
ControllerBase::$keyValue protected property The key-value storage. 1
ControllerBase::$languageManager protected property The language manager. 1
ControllerBase::$moduleHandler protected property The module handler. 2
ControllerBase::$stateService protected property The state service.
ControllerBase::cache protected function Returns the requested cache bin.
ControllerBase::config protected function Retrieves a configuration object.
ControllerBase::container private function Returns the service container.
ControllerBase::currentUser protected function Returns the current user. 1
ControllerBase::entityFormBuilder protected function Retrieves the entity form builder.
ControllerBase::entityTypeManager protected function Retrieves the entity type manager.
ControllerBase::formBuilder protected function Returns the form builder service. 2
ControllerBase::keyValue protected function Returns a key/value storage collection. 1
ControllerBase::languageManager protected function Returns the language manager service. 1
ControllerBase::moduleHandler protected function Returns the module handler. 2
ControllerBase::redirect protected function Returns a redirect response object for the specified route.
ControllerBase::state protected function Returns the state storage service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 27
MessengerTrait::messenger public function Gets the messenger. 27
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UserEnrollInviteController::access public function Checks access for a specific request. Overrides CancelEnrollInviteController::access
UserEnrollInviteController::getMessage public function Generates a nice message for the user.
UserEnrollInviteController::updateEnrollmentInvite public function