You are here

function lingotek_get_and_update_target_progress in Lingotek Translation 7.6

Same name and namespace in other branches
  1. 7.7 lingotek.sync.inc \lingotek_get_and_update_target_progress()
  2. 7.4 lingotek.sync.inc \lingotek_get_and_update_target_progress()
  3. 7.5 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 string reference to 'lingotek_get_and_update_target_progress'
lingotek_update_target_progress_batch_create in ./lingotek.batch.inc

File

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

Code

function lingotek_get_and_update_target_progress($entity_type, $document_ids, $current_nids, $total_nids, &$context) {
  $context['message'] = t('Checking status of translations (@current of @total complete)', array(
    '@current' => $current_nids,
    '@total' => $total_nids,
  ));
  $api = LingotekApi::Instance();
  if (empty($document_ids)) {
    return;
  }
  if (!is_array($document_ids)) {
    $document_ids = array(
      $document_ids,
    );
  }
  $project_id = variable_get('lingotek_project', NULL);
  $progress_report = $api
    ->getProgressReport($project_id, $document_ids);
  $targets_count = LingotekSync::getTargetCountByDocumentIds($document_ids);
  if (isset($progress_report) && $progress_report->results == 'success') {
    $delete_nids_maybe = array();
    $entity_values = array();
    $trans_obj = NULL;
    if (!empty($progress_report->errors)) {
      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':
            lingotek_keystore($entity_type, $entity_id, 'upload_status', LingotekSync::STATUS_PENDING);
            break;
          case 'NOT_FOUND':
          default:
            lingotek_keystore($entity_type, $entity_id, 'upload_status', LingotekSync::STATUS_FAILED);
            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;
      }
      else {
        $delete_nids_maybe[] = $entity_id;
        $target_number = isset($targets_count[$entity_id]->targets) ? $targets_count[$entity_id]->targets : 0;
      }
      foreach ($completion as $language => $percent) {
        $status = LingotekSync::getTargetStatus($doc_id, $language);
        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
            $to_status = LingotekSync::STATUS_PENDING;

            // Set it to pending
          }
          $entity_values[] = array(
            $entity_type,
            $entity_id,
            'target_sync_status_' . $language,
            $to_status,
          );
        }
      }

      // update source status when necessary
      $entity_source_status = lingotek_keystore($entity_type, $entity_id, 'upload_status');
      if ($entity_source_status == LingotekSync::STATUS_FAILED || $entity_source_status == LingotekSync::STATUS_PENDING) {
        lingotek_keystore($entity_type, $entity_id, 'upload_status', 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];
      $query = 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();
    }
    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($error_message, 'error');
  }
}