You are here

function _nodehierarchy_save_node in Node Hierarchy 6.3

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

Do the actual insertion or update. No permissions checking is done here.

2 calls to _nodehierarchy_save_node()
nodehierarchy_insert_node in ./nodehierarchy.module
Insert a node. Create parents and menus etc.
nodehierarchy_update_node in ./nodehierarchy.module
Update a node's parent and create menus etc.

File

./nodehierarchy.module, line 767
A module to make nodes hierarchical.

Code

function _nodehierarchy_save_node(&$node) {

  // Update all of the pre-existing or default parents.
  for ($i = 0; $i < count(@$node->nodehierarchy_menu_links); $i++) {

    // Reference menu link to clean up code and readability
    $link =& $node->nodehierarchy_menu_links[$i];

    // Get the parent mlid from the parent's node id.
    $plid = _nodehierarchy_get_node_mlid($link['pnid'], TRUE);

    // Convert NULL to 0 to distinguish between a new menu item (null) and one with parent set to none (0).
    $link['plid'] = $plid ? $plid : 0;

    // Mark the menu item to be removed if it is null and not the primary parent (ie: Secondary parents that are not enabled and with a null parent).
    // All primary parents must have a menu link even if there is no parent and it is hidden. This is necessary so the node's menu settings are
    // referenced for edits in the future. Otherwise every time the node is edited the default settings of the node type are loaded.
    if (empty($link['enabled']) && empty($link['plid']) && $i != 0) {
      $link['remove'] = TRUE;
    }

    // If the node type cannot be a parent, and has no parent itself, then do not save a link.
    if ($link['remove']) {

      // We can only delete menu links that don't have children associated with them.
      if (!_nodehierarchy_get_children_count_plid(@$link['mlid'])) {

        // Delete the the menu if it exists.
        if (@$link['mlid']) {
          nodehierarchy_delete_node_nodehierarchy_menu_link($link['mlid']);
        }

        // Do not save a new menu_link.
        continue;
      }
      else {
        $link['enabled'] = $link['plid'] = 0;
      }
    }

    // If enabled is selected, then reverse it for hidden.
    if (isset($link['enabled'])) {
      $link['hidden'] = (int) (!$link['enabled']);
    }

    // Set the menu title to the node title unless it has been customized.
    if (!@$link['customized']) {
      $link['link_title'] = $node->title;
    }
    if (isset($link['description'])) {
      $link['options']['attributes']['title'] = $link['description'];
    }

    // Update the paths (needed for new nodes).
    $link['nid'] = $node->nid;
    $link['link_path'] = 'node/' . $node->nid;

    // Do the actual save.
    _nodehierarchy_save_menu_link($link);
  }
}