You are here

function og_subgroups_group_tree_json in Subgroups for Organic groups 6

Generate a group hierarchy tree in JSON format

This is used by jQuery Treeview to load the tree via AJAX

Parameters

$node: The group node

1 string reference to 'og_subgroups_group_tree_json'
og_subgroups_menu in ./og_subgroups.module
Implementation of hook_menu().

File

includes/json.inc, line 16
JSON callback functions for og subgroups

Code

function og_subgroups_group_tree_json($group) {
  $json = new stdClass();
  og_subgroups_include('tree');

  // Make sure this is a group node
  if (!og_is_group_type($group->type)) {
    return drupal_json();
  }

  // Fetch the group tree
  if (!($tree = og_subgroups_get_group_tree($group))) {

    // If no tree, then no menu
    return drupal_json();
  }

  // Generate a list of the groups parents
  $parents = og_subgroups_get_group_parents($group);

  // Iterate the tree to begin generating nested links
  foreach ($tree as $branch) {

    // If the branch has no children, end here
    if (empty($branch->children)) {
      return drupal_json();
    }
    $json->text = theme('og_subgroups_menu_tree_link', $group, $branch);
    $json->expanded = TRUE;

    // Recursively add the rest of the tree
    if (!empty($branch->children)) {
      $json->children = _og_subgroups_group_tree_branch_json($group, $branch->children, $parents);
    }
  }
  return drupal_json(array(
    $json,
  ));
}