You are here

function node_node_import_prepare in Node import 5

Implementation of hook_node_import_prepare().

File

supported/node.inc, line 50

Code

function node_node_import_prepare(&$node, $preview = FALSE) {
  global $user;
  $type_info = node_get_types('type', $node->type);
  $globals = $node->node_import_node;

  //unset($node->node_import_node);
  $author = $globals['author'];
  $options = $globals['options'];
  $unique_title = $globals['unique_title'];
  $errors = array();

  // 1. User name and uid.
  if (isset($node->name)) {

    // We have a mapped username.
    if (empty($node->name)) {
      unset($node->name);
    }
    else {
      if ($uid = node_import_userreference($node->name)) {
        $account = user_load(array(
          'uid' => $uid,
        ));
        $node->name = $account->name;
        $node->uid = $account->uid;
      }
      else {
        $errors[] = t('The username %name does not exist.', array(
          '%name' => $node->name,
        ));
        unset($node->name);
      }
    }
  }
  if (!isset($node->name) && isset($author)) {

    // We don't have a name yet (not mapped or error above), but we
    // do have some global options.
    if ($author['name'] == '') {
      $node->name = '';
      $node->uid = 0;
    }
    else {
      if ($uid = node_import_userreference($author['name'])) {
        $account = user_load(array(
          'uid' => $uid,
        ));
        $node->name = $account->name;
        $node->uid = $account->uid;
      }
      else {
        $errors[] = t('The username %name does not exist.', array(
          '%name' => $account->name,
        ));
        unset($node->name);
      }
    }
  }
  if (!isset($node->name)) {

    // We still don't have a name yet, use the current user.
    $node->name = $user->name;
    $node->uid = $user->uid;
  }

  // 2. Creation date.
  if (isset($node->date)) {

    // We have a mapped date.
    if (empty($node->date)) {
      unset($node->date);
    }
    else {
      if (($date = node_import_valid_date($node->date)) > -1) {
        $node->date = $date;
      }
      else {
        $errors[] = t('The date %date is not a valid date.', array(
          '%date' => $node->date,
        ));
        unset($node->date);
      }
    }
  }
  if (!isset($node->date) && isset($author)) {

    // We don't have a date yet (not mapped or error above), but we
    // do have some global options.
    if ($author['date'] == '') {
      $node->date = time();
    }
    else {
      if (($date = node_import_valid_date($author['date'])) > -1) {
        $node->date = $date;
      }
      else {
        $errors[] = t('The date %date is not a valid date.', array(
          '%date' => $author['date'],
        ));
      }
    }
  }
  if (!isset($node->date)) {

    // We still don't have a date yet, use the current time.
    $node->date = time();
  }
  $node->created = $node->date;
  $node->date = format_date($node->created, 'custom', 'Y-m-d H:i:s O');

  // 3. Last updated
  if (isset($node->updated)) {

    // We have a mapped updated date.
    if (empty($node->updated)) {
      unset($node->updated);
    }
    else {
      if (($date = node_import_valid_date($node->updated)) > -1) {
        $node->updated = $date;
      }
      else {
        $errors[] = t('The date %date is not a valid date.', array(
          '%date' => $node->updated,
        ));
        unset($node->updated);
      }
    }
  }
  if (!isset($node->updated)) {

    // We still don't have a date yet, use the current time.
    $node->updated = time();
  }

  // 4. Options (published, promoted, sticky, moderated, new revision).
  if (!isset($options)) {
    $options = array();
    foreach ((array) variable_get('node_options_' . $node->type, array(
      'status',
      'promote',
    )) as $option) {
      $options[$option] = 1;
    }
  }
  $all_options = array(
    'status',
    'moderate',
    'promote',
    'sticky',
    'revision',
  );
  foreach ($all_options as $key) {
    if (isset($node->{$key}) && strlen($node->{$key}) > 0) {

      // If the field was mapped, use that value.
      $node->{$key} = $node->{$key} ? 1 : 0;
    }
    else {

      // If not, use the global option.
      $node->{$key} = isset($options[$key]) ? $options[$key] : 0;
    }
  }

  // 5. Title.
  if ($type_info->has_title && (!isset($node->title) || empty($node->title))) {
    $errors[] = t('You need to provide a non-empty title.');
  }
  else {
    if ($unique_title) {
      $count = db_fetch_object(db_query("SELECT count(*) AS cnt FROM {node} WHERE title = '%s' AND type = '%s'", $node->title, $node->type));
      if ($count->cnt > 0) {
        $errors[] = t('The node title %title is not unique for this node type.', array(
          '%title' => $node->title,
        ));
      }
    }
  }
  return $errors;
}