You are here

function lingotek_entity_download in Lingotek Translation 7.5

Same name and namespace in other branches
  1. 7.7 lingotek.module \lingotek_entity_download()
  2. 7.6 lingotek.module \lingotek_entity_download()
3 calls to lingotek_entity_download()
lingotek_entity_download_triggered in ./lingotek.module
lingotek_rules_entity_download in ./lingotek.rules.inc
lingotek_rules_entity_download_all in ./lingotek.rules.inc
3 string references to 'lingotek_entity_download'
lingotek_get_sync_download_batch_elements in ./lingotek.batch.inc
Sync - Download Batch Elements: Creates the batch elements for nodes/documents that need to be downloaded.
lingotek_grid_download_ready in ./lingotek.bulk_grid.inc
lingotek_grid_download_selected in ./lingotek.bulk_grid.inc

File

./lingotek.module, line 1545

Code

function lingotek_entity_download($entity, $entity_type, $lingotek_locale, &$context = FALSE) {
  global $_lingotek_client;
  if (is_numeric($entity)) {
    $entity = lingotek_entity_load_single($entity_type, $entity);
  }
  if ($entity->lingotek['profile'] == LingotekSync::PROFILE_DISABLED) {
    return FALSE;
  }
  if ($entity->language == Lingotek::convertLingotek2Drupal($lingotek_locale)) {
    return FALSE;
  }
  list($id, $vid, $bundle) = lingotek_entity_extract_ids($entity_type, $entity);
  if (module_exists('rules')) {
    rules_invoke_event('lingotek_entity_translation_pre_download', new EntityDrupalWrapper($entity_type, $entity));
  }
  if ($context) {
    $context['message'] = t('Downloading "@locale" translation for entity @type @id', array(
      '@locale' => $lingotek_locale,
      '@type' => $entity_type,
      '@id' => $id,
    ));
  }
  $document_id = $entity->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($entity->lingotek['use_source']) ? $entity->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
    ->downloadTriggered("downloadDocument", $params);
  try {
    $xml = new SimpleXMLElement($text);
  } catch (Exception $e) {
    LingotekLog::error("downloadDocument FAILED. Error: @error. Text: !xml.", array(
      '!xml' => $text,
      '@error' => $e
        ->getMessage(),
    ));
    if ($context) {
      $context['results']['download_fails'] = isset($context['results']['download_fails']) && is_numeric($context['results']['download_fails']) ? $context['results']['download_fails'] + 1 : 1;
      if (!isset($context['results']['download_fail_node_targets']) || !is_array($context['results']['download_fail_node_targets'])) {
        $context['results']['download_fail_node_targets'] = array();
      }
      $context['results']['download_fail_node_targets'][] = array(
        "nid" => $id,
        "locale" => $lingotek_locale,
      );
    }
    return FALSE;
  }
  $storage_method = $entity->lingotek['lingotek_nodes_translation_method'];
  $node_based_translation = $entity_type == 'node' && $storage_method == 'node';
  $url_alias_translation = $entity->lingotek['url_alias_translation'];
  if ($entity_type == 'node' && $storage_method == 'node') {

    //node-based translations
    $storage_entity = lingotek_get_translated_node($entity, $drupal_language_code);
  }
  else {
    if (module_exists('entity_translation') && ($entity_type != 'node' || $storage_method == 'field')) {
      lingotek_entity_translation_save_status($entity_type, $entity, array(
        $drupal_language_code,
      ));
    }
    $storage_entity = $entity;
  }
  lingotek_process_entity_xml($xml, $storage_entity, $entity_type, $drupal_language_code, $node_based_translation, $url_alias_translation);
  if ($entity_type == 'node') {

    //Fix for pathauto expecting the form:
    $storage_entity->path = path_load(array(
      'source' => 'node/' . $storage_entity->nid,
      'language' => $storage_entity->language,
    ));
    $storage_entity->path['alias'] = isset($storage_entity->path['alias']) ? $storage_entity->path['alias'] : '';
    $storage_entity->path['pathauto'] = 0;
  }
  cache_clear_all('field:' . $entity_type . ':' . $id, 'cache_field');
  $status = lingotek_keystore($entity_type, $id, 'target_sync_status_' . $lingotek_locale);
  if ($status == LingotekSync::STATUS_READY) {
    lingotek_keystore($entity_type, $id, 'target_sync_status_' . $lingotek_locale, LingotekSync::STATUS_CURRENT);
  }
  elseif (empty($status)) {
    lingotek_keystore($entity_type, $id, 'target_sync_status_' . $lingotek_locale, LingotekSync::STATUS_PENDING);
  }
  if ($context) {
    $context['results']['downloads'] = isset($context['results']['downloads']) && is_numeric($context['results']['downloads']) ? $context['results']['downloads'] + 1 : 1;
    if (!isset($context['results']['downloaded_node_targets']) || !is_array($context['results']['downloaded_node_targets'])) {
      $context['results']['downloaded_node_targets'] = array();
    }
    $context['results']['downloaded_node_targets'][] = array(
      "nid" => $id,
      "locale" => $lingotek_locale,
    );
  }
  if (module_exists('rules')) {
    rules_invoke_event('lingotek_entity_translation_post_download', new EntityDrupalWrapper($entity_type, $storage_entity));
  }
  return TRUE;
}