You are here

function lingotek_download_document in Lingotek Translation 7.4

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.3 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 81

Code

function lingotek_download_document(&$source_node, $lingotek_locale, $sync_success_status = LingotekSync::STATUS_CURRENT) {
  global $_lingotek_client, $_lingotek_locale;
  if ($source_node->lingotek['node_sync_status'] == LingotekSync::STATUS_DISABLED) {
    return FALSE;
  }
  $document_id = $source_node->lingotek['document_id'];
  LingotekLog::trace('lingotek_download_document @doc_id (@target)', array(
    '@doc_id' => $document_id,
    '@target' => $lingotek_locale,
  ));
  $drupal_language_code = Lingotek::convertLingotek2Drupal($lingotek_locale);
  $params = array(
    'documentId' => $document_id,
    'targetLanguage' => $lingotek_locale,
  );

  //CAREFUL of alternate values for $use_source, must be string 'TRUE' for api, not boolean
  $use_source = isset($source_node->lingotek['use_source']) ? $source_node->lingotek['use_source'] : variable_get('lingotek_use_source', FALSE);
  $params['useSource'] = $use_source ? 'TRUE' : 'FALSE';

  // API expects a string, not boolean

  //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 FALSE;
  }
  $node_method = $source_node->lingotek['lingotek_nodes_translation_method'];
  $url_alias_translation = $source_node->lingotek['url_alias_translation'];
  if ($node_method == 'node') {

    //node-based translations
    $node = lingotek_get_translated_node($source_node, $drupal_language_code);
  }
  else {
    $node = $source_node;
  }
  lingotek_process_entity_xml($xml, $node, 'node', $drupal_language_code, $url_alias_translation);

  //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($source_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)

  //Add entries for entity translation, if applicable
  if (module_exists('entity_translation') && lingotek_managed_by_entity_translation($node->type)) {
    lingotek_entity_translation_save_status($node, array(
      $drupal_language_code,
    ));
  }

  //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
    }
  }
  lingotek_lingonode_variable_delete($node->nid, 'target_last_downloaded_' . $lingotek_locale);
  lingotek_lingonode($node->nid, 'target_last_downloaded_' . $lingotek_locale, time());
  return TRUE;

  // node_save($node);
}