You are here

public function Lingotek::uploadDocument in Lingotek Translation 3.6.x

Same name and namespace in other branches
  1. 8 src/Lingotek.php \Drupal\lingotek\Lingotek::uploadDocument()
  2. 8.2 src/Lingotek.php \Drupal\lingotek\Lingotek::uploadDocument()
  3. 4.0.x src/Lingotek.php \Drupal\lingotek\Lingotek::uploadDocument()
  4. 3.0.x src/Lingotek.php \Drupal\lingotek\Lingotek::uploadDocument()
  5. 3.1.x src/Lingotek.php \Drupal\lingotek\Lingotek::uploadDocument()
  6. 3.2.x src/Lingotek.php \Drupal\lingotek\Lingotek::uploadDocument()
  7. 3.3.x src/Lingotek.php \Drupal\lingotek\Lingotek::uploadDocument()
  8. 3.4.x src/Lingotek.php \Drupal\lingotek\Lingotek::uploadDocument()
  9. 3.5.x src/Lingotek.php \Drupal\lingotek\Lingotek::uploadDocument()
  10. 3.7.x src/Lingotek.php \Drupal\lingotek\Lingotek::uploadDocument()
  11. 3.8.x src/Lingotek.php \Drupal\lingotek\Lingotek::uploadDocument()

Uploads a document to the Lingotek service.

Parameters

string $title: The title of the document as it will be seen in the TMS.

string|array $content: The content of the document. It can be a json string or an array that will be json encoded.

string $locale: The Lingotek locale.

string $url: (optional) The document url in the site if any. This allows support for in-context review.

\Drupal\lingotek\LingotekProfileInterface $profile: (optional) The profile being used.

string $job_id: (optional) The job ID that will be associated.

Return value

string The document ID assigned to the uploaded document.

Throws

\Drupal\lingotek\Exception\LingotekPaymentRequiredException

\Drupal\lingotek\Exception\LingotekApiException

Overrides LingotekInterface::uploadDocument

File

src/Lingotek.php, line 233

Class

Lingotek
The connecting class between Drupal and Lingotek

Namespace

Drupal\lingotek

Code

public function uploadDocument($title, $content, $locale, $url = NULL, LingotekProfileInterface $profile = NULL, $job_id = NULL) {
  if (!is_array($content)) {
    $data = json_decode($content, TRUE);

    // This is the quickest way if $content is not a valid json object.
    $content = $data === NULL ? $content : $data;
  }

  // Handle adding site defaults to the upload here, and leave
  // the handling of the upload call itself to the API.
  $defaults = [
    'format' => 'JSON',
    'project_id' => $this->configFactory
      ->get(static::SETTINGS)
      ->get('default.project'),
    'fprm_id' => $this->lingotekFilterManager
      ->getFilterId($profile),
    'fprm_subfilter_id' => $this->lingotekFilterManager
      ->getSubfilterId($profile),
    'external_application_id' => 'e39e24c7-6c69-4126-946d-cf8fbff38ef0',
  ];

  // Remove filters set to NULL
  $defaults = array_filter($defaults);
  $workflow_id = NULL;
  $metadata = $this
    ->getIntelligenceMetadata($content);
  if ($profile !== NULL && ($project = $profile
    ->getProject())) {
    if ($project !== 'default') {
      $defaults['project_id'] = $project;
    }
  }
  if ($profile !== NULL && ($vault = $profile
    ->getVault())) {
    if ($vault === 'default') {
      $vault = $this->configFactory
        ->get(static::SETTINGS)
        ->get('default.vault');
    }
    $defaults['vault_id'] = $vault;

    // If we use the project workflow template default vault, we omit the
    // vault parameter and the TMS will decide.
    if ($vault === 'project_default') {
      unset($defaults['vault_id']);
    }
  }
  if ($profile !== NULL && ($workflow_id = $profile
    ->getWorkflow()) && $workflow_id !== 'default') {
    $defaults['translation_workflow_id'] = $workflow_id;
  }
  else {
    $defaults['translation_workflow_id'] = $this->configFactory
      ->get(static::SETTINGS)
      ->get('default.workflow');
  }
  $args = array_merge($metadata, $defaults);
  $request_locales = [];
  $request_workflows = [];
  $request_translation_vaults = [];
  if ($profile) {
    $languages = $this->lingotekConfiguration
      ->getEnabledLanguages();
    if (!empty($languages)) {
      foreach ($languages as $language) {
        if ($profile
          ->hasAutomaticRequestForTarget($language
          ->getId())) {
          $target_locale = $this->languageLocaleMapper
            ->getLocaleForLangcode($language
            ->getId());
          if ($target_locale !== $locale) {
            $workflow_id = $profile
              ->getWorkflowForTarget($language
              ->getId());
            if ($workflow_id === 'default') {
              $workflow_id = $this->configFactory
                ->get(static::SETTINGS)
                ->get('default.workflow');
            }
            $translation_vault_id = $profile
              ->getVaultForTarget($language
              ->getId());
            if ($translation_vault_id === 'default') {

              // If using overrides, we can never specify the document vault
              // as this cannot be empty, nor force to use the project template
              // vault, as it is unknown to us.
              $translation_vault_id = $this->configFactory
                ->get(static::SETTINGS)
                ->get('default.vault');
            }
            $request_locales[] = $target_locale;
            $request_workflows[] = $workflow_id;
            $request_translation_vaults[] = $translation_vault_id;
          }
        }
      }
    }
  }
  if (!empty($request_locales) && !empty($request_workflows)) {
    $args['translation_locale_code'] = $request_locales;
    $args['translation_workflow_id'] = $request_workflows;
    $args['translation_vault_id'] = $request_translation_vaults;
  }
  if ($workflow_id && $workflow_id === 'project_default' || empty($request_locales)) {
    unset($args['translation_workflow_id']);
  }
  $args = array_merge([
    'content' => json_encode($content),
    'title' => $title,
    'locale_code' => $locale,
  ], $args);
  if ($url !== NULL) {
    $args['external_url'] = $url;
  }
  if ($job_id !== NULL) {
    $args['job_id'] = $job_id;
  }
  $response = $this->api
    ->addDocument($args);
  $statusCode = $response
    ->getStatusCode();
  if ($statusCode == Response::HTTP_ACCEPTED) {
    $responseBody = Json::decode($response
      ->getBody(), TRUE);
    if (!empty($responseBody) && !empty($responseBody['properties']['id'])) {
      return $responseBody['properties']['id'];
    }
    else {
      return FALSE;
    }
  }
  elseif ($statusCode == Response::HTTP_PAYMENT_REQUIRED) {

    // This is only applicable to subscription-based connectors, but the
    // recommended action is to present the user with a message letting them
    // know their Lingotek account has been disabled, and to please contact
    // support to re-enable their account.
    $responseBody = Json::decode($response
      ->getBody());
    $message = '';
    if (!empty($responseBody) && isset($responseBody['messages'])) {
      $message = $responseBody['messages'][0];
    }
    throw new LingotekPaymentRequiredException($message);
  }
  else {
    return FALSE;
  }
  return FALSE;
}