You are here

class LingotekBatchController in Lingotek Translation 8

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

Hierarchy

Expanded class hierarchy of LingotekBatchController

File

src/Controller/LingotekBatchController.php, line 9

Namespace

Drupal\lingotek\Controller
View source
class LingotekBatchController extends LingotekControllerBase {
  public function dispatch($action, $entity_type, $entity_id) {

    /** @var \Drupal\lingotek\LingotekContentTranslationServiceInterface $translation_service */
    $translation_service = \Drupal::service('lingotek.content_translation');

    /** @var \Drupal\lingotek\LingotekConfigurationServiceInterface $configuration_service */
    $configuration_service = \Drupal::service('lingotek.configuration');

    /** @var Lingotek $lingotek */
    $lingotek = \Drupal::service('lingotek');
    $entity = \Drupal::entityManager()
      ->getStorage($entity_type)
      ->load($entity_id);

    // This forces the hash to be set.
    if ($translation_service
      ->hasEntityChanged($entity)) {
      $entity
        ->save();
    }
    $profile = $configuration_service
      ->getEntityProfile($entity, TRUE);
    switch ($action) {
      case 'uploadSingle':
        return $this
          ->uploadSingle($entity_type, $entity_id, $profile);
      case 'downloadSingle':
        return $this
          ->downloadSingle($entity_type, $entity_id, $profile);
      default:
        return $this
          ->noAction();
    }
  }
  public function uploadSingle($entity_type, $entity_id, LingotekProfile $profile = NULL) {
    $batch = array(
      'title' => $this
        ->t('Uploading content to Lingotek'),
      'operations' => $this
        ->getUploadOperations($entity_type, array(
        $entity_id,
      )),
      'error_message' => t('An error happened.'),
      'finished' => 'lingotek_operation_content_upload_finished',
      'file' => drupal_get_path('module', 'lingotek') . '/lingotek.batch.inc',
    );
    $redirect_url = \Drupal::urlGenerator()
      ->generate("entity.{$entity_type}.content_translation_overview", [
      $entity_type => $entity_id,
    ], UrlGeneratorInterface::ABSOLUTE_URL);
    batch_set($batch);
    return batch_process($redirect_url);
  }
  public function downloadSingle($entity_type, $entity_id, $locales) {
    $batch = array(
      'title' => $this
        ->t('Downloading translations from Lingotek'),
      'operations' => $this
        ->getDownloadOperations($entity_type, array(
        $entity_id,
      ), $locales),
      'finished' => 'lingotek_operation_content_download_finished',
      'file' => drupal_get_path('module', 'lingotek') . '/lingotek.batch.inc',
    );
    $entity = \Drupal::entityManager()
      ->getStorage($entity_type)
      ->load($entity_id);
    $redirect_url = \Drupal::urlGenerator()
      ->generate("entity.{$entity_type}.content_translation_overview", [
      $entity_type => $entity_id,
    ], UrlGeneratorInterface::ABSOLUTE_URL);
    batch_set($batch);
    return batch_process($redirect_url);
  }
  public function checkUploadStatus($entity_type, $entity_id) {

    // TODO
  }
  public function checkTargetStatus($entity_type, $entity_id) {

    // TODO
  }
  public function addLanguageSingle($entity_type, $entity_id) {

    // TODO
  }
  protected function getUploadOperations($entity_type, $entity_ids) {
    $operations = array();
    if (is_array($entity_ids)) {
      foreach ($entity_ids as $id) {
        $operations[] = array(
          'lingotek_operation_content_upload',
          array(
            $entity_type,
            $id,
          ),
        );
      }
    }
    return $operations;
  }
  protected function getDownloadOperations($entity_type, $entity_ids, $locales) {
    $operations = array();
    if (is_array($entity_ids)) {
      foreach ($entity_ids as $id) {
        foreach ($locales as $locale) {
          $operations[] = array(
            'lingotek_operation_translation_download',
            array(
              $entity_type,
              $id,
              $locale,
            ),
          );
        }
      }
    }
    return $operations;
  }
  public function noAction() {
    $markup = $this
      ->t('Lingotek batch operation error: You must supply a valid action.');
    return array(
      '#type' => 'markup',
      '#markup' => $markup,
    );
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ControllerBase::$currentUser protected property The current user service. 1
ControllerBase::$entityFormBuilder protected property The entity form builder.
ControllerBase::$entityManager protected property The entity manager.
ControllerBase::$entityTypeManager protected property The entity type manager.
ControllerBase::$keyValue protected property The key-value storage. 1
ControllerBase::$languageManager protected property The language manager. 1
ControllerBase::$moduleHandler protected property The module handler. 2
ControllerBase::$stateService protected property The state service.
ControllerBase::cache protected function Returns the requested cache bin.
ControllerBase::config protected function Retrieves a configuration object.
ControllerBase::container private function Returns the service container.
ControllerBase::currentUser protected function Returns the current user. 1
ControllerBase::entityFormBuilder protected function Retrieves the entity form builder.
ControllerBase::entityManager Deprecated protected function Retrieves the entity manager service.
ControllerBase::entityTypeManager protected function Retrieves the entity type manager.
ControllerBase::formBuilder protected function Returns the form builder service. 2
ControllerBase::keyValue protected function Returns a key/value storage collection. 1
ControllerBase::languageManager protected function Returns the language manager service. 1
ControllerBase::moduleHandler protected function Returns the module handler. 2
ControllerBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
ControllerBase::state protected function Returns the state storage service.
LingotekBatchController::addLanguageSingle public function
LingotekBatchController::checkTargetStatus public function
LingotekBatchController::checkUploadStatus public function
LingotekBatchController::dispatch public function
LingotekBatchController::downloadSingle public function
LingotekBatchController::getDownloadOperations protected function
LingotekBatchController::getUploadOperations protected function
LingotekBatchController::noAction public function
LingotekBatchController::uploadSingle public function
LingotekControllerBase::$configFactory protected property The config factory. Overrides ControllerBase::$configFactory
LingotekControllerBase::$formBuilder protected property The form builder. Overrides ControllerBase::$formBuilder
LingotekControllerBase::$languageLocaleMapper protected property The language-locale mapper.
LingotekControllerBase::$logger protected property A logger instance.
LingotekControllerBase::$request protected property A Symfony request instance
LingotekControllerBase::connected public function Checks if site is connected to Lingotek.
LingotekControllerBase::create public static function Instantiates a new instance of this class. Overrides ControllerBase::create 1
LingotekControllerBase::getLingotekForm protected function Return a Lingotek form (convenience function)
LingotekControllerBase::__construct public function Constructs a LingotekControllerBase object. 1
LingotekSetupTrait::$lingotek protected property A lingotek connector object
LingotekSetupTrait::checkSetup protected function Verify the Lingotek Translation module has been properly initialized.
LingotekSetupTrait::setupCompleted public function Checks if Lingotek module is completely set up.
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.