You are here

function lingotek_get_and_update_target_progress in Lingotek Translation 7.7

Same name and namespace in other branches
  1. 7.4 lingotek.sync.inc \lingotek_get_and_update_target_progress()
  2. 7.5 lingotek.sync.inc \lingotek_get_and_update_target_progress()
  3. 7.6 lingotek.sync.inc \lingotek_get_and_update_target_progress()

Updates the 'target_sync_status_[lang-code]' field for every target in the lingotek table with the overall progress returned by TMS

Parameters

int array $document_ids: array of Document IDs that you want to update

1 call to lingotek_get_and_update_target_progress()
lingotek_sync_target_locales in ./lingotek.module
2 string references to 'lingotek_get_and_update_target_progress'
lingotek_grid_download_selected in ./lingotek.bulk_grid.inc
lingotek_update_target_progress_batch_create in ./lingotek.batch.inc

File

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

Code

function lingotek_get_and_update_target_progress(&$update_context = array(), $entity_type = NULL, $document_ids = NULL, $current_nids = -1, $total_nids = -1) {
  if (empty($update_context)) {
    return;
  }
  $api = LingotekApi::instance();
  if (isset($update_context['entity'])) {
    return check_entity_progress_for_download($update_context, $api);
  }
  if (empty($document_ids)) {
    return;
  }
  if (!is_array($document_ids)) {
    $document_ids = array(
      $document_ids,
    );
  }
  $failed_doc_ids = update_pending_sources($document_ids, $api);
  $update_context['message'] = t('Checking status of translations (@current of @total complete)', array(
    '@current' => $current_nids,
    '@total' => $total_nids,
  ));
  $project_id = variable_get('lingotek_project', NULL);
  $progress_report = $api
    ->getProgressReport($project_id, $document_ids);
  if (isset($progress_report) && $progress_report->results == 'success') {
    $delete_nids_maybe = array();
    $entity_values = array();
    if (count(get_object_vars($progress_report->errors)) > 0) {
      foreach (get_object_vars($progress_report->errors) as $doc_id => $error) {
        list($entity_id, $entity_type) = LingotekSync::getEntityIdFromDocId($doc_id, $entity_type);
        switch ($error->status) {
          case 'IN_QUEUE':
            LingotekSync::setUploadStatus($entity_type, $entity_id, LingotekSync::STATUS_PENDING);
            break;
          case 'NOT_FOUND':
          default:
            LingotekSync::setUploadStatus($entity_type, $entity_id, LingotekSync::STATUS_ERROR);
            lingotek_keystore($entity_type, $entity_id, 'last_sync_error', substr($error, 0, 255));
            LingotekLog::error('Received unexpected error status (@status) from Lingotek for @entity_type #@id: <pre>@error</pre>', array(
              '@status' => $error->status,
              '@entity_type' => $entity_type,
              '@id' => $entity_id,
              '@error' => $error,
            ));
        }
      }
    }
    foreach ($progress_report->byDocumentIdAndTargetLocale as $doc_id => $completion) {
      list($entity_id, $entity_type_from_table) = LingotekSync::getEntityIdFromDocId($doc_id);
      if (!$entity_id) {
        LingotekLog::error("Lingotek doc ID '@doc_id' not found", array(
          '@doc_id' => $doc_id,
        ));
        continue;
      }
      elseif (in_array($doc_id, $failed_doc_ids)) {
        LingotekLog::error("Doc ID '@doc_id' failed to update. Check TMS for details", array(
          '@doc_id' => $doc_id,
        ));
        continue;
      }
      else {
        $delete_nids_maybe[] = $entity_id;
      }
      $entity_source_status = lingotek_keystore($entity_type, $entity_id, 'upload_status');
      foreach ($completion as $language => $percent) {
        $status = LingotekSync::getTargetStatus($doc_id, $language);
        $to_status = $status;
        if (isset($progress_report->workflowCompletedByDocumentIdAndTargetLocale->{$doc_id}->{$language})) {
          if ($progress_report->workflowCompletedByDocumentIdAndTargetLocale->{$doc_id}->{$language}) {

            // If the workflow is complete
            if ($status != LingotekSync::STATUS_CURRENT) {

              // If the status is not current
              $to_status = LingotekSync::STATUS_READY;

              // Set it to ready
            }
            else {
              $to_status = LingotekSync::STATUS_CURRENT;

              // Otherwise keep it at current
            }
          }
          else {

            // If the workflow is not complete
            if ($percent == 0) {
              $to_status = LingotekSync::STATUS_PENDING;

              // Set it to pending
            }
            elseif ($percent > 0) {
              $to_status = LingotekSync::STATUS_READY_INTERIM;
            }
            else {
              $to_status = $status;
            }
          }
          if ($status != LingotekSync::STATUS_UNTRACKED) {
            $entity_values[] = array(
              $entity_type,
              $entity_id,
              'target_sync_status_' . $language,
              $to_status,
            );
          }
        }
      }

      // update source status when necessary
      if ($entity_source_status == LingotekSync::STATUS_ERROR || $entity_source_status == LingotekSync::STATUS_PENDING) {
        LingotekSync::setUploadStatus($entity_type, $entity_id, LingotekSync::STATUS_CURRENT);
      }
    }

    // merge status info for entities
    foreach ($entity_values as $record) {
      $entity_id = isset($record['entity_id']) ? $record['entity_id'] : $record[1];
      $entity_key = isset($record['entity_key']) ? $record['entity_key'] : $record[2];
      $value = isset($record['value']) ? $record['value'] : $record[3];
      db_merge('lingotek_entity_metadata')
        ->key(array(
        'entity_id' => $entity_id,
        'entity_type' => $entity_type,
        'entity_key' => $entity_key,
      ))
        ->fields(array(
        'entity_type' => $entity_type,
        'entity_id' => $entity_id,
        'entity_key' => $entity_key,
        'value' => $value,
      ))
        ->execute();
      lingotek_cache_clear($entity_type, $entity_id);
    }
    return $progress_report;
  }
  else {
    $error_message = t("API Error(s):") . " <ul>";
    if (is_array($progress_report->errors)) {
      foreach ($progress_report->errors as $error) {
        $error_message .= "<li>" . $error . "</li>";
      }
    }
    $error_message .= "</ul><i>" . t('For additional information, check your <a href="@link">recent log messages</a>', array(
      '@link' => url('admin/reports/dblog'),
    )) . "</i>";
    drupal_set_message(filter_xss($error_message), 'error');
  }
}