You are here

function node_import_automap in Node import 5

Same name and namespace in other branches
  1. 6 node_import.inc \node_import_automap()

Try to find a likely mapping for given headers.

Parameters

$type: String. Node type (key in the array returned by node_import_types()).

$headers: Array of string. Column headers of a CSV or TSV file.

Return value

Array of string. A likely mapping given the $type and $header.

1 call to node_import_automap()
_node_import_mapping in ./node_import.module

File

./node_import.api.inc, line 97

Code

function node_import_automap($type, $headers) {

  // See if we saved a mapping already.
  $sql = "SELECT mapping FROM {node_import_mappings} WHERE type = '%s' AND csv_headers = '%s'";
  $obj = db_fetch_object(db_query($sql, $type, serialize($headers)));
  if ($obj) {
    return unserialize($obj->mapping);
  }

  // Try to automap by looking at the column headers.
  $fields = node_import_fields($type);
  $titles_to_fields = array();
  $fields_to_fields = array();
  foreach ($fields as $field => $title) {
    $titles_to_fields[strtolower($title)] = $field;
    $fields_to_fields[strtolower($field)] = $field;
  }
  $mapping = array();
  foreach (array_map('strtolower', $headers) as $header) {
    if (isset($titles_to_fields[$header])) {
      $mapping[] = $titles_to_fields[$header];
    }
    else {
      if (isset($fields_to_fields[$header])) {
        $mapping[] = $fields_to_fields[$header];
      }
      else {
        $mapping[] = "";
      }
    }
  }
  return $mapping;
}