You are here

function lingotek_notifications in Lingotek Translation 7.3

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.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.

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;
  $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($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 = LingotekSync::getNodeIdFromDocId($document_id);
  LingotekLog::trace('Notification Recieved <h2>node: @node_id (@language_code)</h2>
      <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,
  ), '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'] = $trans_obj->cid;
    }
    elseif ($trans_obj = LingotekComment::loadByLingotekDocumentId($document_id, $source_language, $project_id)) {
      $replacements['@id'] = $trans_obj->id;
      $replacements['@trans_type'] = $trans_obj->id;
    }
    else {
      LingotekLog::error('Lingotek document ID not found. Please clear the Drupal cache to refresh the autoload registry', array());
      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 = 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
      $status = $completed ? LingotekSync::STATUS_CURRENT : LingotekSync::STATUS_PENDING;
      lingotek_sync_download_node_target($nid, $lingotek_locale, $status, $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 = lingotek_variable_get(lingotek_lingonode($nid, 'sync_method_workbench_moderation'), 'lingotek_sync_workbench_moderation', 0);
        $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');
      }
    }
  }
  $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);
}