You are here

function _og_subgroups_get_tree in Subgroups for Organic groups 6

Callback to generate a hierarchy tree of all groups from the database

This is called by og_subgroups_get_tree() if a cached version of the tree is not currently available

1 call to _og_subgroups_get_tree()
og_subgroups_get_tree in includes/tree.inc
Get a hierarchy tree of all groups on the site

File

includes/tree.inc, line 50
Functions to generate and use the group hierarchy trees

Code

function _og_subgroups_get_tree() {

  // Fetch all subgroup relationships
  $sql = "SELECT ogs.gid, ogs.parent, og.og_private, og.og_selective, n.title, n.status, n.type";
  $sql .= " FROM {og_subgroups} ogs";
  $sql .= " INNER JOIN {node} n ON ogs.gid = n.nid";
  $sql .= " INNER JOIN {og} og ON ogs.gid = og.nid";
  $results = db_query($sql);

  // Move the children into an array
  $children = array();
  while ($child = db_fetch_object($results)) {

    // Change gid to nid
    $child->nid = $child->gid;
    unset($child->gid);

    // Initialize the children array
    $child->children = array();

    // Add to our array of children
    $children[$child->nid] = $child;
  }

  // Fetch all groups which are only parents
  $sql = "SELECT og.nid, og.og_private, og.og_selective, n.title, n.status, n.type";
  $sql .= " FROM {og} og";
  $sql .= " INNER JOIN {node} n ON og.nid = n.nid";
  $sql .= " LEFT JOIN {og_subgroups} ogs ON og.nid = ogs.gid";
  $sql .= " WHERE ogs.gid IS NULL";
  $results = db_query($sql);

  // Move the parents into an array
  $parents = array();
  while ($parent = db_fetch_object($results)) {

    // Add a parent field to keep the array uniform, but set to zero
    $parent->parent = 0;

    // Initialize the children array
    $parent->children = array();

    // Add to our array of parents
    $parents[$parent->nid] = $parent;
  }

  // Iterate each parent in order to detect the children
  foreach ($parents as $parent) {
    $parents[$parent->nid]->children = _og_subgroups_get_tree_recursive($parent, $children);
  }

  // Remove parents with no children
  foreach ($parents as $parent) {
    if (empty($parent->children)) {
      unset($parents[$parent->nid]);
    }
  }
  return $parents;
}