You are here

function _fieldgroup_get_tree in Content Construction Kit (CCK) 6.3

create a tree of fieldgroups for nesting them

1 call to _fieldgroup_get_tree()
fieldgroup_groups in modules/fieldgroup/fieldgroup.module
Returns all groups for a content type

File

modules/fieldgroup/fieldgroup.module, line 256
Create field groups for CCK fields.

Code

function _fieldgroup_get_tree($type_name, $parent = '', $depth = -1, $max_depth = null) {
  static $children, $parents, $groups;
  $depth++;

  // We cache trees, so it's not CPU-intensive to call get_tree() on a term
  // and its children, too.
  if (!isset($children[$type_name])) {
    $children[$type_name] = array();
    $result = db_query("SELECT * FROM {" . fieldgroup_tablename() . "} WHERE type_name='%s' ORDER BY weight", $type_name);
    while ($group = db_fetch_array($result)) {
      $group['settings'] = unserialize($group['settings']);
      $group['fields'] = array();

      // Allow external modules to translate field group strings.
      $group_strings = array(
        'label' => $group['label'],
        'form_description' => $group['settings']['form']['description'],
        'display_description' => $group['settings']['display']['description'],
      );
      $group['label'] = $group_strings['label'];
      $group['settings']['form']['description'] = $group_strings['form_description'];
      $group['settings']['display']['description'] = $group_strings['display_description'];
      $children[$type_name][$group['parent']][] = $group['group_name'];
      $parents[$type_name][$group['group_name']][] = $group['parent'];
      $groups[$type_name][$group['group_name']] = $group;
    }

    //load fields
    $result = db_query("SELECT nfi.*, ng.group_name FROM {" . fieldgroup_tablename() . "} ng " . "INNER JOIN {" . fieldgroup_fields_tablename() . "} ngf ON ngf.type_name = ng.type_name AND ngf.group_name = ng.group_name " . "INNER JOIN {" . content_instance_tablename() . "} nfi ON nfi.field_name = ngf.field_name AND nfi.type_name = ngf.type_name " . "WHERE nfi.widget_active = 1 ORDER BY nfi.weight");
    while ($field = db_fetch_array($result)) {
      $groups[$field['type_name']][$field['group_name']]['fields'][$field['field_name']] = $field;

      // Unserialize arrays.
      foreach (array(
        'widget_settings',
        'display_settings',
        'global_settings',
        'db_columns',
      ) as $key) {
        $groups[$field['type_name']][$field['group_name']]['fields'][$field['field_name']] = !empty($groups[$field['type_name']][$field['group_name']]['fields'][$field['field_name']][$key]) ? (array) unserialize($groups[$field['type_name']][$field['group_name']]['fields'][$field['field_name']][$key]) : array();
      }

      // For fields inside of groups, use the weight given by fieldgroup
      $groups[$field['type_name']][$field['group_name']]['fields'][$field['field_name']]['weight'] = $field['weight'];
    }
  }
  $max_depth = is_null($max_depth) ? count($children[$type_name]) : $max_depth;
  if (isset($children[$type_name][$parent])) {
    foreach ($children[$type_name][$parent] as $child_group_name) {
      if ($max_depth > $depth) {
        $group = $groups[$type_name][$child_group_name];
        $group['depth'] = $depth;
        $group['parents'] = $parents[$type_name][$child_group_name];
        $tree[$group['group_name']] = $group;
        if (!empty($children[$type_name][$child_group_name])) {
          $tree = array_merge($tree, _fieldgroup_get_tree($type_name, $child_group_name, $depth, $max_depth));
        }
      }
    }
  }
  return $tree ? $tree : array();
}