You are here

function push_notifications_get_tokens in Push Notifications 8

Same name and namespace in other branches
  1. 7 push_notifications.module \push_notifications_get_tokens()

Determine all recipients from a specific network.

Parameters

array $filters Filter option for query. Allows filtering by::

  • Language (key = language)
  • Networks (key = networks, value = array)
  • Account type (key = account_type)

$raw: Boolean, set true to retrieve the raw query results.

Return value

mixed Array of results, null if no entries.

1 call to push_notifications_get_tokens()
PushNotificationsSendMessageForm::validateForm in src/Form/PushNotificationsSendMessageForm.php
Form validation handler.

File

./push_notifications.module, line 149
Contains push_notifications.module functionality.

Code

function push_notifications_get_tokens($filters = array(), $raw = FALSE) {

  // Validate format of filters argument.
  if (!is_array($filters)) {
    return FALSE;
  }

  // Select all tokens for this type id.
  $query = db_select('push_notifications_tokens', 'pnt');
  $query
    ->fields('pnt');

  // Filter by network, if required.
  if (array_key_exists('networks', $filters)) {
    $query
      ->condition('pnt.network', $filters['networks'], 'IN');
  }

  // Filter by language, if required.
  if (array_key_exists('language', $filters) && is_string($filters['language'])) {
    $query
      ->condition('pnt.language', $filters['language']);
  }

  // Filter by anonymous vs. authenticated users.
  if (array_key_exists('account_type', $filters) && in_array($filters['account_type'], array(
    'anonymous',
    'authenticated',
  ))) {
    switch ($filters['account_type']) {
      case 'anonymous':
        $query
          ->condition('pnt.uid', 0);
        break;
      case 'authenticated':
        $query
          ->condition('pnt.uid', 0, '!=');
        break;
    }
  }
  $result = $query
    ->execute();

  // Return raw result, if needed.
  if ($raw) {
    return $result;
  }
  else {
    $tokens = array();
    foreach ($result as $record) {
      $tokens[] = $record;
    }
    return $tokens;
  }
}