You are here

function domain_get_node_domains in Domain Access 7.2

Same name and namespace in other branches
  1. 6.2 domain.module \domain_get_node_domains()
  2. 7.3 domain.module \domain_get_node_domains()

Get the domains for multiple matches, mimicking node_load().

Parameters

$nodes: An array of nodes, keyed by node id, or a single node id.

$reset: A boolean flag indicating the need to reset the static variable for the node.

Return value

An array of data, keyed by node id, or a single array for a single node. The data array, consists of two parts. 'domain_id' is an array of active domain ids. 'domain_site' is a TRUE/FALSE boolean indicating affiliate status.

4 calls to domain_get_node_domains()
domain_content_update_nodes in domain_content/domain_content.module
Abstraction function that lets us update access rules.
domain_node_load in ./domain.module
Implements hook_node_load()
domain_source_lookup in domain_source/domain_source.module
Get the source domains for multiple matches, mimicking node_load().
_domain_store_grants in ./domain.module
Store node_access records in the {domain_access} table.

File

./domain.module, line 1585
Core module functions for the Domain Access suite.

Code

function domain_get_node_domains($nodes, $reset = FALSE) {
  $lookup =& drupal_static(__FUNCTION__);
  if (!isset($lookup) || $reset) {
    $lookup = array();
  }

  // Ensure we form our data properly.
  if (!is_array($nodes)) {
    $node_ids[$nodes] = $nodes;
    $array = FALSE;
  }
  else {
    $node_ids = $nodes;
    $array = TRUE;
  }

  // If not an array, just return the requested data.
  if (!$array && isset($lookup[$nodes])) {
    return $lookup[$nodes];
  }

  // Set the proper value for the node, but include a default.
  $domains = array(
    'domain_id' => array(),
    'domain_site' => FALSE,
  );
  $records = array();
  $result = db_query("SELECT nid, gid, realm FROM {domain_access} WHERE nid IN (:nid)", array(
    ':nid' => array_keys($node_ids),
  ));

  // While this should return records for every node, we cannot guarantee success.
  foreach ($result as $data) {
    if ($data->realm == 'domain_id') {

      // Transform the 0 to -1, since we use -1 in forms.
      $data->gid == 0 ? $gid = -1 : ($gid = $data->gid);
      $records[$data->nid]['domain_id'][$gid] = $gid;
    }
    elseif ($data->realm == 'domain_site') {
      $records[$data->nid]['domain_site'] = TRUE;
    }
  }

  // Run through the nodes and ensure they have proper data.
  foreach ($node_ids as $nid => $value) {
    $lookup[$nid] = $domains;
    foreach (array(
      'domain_id',
      'domain_site',
    ) as $key) {
      if (isset($records[$nid][$key])) {
        $lookup[$nid][$key] = $records[$nid][$key];
      }
    }
  }

  // Return all data or just a single node?
  if ($array) {
    return $lookup;
  }
  if (isset($lookup[$nodes])) {
    return $lookup[$nodes];
  }
  return $domains;
}