You are here

function lingotek_entity_upload in Lingotek Translation 7.7

Same name and namespace in other branches
  1. 7.5 lingotek.module \lingotek_entity_upload()
  2. 7.6 lingotek.module \lingotek_entity_upload()
3 calls to lingotek_entity_upload()
lingotek_entity_upload_triggered in ./lingotek.module
lingotek_push_form_submit in ./lingotek.page.inc
Submit handler for the lingotek_push_form form.
lingotek_rules_entity_upload in ./lingotek.rules.inc
3 string references to 'lingotek_entity_upload'
drush_lingotek_push in ./lingotek.drush.inc
Callback function for drush command batch upload translatable content
lingotek_get_sync_upload_batch_elements in ./lingotek.batch.inc
Sync - Upload Batch Elements: Creates the batch elements for nodes/documents that need to be uploaded.
lingotek_grid_upload_edited in ./lingotek.bulk_grid.inc

File

./lingotek.module, line 1715

Code

function lingotek_entity_upload($entity, $entity_type, &$context = array()) {
  if (is_numeric($entity)) {
    $entity = lingotek_entity_load_single($entity_type, $entity);
  }
  if ($entity_type == 'node') {

    // Exclude translated nodes (targets) created using node-based translation.
    if ($entity->tnid != 0 && $entity->tnid != $entity->nid) {
      LingotekLog::trace('Skipping upload of node @id, since it is a translation target.', array(
        '@id' => $entity->nid,
      ));
      return;
    }

    // Exclude completed in-place translations, as they would overwrite the source document with target content
    $source_lang_str = 'source_language_' . Lingotek::convertDrupal2Lingotek($entity->language);
    if (!empty($entity->lingotek['original_language']) && !empty($entity->lingotek[$source_lang_str])) {
      $original_language = $entity->lingotek['original_language'];
      $current_language = $entity->lingotek[$source_lang_str];
      if ($original_language != $current_language) {
        LingotekLog::trace('Skipping upload of node @id, since it is the result of an in-place translation and uploading it would replace the original document contents on the Lingotek TMS.', array(
          '@id' => $entity->nid,
        ));
        return;
      }
    }
  }
  list($id, $vid, $bundle) = lingotek_entity_extract_ids($entity_type, $entity);
  LingotekLog::trace('Uploading @type @id for translation', array(
    '@type' => $entity_type,
    '@id' => $id,
  ));
  $empty_context = FALSE;
  if (empty($context)) {
    $empty_context = TRUE;

    // preserve the fact that $context *was* empty when passed
    $context['message'] = t('Uploading @type @id for translation', array(
      '@type' => $entity_type,
      '@id' => $id,
    ));
  }
  if ($entity->lingotek['profile'] == LingotekSync::PROFILE_DISABLED) {
    $context['results']['disabled'][] = $id;
    return;
  }
  if (module_exists('rules')) {
    rules_invoke_event('lingotek_entity_pre_upload', new EntityDrupalWrapper($entity_type, $entity));
  }

  // Items that are only accessible on node add or edit forms for nodes not yet sent to Lingotek.
  $ln = LingotekEntity::load($entity, $entity_type);
  $entity_has_doc_id = $ln
    ->getMetadataValue('document_id');
  module_invoke_all('lingotek_pre_upload', $ln);

  // Get the source language and available target languages.
  $source = isset($ln->language) ? $ln->language : language_default()->language;
  $source = Lingotek::convertDrupal2Lingotek($source);
  $target_locales = Lingotek::getLanguagesWithoutSource($source);

  // Pull the desired target locales for the entity's current profile.
  if (variable_get('lingotek_enable_language_specific_profiles', FALSE)) {
    $profile = LingotekProfile::loadById($entity->lingotek['profile']);
    $target_locales = $profile
      ->filterTargetLocales(array_keys($target_locales));
    $with_targets = $target_locales;
  }
  else {
    $with_targets = TRUE;
  }
  if ($entity_has_doc_id) {
    $success = LingotekApi::instance()
      ->updateContentDocument($ln);
  }
  else {
    $success = LingotekApi::instance()
      ->addContentDocument($ln, $with_targets);
    if (!empty($ln->lingotek['allow_source_overwriting'])) {
      lingotek_keystore($entity_type, $id, 'original_language', $ln->language);
    }
  }
  if ($empty_context) {
    $message_vars = array(
      '@node_title' => $ln
        ->getTitle(),
      '@entity_type' => $ln
        ->getEntityType(),
    );
    if ($success) {
      drupal_set_message(t('<em>@node_title (@entity_type)</em> sent to Lingotek successfully.', $message_vars));
      LingotekSync::setUploadStatus($entity_type, $id, LingotekSync::STATUS_PENDING);
    }
    else {
      drupal_set_message(t('Unable to send <em>@node_title (@entity_type)</em> to Lingotek.', $message_vars), 'error');
    }
  }
  else {
    if ($success) {
      $context['results']['uploads'] = isset($context['results']['uploads']) && is_numeric($context['results']['uploads']) ? $context['results']['uploads'] + 1 : 1;
      if (!isset($context['results']['uploaded_nids']) || !is_array($context['results']['uploaded_nids'])) {
        $context['results']['uploaded_nids'] = array();
      }
      $context['results']['uploaded_nids'][] = $id;
      LingotekSync::setUploadStatus($entity_type, $id, LingotekSync::STATUS_PENDING);
    }
    else {
      $context['results']['upload_fails'] = isset($context['results']['upload_fails']) && is_numeric($context['results']['upload_fails']) ? $context['results']['upload_fails'] + 1 : 1;
      if (!isset($context['results']['upload_fail_nids']) || !is_array($context['results']['upload_fail_nids'])) {
        $context['results']['upload_fail_nids'] = array();
      }
      $context['results']['upload_fail_nids'][] = $id;
    }
  }
  lingotek_keystore($entity_type, $id, 'last_uploaded', time());
  if ($entity_type == 'taxonomy_term' && $entity->language == LANGUAGE_NONE && !empty($target_locales['en_US'])) {
    unset($target_locales['en_US']);
  }
  if ($entity_type == 'bean' && $entity->language == LANGUAGE_NONE) {
    $site_locale = Lingotek::convertDrupal2Lingotek(language_default()->language);
    if (!empty($target_locales[$site_locale])) {
      unset($target_locales[$site_locale]);
    }
  }
  if ($entity_type == 'group' && $entity->language == LANGUAGE_NONE) {
    $site_locale = Lingotek::convertDrupal2Lingotek(language_default()->language);
    if (!empty($target_locales[$site_locale])) {
      unset($target_locales[$site_locale]);
    }
  }
  if (module_exists('entitycache')) {
    cache_clear_all($id, 'cache_entity_node');
  }
  if (module_exists('rules')) {
    rules_invoke_event('lingotek_entity_post_upload', new EntityDrupalWrapper($entity_type, $entity));
  }
}