You are here

public function LingotekApi::addContentDocument in Lingotek Translation 7.7

Same name and namespace in other branches
  1. 7.2 lib/Drupal/lingotek/LingotekApi.php \LingotekApi::addContentDocument()
  2. 7.3 lib/Drupal/lingotek/LingotekApi.php \LingotekApi::addContentDocument()
  3. 7.4 lib/Drupal/lingotek/LingotekApi.php \LingotekApi::addContentDocument()
  4. 7.5 lib/Drupal/lingotek/LingotekApi.php \LingotekApi::addContentDocument()
  5. 7.6 lib/Drupal/lingotek/LingotekApi.php \LingotekApi::addContentDocument()

Add a document to the Lingotek platform.

Uploads the translatable object's content in the selected language.

Parameters

object $translatable_object: A Drupal node object or lingotek ConfigChunk object

mixed $with_targets: An optional array of locales to include for translation, or TRUE for all enabled languages.

1 call to LingotekApi::addContentDocument()
LingotekApi::updateContentDocument in lib/Drupal/lingotek/LingotekApi.php
Updates the content of an existing Lingotek document with the current object contents.

File

lib/Drupal/lingotek/LingotekApi.php, line 78
Defines Drupal\lingotek\LingotekApi

Class

LingotekApi
@file Defines Drupal\lingotek\LingotekApi

Code

public function addContentDocument(LingotekTranslatableEntity $translatable_object, $with_targets = FALSE) {
  $success = FALSE;
  $project_id = $translatable_object
    ->getProjectId();
  $source_language = $translatable_object
    ->getSourceLocale();
  if (empty($source_language)) {
    drupal_set_message('Some entities not uploaded because the source language was language neutral.', 'warning', FALSE);
    LingotekLog::warning('Document @docname not uploaded. Language was language neutral.', array(
      '@docname' => $translatable_object
        ->getDocumentName(),
    ));
    return FALSE;
  }
  if ($project_id) {
    $parameters = array(
      'projectId' => $project_id,
      'format' => $this
        ->xmlFormat(),
      'sourceLanguage' => $source_language,
      'tmVaultId' => $translatable_object
        ->getVaultId(),
    );
    $parameters['documentName'] = $translatable_object
      ->getDocumentName();
    $parameters['documentDesc'] = $translatable_object
      ->getDescription();
    $parameters['content'] = $this
      ->check_url_alias_translation($translatable_object, $translatable_object
      ->documentLingotekXML());
    $parameters['url'] = $translatable_object
      ->getUrl(TRUE);
    $parameters['workflowId'] = $translatable_object
      ->getWorkflowId();
    $this
      ->addAdvancedParameters($parameters, $translatable_object);

    // If the document has invalid characters, return without uploading
    $invalid_xml = lingotek_keystore($translatable_object
      ->getEntityType(), $translatable_object
      ->getId(), 'invalid_xml');
    if ($invalid_xml == LingotekSync::INVALID_XML_PRESENT) {
      drupal_set_message(t('Unable to upload to Lingotek because entity contains invalid XML characters.'), 'warning');
      return FALSE;
    }

    // If the entity is empty, also return without uploading
    $empty_entity = lingotek_keystore($translatable_object
      ->getEntityType(), $translatable_object
      ->getId(), 'empty_entity');
    if ($empty_entity === LingotekSync::EMPTY_ENTITY) {
      drupal_set_message(t('Entity was not uploaded to Lingotek because it is empty.'), 'warning');
      return FALSE;
    }
    if ($with_targets) {
      if (is_array($with_targets)) {

        // Assumes language-specific profiles are enabled, so handle adding
        // target locales with *custom workflows* separately, and include
        // all the other target locales here.
        $default_targets = array();
        $language_specific_targets = array();
        foreach ($with_targets as $l => $v) {
          if (empty($v['workflow_id'])) {
            $default_targets[] = $l;
          }
          else {
            $language_specific_targets[$l] = $v;
          }
        }
        if (!empty($default_targets)) {
          $parameters['targetAsJSON'] = json_encode($default_targets);
          $parameters['applyWorkflow'] = 'true';

          // API expects a 'true' string
          $result = $this
            ->request('addContentDocumentWithTargetsAsync', $parameters);
        }
        else {
          $result = $this
            ->request('addContentDocumentAsync', $parameters);
        }
      }
      else {
        $parameters['targetAsJSON'] = Lingotek::getLanguagesWithoutSourceAsJSON($source_language);
        $parameters['applyWorkflow'] = 'true';

        // API expects a 'true' string
        $result = $this
          ->request('addContentDocumentWithTargetsAsync', $parameters);
      }
    }
    else {
      $result = $this
        ->request('addContentDocumentAsync', $parameters);
    }
    $code = isset($result['code']) ? intval($result['code']) : NULL;
    $result = isset($result['body']) ? $result['body'] : NULL;
    if ($code == 402) {
      return $this
        ->handleCommunityError($translatable_object);
    }
    if ($result) {
      if (isset($result->processId)) {
        $translatable_object
          ->setMetadataValue("process_id", $result->processId);
        $translatable_object
          ->setMetadataValue("initial_upload", TRUE);
      }
      if (isset($result->errors) && $result->errors) {
        LingotekLog::error(t('Request to send document to Lingotek failed: ') . print_r($result->errors, TRUE), array());
        $translatable_object
          ->setStatus(LingotekSync::STATUS_ERROR);
        $translatable_object
          ->setLastError(is_array($result->errors) ? array_shift($result->errors) : $result->errors);
        return FALSE;
      }
      if (get_class($translatable_object) == 'LingotekConfigSet') {
        $translatable_object
          ->setDocumentId($result->id);
        $translatable_object
          ->setProjectId($project_id);
        $translatable_object
          ->setStatus(LingotekSync::STATUS_CURRENT);
        $translatable_object
          ->setTargetsStatus(LingotekSync::STATUS_PENDING, $with_targets);

        // WTD: there is a race condition here where a user could modify a locales-
        // source entry between the time the dirty segments are pulled and the time
        // they are set to current at this point.  This same race condition exists
        // for nodes as well; however, the odds may be lower due to number of entries.
        LingotekConfigSet::setSegmentStatusToCurrentById($translatable_object
          ->getId());
      }
      else {
        $entity_type = $translatable_object
          ->getEntityType();
        lingotek_keystore($entity_type, $translatable_object
          ->getId(), 'document_id', $result->id);
        lingotek_keystore($entity_type, $translatable_object
          ->getId(), 'last_uploaded', time());
        if ($with_targets) {
          if (is_array($with_targets)) {
            foreach ($default_targets as $default_target) {
              lingotek_keystore($entity_type, $translatable_object
                ->getId(), 'target_sync_status_' . $default_target, LingotekSync::STATUS_PENDING);
            }
            if (!empty($language_specific_targets)) {
              $this
                ->upload_language_specific_targets($entity_type, $translatable_object
                ->getId(), $result->id, $language_specific_targets);
            }
          }
          else {
            $target_locales = Lingotek::getLanguagesWithoutSource($source_language);
            foreach (array_keys($target_locales) as $locale) {
              lingotek_keystore($entity_type, $translatable_object
                ->getId(), 'target_sync_status_' . $locale, LingotekSync::STATUS_PENDING);
            }
          }
        }
      }
      $success = TRUE;
    }
  }
  return $success;
}