You are here

public function LingotekApi::addContentDocument in Lingotek Translation 7.3

Same name and namespace in other branches
  1. 7.7 lib/Drupal/lingotek/LingotekApi.php \LingotekApi::addContentDocument()
  2. 7.2 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

File

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

Class

LingotekApi
@file Defines Drupal\lingotek\LingotekApi

Code

public function addContentDocument($translatable_object, $with_targets = FALSE) {
  global $_lingotek_locale;
  $success = FALSE;

  // see if the node is a true content node (false if config chunk or etc.)
  $isContentNode = property_exists($translatable_object, "nid") && $translatable_object->nid ? TRUE : FALSE;

  // populate project_id
  $project_id = empty($translatable_object->lingotek_project_id) ? NULL : $translatable_object->lingotek_project_id;
  if ($isContentNode) {
    $project_id = empty($project_id) ? lingotek_lingonode($translatable_object->nid, 'project_id') : $project_id;
  }
  $project_id = empty($project_id) ? variable_get('lingotek_project', NULL) : $project_id;

  // populate vault_id
  $vault_id = empty($translatable_object->lingotek_vault_id) ? NULL : $translatable_object->lingotek_vault_id;
  if ($isContentNode) {
    $vault_id = empty($vault_id) ? lingotek_lingonode($translatable_object->nid, 'vault_id') : $vault_id;
  }
  $vault_id = empty($vault_id) ? variable_get('lingotek_vault', 1) : $vault_id;

  // populate workflow_id
  $workflow_id = empty($translatable_object->lingotek_workflow_id) ? NULL : $translatable_object->lingotek_workflow_id;
  if ($isContentNode) {
    $workflow_id = empty($workflow_id) ? lingotek_lingonode($translatable_object->nid, 'lingotek_workflow') : $workflow_id;
  }
  $workflow_id = empty($workflow_id) ? variable_get('lingotek_workflow', NULL) : $workflow_id;
  $node_language = property_exists($translatable_object, 'language') ? $translatable_object->language : NULL;
  if (is_object($node_language)) {

    // Allow language attributes to be objects (e.g., config chunks)
    $node_language = $node_language->language;
  }
  $source_lingotek_locale = Lingotek::convertDrupal2Lingotek($node_language);
  $source_language = isset($source_lingotek_locale) ? $source_lingotek_locale : Lingotek::convertDrupal2Lingotek(lingotek_get_source_language());
  if ($project_id) {
    $parameters = array(
      'projectId' => $project_id,
      'format' => $this
        ->xmlFormat(),
      'sourceLanguage' => $source_language,
      'tmVaultId' => $vault_id,
    );
    if (get_class($translatable_object) == 'LingotekConfigChunk') {
      $parameters['documentName'] = $translatable_object
        ->getTitle();
      $parameters['documentDesc'] = $translatable_object
        ->getDescription();
      $parameters['content'] = $translatable_object
        ->documentLingotekXML();
      $parameters['note'] = 'configuration file #' . $translatable_object->cid;
      $cid = $translatable_object
        ->getId();
      if (!$cid) {
        $cid = '(new/unassigned)';
      }
      $parameters['note'] = 'config chunk #' . $cid;
    }
    else {
      $parameters['content'] = lingotek_xml_node_body($translatable_object);
      $parameters['documentName'] = $translatable_object->title;
      $parameters['documentDesc'] = $translatable_object->title;
      $parameters['note'] = url('node/' . $translatable_object->nid, array(
        'absolute' => TRUE,
        'alias' => TRUE,
      ));
    }
    if (!empty($workflow_id)) {
      $parameters['workflowId'] = $workflow_id;
    }
    $this
      ->addAdvancedParameters($parameters, $translatable_object);
    if ($with_targets) {
      $parameters['targetAsJSON'] = Lingotek::availableLanguageTargetsWithoutSourceAsJSON($source_language);
      $parameters['applyWorkflow'] = 'true';

      // API expects a 'true' string
      $result = $this
        ->request('addContentDocumentWithTargets', $parameters);
    }
    else {
      $result = $this
        ->request('addContentDocument', $parameters);
    }
    if ($result) {
      if (get_class($translatable_object) == 'LingotekConfigChunk') {
        $translatable_object
          ->setDocumentId($result->id);
        $translatable_object
          ->setProjectId($project_id);
        $translatable_object
          ->setChunkStatus(LingotekSync::STATUS_CURRENT);
        $translatable_object
          ->setChunkTargetsStatus(LingotekSync::STATUS_PENDING);

        // 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.
        LingotekConfigChunk::setSegmentStatusToCurrentById($translatable_object
          ->getId());
      }
      else {
        lingotek_lingonode($translatable_object->nid, 'document_id', $result->id);
        lingotek_lingonode($translatable_object->nid, 'project_id', $project_id);
        LingotekSync::setNodeAndTargetsStatus($translatable_object, LingotekSync::STATUS_CURRENT, LingotekSync::STATUS_PENDING);
      }
      $success = TRUE;
    }
  }
  return $success;
}