You are here

function _serial_generate_value in Serial Field 6

Same name and namespace in other branches
  1. 7 serial.inc \_serial_generate_value()

Generates a unique serial value (unique per node type).

Parameters

$nid: id of the node for which to generate a serial value

$field: the serial field

Return value

the unique serial value number.

2 calls to _serial_generate_value()
serial_field in ./serial.module
Implementation of hook_field().
_serial_init_old_nodes in ./serial.inc
Initializes the value of a new serial field in existing nodes.

File

./serial.inc, line 123
Internal functions for the Serial module.

Code

function _serial_generate_value($nid, $field, $delete = TRUE) {

  // Insert a temporary record to get a new unique serial value:
  $table = _serial_get_field_table_name($field);
  $query = 'INSERT INTO {' . $table . '} (nid) VALUES(%d)';
  db_query($query, $nid);

  // Retrieve the new unique serial value:
  $query = 'SELECT sid FROM {' . $table . '} WHERE nid = %d';
  $result = db_result(db_query($query, $nid));
  $sid = $result ? $result : 0;

  // Delete old temporary records:
  if ($delete && $sid % 10 == 0) {
    $query = 'DELETE FROM {' . $table . '} WHERE nid < %d';
    db_query($query, $nid);
  }

  // Return the new unique serial value:
  return $sid;
}