View source
<?php
namespace Drupal\bbb_node\Controller;
use Drupal\bbb\Service\Api;
use Drupal\bbb_node\Service\NodeMeeting;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Url;
use Drupal\node\NodeInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class BBBMeetingTypeController extends ControllerBase {
use StringTranslationTrait;
protected $nodeStorage;
protected $nodeMeeting;
protected $api;
protected $config;
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_type.manager'), $container
->get('messenger'), $container
->get('bbb.api'), $container
->get('bbb_node.meeting'), $container
->get('config.factory'));
}
public function __construct(EntityTypeManagerInterface $entity_type_manager, MessengerInterface $messenger, Api $api, NodeMeeting $node_meeting, ConfigFactoryInterface $config_factory) {
$this->entityTypeManager = $entity_type_manager;
$this->messenger = $messenger;
$this->api = $api;
$this->nodeMeeting = $node_meeting;
$this->config = $config_factory
->get('bbb_node.settings');
$this->nodeStorage = $this->entityTypeManager
->getStorage('node');
}
public function attend(NodeInterface $node) {
if (is_numeric($node)) {
$node = $this->nodeStorage
->load($node);
}
$node_type = $node
->getType();
$meeting_settings = $this->entityTypeManager
->getStorage('bbb_node_type')
->load($node_type);
$mode = 'attend';
$meeting = $this->nodeMeeting
->get($node);
$params = [
'meetingID' => $meeting->meetingID,
'password' => $meeting->attendeePW,
];
$status = $this->api
->getMeetingInfo($params);
if ($status && property_exists($status, 'hasBeenForciblyEnded') && $status
->hasBeenForciblyEnded() == 'true') {
$this->messenger
->addWarning('The meeting has been terminated and is not available for attending.');
return new RedirectResponse(Url::fromRoute('entity.node.canonical', [
'node' => $node
->id(),
], [
'absolute' => TRUE,
]));
}
if ($meeting->running) {
if ($this
->getDisplayMode() === 'blank') {
$this
->redirect($node, $mode);
}
}
else {
if ($meeting_settings
->get('moderatorRequired')) {
$this->messenger
->addStatus($this
->t('You signed up for this meeting. Please stay on this page, you will be redirected immediately after the meeting has started.'));
$render = $this->entityTypeManager
->getViewBuilder('node')
->view($node);
$render['#attached']['libraries'][] = 'bbb/check_status';
$render['#attached']['drupalSettings']['bbb']['check_status']['check_status_url'] = Url::fromRoute('bbb_node.meeting.end_status', [
'node' => $node
->id(),
]);
return $render;
}
else {
if (empty($meeting->initialized)) {
if ($data = $this->nodeMeeting
->create($node, $params)) {
$this->nodeMeeting
->update($node, array_merge((array) $meeting, (array) $data));
}
}
if ($this
->getDisplayMode() == 'blank') {
$this
->redirect($node, $mode);
}
}
}
return [
'#theme' => 'bbb_meeting',
'#meeting' => $meeting,
'#mode' => $mode,
'#height' => $this
->getDisplayHeight(),
'#width' => $this
->getDisplayWidth(),
];
}
public function moderate(NodeInterface $node) {
if (is_numeric($node)) {
$node = $this->nodeStorage
->load($node);
}
$mode = 'moderate';
$meeting = $this->nodeMeeting
->get($node);
$meeting_info = $meeting['info'];
$params = [
'meetingID' => $meeting->meetingID,
'password' => $meeting->moderatorPW,
];
$status = $this->api
->getMeetingInfo($params);
if ($status && property_exists($status, 'hasBeenForciblyEnded') && $status
->hasBeenForciblyEnded() == 'true') {
$this->messenger
->addStatus('The meeting has been terminated and is not available for reopening.');
return new RedirectResponse(Url::fromRoute('entity.node.canonical', [
'node' => $node
->id(),
], [
'absolute' => TRUE,
]));
}
if (empty($meeting->initialized)) {
if ($data = $this->nodeMeeting
->create($node, (array) $params)) {
$this->nodeMeeting
->update($node, array_merge((array) $meeting, (array) $data));
}
}
if ($this
->getDisplayMode() === 'blank') {
$this
->attendRedirect($node, $mode);
}
return [
'#theme' => 'bbb_meeting',
'#meeting' => $meeting,
'#mode' => $mode,
'#height' => $this
->getDisplayHeight(),
'#width' => $this
->getDisplayWidth(),
];
}
public function attendRedirect(NodeInterface $node, $mode = 'attend') {
if (is_numeric($node)) {
$node = $this->nodeStorage
->load($node);
}
$meeting = $this->nodeMeeting
->get($node, NULL, FALSE);
if (empty($meeting->url[$mode])) {
throw new NotFoundHttpException();
}
$url = parse_url($meeting->url[$mode]);
$fullurl = $url['scheme'] . '://' . $url['host'] . (isset($url['port']) ? ':' . $url['port'] : '') . $url['path'] . '?' . $url['query'];
return new RedirectResponse($fullurl, 301);
}
public function status($node) {
if (is_numeric($node)) {
$node = $this->nodeStorage
->load($node);
}
$meeting = $this->nodeMeeting
->get($node);
return new JsonResponse([
'running' => $meeting->running,
]);
}
public function getTitle(NodeInterface $node) {
return $node
->getTitle();
}
public function getDisplayMode() {
return $this->config
->get('display_mode');
}
public function getDisplayHeight() {
return $this->config
->get('display_height');
}
public function getDisplayWidth() {
return $this->config
->get('display_width');
}
}