UserEnrollInviteController.php in Open Social 8.9
File
modules/social_features/social_event/modules/social_event_invite/src/Controller/UserEnrollInviteController.php
View source
<?php
namespace Drupal\social_event_invite\Controller;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Link;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Url;
use Drupal\social_event\EventEnrollmentInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
class UserEnrollInviteController extends CancelEnrollInviteController {
public function updateEnrollmentInvite(EventEnrollmentInterface $event_enrollment, $accept_decline) {
if (!empty($event_enrollment)) {
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)) {
$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)) {
$this
->messenger()
->deleteAll();
$this
->messenger()
->addStatus($statusMessage);
}
}
$event_enrollment
->save();
$tags = [];
$tags[] = 'enrollment:' . $event_enrollment->field_event->value . '-' . $this->currentUser
->id();
$tags[] = 'event_content_list:entity:' . $this->currentUser
->id();
Cache::invalidateTags($tags);
}
$destination = Url::fromRoute('view.user_event_invites.page_user_event_invites', [
'user' => $this->currentUser
->id(),
])
->toString();
return new RedirectResponse($destination);
}
public function getMessage(EventEnrollmentInterface $event_enrollment, $accept_decline) {
$statusMessage = NULL;
$target_event_id = $event_enrollment
->get('field_event')
->getValue();
$event = $this
->entityTypeManager()
->getStorage('node')
->load($target_event_id[0]['target_id']);
if (!empty($event)) {
$link = Link::createFromRoute($this
->t('@node', [
'@node' => $event
->get('title')->value,
]), 'entity.node.canonical', [
'node' => $event
->id(),
])
->toString();
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;
}
public function access(AccountInterface $account) {
$user_parameter = $this->requestStack
->getCurrentRequest()->attributes
->get('user');
if ($account
->id() === $user_parameter) {
return AccessResult::allowed();
}
return AccessResult::neutral();
}
}