You are here

function lingotek_notifications in Lingotek Translation 7.2

Same name and namespace in other branches
  1. 7.7 lingotek.sync.inc \lingotek_notifications()
  2. 7.3 lingotek.sync.inc \lingotek_notifications()
  3. 7.4 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. This URL will be called when a document translation is complete, and can be downloaded.

Format: ?doc_id={document_id}&target_code={target_language}&project_id={project_id}

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

File

./lingotek.sync.inc, line 138
Content translation management and sync functions.

Code

function lingotek_notifications() {
  $document_id = isset($_GET['doc_id']) ? $_GET['doc_id'] : NULL;
  $lingotek_locale = isset($_GET['target_code']) ? $_GET['target_code'] : NULL;
  $project_id = isset($_GET['project_id']) ? $_GET['project_id'] : NULL;
  if (!isset($document_id) || !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);
  $nid = lingotek_get_node_id_from_document_id($document_id);
  watchdog('lingotek_callback', '
      node: @node_id
      <br /><strong>doc:</strong> @document_id
      <br /><strong>code:</strong> @language_code
      <br /><strong>project:</strong> @project_id', array(
    '@node_id' => $nid,
    '@document_id' => $document_id,
    '@language_code' => $lingotek_locale,
    '@project_id' => $project_id,
  ), WATCHDOG_DEBUG);
  if (!$nid) {

    // Look for and sync a comment if one is associated with the passed Lingotek Document ID.
    if (class_exists('LingotekComment')) {
      $source_language = lingotek_get_source_language();

      //TO-DO: use the source_language of the comment?
      if ($comment = LingotekComment::loadByLingotekDocumentId($document_id, $source_language, $project_id)) {
        $replacements = array(
          '@id' => $comment->id,
          '@document' => $document_id,
          '@language_code' => $lingotek_locale,
          '@project_id' => $project_id,
        );
        if ($comment
          ->updateLocalContent()) {
          if (variable_get('lingotek_api_debug', FALSE)) {
            watchdog('lingotek_debug', 'Updated local content for <strong>comment</strong> @id based on hit
                from external API for document: @document, language code @language_code, project ID: @project_id', $replacements, WATCHDOG_DEBUG);
          }
        }
        else {
          if (variable_get('lingotek_api_debug', FALSE)) {
            watchdog('lingotek_debug', 'Unable to update local content for <strong>comment</strong> @id based on hit
                from external API for document: @document, language code @language_code, project ID: @project_id', $replacements, WATCHDOG_ERROR);
          }
        }
      }
    }
    else {
      watchdog('lingotek', 'LingotekComment class not found.
          Please clear the Drupal cache to refresh the autoload registry', array(), WATCHDOG_ERROR);
    }
  }
  else {
    $node = node_load($nid);
    $source_language = $node->language;
    $node_setting = lingotek_lingonode($nid, 'sync_method');
    $auto_download = $node_setting !== FALSE ? $node_setting : variable_get('lingotek_sync', TRUE);
    if ($auto_download) {

      // download only when automatic download is enabled
      lingotek_mt_sync_download_node_target($nid, $lingotek_locale, $context);
    }
  }
  $found = $nid || isset($comment) && $comment ? TRUE : FALSE;

  //print_r($comment);

  //Response
  $response = $found ? array_merge($_GET, array(
    'target_drupal_language_code' => $target_drupal_language_code,
    'source_language' => $source_language,
    'type' => isset($comment) ? 'comment' : 'node',
    'id' => isset($comment) ? $comment->cid : $nid,
    'found' => $found,
    'download' => $found && isset($comment) ? TRUE : isset($auto_download) && $auto_download == TRUE,
  )) : array_merge($_GET, array(
    'found' => $found,
  ));
  drupal_json_output($response);
}