You are here

function lingotek_download_document in Lingotek Translation 7.3

Same name and namespace in other branches
  1. 6 lingotek.api.inc \lingotek_download_document()
  2. 7.2 lingotek.api.inc \lingotek_download_document()
  3. 7.4 lingotek.api.inc \lingotek_download_document()
1 call to lingotek_download_document()
lingotek_sync_download_node_target in ./lingotek.batch.inc
Download Batch Worker Function: Download Translated Node Content

File

./lingotek.api.inc, line 20

Code

function lingotek_download_document(&$node, $lingotek_locale, $sync_success_status = LingotekSync::STATUS_CURRENT) {
  global $_lingotek_client, $_lingotek_locale;
  $document_id = lingotek_lingonode($node->nid, 'document_id');
  LingotekLog::trace('lingotek_download_document @doc_id (@target)', array(
    '@doc_id' => $document_id,
    '@target' => $lingotek_locale,
  ));

  //CAREFUL of alternate values for $use_source, must be string 'TRUE' for api, not boolean
  $use_source = lingotek_lingonode($node->nid, 'use_source');
  if ($use_source === FALSE) {
    $use_source = variable_get('lingotek_use_source', TRUE);
  }
  if ($use_source == '1') {
    $use_source = 'TRUE';
  }
  $drupal_language_code = Lingotek::convertLingotek2Drupal($lingotek_locale);

  //lingotek_lookup_language_by_locale($lingotek_locale)->language
  $params = array(
    'documentId' => $document_id,
    'targetLanguage' => $lingotek_locale,
    'useSource' => $use_source,
  );

  //Get the finished document
  $text = $_lingotek_client
    ->download("downloadDocument", $params);
  try {
    $xml = new SimpleXMLElement($text);
  } catch (Exception $e) {
    LingotekLog::error("downloadDocument FAILED. Error: @error. Text: !xml.", array(
      '!xml' => $text,
      '@error' => $e
        ->getMessage(),
    ));
    return;
  }
  $url_alias_translation = lingotek_variable_get(lingotek_lingonode($node->nid, 'url_alias_translation'), 'lingotek_url_alias_translation', 1);
  $delta = 0;
  $last_tag = NULL;
  foreach ($xml as $tag => $content) {
    if ($tag == $last_tag) {
      $delta++;
    }
    else {
      $delta = 0;
    }
    if ($tag == 'url_alias' && $url_alias_translation == 1) {
      $target = check_plain($content);

      //URL Alias related to the page:
      $conditions = array(
        'source' => 'node/' . $node->nid,
      );
      if ($node->language != LANGUAGE_NONE) {
        $conditions['language'] = $node->language;
      }
      $path = path_load($conditions);
      if ($path !== FALSE) {
        $conditions['language'] = $drupal_language_code;
        if ($path['alias'] != $target) {
          $original = path_load($conditions);
          $conditions['alias'] = $target;
          if ($original === FALSE) {
            path_save($conditions);
          }
          else {
            path_delete($original);
            path_save($conditions);
          }
        }
      }
    }
    else {
      $drupal_field_name = $tag;
      $target_key = 'value';
      $subfield_parts = explode('__', $tag);
      if (count($subfield_parts) == 2) {
        $drupal_field_name = $subfield_parts[0];
        $target_key = $subfield_parts[1];
      }
      $field = field_info_field($drupal_field_name);
      if (isset($field) && array_key_exists('lingotek_translatable', $field) && $field['lingotek_translatable'] == 1) {
        $node_field =& $node->{$drupal_field_name};
        $index = 0;
        if (module_exists('link') && $field['type'] == 'link_field') {
          $target_key = array(
            'url',
            'title',
          );
        }
        if (!is_array($target_key)) {
          $target_key = array(
            $target_key,
          );
        }
        $insert_array = array(
          'entity_type' => 'node',
          'bundle' => $node->type,
          'entity_id' => $node->nid,
          'revision_id' => $node->vid,
          'language' => $drupal_language_code,
          'delta' => $delta,
          'deleted' => '0',
        );
        foreach ($content as $text) {
          $array_key = $target_key[$index];
          $db_field_name = $field['field_name'] . '_' . $array_key;
          $insert_array[$db_field_name] = decode_entities($text);

          //$node_field[$drupal_language_code][$delta][$array_key] = decode_entities($text);
          if (array_key_exists($node->language, $node_field)) {
            if (array_key_exists('format', $node_field[$node->language][0])) {

              //$node_field[$drupal_language_code][$index]['format'] = $node_field[$node->language][0]['format'];
              $format_db_field_name = $field['field_name'] . '_format';
              $insert_array[$format_db_field_name] = $node_field[$node->language][0]['format'];
            }
          }
          $index++;
        }
        $field_names = array(
          'field_revision_' . $field['field_name'],
        );
        $field_names[] = 'field_data_' . $field['field_name'];

        // put fields into both field_data_ and field_revision_ tables
        // if published version (in node table) is as current as most current revision (in node_revision table)
        if (isset($node->workbench_moderation) && isset($node->workbench_moderation['published']) && $node->workbench_moderation['published']->vid != $node->workbench_moderation['current']->vid) {
          $only_write_revisions = 1;
          unset($field_names[$only_write_revisions]);
        }
        foreach ($field_names as $field_name) {

          // using drupal_write_record to avoid node_save - node_save overwrites publications unless called on both revised and published versions of the node (i.e. workbench_moderation)
          try {
            drupal_write_record($field_name, $insert_array);
          } catch (PDOException $e) {
            $primary_keys = array(
              'entity_type',
              'entity_id',
              'revision_id',
              'deleted',
              'delta',
              'language',
            );
            drupal_write_record($field_name, $insert_array, $primary_keys);
          }
        }
      }

      //Set URL alias
      $url_alias_translation = lingotek_variable_get(lingotek_lingonode($node->nid, 'url_alias_translation'), 'lingotek_url_alias_translation', 1);
      if ($tag == 'title_field' && $url_alias_translation == 2 && module_exists('pathauto') && $node->language != LANGUAGE_NONE) {
        module_load_include('inc', 'pathauto');
        $uri = entity_uri('node', $node);
        pathauto_create_alias('node', 'update', $uri['path'], array(
          'node' => clone $node,
        ), $node->type, $drupal_language_code);
      }
    }
    $last_tag = $tag;
  }

  //Fix for pathauto expecting the form:
  $node->path = path_load(array(
    'source' => 'node/' . $node->nid,
    'language' => $node->language,
  ));
  $node->path['alias'] = isset($node->path['alias']) ? $node->path['alias'] : '';
  $node->path['pathauto'] = 0;
  LingotekSync::setTargetStatus($node->nid, $lingotek_locale, $sync_success_status);

  //slightly pre-emptive, but certainly more cohesive
  $node->lingotek_upload_override = 0;

  // ensure that no upload to lingotek is triggered on node update (in lingotek_node_update)
  $node->skip_status_updates = 1;

  // ensure that the statuses are not set on node update (in lingotek_node_update)

  //Fix for workbench_moderation changing node state after translation download
  if (module_exists('workbench_moderation')) {
    if (isset($node->workbench_moderation)) {
      $node->workbench_moderation['updating_live_revision'] = 1;

      // ensure that workflow state does not get updated
    }
  }

  // node_save($node);
}