You are here

function oa_core_get_top_parents in Open Atrium Core 7.2

Get top-level spaces/groups (no parents of same bundle type)

Parameters

string $bundle: The node type (default: OA_SPACE_TYPE).

int|NULL $status: (optional) If specified, the node status (ex. NODE_PUBLISHED or NODE_NOT_PUBLISHED) to look for. If not specified, it return nodes of either status.

bool $include_archived: (optional) Whether to include archived nodes or not. By default, archived items aren't included.

Return value

array Array of children NIDs.

1 call to oa_core_get_top_parents()
oa_core_get_groups_by_parent in includes/oa_core.util.inc
Get child Spaces or Groups.

File

includes/oa_core.util.inc, line 561
Code for Utility functions for OpenAtrium spaces

Code

function oa_core_get_top_parents($bundle, $status = NULL, $include_archived = FALSE, $account = NULL) {
  if (!isset($account)) {
    global $user;
    $uid = $user->uid;
  }
  elseif (is_numeric($account)) {
    $uid = $account;
  }
  else {
    $uid = $account->uid;
  }
  $cid = oa_core_get_cache_id(OA_CACHE_TOP_PARENTS, $uid, $include_archived, array(
    $bundle,
    $status,
  ));
  if (oa_core_get_cache(OA_CACHE_TOP_PARENTS, $cid, $result)) {
    return $result;
  }

  // need to also add the spaces whose parents are not the correct bundle
  $query_parents = db_select('node', 'n');
  $query_parents
    ->join('og_membership', 'f', "n.nid = f.etid");
  $query_parents
    ->join('node', 'n2', "n2.nid = f.gid");
  $query_parents
    ->fields('n', array(
    'nid',
  ))
    ->condition('f.entity_type', 'node')
    ->condition('n.type', $bundle)
    ->condition('n2.type', $bundle);
  $query = db_select('node', 'n');
  $query
    ->fields('n', array(
    'nid',
  ))
    ->orderBy('n.title')
    ->addTag('node_access')
    ->condition('n.type', $bundle)
    ->condition('n.nid', $query_parents, 'NOT IN');
  if (isset($status)) {
    $query
      ->condition('n.status', $status);
  }
  if (module_exists('flag') && !$include_archived) {
    if ($flag = flag_get_flag('trash')) {
      $query
        ->leftJoin('flagging', 'a', "a.fid = :fid AND a.entity_id = n.nid", array(
        ':fid' => $flag->fid,
      ));

      // This makes sure that archived content isn't included, because 'uid'
      // will be NULL if the join didn't connect anything.
      $query
        ->isNull('a.uid');
    }
  }
  $result = $query
    ->execute()
    ->fetchCol(0);
  oa_core_set_cache(OA_CACHE_TOP_PARENTS, $cid, $result);
  return $result;
}