You are here

public function Lingotek::updateDocument in Lingotek Translation 3.4.x

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

Updates a document in the Lingotek service.

Parameters

string $doc_id: The document id to update.

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

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

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

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

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

string $locale: (optional) The Lingotek locale.

Return value

bool|string TRUE if the document was successfully updated. FALSE if not (v5.1). New document ID if the document was successfully updated. FALSE if not (v5.2).

Throws

\Drupal\lingotek\Exception\LingotekPaymentRequiredException

\Drupal\lingotek\Exception\LingotekDocumentArchivedException

\Drupal\lingotek\Exception\LingotekDocumentLockedException

\Drupal\lingotek\Exception\LingotekApiException

Overrides LingotekInterface::updateDocument

File

src/Lingotek.php, line 361

Class

Lingotek
The connecting class between Drupal and Lingotek

Namespace

Drupal\lingotek

Code

public function updateDocument($doc_id, $content, $url = NULL, $title = NULL, LingotekProfileInterface $profile = NULL, $job_id = NULL, $locale = NULL) {

  // TODO: Fix the order of the arguments to be consistent with uploadDocument. We can't do this right now without breaking backwards compatibility
  if (!is_array($content)) {
    if ($content !== NULL) {
      $data = json_decode($content, TRUE);

      // This is the quickest way if $content is not a valid json object.
      $content = $data === NULL ? $content : $data;
    }
  }
  $defaults = [
    'format' => 'JSON',
    'fprm_id' => $this->lingotekFilterManager
      ->getFilterId($profile),
    'fprm_subfilter_id' => $this->lingotekFilterManager
      ->getSubfilterId($profile),
    'external_application_id' => 'e39e24c7-6c69-4126-946d-cf8fbff38ef0',
  ];
  if ($profile && ($project = $profile
    ->getProject())) {
    if ($project !== 'default') {
      $defaults['project_id'] = $project;
    }
  }
  $metadata = $this
    ->getIntelligenceMetadata($content);
  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);
  if ($url !== NULL) {
    $args['external_url'] = $url;
  }
  if ($title !== NULL) {
    $args['title'] = $title;
  }
  if ($job_id !== NULL) {
    $args['job_id'] = $job_id;
  }
  if ($content !== NULL && !empty($content)) {
    $args = array_merge([
      'content' => json_encode($content),
    ], $args);
  }
  else {

    // IF there's no content, let's remove filters, we may want to update only
    // the Job ID.
    unset($args['format']);
    unset($args['fprm_id']);
    unset($args['fprm_subfilter_id']);
    unset($args['external_application_id']);
  }
  $request_locales = [];
  $request_workflows = [];
  $workflow_id = NULL;
  if ($profile) {
    $languages = $this->lingotekConfiguration
      ->getEnabledLanguages();
    if (!empty($languages)) {
      foreach ($languages as $language) {
        $target_locale = $this->languageLocaleMapper
          ->getLocaleForLangcode($language
          ->getId());
        if ($locale !== NULL && $target_locale !== $locale) {
          $workflow_id = $profile
            ->getWorkflowForTarget($language
            ->getId());
          if ($workflow_id === 'default') {
            $workflow_id = $this->configFactory
              ->get(static::SETTINGS)
              ->get('default.workflow');
          }
          $request_locales[] = $target_locale;
          $request_workflows[] = $workflow_id;
        }
      }
    }
  }
  if (!empty($request_locales) && !empty($request_workflows)) {
    $args['translation_locale_code'] = $request_locales;
    $args['translation_workflow_id'] = $request_workflows;
  }
  if ($workflow_id && $workflow_id === 'project_default' || empty($request_locales)) {
    unset($args['translation_workflow_id']);
  }
  $response = $this->api
    ->patchDocument($doc_id, $args);
  $statusCode = $response
    ->getStatusCode();
  if ($statusCode == Response::HTTP_ACCEPTED) {
    $responseBody = Json::decode($response
      ->getBody(), TRUE);
    if (empty($responseBody)) {
      return TRUE;
    }
    else {
      $nextDocId = $responseBody['next_document_id'];
      return $nextDocId;
    }
  }
  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);
  }
  elseif ($statusCode == Response::HTTP_GONE) {

    // Set the status of the document back to its pre-uploaded state.
    // Typically this means the state would be set to Upload, or None but this
    // may vary depending on connector. Essentially, the content’s status
    // indicator should show that the source content needs to be re-uploaded
    // to Lingotek.
    throw new LingotekDocumentArchivedException($doc_id, sprintf('Document %s has been archived.', $doc_id));
  }
  elseif ($statusCode == Response::HTTP_LOCKED) {

    // Update the connector’s document mapping with the ID provided in the
    // next_document_id within the API response. This new ID represents the
    // new version of the document.
    $responseBody = Json::decode($response
      ->getBody());
    $nextDocId = '';
    if (!empty($responseBody) && isset($responseBody['next_document_id'])) {
      $nextDocId = $responseBody['next_document_id'];
    }
    throw new LingotekDocumentLockedException($doc_id, $nextDocId, sprintf('Document %s has been updated with a new version. Use document %s for all future interactions.', $doc_id, $nextDocId));
  }
  return FALSE;
}