You are here

function node_save in Drupal 4

Same name and namespace in other branches
  1. 5 modules/node/node.module \node_save()
  2. 6 modules/node/node.module \node_save()
  3. 7 modules/node/node.module \node_save()

Save a node object into the database.

7 calls to node_save()
blogapi_blogger_edit_post in modules/blogapi.module
Blogging API callback. Modifies the specified blog node.
blogapi_blogger_new_post in modules/blogapi.module
Blogging API callback. Inserts a new blog post as a node.
blogapi_mt_set_post_categories in modules/blogapi.module
Blogging API callback. Assigns taxonomy terms to a particular node.
blogap_mti_publish_post in modules/blogapi.module
Blogging API callback. Publishes the given node
book_admin_edit_submit in modules/book.module

... See full list

File

modules/node.module, line 415
The core that allows content to be submitted to the site.

Code

function node_save(&$node) {
  global $user;
  $node->is_new = false;

  // Apply filters to some default node fields:
  if (empty($node->nid)) {

    // Insert a new node.
    $node->is_new = true;
    $node->nid = db_next_id('{node}_nid');
    $node->vid = db_next_id('{node_revisions}_vid');
  }
  else {

    // We need to ensure that all node fields are filled.
    $node_current = node_load($node->nid);
    foreach ($node as $field => $data) {
      $node_current->{$field} = $data;
    }
    $node = $node_current;
    if ($node->revision) {
      $node->old_vid = $node->vid;
      $node->vid = db_next_id('{node_revisions}_vid');
    }
  }

  // Set some required fields:
  if (empty($node->created)) {
    $node->created = time();
  }

  // The changed timestamp is always updated for bookkeeping purposes (revisions, searching, ...)
  $node->changed = time();

  // Split off revisions data to another structure
  $revisions_table_values = array(
    'nid' => $node->nid,
    'vid' => $node->vid,
    'title' => $node->title,
    'body' => $node->body,
    'teaser' => $node->teaser,
    'log' => $node->log,
    'timestamp' => $node->changed,
    'uid' => $user->uid,
    'format' => $node->format,
  );
  $revisions_table_types = array(
    'nid' => '%d',
    'vid' => '%d',
    'title' => "'%s'",
    'body' => "'%s'",
    'teaser' => "'%s'",
    'log' => "'%s'",
    'timestamp' => '%d',
    'uid' => '%d',
    'format' => '%d',
  );
  $node_table_values = array(
    'nid' => $node->nid,
    'vid' => $node->vid,
    'title' => $node->title,
    'type' => $node->type,
    'uid' => $node->uid,
    'status' => $node->status,
    'created' => $node->created,
    'changed' => $node->changed,
    'comment' => $node->comment,
    'promote' => $node->promote,
    'moderate' => $node->moderate,
    'sticky' => $node->sticky,
  );
  $node_table_types = array(
    'nid' => '%d',
    'vid' => '%d',
    'title' => "'%s'",
    'type' => "'%s'",
    'uid' => '%d',
    'status' => '%d',
    'created' => '%d',
    'changed' => '%d',
    'comment' => '%d',
    'promote' => '%d',
    'moderate' => '%d',
    'sticky' => '%d',
  );

  //Generate the node table query and the

  //the node_revisions table query
  if ($node->is_new) {
    $node_query = 'INSERT INTO {node} (' . implode(', ', array_keys($node_table_types)) . ') VALUES (' . implode(', ', $node_table_types) . ')';
    $revisions_query = 'INSERT INTO {node_revisions} (' . implode(', ', array_keys($revisions_table_types)) . ') VALUES (' . implode(', ', $revisions_table_types) . ')';
  }
  else {
    $arr = array();
    foreach ($node_table_types as $key => $value) {
      $arr[] = $key . ' = ' . $value;
    }
    $node_table_values[] = $node->nid;
    $node_query = 'UPDATE {node} SET ' . implode(', ', $arr) . ' WHERE nid = %d';
    if ($node->revision) {
      $revisions_query = 'INSERT INTO {node_revisions} (' . implode(', ', array_keys($revisions_table_types)) . ') VALUES (' . implode(', ', $revisions_table_types) . ')';
    }
    else {
      $arr = array();
      foreach ($revisions_table_types as $key => $value) {
        $arr[] = $key . ' = ' . $value;
      }
      $revisions_table_values[] = $node->vid;
      $revisions_query = 'UPDATE {node_revisions} SET ' . implode(', ', $arr) . ' WHERE vid = %d';
    }
  }

  // Insert the node into the database:
  db_query($node_query, $node_table_values);
  db_query($revisions_query, $revisions_table_values);

  // Call the node specific callback (if any):
  if ($node->is_new) {
    node_invoke($node, 'insert');
    node_invoke_nodeapi($node, 'insert');
  }
  else {
    node_invoke($node, 'update');
    node_invoke_nodeapi($node, 'update');
  }

  // Clear the cache so an anonymous poster can see the node being added or updated.
  cache_clear_all();
}