You are here

public function LingotekContentTranslationService::checkTargetStatuses in Lingotek Translation 3.5.x

Same name and namespace in other branches
  1. 8 src/LingotekContentTranslationService.php \Drupal\lingotek\LingotekContentTranslationService::checkTargetStatuses()
  2. 8.2 src/LingotekContentTranslationService.php \Drupal\lingotek\LingotekContentTranslationService::checkTargetStatuses()
  3. 4.0.x src/LingotekContentTranslationService.php \Drupal\lingotek\LingotekContentTranslationService::checkTargetStatuses()
  4. 3.0.x src/LingotekContentTranslationService.php \Drupal\lingotek\LingotekContentTranslationService::checkTargetStatuses()
  5. 3.1.x src/LingotekContentTranslationService.php \Drupal\lingotek\LingotekContentTranslationService::checkTargetStatuses()
  6. 3.2.x src/LingotekContentTranslationService.php \Drupal\lingotek\LingotekContentTranslationService::checkTargetStatuses()
  7. 3.3.x src/LingotekContentTranslationService.php \Drupal\lingotek\LingotekContentTranslationService::checkTargetStatuses()
  8. 3.4.x src/LingotekContentTranslationService.php \Drupal\lingotek\LingotekContentTranslationService::checkTargetStatuses()
  9. 3.6.x src/LingotekContentTranslationService.php \Drupal\lingotek\LingotekContentTranslationService::checkTargetStatuses()
  10. 3.7.x src/LingotekContentTranslationService.php \Drupal\lingotek\LingotekContentTranslationService::checkTargetStatuses()
  11. 3.8.x src/LingotekContentTranslationService.php \Drupal\lingotek\LingotekContentTranslationService::checkTargetStatuses()

Gets the current status of all the target translations.

Parameters

\Drupal\Core\Entity\ContentEntityInterface &$entity: The entity which status we want to check.

Overrides LingotekContentTranslationServiceInterface::checkTargetStatuses

File

src/LingotekContentTranslationService.php, line 234

Class

LingotekContentTranslationService
Service for managing Lingotek content translations.

Namespace

Drupal\lingotek

Code

public function checkTargetStatuses(ContentEntityInterface &$entity) {

  /** @var \Drupal\lingotek\LingotekProfileInterface $profile */
  $profile = $this->lingotekConfiguration
    ->getEntityProfile($entity);
  if ($profile
    ->id() === Lingotek::PROFILE_DISABLED || $this
    ->getSourceStatus($entity) === Lingotek::STATUS_CANCELLED) {
    return FALSE;
  }
  $document_id = $this
    ->getDocumentId($entity);
  $translation_statuses = $this->lingotek
    ->getDocumentTranslationStatuses($document_id);
  $source_status = $this
    ->getSourceStatus($entity);
  $statuses = [];
  $languages = $this->languageManager
    ->getLanguages();
  foreach ($languages as $language) {
    $statuses[$language
      ->getId()] = $this
      ->getTargetStatus($entity, $language
      ->getId());
  }

  // Let's reset all statuses, but keep the source one.
  $this
    ->clearTargetStatuses($entity);
  foreach ($translation_statuses as $lingotek_locale => $progress) {
    $drupal_language = $this->languageLocaleMapper
      ->getConfigurableLanguageForLocale($lingotek_locale);
    if ($drupal_language == NULL) {

      // Language existing in TMS, but not configured on Drupal.
      continue;
    }
    $langcode = $drupal_language
      ->id();
    $current_target_status = $statuses[$langcode];
    if (in_array($current_target_status, [
      Lingotek::STATUS_UNTRACKED,
      Lingotek::STATUS_DISABLED,
      Lingotek::STATUS_EDITED,
      Lingotek::STATUS_REQUEST,
      Lingotek::STATUS_NONE,
      Lingotek::STATUS_READY,
      Lingotek::STATUS_PENDING,
      Lingotek::STATUS_CANCELLED,
      NULL,
    ])) {
      if ($progress === Lingotek::STATUS_CANCELLED) {
        $this
          ->setTargetStatus($entity, $langcode, Lingotek::STATUS_CANCELLED);
      }
      elseif ($progress === Lingotek::PROGRESS_COMPLETE) {
        $this
          ->setTargetStatus($entity, $langcode, Lingotek::STATUS_READY);
      }
      else {
        if (!$profile
          ->hasDisabledTarget($langcode)) {
          $this
            ->setTargetStatus($entity, $langcode, Lingotek::STATUS_PENDING);
        }
        else {
          $this
            ->setTargetStatus($entity, $langcode, Lingotek::STATUS_DISABLED);
        }
      }
    }
    if ($source_status !== Lingotek::STATUS_CURRENT && $statuses[$langcode] === Lingotek::STATUS_EDITED && $langcode !== $entity
      ->getUntranslated()
      ->language()
      ->getId()) {
      $this
        ->setTargetStatus($entity, $langcode, Lingotek::STATUS_EDITED);
    }
    if ($source_status === Lingotek::STATUS_CURRENT && $statuses[$langcode] === Lingotek::STATUS_CURRENT && $langcode !== $entity
      ->getUntranslated()
      ->language()
      ->getId()) {
      $this
        ->setTargetStatus($entity, $langcode, Lingotek::STATUS_CURRENT);
    }
    if ($profile
      ->hasDisabledTarget($langcode)) {
      $this
        ->setTargetStatus($entity, $langcode, Lingotek::STATUS_DISABLED);
    }
  }
  if ($this
    ->getSourceStatus($entity) == Lingotek::STATUS_DISABLED) {
    $this
      ->setTargetStatuses($entity, Lingotek::STATUS_DISABLED);
  }
}