social_event_invite_flow-1.0.0-beta3/src/Controller/ConvertEnrollRequestController.php

src/Controller/ConvertEnrollRequestController.php
<?php

namespace Drupal\social_event_invite_flow\Controller;

use Drupal\Core\Access\AccessResult;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\node\NodeInterface;
use Drupal\social_event\EventEnrollmentInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\RequestStack;
use Drupal\social_event_invite_flow\Service\EventInviteFlowService;
use Drupal\social_event_invite_flow\EventInviteSettingsInterface;

/**
 * Converts a pending enrollment request.
 *
 * @package Drupal\social_event_invite_flow\Controller
 */
class ConvertEnrollRequestController extends ControllerBase {

  /**
   * The request stack.
   *
   * @var \Symfony\Component\HttpFoundation\RequestStack
   */
  protected $requestStack;

  /**
   * The current user.
   *
   * @var \Drupal\Core\Session\AccountProxyInterface
   */
  protected $currentUser;

  /**
   * The Event Invite Flow Service
   *
   * @var \Drupal\social_event_invite_flow\Service\EventInviteFlowService
   */
  protected $eventInviteFlowService;  

  /**
   * UpdateEnrollRequestController constructor.
   *
   * @param \Symfony\Component\HttpFoundation\RequestStack $requestStack
   *   The request stack.
   * @param \Drupal\Core\Session\AccountProxyInterface $currentUser
   *   The current user.
   */
  public function __construct(
    RequestStack $requestStack, 
    AccountProxyInterface $currentUser, 
    EventInviteFlowService $event_invite_flow_service
  ) {
    $this->requestStack = $requestStack;
    $this->currentUser = $currentUser;
    $this->eventInviteFlowService = $event_invite_flow_service;    
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('request_stack'),
      $container->get('current_user'),
      $container->get('social_event_invite_flow.invite_flow_service')
    );
  }

  /**
   * Converts the enrollment request to invite.
   *
   * @param \Drupal\node\NodeInterface $node
   *   The current event node.
   * @param \Drupal\social_event\EventEnrollmentInterface $event_enrollment
   *   The entity event_enrollment.
   * @param int $approve
   *   Approve the enrollment request, TRUE(1) or FALSE(0).
   *
   * @return \Symfony\Component\HttpFoundation\RedirectResponse
   *   Return to the original destination from the current request.
   *
   * @throws \Drupal\Core\Entity\EntityStorageException
   */
  public function convertEnrollmentRequest(NodeInterface $node, EventEnrollmentInterface $event_enrollment) {
    // First, lets delete all messages to keep the messages clean.
    $this->messenger()->deleteAll();

    // First we have to remove the field_request_or_invite_status
    $event_enrollment->field_request_or_invite_status->value = '';
    // Now we need to properly enroll the user.
    $event_enrollment->field_enrollment_status->value = 1;
    // Inform the manger about it
    $this->messenger()->addStatus(t('The event enrollment request has been converted.'));


    // 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().
    $event_enrollment->setOwnerId($this->currentUser->id());

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

    // Get the service
    $event_invite_flow_service = $this->eventInviteFlowService;    

    // Now make sure we set the event settings to shareable link
    $event_invite_setting = $event_invite_flow_service->getEventInviteSettings($node->id());

    if ($event_invite_setting) {
      // Set the invite setting to enable shareable link
      $event_invite_setting->setEnableShareableLink(TRUE);
      $event_invite_setting->save();

      // Get Account from user id.
      $user = $event_invite_flow_service->getAccountFromUserId($event_enrollment->getAccount());

       \Drupal::logger('debug')->debug('event enrollment ID<pre><code>' . print_r($event_enrollment->id(), TRUE) . '</code></pre>');

      // Send invite email to user.
      $event_invite_flow_service->sendInviteEmails(
        EventInviteSettingsInterface::InviteModeExistingAccountsDefaultEnrollment,
        $user,
        $node->id(),
        $event_enrollment
      );

    }

    // Get the redirect destination we're given in the request for the response.
    $destination = $this->requestStack->getCurrentRequest()->query->get('destination');

    return new RedirectResponse($destination);

  }

  /**
   * Checks access for a specific request.
   *
   * @param \Drupal\Core\Session\AccountInterface $account
   *   Run access checks for this account.
   *
   * @return \Drupal\Core\Access\AccessResultInterface
   *   The access result.
   */
  public function access(AccountInterface $account) {
    $hasPermissionIsOwnerOrOrganizer = social_event_manager_or_organizer();
    return AccessResult::allowedIf($hasPermissionIsOwnerOrOrganizer === TRUE);
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc