You are here

class EventAnEnrollService in Open Social 8.7

Same name and namespace in other branches
  1. 8.9 modules/social_features/social_event/modules/social_event_an_enroll/src/EventAnEnrollService.php \Drupal\social_event_an_enroll\EventAnEnrollService
  2. 8.5 modules/social_features/social_event/modules/social_event_an_enroll/src/EventAnEnrollService.php \Drupal\social_event_an_enroll\EventAnEnrollService
  3. 8.6 modules/social_features/social_event/modules/social_event_an_enroll/src/EventAnEnrollService.php \Drupal\social_event_an_enroll\EventAnEnrollService
  4. 8.8 modules/social_features/social_event/modules/social_event_an_enroll/src/EventAnEnrollService.php \Drupal\social_event_an_enroll\EventAnEnrollService
  5. 10.3.x modules/social_features/social_event/modules/social_event_an_enroll/src/EventAnEnrollService.php \Drupal\social_event_an_enroll\EventAnEnrollService
  6. 10.0.x modules/social_features/social_event/modules/social_event_an_enroll/src/EventAnEnrollService.php \Drupal\social_event_an_enroll\EventAnEnrollService
  7. 10.1.x modules/social_features/social_event/modules/social_event_an_enroll/src/EventAnEnrollService.php \Drupal\social_event_an_enroll\EventAnEnrollService
  8. 10.2.x modules/social_features/social_event/modules/social_event_an_enroll/src/EventAnEnrollService.php \Drupal\social_event_an_enroll\EventAnEnrollService

Class EventAnEnrollService.

Hierarchy

Expanded class hierarchy of EventAnEnrollService

1 string reference to 'EventAnEnrollService'
social_event_an_enroll.services.yml in modules/social_features/social_event/modules/social_event_an_enroll/social_event_an_enroll.services.yml
modules/social_features/social_event/modules/social_event_an_enroll/social_event_an_enroll.services.yml
1 service uses EventAnEnrollService
social_event_an_enroll.service in modules/social_features/social_event/modules/social_event_an_enroll/social_event_an_enroll.services.yml
Drupal\social_event_an_enroll\EventAnEnrollService

File

modules/social_features/social_event/modules/social_event_an_enroll/src/EventAnEnrollService.php, line 12

Namespace

Drupal\social_event_an_enroll
View source
class EventAnEnrollService {

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

  /**
   * The current route.
   *
   * @var \Drupal\Core\Routing\CurrentRouteMatch
   */
  protected $currentRouteMatch;

  /**
   * Database connection.
   *
   * @var \Drupal\Core\Database\Connection
   */
  protected $database;

  /**
   * EventAnEnrollService constructor.
   *
   * @param \Drupal\Core\Session\AccountProxyInterface $account_proxy
   *   Account proxy.
   * @param \Drupal\Core\Routing\CurrentRouteMatch $current_route_match
   *   Current route match.
   * @param \Drupal\Core\Database\Connection $connection
   *   Database connection.
   */
  public function __construct(AccountProxyInterface $account_proxy, CurrentRouteMatch $current_route_match, Connection $connection) {
    $this->currentUser = $account_proxy;
    $this->currentRouteMatch = $current_route_match;
    $this->database = $connection;
  }

  /**
   * Returns number of anonymous enrollments.
   *
   * @param int $nid
   *   The node ID.
   *
   * @return int
   *   The number of anonymous event enrollments.
   */
  public function enrollmentCount($nid) {
    $query = $this->database
      ->select('event_enrollment__field_account', 'eefa');
    $query
      ->join('event_enrollment__field_event', 'eefe', 'eefa.entity_id = eefe.entity_id');
    $query
      ->condition('eefa.field_account_target_id', 0);
    $query
      ->condition('eefe.field_event_target_id', $nid);
    return (int) $query
      ->countQuery()
      ->execute()
      ->fetchField();
  }

  /**
   * Returns number of anonymous enrollments.
   *
   * @param string $token
   *   Token to validate.
   * @param int $nid
   *   The node ID.
   *
   * @return bool
   *   TRUE if token exists, FALSE otherwise.
   */
  public function tokenExists($token, $nid) {
    $query = $this->database
      ->select('event_enrollment__field_token', 'eeft');
    $query
      ->join('event_enrollment__field_event', 'eefe', 'eeft.entity_id = eefe.entity_id');
    $query
      ->condition('eeft.field_token_value', $token);
    $query
      ->condition('eefe.field_event_target_id', $nid);
    $results = $query
      ->countQuery()
      ->execute()
      ->fetchField();
    return !empty($results);
  }

  /**
   * Checks if a visitor is enrolled.
   *
   * @return bool
   *   Returns TRUE if the visitor is enrolled to this event, otherwise FALSE.
   */
  public function isEnrolled() {

    // Make sure the current user is anonymous.
    if (!$this->currentUser
      ->isAnonymous()) {
      return FALSE;
    }

    // Get the token and Node ID from the route.
    $token = $this->currentRouteMatch
      ->getParameter('token');

    /** @var \Drupal\node\NodeInterface $node */
    $node = $this->currentRouteMatch
      ->getParameter('node');

    // If some data is missing we can already return FALSE.
    if (!empty($token) || !empty($node)) {
      return FALSE;
    }

    // Check if the token is valid.
    return $this
      ->tokenExists($token, $node
      ->id());
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EventAnEnrollService::$currentRouteMatch protected property The current route.
EventAnEnrollService::$currentUser protected property The current user.
EventAnEnrollService::$database protected property Database connection.
EventAnEnrollService::enrollmentCount public function Returns number of anonymous enrollments.
EventAnEnrollService::isEnrolled public function Checks if a visitor is enrolled.
EventAnEnrollService::tokenExists public function Returns number of anonymous enrollments.
EventAnEnrollService::__construct public function EventAnEnrollService constructor.