You are here

function lingotek_download_document in Lingotek Translation 7.2

Same name and namespace in other branches
  1. 6 lingotek.api.inc \lingotek_download_document()
  2. 7.3 lingotek.api.inc \lingotek_download_document()
  3. 7.4 lingotek.api.inc \lingotek_download_document()
4 calls to lingotek_download_document()
lingotek_download_node_machine_translation in ./lingotek.batch.inc
Batch Worker Function: Download a Machine Translation
lingotek_mt_sync_download_node_target in ./lingotek.batch.inc
Download Batch Worker Function: Download Translated Node Content
lingotek_node_sync in ./lingotek.util.inc
Synchronize the node's content with current translations as stored on the Lingotek platform.
lingotek_update in ./lingotek.page.inc

File

./lingotek.api.inc, line 19

Code

function lingotek_download_document(&$node, $lingotek_locale) {
  global $_lingotek_client, $_lingotek_locale;
  $document_id = lingotek_lingonode($node->nid, 'document_id');
  lingotek_trace('lingotek_download_document', array(
    'document_id' => $document_id,
    "language" => $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);
  $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) {
    lingotek_error("downloadDocument FAILED", 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);
  foreach ($xml as $tag => $content) {
    if ($tag == 'menu_title') {

      //TODO handle menu translation
    }
    elseif ($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;
        foreach ($content as $text) {
          $node_field[$drupal_language_code][$index][$target_key] = decode_entities(lingotek_xml_decode($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'];
            }
          }
          $index++;
        }
      }

      //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);
      }
    }
  }

  //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;
  lingotek_set_target_sync_status($node->nid, $lingotek_locale, LINGOTEK_TARGET_SYNC_STATUS_CURRENT);

  //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)
  node_save($node);
}