You are here

public function GlobalLinkTranslator::requestJobItemsTranslation in GlobalLink Connect for Drupal 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/tmgmt/Translator/GlobalLinkTranslator.php \Drupal\globallink\Plugin\tmgmt\Translator\GlobalLinkTranslator::requestJobItemsTranslation()

Requests the translation of a JobItem.

Parameters

JobItemInterface[] $job_items: The JobItem we want to translate.

Overrides ContinuousTranslatorInterface::requestJobItemsTranslation

1 call to GlobalLinkTranslator::requestJobItemsTranslation()
GlobalLinkTranslator::requestTranslation in src/Plugin/tmgmt/Translator/GlobalLinkTranslator.php
@abstract

File

src/Plugin/tmgmt/Translator/GlobalLinkTranslator.php, line 775

Class

GlobalLinkTranslator
GlobalLink translation plugin controller.

Namespace

Drupal\globallink\Plugin\tmgmt\Translator

Code

public function requestJobItemsTranslation(array $job_items) {

  /** @var \Drupal\tmgmt\Entity\Job $job */
  $job = reset($job_items)
    ->getJob();
  try {
    $source_language = $job
      ->getRemoteSourceLanguage();
    $target_language = $job
      ->getRemoteTargetLanguage();
    $settings = $job
      ->getTranslator()
      ->getSettings();
    $pd_config = $this->glExchangeAdapter
      ->getPDConfig($settings);
    $glexchange = $this->glExchangeAdapter
      ->getGlExchange($pd_config);
    $submission_settings = [
      'name' => $settings['pd_submissionprefix'] . $job
        ->label(),
      'submitter' => $settings['pd_username'],
      'urgent' => $job
        ->getSetting('urgent'),
      'comment' => $job
        ->getSetting('comment'),
    ];
    if ($job
      ->isContinuous()) {
      $required_by = $job
        ->getSetting('required_by');
      $due_datetime = new DrupalDateTime("+{$required_by} weekday");
    }
    else {
      $due_setting = $job
        ->getSetting('due');

      /** @var DrupalDateTime $due_datetime */
      $due_datetime = unserialize(serialize($due_setting['object']));
    }
    $submission_settings['due'] = $due_datetime
      ->format('U') * 1000;
    $project = $glexchange
      ->getProject($settings['pd_projectid']);
    $submission = $this->glExchangeAdapter
      ->getSubmission($project, $submission_settings);
    $glexchange
      ->initSubmission($submission);
    $upload_result_ids = [];
    if ($job
      ->getTranslator()
      ->getSetting('pd_combine')) {
      $data = $this
        ->getXliffData($job, $job_items);
      $document_settings = [
        'name' => $job
          ->label() . '.xliff',
        'source_language' => $source_language,
        'target_languages' => [
          $target_language,
        ],
        'data' => $data,
        'client_identifier' => $job
          ->uuid(),
        'classifier' => $settings['pd_classifier'],
      ];
      $document = $this->glExchangeAdapter
        ->getPdDocument($document_settings);
      $upload_result_id = $glexchange
        ->uploadTranslatable($document);
      foreach ($job_items as $job_item) {
        $upload_result_ids[$job_item
          ->id()] = $upload_result_id;
      }
    }
    else {
      foreach ($job_items as $job_item) {
        $data = $this
          ->getXliffData($job, [
          $job_item,
        ]);
        $document_settings = [
          'name' => $job_item
            ->label() . '.xliff',
          'source_language' => $source_language,
          'target_languages' => [
            $target_language,
          ],
          'data' => $data,
          'client_identifier' => $job
            ->uuid(),
          'classifier' => $settings['pd_classifier'],
        ];
        $document = $this->glExchangeAdapter
          ->getPdDocument($document_settings);
        $upload_result_ids[$job_item
          ->id()] = $glexchange
          ->uploadTranslatable($document);
      }
    }
    $submission_result_id = $glexchange
      ->startSubmission();

    /** @var JobItemInterface $job_item */
    foreach ($job_items as $job_item) {
      $job_item
        ->addRemoteMapping(NULL, $submission_result_id, [
        'remote_identifier_2' => $upload_result_ids[$job_item
          ->id()],
      ]);
      $job_item
        ->active('Ticket with id %submission_id created', [
        '%submission_id' => $submission_result_id,
      ]);
    }
  } catch (\Exception $e) {
    $this->logger
      ->error('Job has been rejected with following error: @error', [
      '@error' => $e
        ->getMessage(),
    ]);
    $job
      ->rejected('Job has been rejected with following error: @error', [
      '@error' => $e
        ->getMessage(),
    ], 'error');
    $job
      ->addMessage('Job has been rejected with following error: @error', [
      '@error' => $e
        ->getMessage(),
    ], 'error');
  }
}