You are here

public function LingotekApi::updateContentDocument in Lingotek Translation 7.7

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

Updates the content of an existing Lingotek document with the current object contents.

Parameters

stdClass $translatable_object: A Drupal node object or another object, such as a config chunk, etc.

Return value

bool TRUE on success, FALSE on failure.

File

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

Class

LingotekApi
@file Defines Drupal\lingotek\LingotekApi

Code

public function updateContentDocument(LingotekTranslatableEntity $translatable_object) {
  $parameters['documentId'] = $translatable_object
    ->getMetadataValue('document_id');
  $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();
  $parameters['format'] = $this
    ->xmlFormat();
  $parameters['projectId'] = $translatable_object
    ->getProjectId();

  // 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;
  }
  $this
    ->addAdvancedParameters($parameters, $translatable_object);
  $response = $this
    ->request('updateContentDocumentAsync', $parameters);
  $result = isset($response['body']) ? $response['body'] : NULL;
  $code = isset($response['code']) ? intval($response['code']) : NULL;
  if ($result) {
    if ($code === 200) {
      if (get_class($translatable_object) == 'LingotekConfigSet') {

        // 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());
      }
      $translatable_object
        ->setStatus(LingotekSync::STATUS_PENDING);
      $translatable_object
        ->setTargetsStatus(LingotekSync::STATUS_PENDING);
      if (isset($result->next_document_id) && isset($result->process_id)) {
        $next_doc_id = $result->next_document_id;
        $process_id = $result->process_id;
        $translatable_object
          ->setMetadataValue('previous_doc_id', $translatable_object
          ->getMetadataValue('document_id'));
        $translatable_object
          ->setMetadataValue('document_id', $next_doc_id);
        $translatable_object
          ->setMetadataValue('process_id', $process_id);
      }
      return TRUE;
    }
    elseif ($code == 402) {
      return $this
        ->handleCommunityError($translatable_object);
    }
    elseif ($code == 410) {
      $translatable_object
        ->setStatus(LingotekSync::STATUS_NONE);
      return $this
        ->addContentDocument($translatable_object, true);
    }
    elseif ($code == 423) {
      $params = [];
      $params['documentId'] = $translatable_object
        ->getMetadataValue('document_id');

      // Query the API to get the updated Lingotek DocumentId
      $response = $this
        ->request('getDocument', $params);
      $body = $response['body'];
      if (intval($response['code']) > 200) {
        throw Error("Document retrieval unsuccessful");
      }
      if (empty($body->nextDocumentId)) {
        LingotekLog::error('Error updating document: unable to retrieve next document id for record @id', params['documentId']);
        throw Error("Error updating document: unable to retrieve next document id");
      }
      $translatable_object
        ->setMetadataValue('document_id', $body->nextDocumentId);
      $this
        ->updateContentDocument($translatable_object);
      return TRUE;
    }
    else {
      throw new OAuthException2('Request failed with code ' . $code . ': ' . json_encode($result));
    }
  }
  return isset($result);
}