You are here

function _nodesymlinks_nodeapi_insert_update in NodeSymlinks 7

Same name and namespace in other branches
  1. 6 nodesymlinks.inc \_nodesymlinks_nodeapi_insert_update()

Implements hook_nodeapi() OP: Insert & Update.

See also

nodesymlinks_nodeapi()

2 calls to _nodesymlinks_nodeapi_insert_update()
nodesymlinks_node_insert in ./nodesymlinks.module
Implements hook_node_insert().
nodesymlinks_node_update in ./nodesymlinks.module
Implements hook_node_update().

File

./nodesymlinks.inc, line 102
Main NodeSymlinks callbacks

Code

function _nodesymlinks_nodeapi_insert_update(&$node, $op) {
  $count_deleted = $count_saved = 0;

  // Check if a menu item will be created and keep $items consistent.
  $items = array();
  if (!empty($node->menu['nodesymlinks']['items'])) {
    $items = $node->menu['nodesymlinks']['items'];
  }
  $saved_items = array();

  // Save all menulinks from form (insert/update is handled by Menu API).
  foreach ($items as &$item) {

    // Load defaults and process item before save.
    nodesymlinks_item_process($item, $node);
    if (!$item['is_new']) {
      $saved_items[] = $item['mlid'];
    }
    if ($mlid = nodesymlinks_item_save($item, $node)) {
      ++$count_saved;
      if ($item['is_new']) {
        $saved_items[] = $mlid;
      }
    }
    else {
      drupal_set_message(t('There was an error when saving the menu link.'), 'error');
    }
  }

  // Detect which items exists in database, but it was not saved.
  // Delete them.
  $db_items = nodesymlinks_get_items_by_nid($node->nid);
  $items_to_delete = array_diff(array_keys($db_items), $saved_items);
  while ($mlid = array_pop($items_to_delete)) {
    nodesymlinks_item_delete($db_items[$mlid]);
    ++$count_deleted;
  }
  if (variable_get('nodesymlinks_show_messages', TRUE)) {
    drupal_set_message(format_plural($count_saved, 'Saved 1 nodesymlink.', 'Saved @count nodesymlinks.') . ' ' . format_plural($count_deleted, 'Deleted 1 nodesymlink.', 'Deleted @count nodesymlinks.'));
  }
}