You are here

function lingotek_update_config_progress in Lingotek Translation 7.6

Same name and namespace in other branches
  1. 7.7 lingotek.sync.inc \lingotek_update_config_progress()
  2. 7.5 lingotek.sync.inc \lingotek_update_config_progress()

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

Parameters

int array $document_ids: array of Document IDs to be updated

1 call to lingotek_update_config_progress()
LingotekSync::getDownloadableReport in lib/Drupal/lingotek/LingotekSync.php
1 string reference to 'lingotek_update_config_progress'
lingotek_config_update_selected in ./lingotek.batch.inc

File

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

Code

function lingotek_update_config_progress($document_ids) {
  $api = LingotekApi::Instance();
  if (empty($document_ids)) {
    return;
  }
  if (!is_array($document_ids)) {
    $document_ids = array(
      $document_ids,
    );
  }
  $config_profile = lingotek_get_profile_settings(LingotekSync::PROFILE_CONFIG);
  $project_id = array_key_exists('project_id', $config_profile) ? $config_profile['project_id'] : variable_get('lingotek_project', '');
  $progress_report = $api
    ->getProgressReport($project_id, $document_ids);
  if (isset($progress_report) && $progress_report->results == 'success') {
    $cids = array();
    $cfg_values = array();
    $trans_obj = NULL;
    if (isset($progress_report->errors)) {
      foreach (get_object_vars($progress_report->errors) as $doc_id => $error) {
        $set = LingotekConfigSet::loadByLingotekDocumentId($doc_id);
        switch ($error->status) {
          case 'IN_QUEUE':
            $set
              ->setMetadataValue('upload_status', LingotekSync::STATUS_PENDING);
            break;
          case 'NOT_FOUND':
          default:
            $set
              ->setMetadataValue('upload_status', LingotekSync::STATUS_FAILED);
            LingotekLog::error('Received unexpected error status (@status) from Lingotek for config chunk #@id: <pre>@error</pre>', array(
              '@status' => $error->status,
              '@id' => $chunk,
              '@error' => $error,
            ));
        }
      }
    }
    foreach ($progress_report->byDocumentIdAndTargetLocale as $doc_id => $completion) {
      $trans_obj = LingotekConfigSet::loadByLingotekDocumentId($doc_id);
      if (!$trans_obj) {
        LingotekLog::error("Lingotek doc ID '@doc_id' not found", array(
          '@doc_id' => $doc_id,
        ));
        continue;
      }
      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
          }
          $cfg_values[] = array(
            $trans_obj->sid,
            'target_sync_status_' . $language,
            $to_status,
          );
        }
      }
    }

    // insert status info for config
    foreach ($cfg_values as $record) {
      $query = db_merge('lingotek_config_metadata')
        ->key(array(
        'id' => $record[0],
        'config_key' => $record[1],
      ))
        ->fields(array(
        'id' => $record[0],
        'config_key' => $record[1],
        'value' => $record[2],
      ))
        ->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');
  }
}