You are here

function node_convert_field_convert in Node Convert 5

Same name and namespace in other branches
  1. 6 node_convert.module \node_convert_field_convert()
  2. 7 node_convert.util.inc \node_convert_field_convert()

Transfers information from source_field to dest_field.

Parameters

$nid: The nid of the node to be converted.

$source_field: A string containing the name of the source field which contains to be transfered information.

$dest_field: A string containing the name of the destination field where the information should be strored.

1 call to node_convert_field_convert()
node_convert_node_convert in ./node_convert.module
Converts a node to another content type.

File

./node_convert.module, line 367

Code

function node_convert_field_convert($nid, $source_field, $dest_field) {
  $node = node_load($nid);
  $vid = $node->vid;
  $field_info_source = content_fields($source_field);

  // Get source field information
  $db_info_source = content_database_info($field_info_source);

  // Get DB specific source field information
  // If the source field has a separate table, we will have to delete the node info in it
  if (strpos($db_info_source['table'], "content_field_") !== false) {
    $db_source_content_field = true;
  }
  else {
    $db_source_content_field = false;
  }
  if ($dest_field == "discard") {

    // If the source field value should be discarded
    // Delete node info in the separate field table
    if ($db_source_content_field == true) {
      db_query("DELETE FROM {%s} WHERE nid = %d", $db_info_source['table'], $nid);
    }
    return;
  }
  $field_info_dest = content_fields($dest_field);

  // Get destination field information
  $db_info_dest = content_database_info($field_info_dest);

  // Get DB specific destination field information
  if (strpos($db_info_dest['table'], "content_field_") !== false) {
    $db_dest_content_field = true;
  }
  else {
    $db_dest_content_field = false;
  }
  $data = array();
  $dest_column_names = array();
  $source_column_names = array();
  $column_placeholders = array();
  $column_assignments = array();
  $source_values = array();
  foreach ($db_info_source['columns'] as $column => $attributes) {
    $source_column_names[] = $attributes['column'];

    // Collect the DB columns names of the source field
  }
  if ($field_info_source['multiple'] != 0) {

    // If the field has multiple values, we remember each value from the DB
    $query = db_query("SELECT " . implode(", ", $source_column_names) . ", delta FROM {%s} WHERE nid = %d AND vid = %d", $db_info_source['table'], $nid, $vid);
    while ($db_row = db_fetch_array($query)) {
      $source_values[] = array_merge(array_values($db_row), array(
        $nid,
        $vid,
      ));
    }
  }
  else {

    // Otherwise we just remember one row of info
    $source_values = db_fetch_array(db_query("SELECT " . implode(", ", $source_column_names) . " FROM {%s} WHERE nid = %d AND vid = %d", $db_info_source['table'], $nid, $vid));
    $source_values = array_values($source_values);
    $source_values[] = $nid;
    $source_values[] = $vid;
  }

  // After getting the source field values, we delete them in the DB
  if ($db_source_content_field == true) {
    db_query("DELETE FROM {%s} WHERE nid = %d", $db_info_source['table'], $nid);
  }

  // Prepare the INSERT and UDPATE queries, for transfering the values in the destination fields
  foreach ($db_info_dest['columns'] as $column => $attributes) {
    $dest_column_names[] = $attributes['column'];
    switch ($attributes['type']) {
      case 'int':
      case 'mediumint':
      case 'tinyint':
      case 'bigint':
        $column_placeholders[] = '%d';
        $column_assignments[] = $attributes['column'] . ' = %d';
        break;
      case 'float':
        $column_placeholders[] = '%f';
        $column_assignments[] = $attributes['column'] . ' = %f';
        break;
      default:
        $column_placeholders[] = "'%s'";
        $column_assignments[] = $attributes['column'] . " = '%s'";
    }
  }
  if ($field_info_source['multiple'] != 0) {

    // If the field has multiple values, we put each value into the DB
    foreach ($source_values as $values) {
      $db_message = db_query("INSERT INTO {" . $db_info_dest['table'] . "} (" . implode(", ", $dest_column_names) . ", delta, nid, vid) VALUES (" . implode(', ', $column_placeholders) . ", %d, %d, %d)", $values);
    }
  }
  else {
    if ($db_dest_content_field == true) {

      // If the field is re-used, we insert the data in the field's own table in the DB
      $db_message = db_query("INSERT INTO {" . $db_info_dest['table'] . "} (" . implode(", ", $dest_column_names) . ", nid, vid) VALUES (" . implode(', ', $column_placeholders) . ", %d, %d)", $source_values);
    }
    else {

      // Otherwise we just update it in the content_type table
      $db_message = db_query("UPDATE {" . $db_info_dest['table'] . "} SET " . implode(", ", $column_assignments) . " WHERE nid = %d AND vid = %d", $source_values);
    }
  }
}