You are here

function lingotek_notifications in Lingotek Translation 7.4

Same name and namespace in other branches
  1. 7.7 lingotek.sync.inc \lingotek_notifications()
  2. 7.2 lingotek.sync.inc \lingotek_notifications()
  3. 7.3 lingotek.sync.inc \lingotek_notifications()
  4. 7.5 lingotek.sync.inc \lingotek_notifications()
  5. 7.6 lingotek.sync.inc \lingotek_notifications()

Registers the site translation notfication callback.

1 string reference to 'lingotek_notifications'
lingotek_menu in ./lingotek.module
Implements hook_menu().

File

./lingotek.sync.inc, line 55
Sync and management

Code

function lingotek_notifications() {
  $document_id = isset($_GET['doc_id']) ? $_GET['doc_id'] : NULL;

  // uuid
  $document_idx = isset($_GET['doc_idx']) ? $_GET['doc_idx'] : NULL;

  // this is the deprecated document number
  $lingotek_locale = isset($_GET['target_code']) ? $_GET['target_code'] : NULL;
  $project_id = isset($_GET['project_id']) ? $_GET['project_id'] : NULL;
  $completed = isset($_GET['completed']) ? $_GET['completed'] : 1;
  $security_token = isset($_GET['security_token']) ? $_GET['security_token'] : NULL;
  $stored_security_token = variable_get('lingotek_notify_security_token', NULL);
  if (!is_null($stored_security_token)) {

    // only enforce security token matching if set as variable
    if (strcmp($security_token, $stored_security_token) != 0) {
      return drupal_json_output(array(
        "message" => "Invalid security token",
      ));
    }
  }
  if (!isset($document_id) && !isset($document_idx) || !isset($lingotek_locale)) {
    return drupal_json_output(array(
      "message" => "Missing Required Parameter(s).  Required: doc_id, target_code",
    ));
  }

  // Adding a delay in the update.  Without the delay all the different language updates hit at once, causing node lock issues as multiple languages try to update the same node at once.
  $min = 0;
  $max = 3;
  $sleep = rand($min, $max);
  sleep($sleep);
  include_once 'lingotek.batch.inc';
  $context = '';
  $target_drupal_language_code = Lingotek::convertLingotek2Drupal($lingotek_locale);

  // try 3 times if by chance we don't find the document in the database yet..
  $attempts = 0;
  while ($attempts < 3) {
    $nid = LingotekSync::getNodeIdFromDocId($document_id);
    if (!$nid) {

      // check for the old style document_id
      $nid = LingotekSync::getNodeIdFromDocId($document_idx);
      if ($nid) {
        $document_id = $document_idx;
      }
    }
    LingotekLog::info('<b>code:</b> @lingotek_locale <br/><b>doc_id:</b> @document_id<br/><b>project:</b> @project_id <br/><b>node:</b> @node_id (@target_drupal_language_code) <br/><b>completed</b>: @completed', array(
      '@document_id' => $document_id,
      '@lingotek_locale' => $lingotek_locale,
      '@project_id' => $project_id,
      '@node_id' => $nid ? $nid : "NA",
      '@target_drupal_language_code' => $target_drupal_language_code,
      '@completed' => $completed,
    ), 'callback');

    // If there is not a node ID associated with this document, check for config chunks and comments
    // associated with the document ID
    if (!$nid) {
      $source_language = lingotek_get_source_language();

      //TO-DO: use the source_language of the object?
      $replacements = array(
        '@document' => $document_id,
        '@language_code' => $lingotek_locale,
        '@project_id' => $project_id,
      );

      // Look for and sync a config chunk if one is associated with the passed Lingotek Document ID.
      if ($trans_obj = LingotekConfigChunk::loadByLingotekDocumentId($document_id, $source_language, $project_id)) {
        $replacements['@id'] = $trans_obj->cid;
        $replacements['@trans_type'] = "ConfigChunk";
      }
      elseif ($trans_obj = LingotekComment::loadByLingotekDocumentId($document_id, $source_language, $project_id)) {
        $replacements['@id'] = $trans_obj->id;
        $replacements['@trans_type'] = "Comment";
      }
      else {
        if ($attempts < 10) {
          LingotekLog::info('Did not find doc ID @doc_id yet on attempt #@attempt, retrying...', array(
            '@attempt' => $attempts,
            '@doc_id' => $document_id,
          ));
          sleep(2);
          $attempts++;
          continue;
        }
        LingotekLog::error('Lingotek document ID (@doc_id) not found.', array(
          '@doc_id' => $document_id,
        ));
        return drupal_json_output(array(
          "message" => "The doc_id was not found on the site.",
        ));
      }
      if ($trans_obj
        ->updateLocalContentByTarget($lingotek_locale)) {
        LingotekLog::trace('Updated local content for <strong>@trans_type</strong> @id based on hit
              from external API for document: @document, language code @language_code, project ID: @project_id', $replacements, 'api');
      }
      else {
        LingotekLog::trace('Unable to update local content for <strong>@trans_type</strong> @id based on hit
              from external API for document: @document, language code @language_code, project ID: @project_id', $replacements, 'api');
      }
    }
    else {
      $node = lingotek_node_load_default($nid);
      $source_language = $node->language;
      $node_setting = $node->lingotek['sync_method'];
      $auto_download = $node_setting !== FALSE ? $node_setting : variable_get('lingotek_sync', TRUE);
      if ($completed) {
        LingotekSync::setTargetStatus($nid, $lingotek_locale, LingotekSync::STATUS_READY);
      }
      if ($auto_download) {

        // download only when automatic download is enabled
        $status = $completed ? LingotekSync::STATUS_CURRENT : LingotekSync::STATUS_PENDING;
        lingotek_sync_download_node_target($nid, $lingotek_locale, $status, TRUE, $context);

        // if workbench moderation is enabled for this node, and node should be moderated, moderate it based on the options
        $target_statuses = LingotekSync::getAllTargetStatusNotCurrent($nid);
        if (empty($target_statuses)) {
          lingotek_lingonode($nid, 'workbench_moderate', 1);
        }
        if (module_exists('workbench_moderation') && isset($node->workbench_moderation) && lingotek_lingonode($nid, 'workbench_moderate') == 1) {
          $options_index = $node->lingotek['sync_method_workbench_moderation'];
          $trans_options = lingotek_get_workbench_moderation_transitions();
          if ($options_index == 1) {
            $from_state = $node->workbench_moderation['current']->state;
            $mult_trans = variable_get('lingotek_sync_wb_select_' . $from_state, NULL);
            if ($mult_trans) {
              $trans_options[$from_state] = $mult_trans;
            }
          }
          lingotek_workbench_moderation_moderate($nid, $options_index, $trans_options);

          // moderate if all languages have been downloaded
          lingotek_lingonode_variable_delete($nid, 'workbench_moderate');
        }
      }
      if (!$completed) {
        lingotek_get_and_update_target_progress($document_id);
      }
      else {
        lingotek_lingonode($nid, 'target_sync_progress_' . $lingotek_locale, 100);
        $query = db_select('lingotek');
        $query
          ->addExpression('AVG(lingovalue)');
        $total = $query
          ->condition('lingokey', 'target_sync_progress_%', 'LIKE')
          ->condition('nid', $nid)
          ->execute()
          ->fetchField();
        lingotek_lingonode($nid, 'translation_progress', $total);
        lingotek_lingonode($nid, 'target_sync_last_progress_updated_' . $lingotek_locale, time());
      }

      // clear any caching from entitycache module to allow the new translation to show immediately
      if (module_exists('entitycache')) {
        cache_clear_all($nid, 'cache_entity_node');
      }
    }
    break;

    // if the function did not continue, then the doc ID was found.
  }
  $found = $nid || isset($trans_obj) && $trans_obj ? TRUE : FALSE;
  $response = $found ? array_merge($_GET, array(
    'target_drupal_language_code' => $target_drupal_language_code,
    'source_language' => $source_language,
    'type' => isset($trans_obj) ? $trans_obj
      ->getEntityType() : 'node',
    'id' => isset($trans_obj) ? $trans_obj
      ->getId() : $nid,
    'found' => $found,
    'download' => $found && isset($trans_obj) && $trans_obj
      ->getEntityType() == 'comment' ? TRUE : isset($auto_download) && $auto_download == TRUE,
  )) : array_merge($_GET, array(
    'found' => $found,
  ));
  drupal_json_output($response);
}