You are here

function lingotek_get_target_node in Lingotek Translation 7.7

Same name and namespace in other branches
  1. 7.6 lingotek.remote.inc \lingotek_get_target_node()
1 call to lingotek_get_target_node()
lingotek_entity_download in ./lingotek.module

File

./lingotek.remote.inc, line 18

Code

function lingotek_get_target_node($node, $drupal_language_code) {

  // Allow the source to be overwritten as the target, if applicable, and update
  // the current language to be the same as the target.
  if ($node->language == $drupal_language_code && !empty($node->lingotek['allow_source_overwriting'])) {
    lingotek_keystore('node', $node->nid, 'source_language_' . Lingotek::convertDrupal2Lingotek($drupal_language_code), $drupal_language_code);
    return $node;
  }
  $tset = lingotek_node_get_translations($node->nid);
  if ($node->tnid == 0) {
    $node->tnid = $node->nid;
    db_update('node')
      ->fields(array(
      'tnid' => $node->nid,
    ))
      ->condition('nid', $node->nid)
      ->execute();
  }
  if (isset($tset[$drupal_language_code])) {

    // If revisioning is enabled for this content type,
    // then create a new revision of it;
    // otherwise, just load the existing one to be updated.
    $current_node = lingotek_node_load_default($tset[$drupal_language_code]->nid);
    $content_type_options = variable_get('node_options_' . $current_node->type, array());
    if (in_array('revision', $content_type_options)) {
      $current_node->revision = TRUE;
      $current_node->log = "Downloaded translation changes from Lingotek for language code '{$drupal_language_code}'.";

      // Provide compatibility with the Revisioning module.
      if (!module_exists('revisioning')) {
        $current_node->status = NODE_NOT_PUBLISHED;
      }
      node_save($current_node);
      $localized_node = lingotek_node_load_default($tset[$drupal_language_code]->nid);
    }
    else {
      $localized_node = $current_node;
    }
  }
  else {
    $localized_node = new stdClass();
    $localized_node->type = $node->type;
    node_object_prepare($localized_node);

    // Grandfather the lingotek settings (ie. profile, etc.) on the new node.
    if (!empty($node->lingotek)) {
      $localized_node->lingotek = $node->lingotek;
    }
    $localized_node->title = $node->title . ' (' . $drupal_language_code . ')';
    $localized_node->tnid = $node->tnid;
    $localized_node->language = $drupal_language_code;
    $localized_node->uid = $node->uid;
    $localized_node->name = $node->name;
    $localized_node->comment = $node->comment;
    $localized_node->promote = $node->promote;
    $localized_node->sticky = $node->sticky;
    $localized_node->status = $node->status;
    $localized_node->auto_upload = FALSE;

    // Grandfather field settings/values from source node to target.
    $source_fields = field_info_instances('node', $node->type);
    foreach (array_keys($source_fields) as $key) {
      $copied_field = $node->{$key};
      if (!empty($copied_field[$node->language])) {
        $copied_field[$localized_node->language] = $copied_field[$node->language];
        unset($copied_field[$node->language]);
      }
      $localized_node->{$key} = $copied_field;
    }
    $lingotek_fields = variable_get('lingotek_enabled_fields');
    foreach ($lingotek_fields['node'][$localized_node->type] as $field_name) {

      // Continue if translation of original node titles is enabled.
      if ($field_name == 'title') {
        continue;
      }
      $field = $node->{$field_name};
      $f = !empty($localized_node->{$field_name}) ? $localized_node->{$field_name} : array();
      if (isset($field[$node->language])) {
        foreach ($field[$node->language] as $key => $value) {
          if (isset($value['format'])) {
            $f[$drupal_language_code][$key]['format'] = $value['format'];
          }
        }
      }
      $localized_node->{$field_name} = $f;
    }
    node_save($localized_node);
    LingotekSync::setUploadStatus('node', $localized_node->nid, LingotekSync::STATUS_TARGET);

    // Child node should keep its parent node's profile, for rules integration.
    $parent_profile = lingotek_keystore('node', $node->nid, 'profile');
    if ($parent_profile !== FALSE) {
      lingotek_keystore('node', $localized_node->nid, 'profile', $parent_profile);
    }
  }
  return $localized_node;
}