You are here

function nodehierarchy_insert_node in Node Hierarchy 6

Same name and namespace in other branches
  1. 5 nodehierarchy.module \nodehierarchy_insert_node()
  2. 6.3 nodehierarchy.module \nodehierarchy_insert_node()
  3. 6.2 nodehierarchy.module \nodehierarchy_insert_node()
  4. 7.4 nodehierarchy.admin.inc \nodehierarchy_insert_node()
  5. 7.2 nodehierarchy.module \nodehierarchy_insert_node()

Insert or update a node. Set it's parent

1 call to nodehierarchy_insert_node()
nodehierarchy_nodehierarchyapi in ./nodehierarchy.module
Implementation of hook_nodehierarchyapi(). Responds to own api calls.

File

./nodehierarchy.module, line 400

Code

function nodehierarchy_insert_node(&$node) {
  global $user;

  // If the node is valid and the parent has changed or the node does not already have a parent.
  if ($node->nid && @$node->old_parent !== @$node->parent || @$node->old_parent === NULL) {
    $node_descendants = nodehierarchy_get_descendant_list($node->nid);

    // Set a the parent to the type default or the previous parent if:
    //  a) parent is not specified (should never happen)
    //  b) node is new, and user does not have 'create child nodes' permissions OR
    //  c) node doesn't belong to user and user doesn't have 'edit all node parents' permissions OR
    //  d) node belongs to user and user doesn't have 'edit own node parents' permissions
    //  e) the parent is somehow set to a descendent of the node
    if (@$node->parent === NULL || @$node->old_parent === NULL && !user_access('create child nodes') || $node->uid != $user->uid && !user_access('edit all node parents') || $node->uid == $user->uid && !user_access('edit own node parents') || in_array(@$node->parent, $node_descendants)) {

      // Set the parent back to the old parent if there was one, otherwise to the type default.
      $node->parent = @$node->old_parent;
      nodehierarchy_invoke_api("default_parent", $node);
    }

    // Check descendants again, in case the default item is in the item's descendant list.
    if (in_array(@$node->parent, $node_descendants)) {
      $node->parent = 0;
    }

    // If after all that the node has a new parent.
    if (@$node->parent !== @$node->old_parent) {

      // Place at the end of the new child list.
      // Massive number will be normalized during the insert.
      $node->order_by = _nodehierarchy_get_next_child_order(@$node->parent);
      db_query('DELETE FROM {nodehierarchy} WHERE nid = %d', $node->nid);
      db_query("INSERT INTO {nodehierarchy} (nid, parent, order_by) VALUES (%d, %d, %f)", $node->nid, $node->parent, $node->order_by);
      _nodehierarchy_normalize_child_order(@$node->old_parent);

      // Reload order (it may have shifted during the sort).
      $additions = nodehierarchy_load_node($node);
      $node->order_by = $additions['order_by'];
    }
  }
  elseif ($node->nid && @$node->old_parent == @$node->parent) {

    // If we have a valid node, and the parent has not changed
    // load up the order_by field, in case it is needed for later operations.
    $additions = nodehierarchy_load_node($node);
    $node->order_by = $additions['order_by'];
  }
}