You are here

function og_subgroups_set_hierarchy in Subgroups for Organic groups 5

Same name and namespace in other branches
  1. 5.4 og_subgroups.module \og_subgroups_set_hierarchy()

API function to set/ update groups hierarchy.

Parameters

$op: Define the operation 'insert' or 'update'.

$node: The node object.

$parent_nid: The node id of the group that will be the parrent

$validate: Set to TRUE when a validation on the node and parent node is required.

$log: The log message.

Return value

boolean indicating if operation preformed in case $validate is enabled.

2 calls to og_subgroups_set_hierarchy()
og_subgroups_outline_submit in ./og_subgroups.module
Handles subgroups form submissions.
og_subgroups_workflow_ng_action_set_parent in ./og_subgroups.workflow_ng.inc
Action: Set groups hierarchy.

File

./og_subgroups.module, line 79
Maintains subgroups hierarchy created by the orgainc groups module.

Code

function og_subgroups_set_hierarchy($op, $node, $parent_nid, $validate = FALSE, $log = NULL) {
  $valid = FALSE;
  if ($validate) {

    // Check node is a group type.
    if (og_is_group_type($node->type)) {

      // Get all accessible/ inaccesiable groups for the user.
      $options = og_subgroups_get_eligable_groups('accessibale');
      $inaccessibale = og_subgroups_get_eligable_groups('inaccessibale');

      // Check parent_nid is a valid parent.
      if (array_key_exists($parent_nid, og_subgroups_tree($options, $node, $inaccessibale))) {
        $valid = TRUE;
      }

      // Check if $op should be insert or update.
      $parent = og_subgroups_get_family($node->nid, 'up') ? $op = 'update' : ($op = 'insert');
    }
  }
  else {
    $valid = TRUE;
  }
  if ($valid) {
    if ($op == 'insert') {
      db_query('INSERT INTO {og_subgroups} (gid, parent) VALUES (%d, %d)', $node->nid, $parent_nid);
    }
    elseif ($op == 'update') {
      db_query('UPDATE {og_subgroups} SET parent = %d WHERE gid = %d', $parent_nid, $node->nid);
    }
    if ($log) {
      db_query("UPDATE {node_revisions} SET log = '%s' WHERE vid = %d", $log, $node->vid);
    }
  }
  return $valid;
}