You are here

function _multiple_node_menu_submit in Multiple Node Menu 6

1 call to _multiple_node_menu_submit()
multiple_node_menu_nodeapi in ./multiple_node_menu.module
Implementation of hook_nodeapi()

File

./multiple_node_menu.module, line 111

Code

function _multiple_node_menu_submit($node) {
  if (count($node->menu_links)) {
    $node->menu = reset($node->menu_links);
  }
  $original_links = array();
  $new_links = array();
  $results = db_query("SELECT * FROM {menu_links} where router_path = 'node/%' AND link_path = 'node/%d' ORDER BY weight ASC", $node->nid);
  while ($row = db_fetch_array($results)) {
    $original_links[$row['mlid']] = $row;
  }
  $first_original_link = reset($original_links);
  $first_new_link = reset($node->menu_links);
  $reinsert = FALSE;

  /*
  Because of a sort order change, there is a new "first" menu item.
  Since Drupal considers the lowest mlid the most important,
  we basically have to delete all of our menu items and re-save them
  */
  if ($first_original_link['mlid'] != $first_new_link['link_mlid']) {
    $reinsert = TRUE;
  }
  foreach ($node->menu_links as $new_link) {
    if (!(isset($new_link['link_mlid']) && is_numeric($new_link['link_mlid']) && $new_link['link_mlid'] > 0)) {
      $new_link['link_mlid'] = 0;
    }
    list($menu_name, $plid) = explode(':', $new_link['link_parent']);
    $item = array(
      'link_title' => $new_link['link_title'],
      'menu_name' => $menu_name,
      'mlid' => $new_link['link_mlid'],
      'router_path' => 'node/%',
      'plid' => $plid,
      'weight' => $new_link['link_weight'],
      'link_path' => 'node/' . $node->nid,
    );
    if ($new_link['link_enable'] != 1) {
      $item['hidden'] = 1;
    }
    if ($reinsert) {
      if ($item['mlid'] > 0) {
        menu_link_delete($item['mlid']);
      }
      $item['mlid'] = 0;
    }
    menu_link_save($item);
    if (!menu_link_save($item)) {
      drupal_set_message(t('There was an error saving the menu link.'), 'error');
    }
    $new_links[$new_link['link_mlid']] = $new_link;
  }

  // Delete menu links that are in the database but not the form -- they've been deleted
  foreach ($original_links as $original_mlid => $original_link) {
    if (!isset($new_links[$original_mlid])) {
      menu_link_delete($original_mlid);
    }
  }

  // don't need this anymore because we don't really want the menu module to do anything else
  unset($node->menu);
}