You are here

function scn_entity_insert in Simple Comment Notify 8

Implements hook_entity_insert().

File

./scn.module, line 96
Main file for the scn module.

Code

function scn_entity_insert(EntityInterface $entity) {
  if ($entity
    ->getEntityTypeId() == 'comment') {
    $config = \Drupal::config('scn.settings');
    $subject = $entity
      ->getSubject();
    $url = $entity
      ->permalink()
      ->setOption('absolute', TRUE)
      ->toString();
    $params = [
      'url' => $url,
      'comment' => $entity,
    ];

    // Send to admin.
    if ($config
      ->get('scn_admin')) {
      $account = User::load(1);
      $to = $account
        ->getEmail();
      _scn_send_mail($to, $subject, $params);
    }

    // Send to users with roles.
    foreach (array_values($config
      ->get('scn_roles')) as $user_role) {
      if ($user_role !== 0) {
        $ids = \Drupal::entityQuery('user')
          ->condition('status', 1)
          ->condition('roles', $user_role)
          ->execute();
        $users = User::loadMultiple($ids);
        if (!empty(array_filter($users))) {
          foreach ($users as $user) {
            $to = $user
              ->getEmail();
            _scn_send_mail($to, $subject, $params);
          }
        }
      }
    }

    // Send to non-registered users.
    if (!empty($config
      ->get('scn_maillist'))) {
      $mails = explode(',', $config
        ->get('scn_maillist'));
      foreach ($mails as $mail) {
        $to = $mail;
        _scn_send_mail($to, $subject, $params);
      }
    }

    // Sent message to telegram.
    if ($config
      ->get('scn_telegram')) {
      $token = $config
        ->get('scn_telegram_bottoken');
      $chatids = explode(',', $config
        ->get('scn_telegram_chatids'));
      foreach ($chatids as $chatid) {
        if ($config
          ->get('scn_telegram_proxy')) {
          $proxy_server = $config
            ->get('scn_telegram_proxy_server');
          $proxy_login = $config
            ->get('scn_telegram_proxy_login');
          $proxy_password = $config
            ->get('scn_telegram_proxy_password');
          _scn_send_telegram($url, $token, $chatid, TRUE, $proxy_server, $proxy_login, $proxy_password);
        }
        else {
          _scn_send_telegram($url, $token, $chatid, $subject);
        }
      }
    }
  }
}