View source
<?php
namespace Drupal\social_event\Form;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\Core\Url;
use Drupal\Core\Link;
use Drupal\node\Entity\Node;
use Drupal\social_event\Entity\EventEnrollment;
use Drupal\social_event\EventEnrollmentInterface;
use Drupal\user\UserInterface;
use Drupal\user\UserStorageInterface;
use Drupal\group\Entity\GroupContent;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
class EnrollActionForm extends FormBase implements ContainerInjectionInterface {
protected $routeMatch;
protected $entityStorage;
protected $userStorage;
protected $entityTypeManager;
protected $currentUser;
protected $configFactory;
protected $moduleHandler;
public function getFormId() {
return 'enroll_action_form';
}
public function __construct(RouteMatchInterface $route_match, EntityStorageInterface $entity_storage, UserStorageInterface $user_storage, EntityTypeManagerInterface $entityTypeManager, AccountProxyInterface $currentUser, ConfigFactoryInterface $configFactory, ModuleHandlerInterface $moduleHandler) {
$this->routeMatch = $route_match;
$this->entityStorage = $entity_storage;
$this->userStorage = $user_storage;
$this->entityTypeManager = $entityTypeManager;
$this->currentUser = $currentUser;
$this->configFactory = $configFactory;
$this->moduleHandler = $moduleHandler;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('current_route_match'), $container
->get('entity_type.manager')
->getStorage('event_enrollment'), $container
->get('entity_type.manager')
->getStorage('user'), $container
->get('entity_type.manager'), $container
->get('current_user'), $container
->get('config.factory'), $container
->get('module_handler'));
}
public function buildForm(array $form, FormStateInterface $form_state) {
$nid = $this->routeMatch
->getRawParameter('node');
$current_user = $this->currentUser;
$uid = $current_user
->id();
if (!empty($nid)) {
if (!is_object($nid) && !is_null($nid)) {
$node = $this->entityTypeManager
->getStorage('node')
->load($nid);
}
$groups = $this
->getGroups($node);
$conditions = [
'field_account' => $current_user
->id(),
'field_event' => $node
->id(),
];
$enrollments = $this->entityStorage
->loadByProperties($conditions);
if (!empty($groups) && $node->field_event_enroll_outside_group->value !== '1' && empty($enrollments) && social_event_manager_or_organizer() === FALSE) {
$group_type_ids = $this->configFactory
->getEditable('social_event.settings')
->get('enroll');
foreach ($groups as $group) {
$group_type_id = $group
->bundle();
if (in_array($group_type_id, $group_type_ids) && $group
->hasPermission('join group', $current_user)) {
break;
}
if ($group
->hasPermission('enroll to events in groups', $current_user) == FALSE) {
return [];
}
}
}
}
$form['event'] = [
'#type' => 'hidden',
'#value' => $nid,
];
$submit_text = $this
->t('Enroll');
$to_enroll_status = '1';
$enrollment_open = TRUE;
$request_to_join = FALSE;
$isNodeOwner = $node
->getOwnerId() === $uid;
$attributes = [
'class' => [
'btn',
'btn-accent brand-bg-accent',
'btn-lg btn-raised',
'dropdown-toggle',
'waves-effect',
],
];
if ((int) $node->field_enroll_method->value === EventEnrollmentInterface::ENROLL_METHOD_REQUEST && !$isNodeOwner) {
$submit_text = $this
->t('Request to enroll');
$to_enroll_status = '2';
if ($current_user
->isAnonymous()) {
$attributes = [
'class' => [
'use-ajax',
'js-form-submit',
'form-submit',
'btn',
'btn-accent',
'btn-lg',
],
'data-dialog-type' => 'modal',
'data-dialog-options' => json_encode([
'title' => t('Request to enroll'),
'width' => 'auto',
]),
];
$request_to_join = TRUE;
}
}
if ($this
->eventHasBeenFinished($node)) {
$submit_text = $this
->t('Event has passed');
$enrollment_open = FALSE;
}
if (!$current_user
->isAnonymous()) {
$conditions = [
'field_account' => $uid,
'field_event' => $nid,
];
$enrollments = $this->entityStorage
->loadByProperties($conditions);
if ($enrollment = array_pop($enrollments)) {
$current_enrollment_status = $enrollment->field_enrollment_status->value;
if ($current_enrollment_status === '1') {
$submit_text = $this
->t('Enrolled');
$to_enroll_status = '0';
}
elseif ($node->field_enroll_method->value && (int) $node->field_enroll_method->value === EventEnrollmentInterface::ENROLL_METHOD_REQUEST && !$isNodeOwner) {
$event_request_ajax = TRUE;
if ((int) $enrollment->field_request_or_invite_status->value === EventEnrollmentInterface::REQUEST_PENDING) {
$submit_text = $this
->t('Pending');
$event_request_ajax = FALSE;
}
}
}
if ($enrollment_open === TRUE) {
if (!$isNodeOwner && (empty($enrollment) && $node->field_enroll_method->value && (int) $node->field_enroll_method->value === EventEnrollmentInterface::ENROLL_METHOD_REQUEST) || isset($event_request_ajax) && $event_request_ajax === TRUE) {
$attributes = [
'class' => [
'use-ajax',
'js-form-submit',
'form-submit',
'btn',
'btn-accent',
'btn-lg',
],
'data-dialog-type' => 'modal',
'data-dialog-options' => json_encode([
'title' => t('Request to enroll'),
'width' => 'auto',
]),
];
$request_to_join = TRUE;
}
}
}
$form['to_enroll_status'] = [
'#type' => 'hidden',
'#value' => $to_enroll_status,
];
$form['enroll_for_this_event'] = [
'#type' => 'submit',
'#value' => $submit_text,
'#disabled' => !$enrollment_open,
'#attributes' => $attributes,
];
if ($request_to_join === TRUE) {
$form['enroll_for_this_event'] = [
'#type' => 'link',
'#title' => $submit_text,
'#url' => Url::fromRoute('social_event.request_enroll_dialog', [
'node' => $nid,
]),
'#attributes' => $attributes,
];
}
$form['#attributes']['name'] = 'enroll_action_form';
if (isset($enrollment->field_enrollment_status->value) && $enrollment->field_enrollment_status->value === '1' || isset($enrollment->field_request_or_invite_status->value) && (int) $enrollment->field_request_or_invite_status->value === EventEnrollmentInterface::REQUEST_PENDING) {
$form['enroll_for_this_event']['#attributes'] = [
'class' => [
'btn',
'btn-accent brand-bg-accent',
'btn-lg btn-raised',
'dropdown-toggle',
'waves-effect',
],
'autocomplete' => 'off',
'data-toggle' => 'dropdown',
'aria-haspopup' => 'true',
'aria-expanded' => 'false',
'data-caret' => 'true',
];
$cancel_text = $this
->t('Cancel enrollment');
$form['feedback_user_has_enrolled'] = [
'#markup' => '<ul class="dropdown-menu dropdown-menu-right"><li><a href="#" class="enroll-form-submit"> ' . $cancel_text . ' </a></li></ul>',
];
$form['#attached']['library'][] = 'social_event/form_submit';
}
return $form;
}
protected function eventHasBeenFinished(Node $node) {
$check_end_date = $node->field_event_date->date;
if (isset($node->field_event_date_end->date)) {
$check_end_date = $node->field_event_date_end->date;
}
$current_time = new DrupalDateTime();
if ($current_time > $check_end_date) {
return TRUE;
}
return FALSE;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$current_user = $this->currentUser;
$uid = $current_user
->id();
$nid = $form_state
->getValue('event') ?? $this->routeMatch
->getRawParameter('node');
$node = $this->entityTypeManager
->getStorage('node')
->load($nid);
if ($current_user
->isAnonymous()) {
$node_url = Url::fromRoute('entity.node.canonical', [
'node' => $nid,
])
->toString();
$destination = $node_url;
if ((int) $node
->get('field_enroll_method')->value === EventEnrollmentInterface::ENROLL_METHOD_REQUEST) {
$destination = $node_url . '?requested-enrollment=TRUE';
}
$form_state
->setRedirect('user.login', [], [
'query' => [
'destination' => $destination,
],
]);
if ($this->configFactory
->get('user.settings')
->get('register') !== UserInterface::REGISTER_ADMINISTRATORS_ONLY) {
$log_in_url = Url::fromUserInput('/user/login');
$log_in_link = Link::fromTextAndUrl($this
->t('log in'), $log_in_url)
->toString();
$create_account_url = Url::fromUserInput('/user/register');
$create_account_link = Link::fromTextAndUrl($this
->t('create a new account'), $create_account_url)
->toString();
$message = $this
->t('Please @log_in or @create_account_link so that you can enroll to the event.', [
'@log_in' => $log_in_link,
'@create_account_link' => $create_account_link,
]);
}
else {
$log_in_url = Url::fromUserInput('/user/login');
$log_in_link = Link::fromTextAndUrl($this
->t('log in'), $log_in_url)
->toString();
$message = $this
->t('Please @log_in so that you can enroll to the event.', [
'@log_in' => $log_in_link,
]);
}
$this
->messenger()
->addStatus($message);
return;
}
$to_enroll_status = $form_state
->getValue('to_enroll_status');
$conditions = [
'field_account' => $uid,
'field_event' => $nid,
];
$enrollments = $this->entityStorage
->loadByProperties($conditions);
$cache_tag = 'enrollment:' . $nid . '-' . $uid;
Cache::invalidateTags([
$cache_tag,
]);
if ($enrollment = array_pop($enrollments)) {
$current_enrollment_status = $enrollment->field_enrollment_status->value;
if ($to_enroll_status === '0' && $current_enrollment_status === '1') {
if ($enrollment->field_request_or_invite_status && (int) $enrollment->field_request_or_invite_status->value === EventEnrollmentInterface::INVITE_ACCEPTED_AND_JOINED) {
$enrollment->field_request_or_invite_status->value = EventEnrollmentInterface::REQUEST_OR_INVITE_DECLINED;
$current_enrollment_status = $enrollment->field_enrollment_status->value;
if ($current_enrollment_status === '1') {
$enrollment->field_enrollment_status->value = '0';
}
$enrollment
->save();
}
else {
$enrollment
->delete();
}
}
elseif ($to_enroll_status === '1' && $current_enrollment_status === '0') {
$enrollment->field_enrollment_status->value = '1';
$enrollment
->save();
}
elseif ($to_enroll_status === '2' && $current_enrollment_status === '0') {
if ((int) $enrollment->field_request_or_invite_status->value === EventEnrollmentInterface::REQUEST_PENDING) {
$enrollment
->delete();
}
}
}
else {
$fields = [
'user_id' => $uid,
'field_event' => $nid,
'field_enrollment_status' => '1',
'field_account' => $uid,
];
if ($to_enroll_status === '2') {
$fields['field_enrollment_status'] = '0';
$fields['field_request_or_invite_status'] = EventEnrollmentInterface::REQUEST_PENDING;
}
$enrollment = EventEnrollment::create($fields);
$enrollment
->save();
}
}
public function getGroups($node) {
$groupcontents = GroupContent::loadByEntity($node);
$groups = [];
if (!empty($groupcontents)) {
foreach ($groupcontents as $groupcontent) {
$group = $groupcontent
->getGroup();
$groups[] = $group;
}
}
return $groups;
}
}