View source
<?php
namespace Drupal\private_message\Controller;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\private_message\Ajax\PrivateMessageInboxUpdateCommand;
use Drupal\private_message\Ajax\PrivateMessageInsertNewMessagesCommand;
use Drupal\private_message\Ajax\PrivateMessageInboxInsertThreadsCommand;
use Drupal\private_message\Ajax\PrivateMessageInsertPreviousMessagesCommand;
use Drupal\private_message\Ajax\PrivateMessageInsertThreadCommand;
use Drupal\private_message\Ajax\PrivateMessageMembersAutocompleteResponseCommand;
use Drupal\private_message\Ajax\PrivateMessageMemberUsernameValidatedCommand;
use Drupal\private_message\Ajax\PrivateMessageUpdateUnreadThreadCountCommand;
use Drupal\private_message\Entity\PrivateMessageThread;
use Drupal\private_message\Service\PrivateMessageServiceInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
class AjaxController extends ControllerBase implements AjaxControllerInterface {
const AUTOCOMPLETE_COUNT = 10;
protected $renderer;
protected $requestStack;
protected $entityManager;
protected $configFactory;
protected $currentUser;
protected $privateMessageService;
public function __construct(RendererInterface $renderer, RequestStack $requestStack, EntityManagerInterface $entityManager, ConfigFactoryInterface $configFactory, AccountProxyInterface $currentUser, PrivateMessageServiceInterface $privateMessageService) {
$this->renderer = $renderer;
$this->requestStack = $requestStack;
$this->entityManager = $entityManager;
$this->configFactory = $configFactory;
$this->currentUser = $currentUser;
$this->privateMessageService = $privateMessageService;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('renderer'), $container
->get('request_stack'), $container
->get('entity.manager'), $container
->get('config.factory'), $container
->get('current_user'), $container
->get('private_message.service'));
}
public function ajaxCallback($op) {
$response = new AjaxResponse();
if ($this->currentUser
->hasPermission('use private messaging system')) {
switch ($op) {
case 'get_new_messages':
$this
->getNewPrivateMessages($response);
break;
case 'get_old_messages':
$this
->getOldPrivateMessages($response);
break;
case 'get_old_inbox_threads':
$this
->getOldInboxThreads($response);
break;
case 'get_new_inbox_threads':
$this
->getNewInboxThreads($response);
break;
case 'validate_private_message_member_username':
$this
->validatePrivateMessageMemberUsername($response);
break;
case 'get_new_unread_thread_count':
$this
->getNewUnreadThreadCount($response);
break;
case 'load_thread':
$this
->loadThread($response);
break;
}
}
return $response;
}
public function privateMessageMembersAutocomplete() {
$response = new AjaxResponse();
$username = $this->requestStack
->getCurrentRequest()
->get('username');
$accounts = $this->privateMessageService
->getUsersFromString($username, self::AUTOCOMPLETE_COUNT);
$user_info = [];
foreach ($accounts as $account) {
if ($account
->access('view', $this->currentUser)) {
$user_info[] = [
'uid' => $account
->id(),
'username' => $account
->getDisplayName(),
];
}
}
$response
->addCommand(new PrivateMessageMembersAutocompleteResponseCommand($username, $user_info));
return $response;
}
protected function getNewPrivateMessages(AjaxResponse $response) {
$thread_id = $this->requestStack
->getCurrentRequest()
->get('threadid');
$message_id = $this->requestStack
->getCurrentRequest()
->get('messageid');
if (is_numeric($thread_id) && is_numeric($message_id)) {
$new_messages = $this->privateMessageService
->getNewMessages($thread_id, $message_id);
if (count($new_messages)) {
$messages = [];
$view_builder = $this->entityManager
->getViewBuilder('private_message');
foreach ($new_messages as $message) {
if ($message
->access('view', $this->currentUser)) {
$messages[] = $view_builder
->view($message);
}
}
$response
->addCommand(new PrivateMessageInsertNewMessagesCommand($this->renderer
->renderRoot($messages)));
}
}
}
protected function getOldPrivateMessages(AjaxResponse $response) {
$current_request = $this->requestStack
->getCurrentRequest();
$thread_id = $current_request
->get('threadid');
$message_id = $current_request
->get('messageid');
if (is_numeric($thread_id) && is_numeric($message_id)) {
$message_info = $this->privateMessageService
->getPreviousMessages($thread_id, $message_id);
if (count($message_info)) {
$messages = [];
$view_builder = $this->entityManager
->getViewBuilder('private_message');
foreach ($message_info as $message) {
if ($message
->access('view', $this->currentUser)) {
$messages[] = $view_builder
->view($message);
}
}
$response
->addCommand(new PrivateMessageInsertPreviousMessagesCommand($this->renderer
->renderRoot($messages)));
}
else {
$response
->addCommand(new PrivateMessageInsertPreviousMessagesCommand(''));
}
}
}
protected function getOldInboxThreads(AjaxResponse $response) {
$timestamp = $this->requestStack
->getCurrentRequest()
->get('timestamp');
$thread_count = $this->requestStack
->getCurrentRequest()
->get('count');
if (is_numeric($timestamp)) {
$thread_info = $this->privateMessageService
->getThreadsForUser($thread_count, $timestamp);
if (count($thread_info['threads'])) {
$oldest_timestamp = FALSE;
$view_builder = $this->entityManager
->getViewBuilder('private_message_thread');
$threads = [];
foreach ($thread_info['threads'] as $thread) {
if ($thread
->access('view', $this->currentUser)) {
if ($thread_info['next_exists']) {
$oldest_timestamp = $thread
->get('updated')->value;
}
$threads[] = $view_builder
->view($thread, 'inbox');
}
}
$response
->addCommand(new PrivateMessageInboxInsertThreadsCommand($this->renderer
->renderRoot($threads), $oldest_timestamp));
}
else {
$response
->addCommand(new PrivateMessageInboxInsertThreadsCommand('', FALSE));
}
}
}
protected function getNewInboxThreads(AjaxResponse $response) {
$info = $this->requestStack
->getCurrentRequest()
->get('ids');
if (is_array($info) && count($info)) {
$inbox_threads = $this->privateMessageService
->getUpdatedInboxThreads($info);
}
else {
$thread_count = $this->configFactory
->get('block.block.privatemessageinbox')
->get('settings.thread_count');
$inbox_threads = $this->privateMessageService
->getUpdatedInboxThreads([], $thread_count);
}
if (count($inbox_threads['thread_ids'])) {
$view_builder = $this->entityManager
->getViewBuilder('private_message_thread');
$rendered_threads = [];
foreach (array_keys($inbox_threads['new_threads']) as $thread_id) {
if ($inbox_threads['new_threads'][$thread_id]
->access('view', $this->currentUser)) {
$renderable = $view_builder
->view($inbox_threads['new_threads'][$thread_id], 'inbox');
$rendered_threads[$thread_id] = $this->renderer
->renderRoot($renderable);
}
}
$response
->addCommand(new PrivateMessageInboxUpdateCommand($inbox_threads['thread_ids'], $rendered_threads));
}
}
protected function validatePrivateMessageMemberUsername(AjaxResponse $response) {
$username = $this->requestStack
->getCurrentRequest()
->get('username');
$valid = $this->privateMessageService
->validatePrivateMessageMemberUsername($username);
$response
->addCommand(new PrivateMessageMemberUsernameValidatedCommand($username, $valid));
}
protected function getNewUnreadThreadCount(AjaxResponse $response) {
$unread_thread_count = $this->privateMessageService
->getUnreadThreadCount();
$response
->addCommand(new PrivateMessageUpdateUnreadThreadCountCommand($unread_thread_count));
}
protected function loadThread(AjaxResponse $response) {
$thread_id = $this->requestStack
->getCurrentRequest()
->get('id');
if ($thread_id) {
$thread = PrivateMessageThread::load($thread_id);
if ($thread && $thread
->access('view', $this->currentUser)) {
$this->privateMessageService
->updateLastCheckTime();
$view_builder = $this->entityManager
->getViewBuilder('private_message_thread');
$renderable = $view_builder
->view($thread);
$rendered_thread = $this->renderer
->renderRoot($renderable);
$index = array_search('private_message/private_message_thread', $renderable['#attached']['library']);
unset($renderable['#attached']['library'][$index]);
$response
->setAttachments($renderable['#attached']);
$response
->addCommand(new PrivateMessageInsertThreadCommand($rendered_thread));
$unread_thread_count = $this->privateMessageService
->getUnreadThreadCount();
$response
->addCommand(new PrivateMessageUpdateUnreadThreadCountCommand($unread_thread_count));
}
}
}
}