You are here

public static function drupalchatController::drupalchat_send in DrupalChat 8

* * Send messages via ajax

1 string reference to 'drupalchatController::drupalchat_send'
drupalchat.routing.yml in ./drupalchat.routing.yml
drupalchat.routing.yml

File

src/Controller/drupalchatController.php, line 131
Contains Drupal\drupalchat\Controller\drupalchatController

Class

drupalchatController

Namespace

Drupal\drupalchat\Controller

Code

public static function drupalchat_send() {

  // Load the current user.
  // $user_id = \Drupal\user\Entity\User::load(\Drupal::currentUser()->id());
  $account = \Drupal::currentUser();
  $token_generator = \Drupal::csrfToken();
  $formToken = Html::escape($_POST['form_token']);
  $formID = Html::escape($_POST['form_id']);
  $form_token = !empty($formToken) ? $formToken : '';
  $form_id = !empty($formID) ? $formID : '';
  if (!$token_generator
    ->validate($form_token, $form_id)) {
    return;
  }
  \Drupal::database()
    ->merge('drupalchat_msg')
    ->key(array(
    'message_id' => Html::escape($_POST['drupalchat_message_id']),
    'uid1' => $account
      ->id() ? $account
      ->id() : '0-' . drupalchatController::_drupalchat_get_sid(),
    'uid2' => Html::escape($_POST['drupalchat_uid2']),
  ))
    ->fields(array(
    'message' => $_POST['drupalchat_message'],
    'timestamp' => time(),
  ))
    ->execute();
  foreach (\Drupal::moduleHandler()
    ->getImplementations('drupalchat_send') as $module) {
    $function = $module . '_drupalchat_send';
    $function($message);
  }
  return new JsonResponse(array());
}